Created
November 5, 2019 20:43
-
-
Save jcampbell05/67e20714cc07006703ee1fced0809086 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.relativedelta import * | |
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" | |
important_dates = { | |
"GE2015": datetime.datetime(2015,5,7), | |
"EURef2016": datetime.datetime(2016,6,23), | |
"GE2017": datetime.datetime(2017,6,8), | |
"GE2019": datetime.datetime(2019,12,12) | |
} | |
months_back = 6 | |
age_groups = [ | |
"18-24", | |
"25-34" | |
] | |
metrics = {} | |
f = open("./metrics.csv", "w") | |
writer = csv.writer(f) | |
headers = ["Event"] + [x for x in range(months_back)] | |
months_back -= 1 | |
for event, event_date in important_dates.items(): | |
headers.append(event) | |
(_, last) = calendar.monthrange(event_date.year, event_date.month) | |
start_date = event_date - relativedelta(months=months_back) | |
start_date = start_date.replace(day=1) | |
end_date = datetime.datetime(event_date.year, event_date.month, last) | |
index = 0 | |
for date in rrule(MONTHLY, dtstart=start_date, until=end_date): | |
index += 1 | |
(_, 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"] | |
metrics[event] = metrics.get(event) or {} | |
for segment in data: | |
if segment.get('value') in age_groups: | |
year_stats = metrics[event] | |
year_stats[index] = year_stats.get(index) or 0 | |
year_stats[index] += segment.get('count:sum') | |
writer.writerow(headers) | |
for event, value in metrics.items(): | |
values = list(value.values()) | |
row = [event] + values | |
writer.writerow(row) |
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
Requires Python 3 will create file in folder with the final stats called "metrics.csv" - create graph by importing into excel