Skip to content

Commit 5b185a6

Browse files
committed
finally get around to adopting that brand new json api lol
1 parent c67606d commit 5b185a6

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

top/common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
TOP_API_URL = 'http://wikimedia.org/api/rest_v1/metrics/pageviews/'\
2727
'top/{lang}.{project}/all-access/{year}/{month}/{day}'
2828
MW_API_URL = 'http://{lang}.{project}.org/w/api.php?'
29-
TOTAL_TRAFFIC_URL = 'https://metrics.wmflabs.org/static/public/'\
30-
'datafiles/Pageviews/{lang}{project}.csv'
29+
TOTAL_TRAFFIC_URL = 'https://wikimedia.org/api/rest_v1/metrics/pageviews/aggregate/{lang}.{project}.org/all-access/all-agents/daily/{datestr}00/{datestr}00'
3130

3231

3332
# Other variables

top/get_data.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,20 @@ def is_article(title, wiki_info):
8484
return True
8585

8686

87-
# TODO: switch to new JSON API
87+
8888
def get_project_traffic(date, lang, project):
89-
if project == 'wikipedia':
90-
project = 'wiki'
91-
url = TOTAL_TRAFFIC_URL.format(lang=lang, project=project)
89+
datestr = date.strftime('%Y%m%d')
90+
url = TOTAL_TRAFFIC_URL.format(lang=lang, project=project, datestr=datestr)
9291
resp = urllib2.urlopen(url)
93-
data = csvDictReader(resp)
94-
date_str = date.strftime('%Y-%m-%d')
92+
data = json.loads(resp.read())
93+
try:
94+
date_data = data['items'][0]
95+
except IndexError:
96+
# backwards compat to the old metrics api which would raise urlerror
97+
raise urllib2.URLError('no traffic data available from %r' % url)
98+
99+
return date_data['views']
95100

96-
date_total_list = [(d['Date'], d['Total']) for d in data]
97-
total_traffic = dict(date_total_list).get(date_str, 0)
98-
return total_traffic
99101

100102

101103
def get_query(params, lang, project, extractor, default_val):

0 commit comments

Comments
 (0)