forked from priyankamandikal/arowf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpageviews.py
More file actions
23 lines (19 loc) · 803 Bytes
/
Copy pathpageviews.py
File metadata and controls
23 lines (19 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from datetime import date, datetime, timedelta
from traceback import format_exc
from requests import get
pageviews_url = 'https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article'
def format_date(d):
return datetime.strftime(d, '%Y%m%d%H')
def article_views(article, project='en.wikipedia', access='all-access', agent='all-agents', granularity='daily', start=None, end=None):
endDate = date.today()
startDate = endDate - timedelta(30)
url = '/'.join([pageviews_url, project, access, agent, article, 'daily', format_date(startDate), format_date(endDate)])
try:
result = get(url).json()
last30dayscount = 0
for item in result['items']:
last30dayscount += item['views']
return last30dayscount
except:
print 'Error while fetching page views of ' + article
print format_exc()