initial import of solutions for wikipedia data challenges
[wikipedia-cdsw-solutions] / solution_8.py
1 # 8. How many edits to the article "Python (programming language)"
2 # where made in 2014?
3
4 # Useful page is here: https://www.mediawiki.org/wiki/API:Revisions
5
6 import requests
7
8
9 # parameter version which makes a little more sense
10 parameters = {'action' : 'query',
11               'prop' : 'revisions',
12               'titles' : 'Python (programming language)',
13               'rvlimit' : 500,
14               'rvprop' : "timestamp",
15               'format' : 'json',
16               'continue' : ''}
17
18 edits_in_2014 = 0
19
20 # run a "while True" loop
21 while True:
22     wp_call = requests.get('https://en.wikipedia.org/w/api.php', params=parameters)
23     response = wp_call.json()
24     
25     for page_id in response["query"]["pages"].keys():
26         revisions = response["query"]["pages"][page_id]["revisions"]
27         
28         for rev in revisions:
29             if rev['timestamp'][0:4] == "2014":
30                 edits_in_2014 = edits_in_2014 + 1
31
32     if 'continue' in response:
33         parameters.update(response['continue'])
34     else:
35         break
36
37 print(edits_in_2014)

Benjamin Mako Hill || Want to submit a patch?