home · contact · privacy
bbb4835c00fc5eedebe064ab540264d1470b0c05
[berlin-corona-table] / scrape.py
1 #!/usr/bin/env python3
2 import urllib.request
3 import datetime
4 import bs4
5 import re
6
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='
10
11 # Map abbreviations to full names (and their alternate spellings).
12 abbrevs = {
13   'CW': {'Charlottenburg-Wilmersdorf'},
14   'FK': {'Friedrichshain-Kreuzberg'},
15   'Li': {'Lichtenberg'},
16   'MH': {'Marzahn-Hellersdorf'},
17   'Mi': {'Mitte'},
18   'Ne': {'Neukölln', 'Neuköln'},
19   'Pa': {'Pankow'},
20   'Re': {'Reinickendorf'},
21   'Sp': {'Spandau'},
22   'SZ': {'Steglitz-Zehlendorf'},
23   'TS': {'Tempelhof-Schöneberg'},
24   'TK': {'Treptow-Köpenick'},
25   'sum': {'Summe', 'Berlin'},
26 }
27
28 # some pre-filled values
29 data = {
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}
45     },
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}
60     },
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}
75     },
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}
90     },
91    # This one has no press release but can be reconstructed from
92    # the neighbour ones.
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}
107    },
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}
123    },
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}
139    },
140    # Here the growth numbers needed to be reconstructed.
141    datetime.datetime(2020, 3, 8): {
142         'CW': {'growth': 0, 'total': 9},
143         'FK': {'growth': 4, 'total': 9},
144         'Li': {'growth': 1, 'total': 1},
145         'MH': {'growth': 0, 'total': 1},
146         'Mi': {'growth': 0, 'total': 8},
147         'Ne': {'growth': 0, 'total': 2},
148         'Pa': {'growth': 0, 'total': 5},
149         'Re': {'growth': 0, 'total': 3},
150         'Sp': {'growth': 2, 'total': 2},
151         'SZ': {'growth': 1, 'total': 3},
152         'TS': {'growth': 0, 'total': 5},
153         'TK': {'growth': 0, 'total': 0},
154         'sum': {'growth': 8, 'total': 48}
155    },
156    # Here the growth numbers needed to be reconstructed.
157    datetime.datetime(2020, 3, 7): {
158         'CW': {'growth': 6, 'total': 9},
159         'FK': {'growth': 1, 'total': 5},
160         'Li': {'growth': 0, 'total': 0},
161         'MH': {'growth': 0, 'total': 1},
162         'Mi': {'growth': 1, 'total': 8},
163         'Ne': {'growth': 0, 'total': 2},
164         'Pa': {'growth': 1, 'total': 5},
165         'Re': {'growth': 0, 'total': 3},
166         'Sp': {'growth': 0, 'total': 0},
167         'SZ': {'growth': 2, 'total': 2},
168         'TS': {'growth': 1, 'total': 5},
169         'TK': {'growth': 0, 'total': 0},
170         'sum': {'growth': 12, 'total': 40}
171    },
172 }
173 fixes = {
174    # Here the official total is 215, while the summation of district
175    # numbers only adds up to 125 – pretty much looks like a mere
176    # transposition of digits.
177    datetime.datetime(2020, 3, 27): {
178        'sum': {
179            'growth': 125
180        }
181    },
182    # Here the official total is 1937, while the summation of district
183    # numbers only adds up to 1917; furthermore, the original value for
184    # SZ is 118 (+18), which makes no sense, as the day before is
185    # 120 (+15) and the day after is 147 (+15).  The following is a
186    # compromise to keep as many surrounding numbers stable as possible.
187    datetime.datetime(2020, 3, 26): {
188        'SZ': {
189            'growth': 12
190        },
191        'sum': {
192            'growth': 286
193        }
194    },
195    # Here the official total is 220, while the summation of district
196    # numbers adds up to 228 – looks like someone misread an 8 as a 0.
197    datetime.datetime(2020, 3, 25): {
198        'sum': {
199            'growth': 220
200        }
201    },
202 }
203
204 # Scan navigation bar for maximum pagination value.
205 url = url_prefix + pm_dir
206 with urllib.request.urlopen(url) as response:
207    html = response.read()
208 soup = bs4.BeautifulSoup(html, 'html.parser')
209 max_page=0
210 for link in soup.find_all('a'):
211     href = link['href']
212     if str.startswith(href, pm_nav_path):
213         max_test = int(href.split('=')[1])
214         max_page = max_test if max_test > max_page else max_page
215
216 # Scan paginated press release links for daily Corona number briefing links.
217 day_urls = []
218 for i in range(max_page):
219     url = url_prefix + pm_nav_path + str(i + 1)
220     with urllib.request.urlopen(url) as response:
221         html = response.read()
222     soup = bs4.BeautifulSoup(html, 'html.parser')
223     for link in soup.find_all('a'):
224         if (not link.string) or\
225            (not link.string.startswith('Coronavirus: Derzeit') and
226             not link.string.startswith('Coronavirus in Berlin: Bestätigte Fälle')):
227             continue
228         day_urls += [link['href']]
229
230 # Collect infection data.
231 first_run = True
232 districts_sorted = []
233 # TODO: Push limit further back (might need more data fixes for that).
234 date_limit = datetime.datetime(2020, 3, 12)
235 for path in day_urls:
236     url = url_prefix + path
237     with urllib.request.urlopen(url) as response:
238         html = response.read()
239     soup = bs4.BeautifulSoup(html, 'html.parser')
240     date_title = soup.find('div', class_='pressnumber')
241     m = re.search('[0-9]+\\.[0-9]+\\.[0-9]+', date_title.string)
242     date_formatted = m.group(0)
243     date = datetime.datetime.strptime(date_formatted , '%d.%m.%Y')
244     if date_limit > date:
245         break
246     # On that day, two press releases were released, for that and the prev day.
247     if date == datetime.datetime(2020, 3, 15) and date in data:
248        date = datetime.datetime(2020, 3, 14)
249     # From here on, press releases describe numbers from prev day.
250     if date <= datetime.datetime(2020, 3, 13):
251        date = date - datetime.timedelta(days=1)
252     table = soup.find('table')
253     if table is None and date in data:
254         continue
255     data[date] = {}
256     for tr in [tr for tr in table.children if type(tr) == bs4.element.Tag][1:]:
257         printable_tds = []
258         for td in [td for td in tr.children if type(td) == bs4.element.Tag][:2]:
259             printable_string = ' '.join([s for s in td.strings])
260             printable_tds += [printable_string.strip()]
261         district_long = printable_tds[0]
262         district_short = [k for k in abbrevs if district_long in abbrevs[k]][0]
263         if first_run:
264             districts_sorted += [district_short]
265         split_char = ' '
266         if not split_char in printable_tds[1]:
267             split_char = '('
268         total_str, growth_str = printable_tds[1].split(split_char)
269         growth = int(growth_str.replace('(', '').replace(')', '').replace('+', ''))
270         total = int(total_str.replace('.', ''))
271         data[date][district_short] = {'growth': growth, 'total': total}
272     first_run = False
273 dates_sorted = list(data.keys())
274 dates_sorted.sort()
275 dates_sorted.reverse()
276
277 # Apply fixes and ensure integrity of results
278 for date in fixes:
279     for district in fixes[date]:
280         for type_ in fixes[date][district]:
281             data[date][district][type_] = fixes[date][district][type_]
282 for date in dates_sorted:
283     if date in fixes:
284        continue
285     for district in [d for d in districts_sorted if not d=='sum']:
286         prev_date = date - datetime.timedelta(days=1)
287         if prev_date not in dates_sorted:
288            if prev_date >= date_limit:
289               raise Exception('Dates not contiguous: %s missing', prev_date)
290            else:
291               continue
292         prev_total = data[prev_date][district]['total']
293         cur_total = data[date][district]['total']
294         if cur_total - data[date][district]['growth'] != prev_total:
295             raise Exception('Questionable district infection total in %s/%s' % (district, date))
296     day_sum = 0
297     for district in [d for d in districts_sorted if not d=='sum']:
298        day_sum += data[date][district]['total']
299     if day_sum != data[date]['sum']['total']:
300         raise Exception('Questionable district infection total sum in %s' % date)
301     day_sum = 0
302     for district in [d for d in districts_sorted if not d=='sum']:
303        day_sum += data[date][district]['growth']
304     if day_sum != data[date]['sum']['growth']:
305         raise Exception('Questionable district infection growth sum in %s' % date)
306
307 # Final output.
308 print(' '*10, ' '.join(['%3s' % d for d in districts_sorted]))
309 for date in dates_sorted:
310     growths = []
311     for d in districts_sorted:
312         growths += [data[date][d]['growth']]
313     print(date.strftime('%Y-%m-%d'), ' '.join(['%3s' % g for g in growths]))