forked from overlordtm/COVID19.si
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
225 lines (187 loc) · 7.12 KB
/
import.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
from __future__ import print_function
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from dateutil import parser
import csv
import os
import os.path
import sys
# old legacy stuff
def api_key():
api_key = os.environ["GOOGLE_API_KEY"]
if not api_key:
raise Exception("No GOOGLE_API_KEY in env")
else:
return api_key
service = build("sheets", "v4", developerKey=api_key())
sheet = service.spreadsheets()
def parse_date(datestr):
return parser.isoparse(datestr[0:10]).date()
def cell_index(letter):
# ord(a) = 97
letter = letter.lower()
if len(letter) == 1:
return ord(letter) - 97
elif len(letter) == 2:
return (ord(letter[0]) - 97) * 26 + ord(letter[1]) - 97 + 26
else:
raise Exception("Unsupported cell index")
def col_name(idx):
i2 = idx // 26
i1 = idx % 26
if i2 > 0:
return "{}{}".format(chr(97 + i2), chr(97 + i1))
else:
return chr(97 + i1)
def pad(lst, l):
if len(lst) == 0:
return [""] * l
if len(lst) < l:
lst.extend([""] * (l - len(lst)))
return lst
else:
return lst
def get_range(row, col_start, col_end):
desired_len = cell_index(col_end) - cell_index(col_start) + 1
data = row[cell_index(col_start) : cell_index(col_end) + 1]
padded = pad(data, desired_len)
return padded
def import_cases(filename="covid19-slovenia-cases.csv"):
result = (
sheet.values()
.get(
spreadsheetId="1N1qLMoWyi3WFGhIpPFzKsFmVE0IwNP3elb_c18t2DwY",
range="Primeri!A2:I",
)
.execute()
)
values = result.get("values", [])
if not values:
raise Exception("No data found.")
with open(filename, "w", newline="") as csvfile:
writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
fieldnames = [
"case #",
"date",
"region",
"city",
"source",
"desc",
"cluster",
"state",
"news_source",
]
writer.writerow(fieldnames)
for row in values:
try:
row[1] = parse_date(row[1])
prow = pad(row, 9)
writer.writerow(prow)
except Exception as e:
print("import_cases: Failed to parse row: {} {}".format(row, e))
def import_stats(filename="covid19-slovenia-full.csv"):
result = (
sheet.values()
.get(
spreadsheetId="1N1qLMoWyi3WFGhIpPFzKsFmVE0IwNP3elb_c18t2DwY",
range="Statistika!A2:BS",
)
.execute()
)
values = result.get("values", [])
if not values:
raise Exception("No data found.")
else:
with open(filename, "w", newline="") as csvfile:
# find columns
headers = values[0]
COL_DAY_OF_OUTBREAK = headers.index("dan")
COL_DATE = headers.index("datum")
COL_PANDEMIC_PHASE = 2
COL_TESTS_PERFORMED_CUM = headers.index("testov")
COL_TESTS_PERFORMED_DAY = headers.index("testov na dan")
COL_POSITIVE_TESTS_CUM = headers.index("potrjeni")
COL_POSITIVE_TESTS_DAY = headers.index("novi potrjeni")
COL_COUNT_IN_CARE = headers.index("v zdr. oskrbi")
COL_COUNT_IN_HOSPITAL = headers.index("hospitalizirano")
COL_COUNT_IN_ICU = headers.index("intenzivna nega ICU")
COL_COUNT_IN_ICU_BAD = headers.index("kritično stanje")
COL_DEATHS_CUM = headers.index("umrli")
regions = get_range(headers, "AA", "AM")
facilities = get_range(headers, "BL", "BR")
age_groups = ["0-15", "16-29", "30-49", "50-59", "60+"]
fieldnames = (
[
"day_of_outbreak",
"date",
"pandemic_phase",
"tests_performed_cum",
"tests_performed_day",
"positive_tests_cum",
"positive_tests_day",
"count_in_care",
"count_in_hospital",
"count_in_icu",
"count_in_icu_bad",
"deaths_cum",
]
+ list(map(lambda g: "region_{}".format(g), regions))
+ list(map(lambda g: "age_{}".format(g), age_groups))
+ list(map(lambda g: "female_age_{}".format(g), age_groups))
+ list(map(lambda g: "male_age_{}".format(g), age_groups))
+ ["source_ita", "source_aut", "source_esp", "source_slo"]
+ list(map(lambda g: "facility_{}".format(g.lower()), facilities))
)
writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
writer.writerow(fieldnames)
for row in values[1:]:
row = pad(row, len(headers))
try:
day_of_outbreak = row[COL_DAY_OF_OUTBREAK]
date = parse_date(row[COL_DATE])
pandemic_phase = row[COL_PANDEMIC_PHASE]
tests_performed_cum = row[COL_TESTS_PERFORMED_CUM]
tests_performed_day = row[COL_TESTS_PERFORMED_DAY]
positive_tests_cum = row[COL_POSITIVE_TESTS_CUM]
positive_tests_day = row[COL_POSITIVE_TESTS_DAY]
count_in_care = row[COL_COUNT_IN_CARE]
count_in_hospital = row[COL_COUNT_IN_HOSPITAL]
count_in_icu = row[COL_COUNT_IN_ICU]
count_in_icu_bad = row[COL_COUNT_IN_ICU_BAD]
deaths_cum = row[COL_DEATHS_CUM]
regional_data = get_range(row, "AA", "AM")
by_age = get_range(row, "AO", "AS")
by_age_female = get_range(row, "AU", "AY")
by_age_male = get_range(row, "BA", "BE")
by_source = get_range(row, "BG", "BJ")
workers = get_range(row, "BL", "BR")
csvrow = (
[
day_of_outbreak,
date,
pandemic_phase,
tests_performed_cum,
tests_performed_day,
positive_tests_cum,
positive_tests_day,
count_in_care,
count_in_hospital,
count_in_icu,
count_in_icu_bad,
deaths_cum,
]
+ regional_data
+ by_age
+ by_age_female
+ by_age_male
+ by_source
+ workers
)
writer.writerow(csvrow)
except IndexError as e:
print(e)
print(row)
if __name__ == "__main__":
import_stats("data/full.csv")
import_cases("data/cases.csv")