Created
July 2, 2014 15:43
-
-
Save frabcus/71ce2eea090a9e5ae433 to your computer and use it in GitHub Desktop.
Compare GOV.UK mobile use week starting 2014-05-12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
import sys | |
import csv | |
import scraperwiki | |
depts = [ | |
"attorney-generals-office", | |
"cabinet-office", | |
"department-for-business-innovation-skills", | |
"department-for-communities-and-local-government", | |
"department-for-culture-media-sport", | |
"department-for-education", | |
"department-for-environment-food-rural-affairs", | |
"department-for-international-development", | |
"department-for-transport", | |
"department-for-work-pensions", | |
"department-of-energy-climate-change", | |
"department-of-health", | |
"driver-and-vehicle-licensing-agency", | |
"driver-and-vehicle-standards-agency", | |
"driving-standards-agency", | |
"environment-agency", | |
"foreign-commonwealth-office", | |
"hm-prison-service", | |
"hm-revenue-customs", | |
"hm-treasury", | |
"home-office", | |
"ministry-of-defence", | |
"ministry-of-justice", | |
"northern-ireland-office", | |
"office-of-the-advocate-general-for-scotland", | |
"the-office-of-the-leader-of-the-house-of-commons", | |
"office-of-the-leader-of-the-house-of-lords", | |
"prime-ministers-office-10-downing-street", | |
"scotland-office", | |
"the-charity-commission-for-england-and-wales", | |
"deputy-prime-ministers-office", | |
"uk-export-finance", | |
"uk-trade-investment", | |
"uk-visas-and-immigration", | |
"vehicle-and-operator-services-agency", | |
"wales-office", | |
] | |
general_url = "https://www.performance.service.gov.uk/data/gov-uk-content/devices-count?filter_by=department%3A{department}&collect=pageviews%3Asum&period=week&start_at=2014-05-12T00%3A00%3A00%2B00%3A00&end_at=2014-05-19T00%3A00%3A00%2B00%3A00&group_by=deviceCategory" | |
def get_one_dept(deptname): | |
url = general_url.format(department = deptname) | |
print url | |
res = requests.get(url) | |
data = res.json()['data'] | |
# Pull out the mobile records | |
ret = {} | |
total = 0 | |
for d in data: | |
total += d['pageviews:sum'] | |
ret[d['deviceCategory']] = d['pageviews:sum'] | |
# Calculate percentages | |
for v in ret.keys(): | |
ret[v + "_percent"] = 100.0 * ret[v] / total | |
# Other useful fields | |
ret['total'] = total | |
ret['department'] = deptname | |
return ret | |
# Main entry point | |
# arr = [ get_one_dept('deputy-prime-ministers-office') ] | |
arr = [] | |
for dept in depts: | |
row = get_one_dept(dept) | |
arr.append(row) | |
scraperwiki.sqlite.save(['department'], row) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment