-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_data.py
executable file
·171 lines (118 loc) · 3.71 KB
/
update_data.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
#!/usr/bin/env python3
import requests
import json
import os
if __name__ == "__main__":
r = requests.get("https://pomber.github.io/covid19/timeseries.json")
if r.status_code != 200:
print(f"Error while fetching new data. http {r.status_code}")
data = r.json()
for country, info in data.items():
info[0]["daily"] = {
"confirmed": 0,
"deaths": 0,
"recovered": 0
}
del info[0]["confirmed"]
del info[0]["deaths"]
del info[0]["recovered"]
info[0]["cumulative"] = {
"confirmed": 0,
"deaths": 0,
"recovered": 0
}
info[0]["active"] = 0
for i, x in enumerate(info[1:]):
cases = x["confirmed"]
deaths = x["deaths"]
recovered = x["recovered"]
del x["confirmed"]
del x["deaths"]
del x["recovered"]
x["cumulative"] = {
"confirmed": cases,
"deaths": deaths,
"recovered": recovered
}
prev_cases = info[i]["cumulative"]["confirmed"]
prev_deaths = info[i]["cumulative"]["deaths"]
prev_recovered = info[i]["cumulative"]["recovered"]
data[country][i + 1]["daily"] = {
"confirmed": abs(prev_cases - cases),
"deaths": abs(prev_deaths - deaths),
"recovered": abs(prev_recovered - recovered)
}
x["active"] = cases - (deaths + recovered)
timeline = {}
for country, days in data.items():
for day in days:
date = day["date"]
if date not in timeline:
timeline[date] = {}
if country not in timeline[date]:
timeline[date][country] = day
# Data for all cases globally for a certain day
for day, countries in timeline.items():
total_confirmed = 0
total_deaths = 0
total_recovered = 0
daily_confirmed = 0
daily_deaths = 0
daily_recovered = 0
total_active = 0
for country in countries:
confirmed = timeline[day][country]["cumulative"]["confirmed"]
deaths = timeline[day][country]["cumulative"]["deaths"]
recovered = timeline[day][country]["cumulative"]["recovered"]
today_confirmed = timeline[day][country]["daily"]["confirmed"]
today_deaths = timeline[day][country]["daily"]["deaths"]
today_recovered = timeline[day][country]["daily"]["recovered"]
today_active = timeline[day][country]["active"]
total_confirmed += confirmed
total_deaths += deaths
total_recovered += recovered
daily_confirmed += today_confirmed
daily_deaths += today_deaths
daily_recovered += today_recovered
total_active += today_active
if "World" not in data:
data["World"] = []
world = {
"date": day,
"cumulative": {
"confirmed": total_confirmed,
"deaths": total_deaths,
"recovered": total_recovered
},
"daily": {
"confirmed": daily_confirmed,
"deaths": daily_deaths,
"recovered": daily_recovered
},
"active": total_active
}
data["World"].append(world)
trimmed_data = {}
for country, info in data.items():
for i, entry in enumerate(info):
cases = entry["cumulative"]["confirmed"]
deaths = entry["cumulative"]["deaths"]
recovered = entry["cumulative"]["recovered"]
cases = cases > 0
deaths = deaths > 0
recovered = recovered > 0
# If we find an entry which has at least one
# value >0, we can assume that all following entries will
# >0 too. We add one previous day before the countries first
# case so we can plot it properly.
if any([cases, deaths, recovered]):
trimmed_data[country] = info[max(i - 1, 0):]
break
for country in list(trimmed_data.keys()):
new_key = "".join([x for x in country if x.lower() in "abcdefghijklmnopqrstuvwxyz "])
trimmed_data[new_key] = trimmed_data.pop(country)
if not os.path.isdir("data"):
os.mkdir("data")
with open("data/data.json", "w") as f:
f.write(json.dumps(trimmed_data, indent = 4))
print("done!")