This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamp_wiki_scraper.py
321 lines (244 loc) · 9.88 KB
/
samp_wiki_scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# written by Andosius
# ----------------------------------------------
# Tool to download all files from old SA:MP-Wiki
# ----------------------------------------------
# some parts are from
# https://www.thepythoncode.com/article/extract-all-website-links-python
#
# import all the packages required so no need to import in methods
# from my points of view it's bad practice which lets the code look messy
import os
import requests
from time import sleep
from urllib.robotparser import RobotFileParser
from urllib.parse import urlparse
from urllib.parse import unquote
from urllib.request import urlopen
from urllib.request import Request
from urllib.error import HTTPError
from bs4 import BeautifulSoup
import colorama
# Define colors before usage, no matter what mode user is going to choose
colorama.init()
GREEN = colorama.Fore.GREEN
GRAY = colorama.Fore.LIGHTBLACK_EX
RESET = colorama.Fore.RESET
YELLOW = colorama.Fore.YELLOW
RED = colorama.Fore.RED
# constants
HEADERS = {'User-Agent': 'samp-wiki-scraper'}
PREFIX = "dump/" # can be used for real path too
WIKIURL = "https://team.sa-mp.com/wiki-archive.html"
SLEEPING = 3
# styles and javascripts required
assets = [
# styles and javascript files
"skins/common/commonPrint.css",
"skins/monobook/main.css",
"skins/monobook/IE50Fixes.css",
"skins/monobook/IE55Fixes.css",
"skins/monobook/IE60Fixes.css",
"skins/monobook/IE70Fixes.css",
"skins/common/IEFixes.js",
"skins/common/wikibits.js",
"raw/gen.js",
"raw/MediaWiki_Common.css",
"raw/MediaWiki_Monobook.css",
"raw/gen.css",
# logos, gifs and other pictures
"skins/common/images/poweredby_mediawiki_88x31.png",
"skins/monobook/headbg.jpg",
"skins/monobook/bullet.gif",
"skins/monobook/discussionitem_icon.gif",
"skins/monobook/external.png"
]
# all sets we need to log all links and remove the trash from the golden ones
internals = set()
externals = set()
critical = set()
# contains already scanned urls
crawled = set()
def collect_all_links(page_url: str):
# create a set for all farmed urls on a single page
urls = set()
# get base domain name
domain_name = urlparse(page_url).netloc
# initialize soup and parse content for <a>-elements
soup = BeautifulSoup(requests.get(page_url, headers=HEADERS).content, "html.parser")
for a_tag in soup.findAll("a"):
# search through href tags for pattern and apply some fixes
href = a_tag.attrs.get("href")
# skip empty hrefs or page internal redirects with no use
if href == "" or href is None or href[0] == "#":
continue
# some formatting hotfixes
url = href
url = url.replace("http://localhost../", "https://team.sa-mp.com/")
url = url.replace("../wiki/", "https://team.sa-mp.com/wiki/")
url = url.replace("../upload/", "https://team.sa-mp.com/upload/")
url = url.replace("../wiki-archive.html",
"https://team.sa-mp.com/wiki-archive.html")
url = url.replace("http://wiki.sa-mp.com", "https://team.sa-mp.com")
url = url.replace("https://wiki.sa-mp.com", "https://team.sa-mp.com")
if url[0:5] == "wiki/":
url = "https://team.sa-mp.com/" + url
url = url.replace("http://", "https://")
url = url.replace("https:/team", "https://team")
# check if an url is not valid:
if url[0:8] != "https://":
print(f"{RED}[CRITICAL] {url}")
critical.add(url)
# skip if already in set, don't waste time
if url in internals:
continue
# domain name does not include internal url base -> external list
if domain_name not in url:
if url not in externals:
print(f"{YELLOW}[EXTERNAL] {url} added to set!")
externals.add(url)
continue
# it cannot be anything else than an internal link so add it to list
print(f"{GREEN}[INTERNAL] {url} added to set!")
internals.add(url)
# add to urls for return value
urls.add(url)
return urls
def crawl(url: str):
# only crawl pages with .html at the end to avoid unnecessary errors
if ".html" not in url:
return
print(f"{GRAY}[CRAWLING] {url}{RESET}")
links = collect_all_links(url)
for link in links:
if link in crawled:
continue
crawled.add(url)
crawl(link)
sleep(SLEEPING)
def create_directory(line: str, prefix=True):
# remove link to get pure structure
link = line.replace("https://team.sa-mp.com/", "")
data = link.split("/")
# create path from list elements
if prefix is True:
path = PREFIX + "/".join(data[0:-1])
else:
if len(data) > 1:
path = "/".join(data[0:-1])
else:
path = link
# 90% sure it's not needed but does it really hurt?
path = path.replace("\n", "")
# in some cases it detects an index.php - just prevent problems
if path == "" or ".php" in path:
return
# create all folders given in path - be careful modifying PREFIX!
if os.path.isdir(path) is False:
os.makedirs(path)
def print_complete_msg():
print("|--------------------------------|")
print("| COMPLETED |")
print("|--------------------------------|")
def main():
print("> Please provie a number for the action you want to choose: ")
print("* 1) Search all links from original SA:MP Wiki")
print("* 2) Create folder structure from internals.txt file")
print("* 3) Download all contents")
print("NOTE: All these options can take a very long time",
"- keep that in mind.")
decision = None
while type(decision) is not int:
try:
decision = int(input("Selection (1-3): "))
except ValueError:
print("ERROR: Please choose between 1, 2 and 3!")
if type(decision) == int:
if bool(1 <= decision <= 3) is False:
decision = None
print("ERROR: Please choose between 1, 2 and 3!")
# User input: 1
if decision == 1:
crawl(WIKIURL)
internals_file = open("internals.txt", "a")
for link in internals:
internals_file.write(link + "\n")
internals_file.close()
externals_file = open("externals.txt", "a")
for link in externals:
externals_file.write(link + "\n")
externals_file.close()
others_file = open("others.txt", "a")
for link in critical:
others_file.write(link + "\n")
others_file.close()
print_complete_msg()
# User input: 2
elif decision == 2:
# take data from internals.txt file because there are all links
internals_file = open("internals.txt", "r")
lines = internals_file.readlines()
# scan, format and create directory path for makedirs()
for line in lines:
create_directory(line)
internals_file.close()
# create directory structure for hardcoded elements
for asset in assets:
create_directory(asset)
print_complete_msg()
# User input: 3
elif decision == 3:
# create directory "logs" in current directory for all output files
create_directory("logs", False)
error_file = open("logs/download_errors.txt", "a")
success_file = open("logs/download_success.txt", "a")
skipped_file = open("logs/download_skipped.txt", "a")
# take data from internals.txt file because there are all links
internals_file = open("internals.txt", "r")
lines = internals_file.readlines()
internals_file.close()
for asset in assets:
lines.insert(0, "https://team.sa-mp.com/" + asset)
for line in lines:
# format url-path to my desires for saving location
path = unquote(line.replace("https://team.sa-mp.com/", ""))
path = path.replace("\n", "")
path = PREFIX + path
# remove \n for cleaner outputs :)
line = line.replace("\n", "")
# links with # inside are most likely not a real page ;)
if "#" in line:
print(f"{GRAY}{line} probably not a page - skipped!{RESET}")
skipped_file.write(f"{line}\n")
continue
# skip file if it exists
if os.path.isfile(path):
print(f"{GRAY}{line} already exists - skipped!{RESET}")
skipped_file.write(f"{line}\n")
continue
# SHORT BREAK to
sleep(SLEEPING)
# try to get page data in byte format
try:
print(f"{YELLOW}Downloading {line}...{RESET}")
rq = Request(line)
rq.add_header('User-Agent', 'samp-wiki-scraper')
resource = urlopen(rq)
content = resource.read()
except HTTPError:
print(f"{RED}{line} could not be downloaded!{RESET}")
error_file.write(f"{line}\n")
continue
# original script contained UnicodeError tracking - changed from
# utf-8 to reading and writing bytes what works just fine
# so no error handling needed, hopefully...
file = open(path, "wb")
file.write(content)
file.close()
print(f"{GREEN}Download completed!{RESET}")
success_file.write(f"{line}\n")
error_file.close()
success_file.close()
skipped_file.close()
print_complete_msg()
if __name__ == "__main__":
main()