MediaWiki API/Pageview API
The following code samples demonstrate functions that may be used in Python to access the Pageview API.
Sample Code
editimport json
import urllib.parse
import urllib.request
titles = dict()
def get_pagestats(wiki, title, start, end):
global titles
title = title.replace(" ", "_")
title = urllib.parse.quote(title)
url = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/"\
+ wiki + "/all-access/user/"\
+ title + "/daily/" + start + "/" + end
try:
page = urllib.request.urlopen(url).read()
except:
print("Error reading " + url)
return
page = page.decode("UTF-8")
items = json.loads(page)
for item in items["items"]:
title = item["article"]
views = int(item["views"])
if title in titles:
titles[title] += views
else:
titles[title] = views
def get_wikistats(wiki, date):
global titles
url = "https://wikimedia.org/api/rest_v1/metrics/pageviews/top/" + wiki + "/all-access/" + date
try:
page = urllib.request.urlopen(url).read()
except:
print("Error reading " + url)
return
page = page.decode("UTF-8")
items = json.loads(page)
items = items["items"][0]
for item in items["articles"]:
title = item["article"]
views = int(item["views"])
if title in titles:
titles[title] += views
else:
titles[title] = views
def get_month(wiki, month):
for day in range(1, 32):
date = month + "/" + '{num:02d}'.format(num=day)
print(date)
try:
get_wikistats(wiki, date)
except:
break
def monthly_stats(wiki, month):
global titles
get_month(wiki, month)
list = sorted(titles.items())
list = sorted(list, key=lambda x: -x[1])
count = 0
for record in list:
text = "# {} - [[{}]]".format(record[1], record[0])
text = text.replace("[[File:", "[[:File:")
text = text.replace("[[Category:", "[[:Category:")
print(text)
count += 1
if count >= 1000:
break
def page_stats(wiki, pages, start, end):
global titles
for page in pages:
print(page)
get_pagestats("en.wikiversity", page, start, end)
list = sorted(titles.items())
list = sorted(list, key=lambda x: -x[1])
count = 0
for record in list:
text = "# {} - [[{}]]".format(record[1], record[0])
text = text.replace("[[File:", "[[:File:")
text = text.replace("[[Category:", "[[:Category:")
print(text)
#monthly_stats("en.wikiversity", "2015/11")
#exit()
pages = [
"MediaWiki API",
"MediaWiki API/Pageview API"
]
page_stats("en.wikiversity", pages, "20150101", "20151231")