-
Notifications
You must be signed in to change notification settings - Fork 9
/
fetch-de-divi-V3.py
executable file
·480 lines (417 loc) · 19.4 KB
/
fetch-de-divi-V3.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env python3.10
# by Dr. Torben Menke https://entorb.net
# https://github.com/entorb/COVID-19-Coronavirus-German-Regions
"""
fetched German hospital occupancy data from DIVI https://www.intensivregister.de
lk_id sind https://de.wikipedia.org/wiki/Amtlicher_Gemeindeschl%C3%BCssel
"""
import csv
import re
import helper
def extractLinkList(cont: str) -> list[str]:
# myPattern = '<a href="(/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-[^"]+/download)"'
myPattern = r'<a href="(/divi-intensivregister-tagesreport-archiv-csv/viewdocument/\d+?/divi-intensivregister-[^"]*)"'
# /divi-intensivregister-tagesreport-archiv-csv/viewdocument/5330/divi-intensivregister-2020-12-21-12-15
myRegExp = re.compile(myPattern)
myMatches = myRegExp.findall(cont)
assert len(myMatches) > 10, "Error: no csv download links found"
return myMatches
def fetch_latest_csv():
"""
fetches the latest (top) file from https://www.divi.de/divi-intensivregister-tagesreport-archiv-csv?layout=table
output: latest.csv (overwrites old file)
#"""
# url = (
# "https://www.divi.de/divi-intensivregister-tagesreport-archiv-csv?layout=table"
# )
# file = "cache/de-divi/list-csv-page-1.html"
# payload = {"filter_order_Dir": "DESC", "filter_order": "tbl.ordering", "start": 0}
# # "cid[]": "0", "category_id": "54", "task": "", "8ba87835776d29f4e379a261512319f1": "1"
# cont = helper.read_url_or_cachefile(
# url=url,
# cachefile=file,
# request_type="post",
# payload=payload,
# cache_max_age=0,
# verbose=True,
# )
# # extract link of from <a href="/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-2020-06-28-12-15/download"
# l_csv_urls = extractLinkList(cont=cont)
# # reduce list to the 5 latest files
# # commented out, since at 07.07.2020 at the source the table sourting was strange, so that the new files where not on top of the list
# # while len(l_csv_urls) > 5:
# # l_csv_urls.pop()
# d_csvs_in_table = {}
# # loop over urls to replace outdated files by latest file per day
# # '/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-2020-06-25-12-15/download'
# # '/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-2020-06-25-12-15-2/download'
# for url in l_csv_urls:
# url = f"https://www.divi.de{url}"
# # '/divi-intensivregister-tagesreport-archiv-csv/viewdocument/5330/divi-intensivregister-2020-12-21-12-15'
# filename = re.search(
# r"/divi-intensivregister-tagesreport-archiv-csv/viewdocument/\d+?/divi-intensivregister-(\d{4}\-\d{2}\-\d{2})[^/]",
# url,
# ).group(1)
# d_csvs_in_table[filename] = url
# del l_csv_urls, filename, url
# l = sorted(d_csvs_in_table.keys())
# latest_filename = l[-1]
# latest_url = d_csvs_in_table[latest_filename]
file = "cache/de-divi/latest.csv"
url = "https://diviexchange.blob.core.windows.net/%24web/zeitreihe-tagesdaten.csv"
_ = helper.read_url_or_cachefile(
url=url,
file_cache=file,
request_type="get",
# payload={},
cache_max_age=0, # 0s because the git pull created files are "new"
verbose=True,
)
# def fetch_all_csvs():
# """
# fetches the all files from https://www.divi.de/divi-intensivregister-tagesreport-archiv-csv?layout=table
# only keeps the latest file per day
# """
# url = (
# "https://www.divi.de/divi-intensivregister-tagesreport-archiv-csv?layout=table"
# )
# cachefile = "cache/de-divi/list-csv-page-1.html"
# payload = {"filter_order_Dir": "DESC", "filter_order": "tbl.ordering", "start": 0}
# # "cid[]": "0", "category_id": "54", "task": "", "8ba87835776d29f4e379a261512319f1": "1"
# cont = helper.read_url_or_cachefile(
# url=url,
# cachefile=cachefile,
# request_type="post",
# payload=payload,
# cache_max_age=0,
# verbose=True,
# )
# # extract link of from <a href="/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-2020-06-28-12-15/download"
# l_csv_urls = extractLinkList(cont=cont)
# # reduce list to the 5 latest files
# # commented out, since at 07.07.2020 at the source the table sourting was strange, so that the new files where not on top of the list
# # while len(l_csv_urls) > 5:
# # l_csv_urls.pop()
# d_csvs_to_fetch = {}
# # loop over urls to replace outdated files by latest file per day
# # '/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-2020-06-25-12-15/download'
# # '/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-2020-06-25-12-15-2/download'
# for url in l_csv_urls:
# url = f"https://www.divi.de{url}"
# # '/divi-intensivregister-tagesreport-archiv-csv/viewdocument/5330/divi-intensivregister-2020-12-21-12-15'
# filename = re.search(
# r"/divi-intensivregister-tagesreport-archiv-csv/viewdocument/\d+?/divi-intensivregister-(\d{4}\-\d{2}\-\d{2})[^/]",
# url,
# ).group(1)
# d_csvs_to_fetch[filename] = url
# del l_csv_urls
# assert len(d_csvs_to_fetch) > 0, "Error: no files to fetch"
# for filename, url in d_csvs_to_fetch.items():
# cachefile = f"data/de-divi/downloaded/{filename}.csv"
# if not os.path.isfile(cachefile):
# cont = helper.read_url_or_cachefile(
# url=url,
# cachefile=cachefile,
# request_type="get",
# payload={},
# cache_max_age=0,
# verbose=True,
# )
def generate_database() -> dict:
# TODO: use Pandas instead of manuall CVS stuff
"""from 2021-10-29 on Divi publisheds all data in the latest file"""
d_database: dict[str, list[dict]] = {}
# d_database_states = {} # Bundesländer
d_database_states = {
"01": {},
"02": {},
"03": {},
"04": {},
"05": {},
"06": {},
"07": {},
"08": {},
"09": {},
"10": {},
"11": {},
"12": {},
"13": {},
"14": {},
"15": {},
"16": {},
"00": {},
}
# 00 = DE-total
# csv_file = sorted(glob.glob('data/de-divi/downloaded/*.csv'))[-1]
csv_file = "cache/de-divi/latest.csv"
# (filepath, fileName) = os.path.split(csv_file)
# (fileBaseName, fileExtension) = os.path.splitext(fileName)
# del filepath, fileName, fileBaseName, fileExtension
# file 2020-04-24.csv:
# bundesland,kreis,anzahl_standorte,betten_frei,betten_belegt,faelle_covid_aktuell_im_bundesland
# file 2020-04-26.csv:
# gemeindeschluessel,anzahl_meldebereiche,faelle_covid_aktuell,faelle_covid_aktuell_beatmet,anzahl_standorte,betten_frei,betten_belegt,bundesland
# 2020-04-28.csv
# gemeindeschluessel,anzahl_meldebereiche,faelle_covid_aktuell,faelle_covid_aktuell_beatmet,anzahl_standorte,betten_frei,betten_belegt,bundesland,daten_stand
# file 2020-06-28.csv
# bundesland,gemeindeschluessel,anzahl_meldebereiche,faelle_covid_aktuell,faelle_covid_aktuell_beatmet,anzahl_standorte,betten_frei,betten_belegt,daten_stand
# file 2021-10-29.csv
# date,bundesland,gemeindeschluessel,anzahl_standorte,anzahl_meldebereiche,faelle_covid_aktuell,faelle_covid_aktuell_invasiv_beatmet,betten_frei,betten_belegt,betten_belegt_nur_erwachsen,betten_frei_nur_erwachsen
with open(csv_file, encoding="utf-8") as f:
csv_reader = csv.DictReader(f, delimiter=",")
for row in csv_reader:
assert len(row) >= 8, "Error: too few rows found"
date = row["date"]
bl_id = row["bundesland"]
lk_id = row["gemeindeschluessel"]
d: dict = {
# "bl_id": row["bundesland"],
# "lk_id": row["gemeindeschluessel"],
"Date": date,
"anzahl_meldebereiche": int(row["anzahl_meldebereiche"]),
"faelle_covid_aktuell": int(row["faelle_covid_aktuell"]),
"anzahl_standorte": int(row["anzahl_standorte"]),
"faelle_covid_aktuell_invasiv_beatmet": int(
row["faelle_covid_aktuell_invasiv_beatmet"],
),
"betten_frei": int(float(row["betten_frei"])),
"betten_belegt": int(float(row["betten_belegt"])),
}
# field was renamed in past
# if "faelle_covid_aktuell_beatmet" in row:
# d["faelle_covid_aktuell_beatmet"] = int(
# row["faelle_covid_aktuell_beatmet"])
# elif "faelle_covid_aktuell_invasiv_beatmet" in row:
# d["faelle_covid_aktuell_beatmet"] = int(
# row["faelle_covid_aktuell_invasiv_beatmet"])
d["betten_ges"] = d["betten_frei"] + d["betten_belegt"]
if d["betten_ges"] > 0:
d["betten_belegt_proz"] = round(
100 * d["betten_belegt"] / d["betten_ges"],
1,
)
d["faelle_covid_aktuell_proz"] = round(
100 * d["faelle_covid_aktuell"] / d["betten_ges"],
1,
)
else:
d["betten_belegt_proz"] = None
d["faelle_covid_aktuell_proz"] = None
if d["faelle_covid_aktuell"] > 0:
d["faelle_covid_aktuell_beatmet_proz"] = round(
100
* d["faelle_covid_aktuell_invasiv_beatmet"]
/ d["faelle_covid_aktuell"],
1,
)
else:
d["faelle_covid_aktuell_beatmet_proz"] = 0
# if "daten_stand" in row:
# d["daten_stand"] = row["daten_stand"]
# else:
# d["daten_stand"] = date
if lk_id not in d_database:
d_database[lk_id] = []
d_database[lk_id].append(d)
# calc de_states_sum
d2 = dict(d)
del (
d2["Date"],
d2["betten_ges"],
d2["betten_belegt_proz"],
d2["faelle_covid_aktuell_proz"],
d2["faelle_covid_aktuell_beatmet_proz"],
)
if date not in d_database_states[bl_id]:
d_database_states[bl_id][date] = d2
else:
for k in d2.keys():
d_database_states[bl_id][date][k] += d2[k]
# 'DE-total'
if date not in d_database_states["00"]:
d_database_states["00"][date] = d2
else:
for k in d2.keys():
d_database_states["00"][date][k] += d2[k]
# print(d_database_states[bl_id][date])
helper.write_json(
"cache/de-divi/de-divi-V3.json",
d_database,
sort_keys=True,
)
l_lkids = d_database.keys()
helper.write_json_list(
filename="data/de-divi/lkids.json",
l=sorted(set(l_lkids)),
sort_keys=True,
)
d_database_states2 = {}
for bl_id in d_database_states.keys():
bl_code = helper.d_BL_code_from_BL_ID[int(bl_id)]
d_database_states2[bl_code] = []
for date, d in d_database_states[bl_id].items():
d["Date"] = date
# copy from above:
d["betten_ges"] = d["betten_frei"] + d["betten_belegt"]
if d["betten_ges"] > 0:
d["betten_belegt_proz"] = round(
100 * d["betten_belegt"] / d["betten_ges"],
1,
)
d["faelle_covid_aktuell_proz"] = round(
100 * d["faelle_covid_aktuell"] / d["betten_ges"],
1,
)
else:
d["betten_belegt_proz"] = None
d["faelle_covid_aktuell_proz"] = None
if d["faelle_covid_aktuell"] > 0:
d["faelle_covid_aktuell_beatmet_proz"] = round(
100
* d["faelle_covid_aktuell_invasiv_beatmet"]
/ d["faelle_covid_aktuell"],
1,
)
else:
d["faelle_covid_aktuell_beatmet_proz"] = 0
d_database_states2[bl_code].append(d)
del d_database_states
helper.write_json(
filename="cache/de-divi/de-divi-V3-states.json",
d=d_database_states2,
sort_keys=True,
)
return d_database
# def generate_database_old() -> dict:
# d_database = {}
# # d_database_states = {} # Bundesländer
# d_database_states = {'01': {}, '02': {}, '03': {}, '04': {}, '05': {}, '06': {}, '07': {
# }, '08': {}, '09': {}, '10': {}, '11': {}, '12': {}, '13': {}, '14': {}, '15': {}, '16': {}, 'DE-total': {}}
# for csv_file in sorted(glob.glob('data/de-divi/downloaded/*.csv')):
# (filepath, fileName) = os.path.split(csv_file)
# (fileBaseName, fileExtension) = os.path.splitext(fileName)
# date = fileBaseName
# del filepath, fileName, fileBaseName, fileExtension
# # file 2020-04-24.csv:
# # bundesland,kreis,anzahl_standorte,betten_frei,betten_belegt,faelle_covid_aktuell_im_bundesland
# # file 2020-04-26.csv:
# # gemeindeschluessel,anzahl_meldebereiche,faelle_covid_aktuell,faelle_covid_aktuell_beatmet,anzahl_standorte,betten_frei,betten_belegt,bundesland
# # 2020-04-28.csv
# # gemeindeschluessel,anzahl_meldebereiche,faelle_covid_aktuell,faelle_covid_aktuell_beatmet,anzahl_standorte,betten_frei,betten_belegt,bundesland,daten_stand
# # file 2020-06-28.csv
# # bundesland,gemeindeschluessel,anzahl_meldebereiche,faelle_covid_aktuell,faelle_covid_aktuell_beatmet,anzahl_standorte,betten_frei,betten_belegt,daten_stand
# # -> skipping file 2020-04-24.csv and 2020-04-25.csv
# if date in ('2020-04-24', '2020-04-25'):
# continue
# with open(csv_file, mode='r', encoding='utf-8') as f:
# csv_reader = csv.DictReader(f, delimiter=",")
# for row in csv_reader:
# assert len(row) >= 8, "Error: too few rows found"
# bl_id = row["bundesland"]
# lk_id = row["gemeindeschluessel"]
# d = {
# # "bl_id": row["bundesland"],
# # "lk_id": row["gemeindeschluessel"],
# "Date": date,
# "anzahl_meldebereiche": int(row["anzahl_meldebereiche"]),
# "faelle_covid_aktuell": int(row["faelle_covid_aktuell"]),
# "anzahl_standorte": int(row["anzahl_standorte"]),
# "betten_frei": int(float(row["betten_frei"])),
# "betten_belegt": int(float(row["betten_belegt"]))
# }
# if "faelle_covid_aktuell_beatmet" in row:
# d["faelle_covid_aktuell_beatmet"] = int(
# row["faelle_covid_aktuell_beatmet"])
# elif "faelle_covid_aktuell_invasiv_beatmet" in row:
# d["faelle_covid_aktuell_beatmet"] = int(
# row["faelle_covid_aktuell_invasiv_beatmet"])
# d["betten_ges"] = d["betten_frei"] + d["betten_belegt"]
# if d["betten_ges"] > 0:
# d["betten_belegt_proz"] = round(100 *
# d["betten_belegt"] / d["betten_ges"], 1)
# d["faelle_covid_aktuell_proz"] = round(100*d["faelle_covid_aktuell"] /
# d["betten_ges"], 1)
# else:
# d["betten_belegt_proz"] = None
# d["faelle_covid_aktuell_proz"] = None
# if d["faelle_covid_aktuell"] > 0:
# d["faelle_covid_aktuell_beatmet_proz"] = round(
# 100*d["faelle_covid_aktuell_beatmet"] / d["faelle_covid_aktuell"], 1)
# else:
# d["faelle_covid_aktuell_beatmet_proz"] = 0
# # if "daten_stand" in row:
# # d["daten_stand"] = row["daten_stand"]
# # else:
# # d["daten_stand"] = date
# if lk_id not in d_database:
# d_database[lk_id] = []
# d_database[lk_id].append(d)
# # calc de_states_sum
# d2 = dict(d)
# del d2['Date'], d2['betten_ges'], d2['betten_belegt_proz'], d2['faelle_covid_aktuell_proz'], d2['faelle_covid_aktuell_beatmet_proz']
# if date not in d_database_states[bl_id]:
# d_database_states[bl_id][date] = d2
# else:
# for k in d2.keys():
# d_database_states[bl_id][date][k] += d2[k]
# # 'DE-total'
# if date not in d_database_states['DE-total']:
# d_database_states['DE-total'][date] = d2
# else:
# for k in d2.keys():
# d_database_states['DE-total'][date][k] += d2[k]
# # print(d_database_states[bl_id][date])
# helper.write_json('cache/de-divi/de-divi-V3.json',
# d_database, sort_keys=True,)
# d_database_states2 = {}
# for bl_id in d_database_states.keys():
# bl_code = helper.d_BL_code_from_BL_ID[int(bl_id)]
# d_database_states2[bl_code] = []
# for date, d in d_database_states[bl_id].items():
# d['Date'] = date
# # copy from above:
# d["betten_ges"] = d["betten_frei"] + d["betten_belegt"]
# if d["betten_ges"] > 0:
# d["betten_belegt_proz"] = round(100 *
# d["betten_belegt"] / d["betten_ges"], 1)
# d["faelle_covid_aktuell_proz"] = round(100*d["faelle_covid_aktuell"] /
# d["betten_ges"], 1)
# else:
# d["betten_belegt_proz"] = None
# d["faelle_covid_aktuell_proz"] = None
# if d["faelle_covid_aktuell"] > 0:
# d["faelle_covid_aktuell_beatmet_proz"] = round(
# 100*d["faelle_covid_aktuell_beatmet"] / d["faelle_covid_aktuell"], 1)
# else:
# d["faelle_covid_aktuell_beatmet_proz"] = 0
# d_database_states2[bl_code].append(d)
# del d_database_states
# helper.write_json('cache/de-divi/de-divi-V3-states.json',
# d_database_states2, sort_keys=True, )
# return d_database
def export_tsv(d_database) -> None:
for lk_id, l_time_series in d_database.items():
fileOut = f"data/de-divi/tsv/{lk_id}"
with open(fileOut + ".tsv", mode="w", encoding="utf-8", newline="\n") as fh:
csvwriter = csv.DictWriter(
fh,
delimiter="\t",
extrasaction="ignore",
fieldnames=[
"Date",
"betten_ges",
"betten_belegt",
"betten_belegt_proz",
"faelle_covid_aktuell",
"faelle_covid_aktuell_proz",
"faelle_covid_aktuell_invasiv_beatmet",
"faelle_covid_aktuell_beatmet_proz",
],
)
csvwriter.writeheader()
for d in l_time_series:
csvwriter.writerow(d)
# fetch_all_csvs()
fetch_latest_csv()
d_database = generate_database()
export_tsv(d_database)