This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetData.py
201 lines (154 loc) · 6.5 KB
/
getData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import argparse
import os
import re
import pandas as pd
import requests
from tqdm.rich import tqdm
from datetime import timedelta, date
def getData(dataDir, force=False):
# check for if there is new data
if os.path.isfile(dataDir + "Last-Modified.txt"):
with open(dataDir + "Last-Modified.txt") as file:
prevLastModified = file.read()
else:
prevLastModified = ""
url = "https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=overview&structure=%7B%7D"
r = requests.get(url)
lastModified = r.headers["Last-Modified"]
if prevLastModified != lastModified or force:
print("Getting new data...")
urlPrefix = 'https://api.coronavirus.data.gov.uk/v1/data?filters=areaType={}&structure=%7B"date":"date",'
urls = [
'"newPillarOneTestsByPublishDate":"newPillarOneTestsByPublishDate","newPillarTwoTestsByPublishDate":"newPillarTwoTestsByPublishDate","newPillarFourTestsByPublishDate":"newPillarFourTestsByPublishDate"',
'"newCasesBySpecimenDate":"newCasesBySpecimenDate"',
'"newCasesByPublishDate":"newCasesByPublishDate"',
'"newDeaths28DaysByDeathDate":"newDeaths28DaysByDeathDate"',
'"newDeaths28DaysByPublishDate":"newDeaths28DaysByPublishDate"',
'"newDailyNsoDeathsByDeathDate":"newDailyNsoDeathsByDeathDate"',
'"newAdmissions":"newAdmissions"',
'"hospitalCases":"hospitalCases"',
'"weeklyPeopleVaccinatedFirstDoseByVaccinationDate":"weeklyPeopleVaccinatedFirstDoseByVaccinationDate","weeklyPeopleVaccinatedSecondDoseByVaccinationDate":"weeklyPeopleVaccinatedSecondDoseByVaccinationDate"',
'"newPeopleVaccinatedFirstDoseByPublishDate":"newPeopleVaccinatedFirstDoseByPublishDate","newPeopleVaccinatedSecondDoseByPublishDate":"newPeopleVaccinatedSecondDoseByPublishDate"',
'"covidOccupiedMVBeds":"covidOccupiedMVBeds"',
]
urlSuffix = "%7D&format=csv"
names = [
"testing.reported",
"cases",
"cases.reported",
"deaths",
"deaths.reported",
"deaths.onCertificate",
"hospitalisations",
"inHospital",
"vaccinations.weekly",
"vaccinations.reported",
"inVentilationBeds",
]
nations = ["Scotland", "England", "Northern Ireland", "Wales"]
t = tqdm(
total=len(names) * 5,
bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} {elapsed_s:.1f}s",
)
for i, name in enumerate(names):
suffix = urls[i] + urlSuffix
for nation in nations:
prefix = urlPrefix.format("nation;areaName={}").format(nation)
fileName = nation + "." + name
updateProgressBar(fileName, t)
getCSV(prefix + suffix, dataDir, fileName)
prefix = urlPrefix.format("overview")
fileName = "UK." + name
updateProgressBar(fileName, t)
getCSV(prefix + suffix, dataDir, fileName)
with open(dataDir + "Last-Modified.txt", "w") as file:
file.write(lastModified)
# getExcessDeaths(dataDir)
print("Done!")
return True
else:
return False
def getCSV(url, dataDir, name):
r = requests.get(url)
text = r.text
fileName = dataDir + name + ".csv"
if r.status_code == 200:
titles, data = text.split("\n", 1)
data = data.split("\n")
data = data[:-1] # remove last blank line
data = list(reversed(data))
titles = titles.split(",")
if "newPillarOneTestsByPublishDate" in titles:
newData = []
for line in data:
arr = line.split(",")
newData.append(
str(arr[0])
+ ","
+ str(parseInt(arr[1]) + parseInt(arr[2]) + parseInt(arr[3]))
)
data = newData
data = [tuple for tuple in data if not re.match(r"[\d-]+,$", tuple) and tuple[-2:] != ",,"]
data = "\n".join(data)
with open(fileName, "w") as file:
file.writelines(data)
elif r.status_code == 204:
print("Error: No data returned for " + name)
else:
print("Error " + str(r.status_code))
print(r.content)
exit()
def getExcessDeaths(dataDir):
data = pd.read_csv(
"https://raw.githubusercontent.com/TheEconomist/covid-19-excess-deaths-tracker/master/output-data/excess-deaths/britain_excess_deaths.csv",
parse_dates=True,
usecols=["region", "end_date", "excess_deaths"],
index_col=[0, 1],
)
data = data.unstack("region")
print(data)
data.loc[:,("excess_deaths","UK")] = data["excess_deaths"]["Britain"]
data.loc[:,("excess_deaths","England")] = (
data["excess_deaths"]["England and Wales"] - data["excess_deaths"]["Wales"]
)
nations = ["UK", "Scotland", "England", "Wales", "Northern Ireland"]
for nation in nations:
series = data.loc[:,("excess_deaths", nation)]
series = series / 7
series = series.round().astype(int)
series.index = series.index - timedelta(3)
series.to_csv(dataDir + nation + ".deaths.excess.csv", header=False)
def parseInt(str):
str = str.strip()
return int(str) if str else 0
def updateProgressBar(figname, t):
t.update()
t.set_description(figname)
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
def defineArgParser():
"""Creates parser for command line arguments"""
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"-f", "--force", help="Get data even if it is not new", action="store_true"
)
parser.add_argument(
"-D",
"--dataDir",
help="Directory where the csv files will be stored [default: data/]",
default="data/",
type=str,
)
return parser
if __name__ == "__main__":
argParser = defineArgParser()
clArgs = argParser.parse_args()
dataDir = clArgs.dataDir
if not os.path.exists(dataDir):
os.mkdir(dataDir)
newData = getData(dataDir, clArgs.force)
if not newData:
print("No new data.")