-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_from_wikipedia.py
674 lines (540 loc) · 22.4 KB
/
get_from_wikipedia.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
from pprint import pprint
from urllib.parse import quote, unquote, urlparse
import datetime
import json
from textstat import textstat
import requests
# URLs
URL_INFOS = "https://{lang}.wikipedia.org/w/api.php"
URL_STATS = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/{lang}.wikipedia/{access}/{agent}/{uri_article_name}/{granularity}/{start}/{end}"
# Parameters
HEADERS = {
"User-Agent": "EPFL WikiStats",
"From": "[email protected]",
"Accept": "json",
}
PARAMS = {
"action": "query",
"format": "json",
}
WIKI_LIMIT = 500 # From the API
GLOBAL_LIMIT = WIKI_LIMIT
BACKLINKS_LIMIT = GLOBAL_LIMIT
CONTRIBS_LIMIT = GLOBAL_LIMIT
DEFAULT_DURATION = int(2 * 365.25)
ACCESS = "all-access"
AGENTS = "all-agents"
GRANULARITY = "daily"
VERBOSE = False
DEFAULT_LANGS = ["en", "fr", "de"]
TARGET_DURATION = DEFAULT_DURATION
def get_session():
# Starts request session, that will be used through the whole process
s = requests.Session()
s.headers.update(HEADERS)
s.params.update(PARAMS)
return s
s = get_session()
def extract_lang_name(link: str) -> tuple[str, str]:
"""
Extract name and lang
"""
if "//" not in link:
link = f"//{link}"
parsed = urlparse(link)
lang = parsed.hostname.split(".")[0]
name = parsed.path.split("/")[-1]
return lang, unquote(name)
def qprint(json_queries):
"""
Check if correct JSON and prints.
"""
print(json.dumps(json_queries, indent=2))
def wiki_quote(page_name):
"""
Transform into valid wiki URI.
"""
return quote(page_name.replace(" ", "_"))
def links_to_find(target_links, target_langs=None):
if target_langs is None:
target_langs = DEFAULT_LANGS
to_find = {}
for link in target_links:
if link == "":
continue
link = link.replace('"', "").replace("'", "").replace(",", "") # If copypasta from Python
# If it's a link, extract lang and name
if "wikipedia.org" in link:
lang, name = extract_lang_name(link)
if lang not in to_find:
to_find[lang] = set()
to_find[lang].add(name)
# Else, we assume it's directly a name, and try to find it
else:
if "*" not in to_find:
to_find["*"] = set()
to_find["*"].add(link)
# Each name without a lang will be tracked down using target langs
if "*" in to_find:
for name in to_find["*"]:
for lang in target_langs:
if lang not in to_find:
to_find[lang] = set()
to_find[lang].add(name)
del to_find["*"]
if VERBOSE:
pprint(to_find)
return to_find
def fetch_data(to_find, target_langs=None):
# Check if the page exists, gather information if it does
# https://www.mediawiki.org/wiki/API:Info
# https://www.mediawiki.org/wiki/API:Langlinks
if target_langs is None:
target_langs = DEFAULT_LANGS
queries = {}
for lang, names in to_find.items():
url_full = URL_INFOS.format(lang=lang)
# We group the queries per target lang for less queries
titles = "|".join(names)
params = {
"titles": titles,
"prop": "info|langlinks",
"lllimit": WIKI_LIMIT, # We want all langs in order to find our target langs
}
results = s.get(url=url_full, params=params)
data = results.json()
if "query" in data and "pages" in data["query"]:
data = data["query"]["pages"]
for pid, obj in data.items():
title = obj["title"]
# Will only keep the latest successful query for same name pages
queries[title] = {
"query": {
"lang": lang,
}
}
# Page was not found with that language
if int(pid) < 0:
queries[title]["error"] = "not found"
continue
queries[title]["query"].update(
{
"pid": int(pid),
"timestamp": datetime.datetime.today().isoformat(),
"duration": TARGET_DURATION,
}
)
# Add the query language in the list of langs
queries[title]["langs"] = {
lang: {
"name": title,
}
}
# Add the other target langs
if "langlinks" in obj:
for langlink in obj["langlinks"]:
if not target_langs or langlink["lang"] in target_langs: # Use all langs if no target lang
queries[title]["langs"][langlink["lang"]] = {"name": langlink["*"]}
if VERBOSE:
qprint(queries)
# Merge linked pages with different names
# We assume here that pages are correctly linked (by Wikipedia) between each other
next_queries = {}
for name, obj in queries.items():
if "error" in obj:
next_queries[name] = obj
continue
skip = False
for _, page in obj["langs"].items():
if page["name"] in next_queries.keys():
skip = True
break
if not skip:
next_queries[name] = obj
queries = next_queries
if VERBOSE:
qprint(queries)
# Dirty hack to get the short description don't judge me
for name, obj in queries.items():
if "error" in obj:
continue
for lang, page in obj["langs"].items():
data = s.get(
url=f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{wiki_quote(page['name'])}?redirect=true"
).json()
if "description" in data:
page["description"] = data["description"]
else:
page["description"] = None
if VERBOSE:
qprint(queries)
return queries
def fetch_backlinks(queries):
# Find the backlinks for each
# For important pages (looking at you, "École polytechnique fédérale de Lausanne"), can take some time!
# Set BACKLINKS_LIMIT to control that.
# https://www.mediawiki.org/wiki/API:Backlinks
for name, obj in queries.items():
if "error" in obj:
continue
for lang, page in obj["langs"].items():
blcontinue = ""
blcounter = 0
url_full = URL_INFOS.format(lang=lang)
while blcounter < BACKLINKS_LIMIT:
params = {
"list": "backlinks",
"bltitle": page["name"],
"bllimit": min(BACKLINKS_LIMIT, WIKI_LIMIT),
}
if blcontinue != "":
params["blcontinue"] = blcontinue
results = s.get(url=url_full, params=params)
data = results.json()
if "query" in data and "backlinks" in data["query"]:
bldata = data["query"]["backlinks"]
else:
obj["error"] = "could not retrieve information (backlinks)"
break
if "backlinks" not in page:
page["backlinks"] = set() # This is to delete doubles
if bldata:
for backlink in bldata:
page["backlinks"].add(backlink["title"])
blcounter += 1
if "continue" in data:
blcontinue = data["continue"]["blcontinue"]
else:
break
if "backlinks" in page and isinstance(page["backlinks"], set):
page["backlinks"] = list(page["backlinks"]) # Sets are not valid JSON objects, lists are
if VERBOSE:
qprint(queries)
return queries
def fetch_pageprops_revisions(queries):
# Get some of the missing information
# https://www.mediawiki.org/wiki/API:Pageprops
# https://www.mediawiki.org/wiki/API:Revisions
for name, obj in queries.items():
if "error" in obj:
continue
for lang, page in obj["langs"].items():
url_full = URL_INFOS.format(lang=lang)
params = {
"titles": page["name"],
"prop": "pageprops|revisions",
"rvlimit": 1,
"rvprop": "timestamp|user",
"rvdir": "newer",
}
results = s.get(url=url_full, params=params)
data = results.json()
if "query" in data and "pages" in data["query"]:
content = data["query"]["pages"]
pid = next(iter(content))
page["pid"] = int(pid)
content = content[pid]
if "pageprops" in content and "wikibase_item" in content["pageprops"]:
page["pwikidata"] = content["pageprops"]["wikibase_item"]
else:
page["pwikidata"] = None
page["creation"] = {
"timestamp": content["revisions"][0]["timestamp"],
"user": content["revisions"][0]["user"],
}
else:
obj["error"] = "could not retrieve information (props)"
if VERBOSE:
qprint(queries)
return queries
def fetch_contributors(queries, target_contributors=None):
# Contributors
# https://www.mediawiki.org/wiki/API:Contributors
for name, obj in queries.items():
if "error" in obj:
continue
for lang, page in obj["langs"].items():
pccontinue = ""
pccounter = 0
url_full = URL_INFOS.format(lang=lang)
params = {
"titles": page["name"],
"prop": "contributors",
"pclimit": min(CONTRIBS_LIMIT, WIKI_LIMIT),
}
while pccounter < CONTRIBS_LIMIT:
if pccontinue != "":
params["pccontinue"] = pccontinue
results = s.get(url=url_full, params=params)
data = results.json()
if "query" in data and "pages" in data["query"]:
pcdata = data["query"]["pages"][str(page["pid"])]
else:
obj["error"] = "could not retrieve information (contributors)"
break
if "contributors" not in page:
page["contributors"] = set() # Data should already be a set, but I'm being cautious
if pcdata:
page["contributors"].update(
[
contributor["name"]
for contributor in pcdata["contributors"]
if not target_contributors or contributor["name"] in target_contributors
# Use all contributors if no target contributors are specified
]
)
pccounter += len(pcdata["contributors"])
if "continue" in data:
pccontinue = data["continue"]["pccontinue"]
else:
break
if "contributors" in page and isinstance(page["contributors"], set):
page["contributors"] = list(page["contributors"]) # Sets are not valid JSON objects, lists are
if VERBOSE:
qprint(queries)
return queries
def fetch_contributions(queries):
# Contributions
# https://www.mediawiki.org/wiki/API:Revisions
for name, obj in queries.items():
if "error" in obj:
continue
for lang, page in obj["langs"].items():
rvcontinue = ""
url_full = URL_INFOS.format(lang=lang)
params = {
"titles": page["name"],
"prop": "revisions",
"rvprop": "ids|timestamp|user|size",
"rvstart": obj["query"]["timestamp"],
"rvend": (
datetime.datetime.fromisoformat(obj["query"]["timestamp"])
- datetime.timedelta(days=obj["query"]["duration"])
).isoformat(),
"rvdir": "older", # rvstart has to be later than rvend with that mode
"rvlimit": WIKI_LIMIT,
}
while True:
if rvcontinue != "":
params["rvcontinue"] = rvcontinue
results = s.get(url=url_full, params=params)
data = results.json()
if (
"query" in data
and "pages" in data["query"]
and "revisions" in data["query"]["pages"][str(page["pid"])]
):
rvdata = data["query"]["pages"][str(page["pid"])]["revisions"]
else:
obj["error"] = "could not retrieve information (contributions)"
break
if "contributions" not in page:
page["contributions"] = {
"items": [],
}
if rvdata:
for revision in rvdata:
page["contributions"]["items"].append(
{
"revid": revision["revid"],
"parentid": revision["parentid"],
"timestamp": revision["timestamp"],
"username": revision["user"],
"size": revision["size"],
}
)
if "continue" in data:
rvcontinue = data["continue"]["rvcontinue"]
else:
break
if VERBOSE:
qprint(queries)
return queries
def fetch_pageviews(queries):
# Pageviews
# https://wikimedia.org/api/rest_v1/#/Pageviews%20data/get_metrics_pageviews_per_article__project___access___agent___article___granularity___start___end_
for name, obj in queries.items():
if "error" in obj:
continue
for lang, page in obj["langs"].items():
url_full = URL_STATS.format(
lang=lang,
access=ACCESS,
agent=AGENTS,
uri_article_name=wiki_quote(page["name"]),
granularity=GRANULARITY,
start=(
datetime.datetime.fromisoformat(obj["query"]["timestamp"])
- datetime.timedelta(days=obj["query"]["duration"])
).strftime("%Y%m%d00"),
end=datetime.datetime.fromisoformat(obj["query"]["timestamp"]).strftime("%Y%m%d00"),
)
results = s.get(url=url_full)
data = results.json()
if "items" in data:
if "pageviews" not in page:
page["pageviews"] = {
"granularity": GRANULARITY,
"access": ACCESS,
"agent": AGENTS,
"items": [],
}
total_views = 0
for item in data["items"]:
page["pageviews"]["items"].append(
{
"timestamp": datetime.datetime.strptime(item["timestamp"], "%Y%m%d%H").isoformat(),
"views": item["views"],
}
)
total_views += item["views"]
page["pageviews_total"] = total_views
else:
obj["error"] = "could not retrieve information (pageviews)"
continue
if VERBOSE:
qprint(queries)
return queries
def fetch_text_and_stats(queries):
for name, obj in queries.items():
if "error" in obj:
continue
for lang, page in obj["langs"].items():
excontinue = ""
url_full = URL_INFOS.format(lang=lang)
params = {
"titles": page["name"],
"prop": "extracts",
"explaintext": 1,
"exsectionformat": "plain",
}
while True:
if excontinue != "":
params["excontinue"] = excontinue
results = s.get(url=url_full, params=params)
data = results.json()
if (
"query" in data
and "pages" in data["query"]
and "extract" in data["query"]["pages"][str(page["pid"])]
):
exdata = data["query"]["pages"][str(page["pid"])]["extract"]
else:
obj["error"] = "could not retrieve information (extract)"
break
if "extract" not in page:
page["extract"] = ""
if exdata:
page["extract"] += exdata
if "continue" in data:
excontinue = data["continue"]["excontinue"]
else:
break
if "extract" in page and page["extract"]:
# _, _, num_words, _, num_sentences = stats(page["extract"], lang) # Legacy
page["stats"] = {
"num_words": textstat.lexicon_count(page["extract"]),
"num_sentences": textstat.sentence_count(page["extract"]),
"reading_time": textstat.reading_time(page["extract"]),
}
# Using textstat
# Here, "min" means harder to read, while "max" means easier to read
# "minimum readability" vs. "maximum readability"
textstat.set_lang(lang)
page["readability"] = {
"fres": {
"name": "Flesch Reading Ease Score",
"link": "https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests#Flesch_reading_ease",
"result": textstat.flesch_reading_ease(page["extract"]),
"min": 0,
"max": 100,
}
}
if lang == "it":
page["readability"]["it_gi"] = {
"name": "Gulpease Index",
"link": "https://it.wikipedia.org/wiki/Indice_Gulpease",
"result": textstat.gulpease_index(page["extract"]),
"min": 0,
"max": 100,
}
if lang == "de":
page["readability"]["de_ws"] = {
"name": "Wiener Sachtextformel",
"link": "https://de.wikipedia.org/wiki/Lesbarkeitsindex#Wiener_Sachtextformel",
"result": textstat.wiener_sachtextformel(page["extract"], 1), # What are the variants?
"min": 15,
"max": 4,
}
# Legacy
# page["readability"] = {
# "fres": flesch(page["extract"], lang),
# "fkgl": flesch_kincaid(page["extract"], lang),
# "ari": automated_readability_index(page["extract"], lang),
# "smog": smog_grade(page["extract"], lang),
# "cli": coleman_liau_index(page["extract"], lang),
# "gfi": gunning_fog_index(page["extract"], lang),
# }
# mean = 0
# for _, score in page["readability"].items():
# mean += score
# page["readability"]["mean"] = mean / len(page["readability"])
if VERBOSE:
qprint(queries)
return queries
def fetch_page_assessments(queries):
for name, obj in queries.items():
if "error" in obj:
continue
for lang, page in obj["langs"].items():
url_full = URL_INFOS.format(lang=lang)
params = {
"titles": page["name"],
"prop": "pageassessments",
}
results = s.get(url=url_full, params=params)
data = results.json()
print(data)
if (
"query" in data
and "pages" in data["query"]
and "pageassessments" in data["query"]["pages"][str(page["pid"])]
):
page["pageassessments"] = data["query"]["pages"][str(page["pid"])]["pageassessments"]
def get_from_wikipedia(target_links, target_langs=None, target_contributors=None):
if target_langs is None:
target_langs = DEFAULT_LANGS
to_find = links_to_find(target_links, target_langs)
queries = fetch_data(to_find, target_langs)
fetch_backlinks(queries)
fetch_pageprops_revisions(queries)
fetch_contributors(queries, target_contributors)
fetch_contributions(queries)
fetch_pageviews(queries)
fetch_text_and_stats(queries)
fetch_page_assessments(queries)
return queries
def main():
# Links are provided
target_links = [
"https://fr.wikipedia.org/wiki/Martin_Vetterli",
"https://en.wikipedia.org/wiki/%C3%89cole_Polytechnique_F%C3%A9d%C3%A9rale_de_Lausanne",
"https://en.wikipedia.org/wiki/Jean-Pierre_Hubaux",
"https://it.wikipedia.org/wiki/Jean-Michel_LErreur",
"https://fr.wikipedia.org/wiki/%C3%89cole_polytechnique_f%C3%A9d%C3%A9rale_de_Lausanne",
"it.wikipedia.org/wiki/Jean-Michel_LErreur",
"Michael_Grätzel",
"Patrick Aebischer",
]
# Chosen Langs
target_langs = ["en", "fr", "de"]
# Chosen contributors
target_contributors = []
# Length for the revisions, in days
target_duration = DEFAULT_DURATION
queries = get_from_wikipedia(target_links, target_langs, target_contributors)
import json
with open("webapp/samples/results.json", "w", encoding="utf8") as f:
json.dump(queries, f, ensure_ascii=False, indent=4)
if __name__ == "__main__":
main()