7 url_prefix = 'https://www.berlin.de'
8 pm_dir = '/sen/gpg/service/presse/2020/'
9 pm_nav_path = pm_dir + '?page_at_1_0='
11 # Map abbreviations to full names (and their alternate spellings).
13 'CW': {'Charlottenburg-Wilmersdorf'},
14 'FK': {'Friedrichshain-Kreuzberg'},
15 'Li': {'Lichtenberg'},
16 'MH': {'Marzahn-Hellersdorf'},
18 'Ne': {'Neukölln', 'Neuköln'},
20 'Re': {'Reinickendorf'},
22 'SZ': {'Steglitz-Zehlendorf'},
23 'TS': {'Tempelhof-Schöneberg'},
24 'TK': {'Treptow-Köpenick'},
25 'sum': {'Summe', 'Berlin'},
28 # some pre-filled values
30 # For these, only image files are available for the table data.
31 datetime.datetime(2020, 7, 2): {
32 'CW': {'growth': 4, 'total': 851},
33 'FK': {'growth': 10, 'total': 681},
34 'Li': {'growth': 3, 'total': 427},
35 'MH': {'growth': 4, 'total': 468},
36 'Mi': {'growth': 0, 'total': 1202},
37 'Ne': {'growth': 7, 'total': 1031},
38 'Pa': {'growth': 3, 'total': 784},
39 'Re': {'growth': 6, 'total': 660},
40 'Sp': {'growth': 3, 'total': 450},
41 'SZ': {'growth': 0, 'total': 591},
42 'TS': {'growth': 3, 'total': 798},
43 'TK': {'growth': 0, 'total': 401},
44 'sum': {'growth': 43, 'total': 8344}
46 datetime.datetime(2020, 4, 5): {
47 'CW': {'growth': 9, 'total': 462},
48 'FK': {'growth': 2, 'total': 352},
49 'Li': {'growth': 0, 'total': 142},
50 'MH': {'growth': 3, 'total': 127},
51 'Mi': {'growth': 14, 'total': 537},
52 'Ne': {'growth': 0, 'total': 392},
53 'Pa': {'growth': 10, 'total': 378},
54 'Re': {'growth': 9, 'total': 248},
55 'Sp': {'growth': 3, 'total': 150},
56 'SZ': {'growth': 0, 'total': 312},
57 'TS': {'growth': 8, 'total': 394},
58 'TK': {'growth': 3, 'total': 193},
59 'sum': {'growth': 61, 'total': 3687}
61 datetime.datetime(2020, 4, 4): {
62 'CW': {'growth': 2, 'total': 453},
63 'FK': {'growth': 7, 'total': 350},
64 'Li': {'growth': 0, 'total': 142},
65 'MH': {'growth': 15, 'total': 124},
66 'Mi': {'growth': 22, 'total': 523},
67 'Ne': {'growth': 15, 'total': 392},
68 'Pa': {'growth': 10, 'total': 368},
69 'Re': {'growth': 5, 'total': 239},
70 'Sp': {'growth': 21, 'total': 147},
71 'SZ': {'growth': 12, 'total': 312},
72 'TS': {'growth': 24, 'total': 386},
73 'TK': {'growth': 7, 'total': 190},
74 'sum': {'growth': 140, 'total': 3626}
76 datetime.datetime(2020, 4, 3): {
77 'CW': {'growth': 44, 'total': 451},
78 'FK': {'growth': 17, 'total': 343},
79 'Li': {'growth': 7, 'total': 142},
80 'MH': {'growth': 4, 'total': 109},
81 'Mi': {'growth': 4, 'total': 501},
82 'Ne': {'growth': 40, 'total': 377},
83 'Pa': {'growth': 39, 'total': 358},
84 'Re': {'growth': 26, 'total': 234},
85 'Sp': {'growth': 9, 'total': 126},
86 'SZ': {'growth': 18, 'total': 300},
87 'TS': {'growth': 41, 'total': 362},
88 'TK': {'growth': 14, 'total': 183},
89 'sum': {'growth': 263, 'total': 3486}
91 # This one has no press release but can be reconstructed from
93 datetime.datetime(2020, 3, 13): {
94 'CW': {'growth': 16, 'total': 47},
95 'FK': {'growth': 8, 'total': 22},
96 'Li': {'growth': 2, 'total': 8},
97 'MH': {'growth': 1, 'total': 4},
98 'Mi': {'growth': 9, 'total': 29},
99 'Ne': {'growth': 6, 'total': 16},
100 'Pa': {'growth': 11, 'total': 26},
101 'Re': {'growth': 0, 'total': 11},
102 'Sp': {'growth': 1, 'total': 9},
103 'SZ': {'growth': 0, 'total': 20},
104 'TS': {'growth': 1, 'total': 17},
105 'TK': {'growth': 3, 'total': 7},
106 'sum': {'growth': 58, 'total': 216}
108 # Here the growth numbers needed to be reconstructed.
109 datetime.datetime(2020, 3, 10): {
110 'CW': {'growth': 2, 'total': 15},
111 'FK': {'growth': 0, 'total': 12},
112 'Li': {'growth': 4, 'total': 5},
113 'MH': {'growth': 1, 'total': 3},
114 'Mi': {'growth': 0, 'total': 8},
115 'Ne': {'growth': 2, 'total': 5},
116 'Pa': {'growth': 2, 'total': 8},
117 'Re': {'growth': 0, 'total': 3},
118 'Sp': {'growth': 4, 'total': 6},
119 'SZ': {'growth': 3, 'total': 6},
120 'TS': {'growth': 2, 'total': 7},
121 'TK': {'growth': 3, 'total': 3},
122 'sum': {'growth': 23, 'total': 81}
124 # Here the totals needed to be reconstructed.
125 datetime.datetime(2020, 3, 9): {
126 'CW': {'growth': 4, 'total': 13},
127 'FK': {'growth': 3, 'total': 12},
128 'Li': {'growth': 0, 'total': 1},
129 'MH': {'growth': 1, 'total': 2},
130 'Mi': {'growth': 0, 'total': 8},
131 'Ne': {'growth': 1, 'total': 3},
132 'Pa': {'growth': 1, 'total': 6},
133 'Re': {'growth': 0, 'total': 3},
134 'Sp': {'growth': 0, 'total': 2},
135 'SZ': {'growth': 0, 'total': 3},
136 'TS': {'growth': 0, 'total': 5},
137 'TK': {'growth': 0, 'total': 0},
138 'sum': {'growth': 10, 'total': 58}
142 # Here the official total is 215, while the summation of district
143 # numbers only adds up to 125 – pretty much looks like a mere
144 # transposition of digits.
145 datetime.datetime(2020, 3, 27): {
150 # Here the official total is 1937, while the summation of district
151 # numbers only adds up to 1917; furthermore, the original value for
152 # SZ is 118 (+18), which makes no sense, as the day before is
153 # 120 (+15) and the day after is 147 (+15). The following is a
154 # compromise to keep as many surrounding numbers stable as possible.
155 datetime.datetime(2020, 3, 26): {
163 # Here the official total is 220, while the summation of district
164 # numbers adds up to 228 – looks like someone misread an 8 as a 0.
165 datetime.datetime(2020, 3, 25): {
172 # Scan navigation bar for maximum pagination value.
173 url = url_prefix + pm_dir
174 with urllib.request.urlopen(url) as response:
175 html = response.read()
176 soup = bs4.BeautifulSoup(html, 'html.parser')
178 for link in soup.find_all('a'):
180 if str.startswith(href, pm_nav_path):
181 max_test = int(href.split('=')[1])
182 max_page = max_test if max_test > max_page else max_page
184 # Scan paginated press release links for daily Corona number briefing links.
186 for i in range(max_page):
187 url = url_prefix + pm_nav_path + str(i + 1)
188 with urllib.request.urlopen(url) as response:
189 html = response.read()
190 soup = bs4.BeautifulSoup(html, 'html.parser')
191 for link in soup.find_all('a'):
192 if (not link.string) or\
193 (not link.string.startswith('Coronavirus: Derzeit') and
194 not link.string.startswith('Coronavirus in Berlin: Bestätigte Fälle')):
196 day_urls += [link['href']]
198 # Collect infection data.
200 districts_sorted = []
201 # TODO: Push limit further back (might need more data fixes for that).
202 date_limit = datetime.datetime(2020, 3, 12)
203 for path in day_urls:
204 url = url_prefix + path
205 with urllib.request.urlopen(url) as response:
206 html = response.read()
207 soup = bs4.BeautifulSoup(html, 'html.parser')
208 date_title = soup.find('div', class_='pressnumber')
209 m = re.search('[0-9]+\\.[0-9]+\\.[0-9]+', date_title.string)
210 date_formatted = m.group(0)
211 date = datetime.datetime.strptime(date_formatted , '%d.%m.%Y')
212 if date_limit > date:
214 # On that day, two press releases were released, for that and the prev day.
215 if date == datetime.datetime(2020, 3, 15) and date in data:
216 date = datetime.datetime(2020, 3, 14)
217 # From here on, press releases describe numbers from prev day.
218 if date <= datetime.datetime(2020, 3, 13):
219 date = date - datetime.timedelta(days=1)
220 table = soup.find('table')
221 if table is None and date in data:
224 for tr in [tr for tr in table.children if type(tr) == bs4.element.Tag][1:]:
226 for td in [td for td in tr.children if type(td) == bs4.element.Tag][:2]:
227 printable_string = ' '.join([s for s in td.strings])
228 printable_tds += [printable_string.strip()]
229 district_long = printable_tds[0]
230 district_short = [k for k in abbrevs if district_long in abbrevs[k]][0]
232 districts_sorted += [district_short]
234 if not split_char in printable_tds[1]:
236 total_str, growth_str = printable_tds[1].split(split_char)
237 growth = int(growth_str.replace('(', '').replace(')', '').replace('+', ''))
238 total = int(total_str.replace('.', ''))
239 data[date][district_short] = {'growth': growth, 'total': total}
241 dates_sorted = list(data.keys())
243 dates_sorted.reverse()
245 # Apply fixes and ensure integrity of results
247 for district in fixes[date]:
248 for type_ in fixes[date][district]:
249 data[date][district][type_] = fixes[date][district][type_]
250 for date in dates_sorted:
253 for district in [d for d in districts_sorted if not d=='sum']:
254 prev_date = date - datetime.timedelta(days=1)
255 if prev_date not in dates_sorted:
256 if prev_date >= date_limit:
257 raise Exception('Dates not contiguous: %s missing', prev_date)
260 prev_total = data[prev_date][district]['total']
261 cur_total = data[date][district]['total']
262 if cur_total - data[date][district]['growth'] != prev_total:
263 raise Exception('Questionable district infection total in %s/%s' % (district, date))
265 for district in [d for d in districts_sorted if not d=='sum']:
266 day_sum += data[date][district]['total']
267 if day_sum != data[date]['sum']['total']:
268 raise Exception('Questionable district infection total sum in %s' % date)
270 for district in [d for d in districts_sorted if not d=='sum']:
271 day_sum += data[date][district]['growth']
272 if day_sum != data[date]['sum']['growth']:
273 raise Exception('Questionable district infection growth sum in %s' % date)
276 print(' '*10, ' '.join(['%3s' % d for d in districts_sorted]))
277 for date in dates_sorted:
279 for d in districts_sorted:
280 growths += [data[date][d]['growth']]
281 print(date.strftime('%Y-%m-%d'), ' '.join(['%3s' % g for g in growths]))