-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_JHU.py
263 lines (243 loc) · 10.8 KB
/
update_JHU.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""
Pull data from the John Hopkins University GitHub - https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data
Emile Villette - March 2021
"""
import json
import os
from datetime import datetime, timedelta
import pandas
import progressbar
from dateutil.parser import parse
import directoryManager
import downloadFile
def update_raw():
"""
Updates the data from the JHU github repository until 14/03/2021.
:return: None
"""
def match_country_info() -> dict:
with open("data/2021-03-31/AA_RawDataProcessed.json", "r") as reference_file:
reference_data = json.load(reference_file)
# print(reference_data[current_data["countryInfo"]["iso2"]], current_data["countryInfo"]["iso2"])
current_data["country"] = cases["Country/Region"][j]
current_data["countryInfo"]["iso2"] = str(
countries_info["inverted_couples"][cases["Country/Region"][j]]
)
current_data["countryInfo"][
"flag"
] = f"""https://disease.sh/assets/img/flags/{current_data["countryInfo"]["iso2"]}.png"""
current_data["countryInfo"]["lat"] = str(cases["Lat"][j])
current_data["countryInfo"]["long"] = str(cases["Long"][j])
current_data["countryInfo"]["iso3"] = reference_data[
current_data["countryInfo"]["iso2"]
]["countryInfo"]["iso3"]
current_data["countryInfo"]["_id"] = reference_data[
current_data["countryInfo"]["iso2"]
]["countryInfo"]["_id"]
current_data["population"] = reference_data[
current_data["countryInfo"]["iso2"]
]["population"]
current_data["continent"] = reference_data[current_data["countryInfo"]["iso2"]][
"continent"
]
return current_data
downloadFile.download_file(
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv",
".csv",
"JHU_time_series_cases",
"data/JHU_DATA/",
)
downloadFile.download_file(
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv",
".csv",
"JHU_time_series_deaths",
"data/JHU_DATA/",
)
downloadFile.download_file(
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv",
".csv",
"JHU_time_series_recovered",
"data/JHU_DATA/",
)
with open("languages/countries.json", "r") as countries_file:
countries_info = json.load(countries_file)
cases = pandas.read_csv("data/JHU_DATA/JHU_time_series_cases.csv")
deaths = pandas.read_csv("data/JHU_DATA/JHU_time_series_deaths.csv")
recovered = pandas.read_csv("data/JHU_DATA/JHU_time_series_recovered.csv")
example = {
"updated": 0,
"country": "country_full_name",
"countryInfo": {
"_id": 0,
"iso2": "CY",
"iso3": "CTY",
"lat": 0,
"long": 0,
"flag": "https://disease.sh/assets/img/flags/CT.png",
},
"cases": 0,
"todayCases": 0,
"deaths": 0,
"todayDeaths": 0,
"recovered": 0,
"todayRecovered": 0,
"active": 0,
"critical": 0,
"casesPerOneMillion": 0,
"deathsPerOneMillion": 0,
"tests": 0,
"testsPerOneMillion": 0,
"population": 0,
"continent": "continent",
"oneCasePerPeople": 0,
"oneDeathPerPeople": 0,
"oneTestPerPeople": 0,
"activePerOneMillion": 0,
"recoveredPerOneMillion": 0,
"criticalPerOneMillion": 0,
}
progress_widgets = [
progressbar.FormatLabel(""),
progressbar.Percentage(),
" ",
progressbar.Bar(marker="█", left="[", right="]", fill="░"),
" ",
progressbar.AdaptiveETA(),
]
for progression in progressbar.progressbar(
range(4, len(cases.columns.values[4:])), widgets=progress_widgets
):
i = cases.columns.values[progression]
if str(parse(i).date()) == "2021-03-14":
break
directoryManager.daily_directory(choose_date=parse(i).date())
for j in range(len(cases)):
if cases["Country/Region"][j] not in countries_info["EN"]:
continue
elif os.path.isfile(
f"""data/{parse(i).date()}/{countries_info["inverted_couples"][cases["Country/Region"][j]]}.json"""
):
with open(
f"""data/{parse(i).date()}/{countries_info["inverted_couples"][cases["Country/Region"][j]]}.json""",
"r",
) as current_country:
current_data = json.load(current_country)
progress_widgets[0] = progressbar.FormatLabel(f"{i}: ")
current_data["updated"] = unix_time_millis(parse(i))
current_data["cases"] += int(cases[i][j])
current_data["deaths"] += int(deaths[i][j])
try:
current_data["recovered"] += int(recovered[i][j])
except KeyError:
pass
try:
with open(
f"""data/{str(parse(i).date() - timedelta(days=1))}/{countries_info["inverted_couples"][cases["Country/Region"][j]]}.json""",
"r",
) as yesterday_file:
yesterday_data = json.load(yesterday_file)
current_data["todayCases"] = (
current_data["cases"] - yesterday_data["cases"]
)
current_data["todayDeaths"] = (
current_data["deaths"] - yesterday_data["deaths"]
)
current_data["todayRecovered"] = (
current_data["recovered"] - yesterday_data["recovered"]
)
except FileNotFoundError:
current_data["todayCases"] = 0
current_data["todayDeaths"] = 0
current_data["todayRecovered"] = 0
current_data = match_country_info()
current_data["casesPerOneMillion"] = (
current_data["cases"] / current_data["population"]
) * 1000000
if current_data["cases"] != 0:
current_data["casesPerOneMillion"] = (
current_data["cases"] / current_data["population"]
) * 1000000
current_data["oneCasePerPeople"] = int(
round(current_data["population"] /
current_data["cases"])
)
if current_data["deaths"] != 0:
current_data["deathsPerOneMillion"] = (
current_data["deaths"] / current_data["population"]
) * 1000000
current_data["oneDeathPerPeople"] = int(
round(current_data["population"] /
current_data["deaths"])
)
current_data["recoveredPerOneMillion"] = (
current_data["recovered"] / current_data["population"]
) * 1000000
with open(
f"""data/{parse(i).date()}/{countries_info["inverted_couples"][cases["Country/Region"][j]]}.json""",
"w",
) as current_country2:
json.dump(current_data, current_country2)
with open(
f"""data/{parse(i).date()}/{countries_info["inverted_couples"][cases["Country/Region"][j]]}.json""",
"w",
) as new_country:
json.dump(current_data, new_country)
else:
current_data = example.copy()
progress_widgets[0] = progressbar.FormatLabel(f"{i}: ")
current_data["updated"] = unix_time_millis(parse(i))
current_data["cases"] = int(cases[i][j])
current_data["deaths"] = int(deaths[i][j])
try:
current_data["recovered"] = int(recovered[i][j])
except KeyError:
current_data["recovered"] = 0
try:
with open(
f"""data/{str(parse(i).date() - timedelta(days=1))}/{countries_info["inverted_couples"][cases["Country/Region"][j]]}.json""",
"r",
) as yesterday_file:
yesterday_data = json.load(yesterday_file)
current_data["todayCases"] = (
current_data["cases"] - yesterday_data["cases"]
)
current_data["todayDeaths"] = (
current_data["deaths"] - yesterday_data["deaths"]
)
current_data["todayRecovered"] = (
current_data["recovered"] - yesterday_data["recovered"]
)
except FileNotFoundError:
current_data["todayCases"] = 0
current_data["todayDeaths"] = 0
current_data["todayRecovered"] = 0
current_data = match_country_info()
if current_data["cases"] != 0:
current_data["casesPerOneMillion"] = (
current_data["cases"] / current_data["population"]
) * 1000000
current_data["oneCasePerPeople"] = int(
round(current_data["population"] /
current_data["cases"])
)
if current_data["deaths"] != 0:
current_data["deathsPerOneMillion"] = (
current_data["deaths"] / current_data["population"]
) * 1000000
current_data["oneDeathPerPeople"] = int(
round(current_data["population"] /
current_data["deaths"])
)
current_data["recoveredPerOneMillion"] = (
current_data["recovered"] / current_data["population"]
) * 1000000
with open(
f"""data/{parse(i).date()}/{countries_info["inverted_couples"][cases["Country/Region"][j]]}.json""",
"w",
) as current_country2:
json.dump(current_data, current_country2)
def unix_time_millis(dt):
epoch = datetime.utcfromtimestamp(0)
return int((dt - epoch).total_seconds() * 1000.0)
if __name__ == "__main__":
update_raw()