/
wikifunctions.py
1044 lines (830 loc) · 43.1 KB
/
wikifunctions.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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pandas as pd
from datetime import datetime
from bs4 import BeautifulSoup
from urllib.parse import unquote, quote
from copy import deepcopy
import requests, re
def response_to_revisions(json_response):
if type(json_response['query']['pages']) == dict:
page_id = list(json_response['query']['pages'].keys())[0]
return json_response['query']['pages'][page_id]['revisions']
elif type(json_response['query']['pages']) == list:
if 'revisions' in json_response['query']['pages'][0]:
return json_response['query']['pages'][0]['revisions']
else:
return list()
else:
raise ValueError("There are no revisions in the JSON")
def get_all_page_revisions(page_title, endpoint='en.wikipedia.org/w/api.php', redirects=1):
"""Takes Wikipedia page title and returns a DataFrame of revisions
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - a Boolean value for whether to follow redirects to another page
Returns:
df - a pandas DataFrame where each row is a revision and columns correspond
to meta-data such as parentid, revid, sha1, size, timestamp, and user name
"""
# A container to store all the revisions
revision_list = list()
# Set up the query
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['titles'] = page_title
query_params['prop'] = 'revisions'
query_params['rvprop'] = 'ids|userid|comment|timestamp|user|size|sha1'
query_params['rvlimit'] = 500
query_params['rvdir'] = 'newer'
query_params['format'] = 'json'
query_params['redirects'] = redirects
query_params['formatversion'] = 2
# Make the query
json_response = requests.get(url = query_url, params = query_params).json()
# Add the temporary list to the parent list
revision_list += response_to_revisions(json_response)
# Loop for the rest of the revisions
while True:
# Newer versions of the API return paginated results this way
if 'continue' in json_response:
query_continue_params = deepcopy(query_params)
query_continue_params['rvcontinue'] = json_response['continue']['rvcontinue']
json_response = requests.get(url = query_url, params = query_continue_params).json()
revision_list += response_to_revisions(json_response)
# Older versions of the API return paginated results this way
elif 'query-continue' in json_response:
query_continue_params = deepcopy(query_params)
query_continue_params['rvstartid'] = json_response['query-continue']['revisions']['rvstartid']
json_response = requests.get(url = query_url, params = query_continue_params).json()
revision_list += response_to_revisions(json_response)
# If there are no more revisions, stop
else:
break
# Convert to a DataFrame
df = pd.DataFrame(revision_list)
# Add in some helpful fields to the DataFrame
final_title = json_response['query']['pages'][0]['title']
df['page'] = final_title
df['userid'] = df['userid'].fillna(0).apply(lambda x:str(int(x)))
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['date'] = df['timestamp'].apply(lambda x:x.date())
df['diff'] = df['size'].diff()
df['lag'] = df['timestamp'].diff()/pd.Timedelta(1,'s')
df['age'] = (df['timestamp'] - df['timestamp'].min())/pd.Timedelta(1,'d')
return df
def get_page_revisions_from_date(page_title, endpoint='en.wikipedia.org/w/api.php', redirects=1, start='2001-01-01',stop='today'):
"""Takes Wikipedia page title and returns a DataFrame of revisions
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - a Boolean value for whether to follow redirects to another page
start - a string, datetime, or Timestamp object when revisions should start
stop - a string, datetime, or Timestamp object when revisions should stop
Returns:
df - a pandas DataFrame where each row is a revision and columns correspond
to meta-data such as parentid, revid, sha1, size, timestamp, and user name
"""
# A container to store all the revisions
revision_list = list()
# Fix dates
start = datetime.strftime(pd.to_datetime(start), '%Y-%m-%dT%H:%M:%SZ')
stop = datetime.strftime(pd.to_datetime(stop), '%Y-%m-%dT%H:%M:%SZ')
# Set up the query
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['titles'] = page_title
query_params['prop'] = 'revisions'
query_params['rvprop'] = 'ids|userid|comment|timestamp|user|size|sha1'
query_params['rvlimit'] = 500
query_params['rvdir'] = 'newer'
query_params['format'] = 'json'
query_params['redirects'] = redirects
query_params['rvstart'] = start
query_params['rvend'] = stop
query_params['formatversion'] = 2
# Make the query
json_response = requests.get(url = query_url, params = query_params).json()
# Add the temporary list to the parent list
revision_list += response_to_revisions(json_response)
# Loop for the rest of the revisions
while True:
# Newer versions of the API return paginated results this way
if 'continue' in json_response:
query_continue_params = deepcopy(query_params)
query_continue_params['rvcontinue'] = json_response['continue']['rvcontinue']
json_response = requests.get(url = query_url, params = query_continue_params).json()
revision_list += response_to_revisions(json_response)
# Older versions of the API return paginated results this way
elif 'query-continue' in json_response:
query_continue_params = deepcopy(query_params)
query_continue_params['rvstartid'] = json_response['query-continue']['revisions']['rvstartid']
json_response = requests.get(url = query_url, params = query_continue_params).json()
revision_list += response_to_revisions(json_response)
# If there are no more revisions, stop
else:
break
# Convert to a DataFrame
df = pd.DataFrame(revision_list)
# Add in some helpful fields to the DataFrame
final_title = json_response['query']['pages'][0]['title']
df['page'] = final_title
df['userid'] = df['userid'].fillna(0).apply(lambda x:str(int(x)))
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['date'] = df['timestamp'].apply(lambda x:x.date())
df['diff'] = df['size'].diff()
df['lag'] = df['timestamp'].diff()/pd.Timedelta(1,'s')
df['age'] = (df['timestamp'] - df['timestamp'].min())/pd.Timedelta(1,'d')
return df
def chunks(l, n=50):
"""
Yield successive n-sized chunks from l.
Adapted from: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
"""
for i in range(0, len(l), n):
yield l[i:i + n]
def get_redirects_linking_here(page_title, endpoint="en.wikipedia.org/w/api.php", namespace=0):
"""Takes a page title and returns a list of redirects linking to the page
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
namespace - limit to pages from a specific namespace, defaults to 0
Returns:
linkshere - a list of strings with the redirect titles
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
lh_list = list()
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['titles'] = page_title
query_params['prop'] = 'linkshere'
query_params['lhprop'] = 'title|redirect'
query_params['lhnamespace'] = namespace
query_params['lhshow'] = 'redirect'
query_params['lhlimit'] = 500
query_params['format'] = 'json'
query_params['formatversion'] = 2
# Make the query
json_response = requests.get(url = query_url, params = query_params).json()
if 'linkshere' in json_response['query']['pages'][0]:
subquery_lh_list = json_response['query']['pages'][0]['linkshere']
lh_list += subquery_lh_list
while True:
if 'continue' not in json_response:
break
else:
query_continue_params = deepcopy(query_params)
query_continue_params['lhcontinue'] = json_response['continue']['lhcontinue']
json_response = requests.get(url = query_url, params = query_continue_params).json()
subquery_lh_list = json_response['query']['pages'][0]['linkshere']
lh_list += subquery_lh_list
return [i['title'] for i in lh_list]
def get_redirects_map(page_list, endpoint="en.wikipedia.org/w/api.php"):
redirects_d = {}
chunked = list(chunks(page_list,50))
for chunk in chunked:
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['prop'] = 'info'
query_params['titles'] = '|'.join(chunk)
query_params['redirects'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url=query_url,params=query_params).json()
if 'redirects' in json_response['query']:
mapping = {redir['from']:redir['to'] for redir in json_response['query']['redirects']}
redirects_d.update(mapping)
return redirects_d
def resolve_redirects(page_list,endpoint="en.wikipedia.org/w/api.php"):
resolved_page_list = []
chunked = list(chunks(page_list,50))
for chunk in chunked:
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['prop'] = 'info'
query_params['titles'] = '|'.join(chunk)
query_params['redirects'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url=query_url,params=query_params).json()
if 'pages' in json_response['query']:
pages = [page['title'] for page in json_response['query']['pages']]
resolved_page_list += pages
return resolved_page_list
def get_page_raw_content(page_title, endpoint='en.wikipedia.org/w/api.php', redirects=1):
"""Takes a page title and returns the raw HTML.
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
Returns:
outlinks_per_lang - a dictionary keyed by language returning a dictionary
keyed by page title returning a list of outlinks
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
#req = requests.get('https://{2}.wikipedia.org/w/api.php?action=parse&format=json&page={0}&redirects={1}&prop=text&disableeditsection=1&disabletoc=1'.format(page_title,redirects,lang))
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'parse'
query_params['page'] = page_title
query_params['redirects'] = redirects
query_params['prop'] = 'text'
query_params['disableeditsection'] = 1
query_params['disabletoc'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'parse' in json_response.keys():
markup = json_response['parse']['text']
final_title = json_response['parse']['title']
else:
markup = str()
final_title = page_title
return markup
def parse_to_links(input,is_json=True):
# Initialize an empty list to store the links
outlinks_list = []
if is_json:
page_html = input['parse']['text']#['*']
else:
page_html = input
# Parse the HTML into Beautiful Soup
soup = BeautifulSoup(page_html,'lxml')
# Remove sections at end
bad_sections = ['See_also','Notes','References','Bibliography','External_links']
bad_titles = ['Special:','Wikipedia:','Help:','Template:','Category:','International Standard','Portal:','s:','File:','Digital object identifier','(page does not exist)']
sections = soup.find_all('h2')
for section in sections:
if section.span['id'] in bad_sections:
# Clean out the divs
div_siblings = section.find_next_siblings('div')
for sibling in div_siblings:
sibling.clear()
# Clean out the ULs
ul_siblings = section.find_next_siblings('ul')
for sibling in ul_siblings:
sibling.clear()
# Delete tags associated with templates
for tag in soup.find_all('tr'):
tag.replace_with('')
# For each paragraph tag, extract the titles within the links
for para in soup.find_all('p'):
for link in para.find_all('a'):
if link.has_attr('title'):
title = link['title']
# Ignore links that aren't interesting or are redlinks
if all(bad not in title for bad in bad_titles) and 'redlink' not in link['href']:
outlinks_list.append(title)
# For each unordered list, extract the titles within the child links
for unordered_list in soup.find_all('ul'):
for item in unordered_list.find_all('li'):
for link in item.find_all('a'):
if link.has_attr('title'):
title = link['title']
# Ignore links that aren't interesting or are redlinks
if all(bad not in title for bad in bad_titles) and 'redlink' not in link['href']:
outlinks_list.append(title)
return outlinks_list
def get_revision_raw_content(revid, endpoint='en.wikipedia.org/w/api.php', redirects=1):
"""Takes a revision ID and returns the raw HTML.
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
Returns:
outlinks_per_lang - a dictionary keyed by language returning a dictionary
keyed by page title returning a list of outlinks
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
#req = requests.get('https://{2}.wikipedia.org/w/api.php?action=parse&format=json&page={0}&redirects={1}&prop=text&disableeditsection=1&disabletoc=1'.format(page_title,redirects,lang))
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'parse'
query_params['oldid'] = revid
query_params['prop'] = 'text'
query_params['disableeditsection'] = 1
query_params['disabletoc'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'parse' in json_response.keys():
markup = json_response['parse']['text']
final_title = json_response['parse']['title']
else:
markup = str()
final_title = page_title
return markup
def get_page_outlinks(page_title, endpoint='en.wikipedia.org/w/api.php', redirects=1):
"""Takes a page title and returns a list of wiki-links on the page. The
list may contain duplicates and the position in the list is approximately
where the links occurred.
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
Returns:
outlinks_per_lang - a dictionary keyed by language returning a dictionary
keyed by page title returning a list of outlinks
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
#req = requests.get('https://{2}.wikipedia.org/w/api.php?action=parse&format=json&page={0}&redirects={1}&prop=text&disableeditsection=1&disabletoc=1'.format(page_title,redirects,lang))
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'parse'
query_params['page'] = page_title
query_params['redirects'] = redirects
query_params['prop'] = 'text'
query_params['disableeditsection'] = 1
query_params['disabletoc'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'parse' in json_response.keys():
links = parse_to_links(json_response)
final_title = json_response['parse']['title']
else:
links = list()
final_title = page_title
return links
def get_revision_outlinks(revid, endpoint='en.wikipedia.org/w/api.php'):
"""Takes a page title and returns a list of wiki-links on the page. The
list may contain duplicates and the position in the list is approximately
where the links occurred.
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
Returns:
outlinks_per_lang - a dictionary keyed by language returning a dictionary
keyed by page title returning a list of outlinks
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
#req = requests.get('https://{2}.wikipedia.org/w/api.php?action=parse&format=json&page={0}&redirects={1}&prop=text&disableeditsection=1&disabletoc=1'.format(page_title,redirects,lang))
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'parse'
query_params['oldid'] = revid
query_params['prop'] = 'text'
query_params['disableeditsection'] = 1
query_params['disabletoc'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'parse' in json_response.keys():
return parse_to_links(json_response)
else:
return list()
return links
def get_page_externallinks(page_title, endpoint='en.wikipedia.org/w/api.php', redirects=1):
"""Takes a revision id and returns a list of external links on the revision
revid - a numeric revision id as a string
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
parse - 1 or 0 for whether to return the raw HTML or paragraph text
Returns:
str - a list of strings with the URLs
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
# req = requests.get('https://{2}.wikipedia.org/w/api.php?action=parse&format=json&oldid={0}&redirects={1}&prop=externallinks&disableeditsection=1&disabletoc=1'.format(revid,redirects,lang))
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'parse'
query_params['page'] = page_title
query_params['redirects'] = redirects
query_params['prop'] = 'externallinks'
query_params['disableeditsection'] = 1
query_params['disabletoc'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'parse' in json_response.keys():
if 'externallinks' in json_response['parse']:
links = json_response['parse']['externallinks']
final_title = json_response['parse']['title']
else:
links = list()
final_title = page_title
return links
def get_revision_externallinks(revid, endpoint='en.wikipedia.org/w/api.php', redirects=1):
"""Takes a revision id and returns a list of external links on the revision
revid - a numeric revision id as a string
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
parse - 1 or 0 for whether to return the raw HTML or paragraph text
Returns:
str - a list of strings with the URLs
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
# req = requests.get('https://{2}.wikipedia.org/w/api.php?action=parse&format=json&oldid={0}&redirects={1}&prop=externallinks&disableeditsection=1&disabletoc=1'.format(revid,redirects,lang))
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'parse'
query_params['oldid'] = revid
query_params['prop'] = 'externallinks'
query_params['disableeditsection'] = 1
query_params['disabletoc'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'parse' in json_response.keys():
if 'externallinks' in json_response['parse']:
links = json_response['parse']['externallinks']
final_title = json_response['parse']['title']
else:
links = list()
final_title = page_title
return links
def parse_to_text(input,is_json=True,parse_text=True):
if is_json:
page_html = input['parse']['text']#['*']
else:
page_html = input
# Parse the HTML into Beautiful Soup
soup = BeautifulSoup(page_html,'lxml')
# Remove sections at end
bad_sections = ['See_also','Notes','References','Bibliography','External_links']
sections = soup.find_all('h2')
for section in sections:
if section.span['id'] in bad_sections:
# Clean out the divs
div_siblings = section.find_next_siblings('div')
for sibling in div_siblings:
sibling.clear()
# Clean out the ULs
ul_siblings = section.find_next_siblings('ul')
for sibling in ul_siblings:
sibling.clear()
# Get all the paragraphs
paras = soup.find_all('p')
text_list = []
for para in paras:
if parse_text:
_s = para.text
# Remove the citations
_s = re.sub(r'\[[0-9]+\]','',_s)
text_list.append(_s)
else:
text_list.append(str(para))
return '\n'.join(text_list)
def get_page_content(page_title, endpoint='en.wikipedia.org/w/api.php', redirects=1, parsed_text=1):
"""Takes a page_title and returns a (large) plaintext string of the content
of the revision.
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
parse - 1 to return plain text or 0 to return raw HTML
Returns:
str - a (large) plaintext string of the content of the revision
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'parse'
query_params['page'] = page_title
query_params['redirects'] = redirects
query_params['prop'] = 'text'
query_params['disableeditsection'] = 1
query_params['disabletoc'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'parse' in json_response.keys():
return parse_to_text(json_response,parsed_text)
def get_revision_content(revid,endpoint='en.wikipedia.org/w/api.php',parsed_text=1):
"""Takes a page_title and returns a (large) plaintext string of the content
of the revision.
revid - the revision ID of a revision on a wiki project
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
parse - 1 to return plain text or 0 to return raw HTML
Returns:
str - a (large) plaintext string of the content of the revision
"""
# Get the response from the API for a query
# After passing a page title, the API returns the HTML markup of the current article version within a JSON payload
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'parse'
query_params['oldid'] = revid
query_params['prop'] = 'text'
query_params['disableeditsection'] = 1
query_params['disabletoc'] = 1
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'parse' in json_response.keys():
return parse_to_text(json_response,parsed_text)
def get_page_redirects(page_list,endpoint='en.wikipedia.org/w/api.php'):
chunked_page_list = list(chunks(page_list))
page_redirects = {}
for chunk in chunked_page_list:
page_titles = '|'.join(chunk)
# Set up the query
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['titles'] = page_titles
query_params['prop'] = 'info'
query_params['format'] = 'json'
query_params['redirects'] = 1
query_params['formatversion'] = 2
# Make the query
json_response = requests.get(url = query_url, params = query_params).json()
# Add the redirects to the dictionary
if 'redirects' in json_response['query']:
for _rd in json_response['query']['redirects']:
page_redirects[_rd['from']] = _rd['to']
# Include the non-redirects for the sake of completeness
for page in page_list:
if page not in page_redirects:
page_redirects[page] = page
return page_redirects
def get_interlanguage_links(page_title, endpoint='en.wikipedia.org/w/api.php', redirects=1):
"""The function accepts a page_title and returns a dictionary containing
the title of the page in its other languages
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
redirects - 1 or 0 for whether to follow page redirects, defaults to 1
Returns:
langlink_dict - a dictionary keyed by lang codes and page title as values
"""
#query_string = "https://{1}.wikipedia.org/w/api.php?action=query&format=json&prop=langlinks&formatversion=2&titles={0}&llprop=autonym|langname&lllimit=500".format(page_title,lang)
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['prop'] = 'langlinks'
query_params['titles'] = page_title
query_params['redirects'] = redirects
query_params['llprop'] = 'autonym|langname'
query_params['lllimit'] = 500
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url=query_url,params=query_params).json()
interlanguage_link_dict = dict()
start_lang = endpoint.split('.')[0]
if 'title' in json_response['query']['pages'][0]:
final_title = json_response['query']['pages'][0]['title']
interlanguage_link_dict[start_lang] = final_title
else:
final_title = page_title
interlanguage_link_dict[start_lang] = final_title
if 'langlinks' in json_response['query']['pages'][0]:
langlink_dict = json_response['query']['pages'][0]['langlinks']
for d in langlink_dict:
lang = d['lang']
title = d['title']
interlanguage_link_dict[lang] = title
return interlanguage_link_dict
def get_pageviews(page_title,endpoint='en.wikipedia.org',start='20150701',stop='today',useragent='brian.keegan@colorado.edu'):
"""Takes Wikipedia page title and returns a all the various pageview records
page_title - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
start - a date string in a YYYYMMDD format, defaults to 20150701 (earliest date)
stop - a date string in a YYYYMMDD format, defaults to today
Returns:
df - a DataFrame indexed by date and multi-columned by agent and access type
"""
quoted_page_title = quote(page_title, safe='')
date_from = datetime.strftime(pd.to_datetime(start),'%Y%m%d')
date_to = datetime.strftime(pd.to_datetime(stop),'%Y%m%d')
#for access in ['all-access','desktop','mobile-app','mobile-web']:
#for agent in ['all-agents','user','spider','bot']:
s = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/{1}/{2}/{3}/{0}/daily/{4}/{5}".format(quoted_page_title,endpoint,'all-access','user',date_from,date_to)
headers = {'User-Agent':useragent}
json_response = requests.get(s,headers=headers).json()
if 'items' in json_response:
df = pd.DataFrame(json_response['items'])
else:
raise KeyError('There is no "items" key in the JSON response.')
df = df[['timestamp','views']]
df['timestamp'] = pd.to_datetime(df['timestamp'],format='%Y%m%d%H')
s = df.set_index('timestamp')['views']
return s
def get_category_memberships(page_title,endpoint='en.wikipedia.org/w/api.php'):
"""The function accepts a page_title and returns a list of categories
the page is a member of
category_title - a string of the page name
Returns:
members - a list containing strings of the categories of which the page is a mamber
"""
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['prop'] = 'categories'
query_params['titles'] = page_title
query_params['clprop'] = 'timestamp'
query_params['clshow'] = '!hidden'
query_params['cllimit'] = 500
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url=query_url,params=query_params).json()
categories = list()
if 'pages' in json_response['query']:
if 'categories' in json_response['query']['pages'][0]:
for category in json_response['query']['pages'][0]['categories']:
title = category['title']#.split(':')[1]
categories.append(title)
#timestamp = category['timestamp']
#categories.append({title:timestamp})
return categories
def get_category_subcategories(category_title,endpoint='en.wikipedia.org/w/api.php'):
"""The function accepts a category_title and returns a list of the category's sub-categories
category_title - a string (including "Category:" prefix) of the category name
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
Returns:
members - a list containing strings of the sub-categories in the category
"""
# Replace spaces with underscores
category_title = category_title.replace(' ','_')
# Make sure "Category:" appears in the title
if 'Category:' not in category_title:
category_title = 'Category:' + category_title
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['list'] = 'categorymembers'
query_params['cmtitle'] = category_title
query_params['cmtype'] = 'subcat'
query_params['cmprop'] = 'title'
query_params['cmlimit'] = 500
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
members = list()
if 'categorymembers' in json_response['query']:
for member in json_response['query']['categorymembers']:
members.append(member['title'])
return members
def get_category_members(category_title,depth=1,endpoint='en.wikipedia.org/w/api.php',namespace=0,prepend=True):
"""The function accepts a category_title and returns a list of category members
category_title - a string (including "Category:" prefix) of the category named
depth - the depth of sub-categories to crawl
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
namespace - namespaces to include (multiple namespaces separated by pipes, e.g. "0|1|2")
Returns:
members - a list containing strings of the page titles in the category
"""
# Replace spaces with underscores
category_title = category_title.replace(' ','_')
# Make sure "Category:" appears in the title
if prepend and 'Category:' not in category_title:
category_title = 'Category:' + category_title
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['list'] = 'categorymembers'
query_params['cmtitle'] = category_title
query_params['cmprop'] = 'title'
query_params['cmnamespace'] = namespace
query_params['cmlimit'] = 500
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
members = list()
if depth < 0:
return members
if 'categorymembers' in json_response['query']:
for member in json_response['query']['categorymembers']:
members.append(member['title'])
while True:
if 'continue' in json_response:
query_continue_params = deepcopy(query_params)
query_continue_params['cmcontinue'] = json_response['continue']['cmcontinue']
json_response = requests.get(url = query_url, params = query_continue_params).json()
if 'categorymembers' in json_response['query']:
for member in json_response['query']['categorymembers']:
members.append(member['title'])
else:
break
subcats = get_category_subcategories(category_title,endpoint=endpoint)
for subcat in subcats:
members += get_category_members(subcat,depth=depth-1,endpoint=endpoint,namespace=namespace)
return members
def get_user_info(username_list,endpoint='en.wikipedia.org/w/api.php'):
"""Takes a list of Wikipedia usernames and returns a JSON of their information
username_list - a list of strings for all the usernames
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
Returns:
users_info - a list of information about users
API endpoint docs: https://www.mediawiki.org/wiki/API:Users
"""
users_info = []
chunked_username_list = list(chunks(username_list,50))
for chunk in chunked_username_list:
usernames = '|'.join(chunk)
query_url = "https://{0}".format(endpoint)
query_params = {}
query_params['action'] = 'query'
query_params['list'] = 'users'
query_params['ususers'] = usernames
query_params['usprop'] = 'blockinfo|groups|editcount|registration|gender'
query_params['format'] = 'json'
query_params['formatversion'] = 2
json_response = requests.get(url = query_url, params = query_params).json()
if 'query' in json_response:
users_info += json_response['query']['users']
return users_info
def get_user_contributions(username,endpoint='en.wikipedia.org/w/api.php', redirects=1,start='2001-01-01',stop='today'):
"""Takes Wikipedia username and returns a DataFrame of user contributions
username - a string with the title of the page on Wikipedia
endpoint - a string that points to the web address of the API.
This defaults to the English Wikipedia endpoint: 'en.wikipedia.org/w/api.php'
Changing the two letter language code will return a different language edition
The Wikia endpoints are slightly different, e.g. 'starwars.wikia.com/api.php'
start - a string, datetime, or Timestamp for the earliest user contributions to retrieve
stop - a string, datetime, or Timestamp for the latest user contributions to retrieve
Returns:
usercontribs_df - a DataFrame containing the revision meta-data such as
parentid, revid,sha1, size, timestamp, and user name
API endpoint docs: https://www.mediawiki.org/wiki/API:Usercontribs
"""
start = datetime.strftime(pd.to_datetime(start), '%Y-%m-%dT%H:%M:%SZ')
stop = datetime.strftime(pd.to_datetime(stop), '%Y-%m-%dT%H:%M:%SZ')
revision_list = list()
# Set up the query