-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikipedia_scraper.py
547 lines (510 loc) · 16.5 KB
/
wikipedia_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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import requests
import ural
import re
from urllib.parse import unquote
from requests.exceptions import RequestException
from common.lib.exceptions import ProcessorInterruptedException
class WikipediaSearch:
"""
Wikipedia scraper utility class
"""
language_map = {
"af": "Afrikaans",
"als": "Alemannic German",
"smn": "Inari Sámi",
"atj": "Atikamekw",
"nrm": "Norman",
"ay": "Aymara",
"az": "Azerbaijani",
"cdo": "Eastern Min",
"mwl": "Mirandese",
"an": "Aragonese",
"bar": "Bavarian",
"cs": "Czech",
"cbk-zam": "Zamboanga Chavacano",
"co": "Corsican",
"dga": "Dagaare",
"da": "Danish",
"se": "Northern Sámi",
"de": "German",
"dsb": "Lower Sorbian",
"et": "Estonian",
"eml": "Emilian–Romagnol",
"ang": "Old English",
"en": "English",
"eu": "Basque",
"fat": "Fante",
"hif": "Fiji Hindi",
"fo": "Faroese",
"fy": "West Frisian",
"gag": "Gagauz",
"gl": "Galician",
"gpe": "Ghanaian Pidgin English",
"ext": "Extremaduran",
"guw": "Gun",
"gur": "Farefare (Gurene)",
"haw": "Hawaiian",
"hsb": "Upper Sorbian",
"hr": "Croatian",
"nah": "Nahuatl",
"is": "Icelandic",
"jam": "Jamaican Patois",
"kl": "Greenlandic",
"csb": "Kashubian",
"lt": "Lithuanian",
"li": "Limburgish",
"ln": "Lingala",
"olo": "Livvi-Karelian",
"hu": "Hungarian",
"pcm": "Nigerian Pidgin",
"nl": "Dutch",
"nds-nl": "Dutch Low Saxon",
"jbo": "Lojban",
"frr": "North Frisian",
"pih": "Norfuk",
"nn": "Norwegian (Nynorsk)",
"no": "Norwegian (Bokmål)",
"uz": "Uzbek",
"om": "Oromo",
"pfl": "Palatine German",
"zh-min-nan": "Southern Min",
"pdc": "Pennsylvania Dutch",
"hak": "Hakka Chinese",
"nds": "Low German",
"pl": "Polish",
"kaa": "Karakalpak",
"qu": "Quechua (Southern Quechua)",
"crh": "Crimean Tatar",
"rmy": "Romani (Vlax Romani)",
"sco": "Scots",
"trv": "Seediq",
"stq": "Saterland Frisian",
"simple": "Simple English",
"sk": "Slovak",
"sl": "Slovene",
"szl": "Silesian",
"so": "Somali",
"srn": "Sranan Tongo",
"sh": "Serbo-Croatian",
"fi": "Finnish",
"sv": "Swedish",
"tly": "Talysh",
"zh": "Chinese (vernacular)",
"wuu": "Wu Chinese",
"zh-yue": "Cantonese",
"gan": "Gan Chinese",
"tr": "Turkish",
"tk": "Turkmen",
"ug": "Uyghur",
"roa-tara": "Tarantino",
"gd": "Scottish Gaelic",
"ik": "Iñupiaq",
"fj": "Fijian",
"za": "Zhuang (Standard Zhuang)",
"chy": "Cheyenne",
"vep": "Veps",
"rm": "Romansh",
"fur": "Friulian",
"la": "Latin",
"lfn": "Lingua Franca Nova",
"ga": "Irish",
"ltg": "Latgalian",
"lv": "Latvian",
"eo": "Esperanto",
"lad": "Judaeo-Spanish",
"gn": "Guarani",
"ca": "Catalan",
"ty": "Tahitian",
"fiu-vro": "Võro",
"frp": "Franco-Provençal",
"vo": "Volapük",
"war": "Waray",
"vls": "West Flemish",
"cy": "Welsh",
"tay": "Atayal",
"nv": "Navajo",
"sn": "Shona",
"bm": "Bambara",
"bew": "Betawi",
"scn": "Sicilian",
"mg": "Malagasy",
"pms": "Piedmontese",
"gor": "Gorontalo",
"id": "Indonesian",
"ms": "Malay",
"ace": "Acehnese",
"ban": "Balinese",
"map-bms": "Banyumasan",
"jv": "Javanese",
"su": "Sundanese",
"bbc": "Toba Batak",
"mad": "Madurese",
"ch": "Chamorro",
"ny": "Chewa",
"tum": "Tumbuka",
"dtp": "Dusun",
"br": "Breton",
"wa": "Walloon",
"pt": "Portuguese",
"es": "Spanish",
"fr": "French",
"vec": "Venetian",
"oc": "Occitan",
"ff": "Fula",
"sm": "Samoan",
"ki": "Kikuyu",
"ha": "Hausa",
"to": "Tongan",
"ig": "Igbo",
"tet": "Tetum",
"ia": "Interlingua",
"ie": "Interlingue",
"it": "Italian",
"ro": "Romanian",
"pcd": "Picard",
"lmo": "Lombard",
"sc": "Sardinian",
"xh": "Xhosa",
"zu": "Zulu",
"kg": "Kongo",
"nia": "Nias",
"lij": "Ligurian",
"bi": "Bislama",
"tpi": "Tok Pisin",
"mi": "Māori",
"avk": "Kotava",
"min": "Minangkabau",
"ast": "Asturian",
"bs": "Bosnian",
"pap": "Papiamento",
"nap": "Neapolitan",
"ilo": "Ilocano",
"ve": "Venda",
"lb": "Luxembourgish",
"pag": "Pangasinan",
"lld": "Ladin",
"roa-rup": "Aromanian",
"btm": "Mandailing Batak",
"st": "Sotho",
"nso": "Northern Sotho",
"tn": "Tswana",
"sq": "Albanian",
"ss": "Swazi",
"kab": "Kabyle",
"din": "Dinka",
"vi": "Vietnamese",
"wo": "Wolof",
"sw": "Swahili",
"gv": "Manx",
"pam": "Kapampangan",
"tl": "Tagalog",
"nov": "Novial",
"mt": "Maltese",
"io": "Ido",
"kbp": "Kabiye",
"ku": "Kurdish (Kurmanji)",
"lg": "Luganda",
"rw": "Kinyarwanda",
"rn": "Kirundi",
"ts": "Tsonga",
"sg": "Sango",
"diq": "Zaza",
"gcr": "French Guianese Creole",
"fon": "Fon",
"kw": "Cornish",
"ht": "Haitian Creole",
"ceb": "Cebuano",
"bcl": "Central Bikol",
"yo": "Yoruba",
"guc": "Wayuu",
"bdr": "West Coast Bajau",
"bjn": "Banjarese",
"dag": "Dagbani",
"tw": "Twi",
"igl": "Igala",
"pwn": "Paiwan",
"shi": "Shilha",
"ee": "Ewe",
"kus": "Kusaal",
"ami": "Amis",
"szy": "Sakizaya",
"ksh": "Ripuarian",
"kcg": "Tyap",
"zea": "Zeelandic",
"bat-smg": "Samogitian",
"el": "Greek",
"pnt": "Pontic Greek",
"av": "Avar",
"ady": "Adyghe",
"kbd": "Kabardian",
"ab": "Abkhaz",
"ba": "Bashkir",
"be": "Belarusian (Narkamaŭka)",
"be-tarask": "Belarusian (Taraškievica)",
"bxr": "Buryat (Russia Buriat)",
"bg": "Bulgarian",
"sr": "Serbian",
"tg": "Tajik",
"inh": "Ingush",
"os": "Ossetian",
"kv": "Komi",
"krc": "Karachay-Balkar",
"ky": "Kyrgyz",
"mrj": "Hill Mari",
"kk": "Kazakh",
"lbe": "Lak",
"lez": "Lezgian",
"mk": "Macedonian",
"mdf": "Moksha",
"mn": "Mongolian",
"ce": "Chechen",
"mhr": "Meadow Mari",
"koi": "Komi-Permyak",
"rue": "Rusyn",
"ru": "Russian",
"sah": "Yakut",
"cu": "Old Church Slavonic",
"tt": "Tatar",
"alt": "Southern Altai",
"tyv": "Tuvan",
"udm": "Udmurt",
"uk": "Ukrainian",
"xal": "Kalmyk Oirat",
"cv": "Chuvash",
"myv": "Erzya",
"xmf": "Mingrelian",
"ka": "Georgian",
"hyw": "Western Armenian",
"hy": "Armenian",
"he": "Hebrew",
"yi": "Yiddish",
"ur": "Urdu",
"ps": "Pashto",
"pnb": "Western Punjabi",
"azb": "South Azerbaijani",
"skr": "Saraiki",
"sd": "Sindhi",
"ks": "Kashmiri",
"glk": "Gilaki",
"mzn": "Mazanderani",
"ar": "Arabic",
"ary": "Moroccan Arabic",
"arz": "Egyptian Arabic",
"fa": "Persian",
"ckb": "Kurdish (Sorani)",
"arc": "Aramaic (Syriac)",
"dv": "Maldivian",
"nqo": "N'Ko",
"zgh": "Standard Moroccan Amazigh",
"am": "Amharic",
"ti": "Tigrinya",
"awa": "Awadhi",
"gom": "Konkani (Goan Konkani)",
"dty": "Doteli",
"ne": "Nepali",
"pi": "Pali",
"bh": "Bihari (Bhojpuri)",
"mr": "Marathi",
"mai": "Maithili",
"new": "Newar",
"anp": "Angika",
"sa": "Sanskrit",
"hi": "Hindi",
"as": "Assamese",
"bn": "Bengali",
"bpy": "Bishnupriya Manipuri",
"pa": "Punjabi",
"gu": "Gujarati",
"or": "Odia",
"ta": "Tamil",
"te": "Telugu",
"kn": "Kannada",
"tcy": "Tulu",
"ml": "Malayalam",
"si": "Sinhala",
"mni": "Meitei",
"th": "Thai",
"lo": "Lao",
"bo": "Central Tibetan (Lhasa Tibetan)",
"dz": "Dzongkha",
"bug": "Buginese",
"blk": "Pa'O",
"my": "Burmese",
"shn": "Shan",
"mnw": "Mon",
"km": "Khmer",
"sat": "Santali",
"chr": "Cherokee",
"iu": "Inuktitut",
"cr": "Cree",
"ko": "Korean",
"ja": "Japanese",
"got": "Gothic",
"zh-classical": "Classical Chinese",
}
def wiki_request(self, auth="", *args, **kwargs):
"""
Send a Wikipedia API request
Does some error handling and authentication scaffolding.
:param str auth: Wikipedia API auth key (can be empty)
:param args: Positional arguments are passed to `requests.get`
:param kwargs: Keyword arguments are passed to `requests.get`
:return dict: Parsed JSON response, or `None` if there was an error
"""
if auth:
if "headers" not in kwargs:
kwargs["headers"] = {}
kwargs["headers"]["Authorization"] = f"Bearer {auth}"
try:
result = requests.get(*args, **kwargs)
print(result.text)
if result.status_code != 200:
raise ValueError(f"Wikipedia API request failed ({result.status_code})")
result_json = result.json()
if "error" in result_json:
raise ValueError(
f"Wikipedia API request failed ({result_json['error'].get('info', result_json['error'])})"
)
return result_json
except (ValueError, RequestException) as e:
self.dataset.log(
f"Encountered an error interfacing with the Wikipedia API ({e})"
)
return None
def normalise_pagenames(self, auth, urls):
"""
Normalise Wikipedia page names
Resolves redirects, et cetera, for a list of Wikipedia URLs
:param str auth: Wikipedia API auth key (can be empty)
:param list urls: List of URLs to normalise
:return dict: Dictionary with language codes as keys and a list of
normalised page names in that language as values
"""
parsed_urls = {}
for url in urls:
domain = ural.get_hostname(url)
if not domain or not domain.endswith("wikipedia.org"):
self.dataset.update_status(
f"{url} is not a Wikipedia URL ({domain}), skipping"
)
continue
if domain.startswith("www.") or len(domain.split(".")) == 2:
language = "en"
else:
language = domain.split(".")[0]
# apparently chinese wiki has URLs along the lines of https://zh.wikipedia.org/zh-hans/%F0%9F%8D%91
page = re.split(r"\/(wiki|zh.*)\/", url)
if len(page) < 3:
if "/w/index.php" in url:
page = url.split("title=")[1].split("&")[0]
else:
self.dataset.update_status(
f"{url} is not a Wikipedia URL, skipping"
)
continue
else:
page = page.pop().split("#")[0].split("?")[0]
if not page.strip():
self.dataset.update_status(
f"{url} is not a Wikipedia article URL, skipping"
)
continue
if language not in parsed_urls:
parsed_urls[language] = set()
parsed_urls[language].add(unquote(page))
self.dataset.update_status(
f"Collecting canonical article names for {len(parsed_urls):,} Wikipedia article(s)"
)
# sort by language (so we can batch requests)
result = {}
for language, pages in parsed_urls.items():
api_base = f"https://{language}.wikipedia.org/w/api.php"
pages = list(pages)
canonical_titles = []
self.dataset.update_status(
f"Collecting canonical article names for articles on {language}.wikipedia.org ({self.map_lang(language)})"
)
# get canonical title for URL
while pages:
batch = pages[:50]
pages = pages[50:]
canonical = self.wiki_request(
auth,
api_base,
params={
"action": "query",
"format": "json",
"redirects": "1",
"titles": "|".join(batch),
},
)
if not canonical:
self.dataset.update_status(
"Could not get canonical name for article batch - skipping"
)
continue
for page in canonical["query"]["pages"].values():
canonical_titles.append(page["title"])
result[language] = canonical_titles
return {lang: result[lang] for lang in result if result[lang]}
def get_revisions(self, wiki_apikey, language, page, rvlimit=500):
"""
Get revisions for a given page
This requires paginating through results, so a wrapper function is
useful.
:param str wiki_apikey: Wikipedia API key
:param str language: Wikipedia language
:param str page: Wikipedia page
:param int rvlimit: Maximum number of revisions to return
:return list: List of dictionaries with revision metadata
"""
page_revisions = []
continue_bit = {}
api_base = f"https://{language}.wikipedia.org/w/api.php"
while len(page_revisions) < rvlimit:
if self.interrupted:
raise ProcessorInterruptedException(
"Interrupted while fetching revisions"
)
# loop because we can only get up to 500 revisions per request
revisions_batch = self.wiki_request(
wiki_apikey,
api_base,
params={
"action": "query",
"format": "json",
"prop": "revisions",
"rvlimit": rvlimit,
"titles": page,
**continue_bit,
},
)
self.dataset.update_status(
f"Fetching revision {len(page_revisions):,}-{min(rvlimit, len(page_revisions) + 500):,} for '{page}' ({self.map_lang(language)}/{language})"
)
if not revisions_batch:
self.dataset.update_status(
f"Could not get revisions for {page} from Wikipedia API - skipping"
)
break
for page_id, page_details in revisions_batch["query"]["pages"].items():
if page_id == "-1":
reason = page_details.get("invalidreason", "unknown error")
self.dataset.update_status(
f"Could not fetch revisions for page {page} (Wikipedia said: '{reason}') - halting. Double-check the URL and try again.",
is_final=True,
)
return []
page_revisions += page_details["revisions"]
if revisions_batch.get("continue"):
continue_bit = {"rvcontinue": revisions_batch["continue"]["rvcontinue"]}
else:
break
return page_revisions
def map_lang(self, code):
"""
Get full name of a Wikipedia language, if available
:param str code: Language code, e.g. `nl`
:return str: Language name in English, or the code if unavailable
"""
return self.language_map.get(code, code)