Last active
July 28, 2020 10:57
-
-
Save cboddy/f5a34d3365707077bd874840f654437c to your computer and use it in GitHub Desktop.
Get the historical cases by day for a UK region as used in coronavirus.data.gov.uk (that only displays the latest value for a region)
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 python3 | |
import urllib.request | |
import json | |
import sys | |
import argparse | |
# Used in https://coronavirus.data.gov.uk | |
URL='https://c19downloads.azureedge.net/downloads/data/ltlas_latest.json' | |
def main(): | |
p = argparse.ArgumentParser(description="Get the historical cases by day for a UK region as used in coronavirus.data.gov.uk") | |
p.add_argument('--region', default='Oxford', help='Region to print daily new cases') | |
args = p.parse_args() | |
target_region = args.region | |
with urllib.request.urlopen(URL) as f: | |
data = f.read() | |
parsed = json.loads(data) | |
for region in parsed.values(): | |
region_name=region.get('name', {}).get('value') | |
if region_name != target_region: | |
continue | |
new_cases = sorted([(i['date'], i['value']) | |
for i in region['dailyConfirmedCases']], | |
key=lambda t: t[0]) | |
total_cases = sorted([(i['date'], i['value']) | |
for i in region['dailyTotalConfirmedCases']], | |
key=lambda t: t[0]) | |
assert list(zip(*new_cases))[0] == list(zip(*total_cases))[0] | |
print(f'Cases for {target_region}') | |
print('date,new,total') | |
for new, total in zip(new_cases, total_cases): | |
print(new[0], new[1], total[1]) | |
break | |
else: | |
all_regions = sorted([r.get('name', {}).get('value', '') for r in parsed.values()]) | |
print(f'Could not find region {target_region}, the following are available {all_regions}') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment