Created
November 5, 2019 20:41
-
-
Save jcampbell05/7e9d6354e59a82f944e6fb62a76540b9 to your computer and use it in GitHub Desktop.
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
import calendar | |
import csv | |
import datetime | |
import dateutil.parser | |
import json | |
from dateutil.rrule import rrule, MONTHLY, YEARLY | |
from urllib import request | |
from urllib.parse import urlencode | |
base_url = "https://www.performance.service.gov.uk/data/register-to-vote/volumetrics" | |
start_date = datetime.datetime(2015,1,1) | |
end_date = datetime.datetime(2019,12,31) | |
age_groups = [ | |
"18-24", | |
"25-34" | |
] | |
metrics = {} | |
f = open("./metrics.csv", "w") | |
writer = csv.writer(f) | |
headers = ["Year"] | |
for date in rrule(MONTHLY, dtstart=start_date, until=end_date): | |
headers.append(date.month) | |
(_, last) = calendar.monthrange(date.year, date.month) | |
start_at = datetime.datetime(date.year, date.month, 1).astimezone(datetime.timezone.utc) | |
end_at = datetime.datetime(date.year, date.month, last, 23, 59, 59).astimezone(datetime.timezone.utc) | |
qs = urlencode({ | |
'period': 'month', | |
'filter_by': 'metricName:age_band', | |
'collect': 'count:sum', | |
'group_by': 'value', | |
'start_at': start_at.isoformat(), | |
'end_at': end_at.isoformat(), | |
'format': 'json', | |
}) | |
url = f'{base_url}?{qs}' | |
print(f'Making request to {url}') | |
req = request.Request(base_url + '?' + qs) | |
resp = request.urlopen(req) | |
payload = json.load(resp) | |
data = payload["data"] | |
key = f'{date.year}-{date.month}' | |
metrics[date.year] = metrics.get(date.year) or {} | |
for segment in data: | |
if segment.get('value') in age_groups: | |
year_stats = metrics[date.year] | |
year_stats[date.month] = year_stats.get(date.month) or 0 | |
year_stats[date.month] += segment.get('count:sum') | |
writer.writerow(headers) | |
for year, value in metrics.items(): | |
values = list(value.values()) | |
row = [year] + values | |
writer.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment