initial import of solutions for wikipedia data challenges
[wikipedia-cdsw-solutions] / solution_5-advanced.py
1 # 5. Find out how many revisions to the article on "Python
2 # (programming language)" were made by user "Peterl"? How about
3 # "Hfastedge"?
4
5 import requests
6
7 # parameter version which makes a little more sense
8 parameters = {'action' : 'query',
9               'prop' : 'revisions',
10               'titles' : 'Python (programming language)',
11               'rvlimit' : 500,
12               'rvprop' : "ids|user",
13               'format' : 'json',
14               'continue' : ''}
15
16 user_counts = {}
17
18 # run a "while True" loop
19 while True:
20     wp_call = requests.get('https://en.wikipedia.org/w/api.php', params=parameters)
21     response = wp_call.json()
22     
23     for page_id in response["query"]["pages"].keys():
24         revisions = response["query"]["pages"][page_id]["revisions"]
25         
26         for rev in revisions:
27             current_user = rev['user']
28             
29             if current_user in user_counts:
30                 user_counts[current_user] = user_counts[current_user] + 1
31             else:
32                 user_counts[current_user] = 1
33
34     if 'continue' in response:
35         parameters.update(response['continue'])
36     else:
37         break
38             
39
40 # now that we've built up the dictionary, lets print it out
41 for editor in user_counts.keys():
42     print("%s made %s edits" % (editor, user_counts[editor]))
43

Benjamin Mako Hill || Want to submit a patch?