forked from entorb/COVID-19-Coronavirus-German-Regions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-de-divi-V3.py
164 lines (134 loc) · 6.76 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
import re
import requests # for read_url_or_cachefile
import glob
import os.path
import csv
# my helper modules
import helper
"""
lk_id sind https://de.wikipedia.org/wiki/Amtlicher_Gemeindeschl%C3%BCssel
"""
def extractLinkList(cont: str) -> list:
# myPattern = '<a href="(/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-[^"]+/download)"'
myPattern = '<a href="(/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-[^"]+/viewdocument[^"]*)"'
myRegExp = re.compile(myPattern)
myMatches = myRegExp.findall(cont)
assert len(myMatches) > 10, "Error: no csv download links found"
return myMatches
def fetch_latest_csvs():
"""
fetches the 5 latest 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=3600, 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}"
filename = re.search(
'/divi-intensivregister-tagesreport-archiv-csv/divi-intensivregister-(\d{4}\-\d{2}\-\d{2})[^/]+/viewdocument', 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"
cont = helper.read_url_or_cachefile(
url=url, cachefile=cachefile, request_type='get', payload={}, cache_max_age=3600, verbose=True)
def generate_database() -> dict:
d_database = {}
for csv_file in 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"]),
"faelle_covid_aktuell_beatmet": int(row["faelle_covid_aktuell_beatmet"]),
"anzahl_standorte": int(row["anzahl_standorte"]),
"betten_frei": int(float(row["betten_frei"])),
"betten_belegt": int(float(row["betten_belegt"]))
}
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)
helper.write_json('cache/de-divi/de-divi-V3.json',
d_database, sort_keys=True, indent=1)
return d_database
def export_tsv(d_database):
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_beatmet',
'faelle_covid_aktuell_beatmet_proz'])
csvwriter.writeheader()
for d in l_time_series:
csvwriter.writerow(d)
pass
fetch_latest_csvs()
d_database = generate_database()
export_tsv(d_database)
pass