-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgremienkalender.py
executable file
·359 lines (306 loc) · 12.2 KB
/
gremienkalender.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
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Crawl and parse calendars of the Berlin Councils' committees.
Crawl and parse calendars of the Berlin Councils' committees and
convert forthcoming committee meetings to iCalendar data format and
write them to one iCalendar file per committee.
"""
import argparse
import http.client
import logging
import os
import ssl
import time
import zlib
import lxml.html
parser = argparse.ArgumentParser()
parser.add_argument(
'--log', type=str, choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='ERROR')
args = parser.parse_args()
level = getattr(logging, args.log.upper(), None)
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S', level=level)
HOST = 'www.berlin.de'
SESSION = http.client.HTTPSConnection(
HOST, context=ssl.create_default_context(), timeout=10)
REQUEST_DELAY = 10
REQUEST_HEADERS = {'Connection': 'keep-alive', 'Accept-Encoding': 'gzip'}
DTSTAMP = '{}{:02d}{:02d}T{:02d}{:02d}{:02d}Z'.format(*time.gmtime())
BOROUGH_NAMES = {
'ba-charlottenburg-wilmersdorf': 'Charlottenburg-Wilmersdorf',
'ba-friedrichshain-kreuzberg': 'Friedrichshain-Kreuzberg',
'ba-lichtenberg': 'Lichtenberg',
'ba-marzahn-hellersdorf': 'Marzahn-Hellersdorf',
'ba-mitte': 'Mitte',
'ba-neukoelln': 'Neukölln',
'ba-pankow': 'Pankow',
'ba-reinickendorf': 'Reinickendorf',
'ba-spandau': 'Spandau',
'ba-steglitz-zehlendorf': 'Steglitz-Zehlendorf',
'ba-tempelhof-schoeneberg': 'Tempelhof-Schöneberg',
'ba-treptow-koepenick': 'Treptow-Köpenick'
}
def save_cookie(response):
"""Find and save ALLRIS session cookies from server response if present."""
session_cookie = response.getheader('Set-Cookie')
if session_cookie:
session_cookie = session_cookie.split(';', 1)[0]
REQUEST_HEADERS['Cookie'] = session_cookie
def decode_response(response_body):
"""Decode response body and return a unicode string."""
try:
response_body = response_body.decode('iso-8859-1', 'strict')
except UnicodeDecodeError:
print('decoding iso-8859-1 failed')
try:
response_body = response_body.decode('iso-8859-15', 'strict')
except UnicodeDecodeError:
print('decoding iso-8859-15 failed, too')
response_body = response_body.decode('windows-1252', 'replace')
return response_body
def find_allriscontainer(response_body, base_url):
"""Find allriscontainer div element in html page source string."""
response_body = response_body.split('s-->', 1)[1]
response_body = response_body.split('<!-- H', 1)[0]
html = lxml.html.fromstring(response_body, base_url=base_url)
for div in html.getiterator('div'):
if div.get('id') and div.get('id') == 'allriscontainer':
return div
def get_allriscontainer(url):
"""Return the *url*s' response body as an lxml.html.HtmlElement."""
logging.info("url: {}".format(url))
request_path = url.split(HOST, 1)[1]
logging.debug("path: {}".format(request_path))
time.sleep(REQUEST_DELAY)
SESSION.request('GET', request_path, headers=REQUEST_HEADERS)
try:
response = SESSION.getresponse()
except (http.client.BadStatusLine, http.client.socket.timeout):
logging.warning('starting a new session')
SESSION.close()
if 'Cookie' in REQUEST_HEADERS:
del REQUEST_HEADERS['Cookie']
time.sleep(REQUEST_DELAY)
SESSION.request('GET', request_path, headers=REQUEST_HEADERS)
response = SESSION.getresponse()
if response.status != 200:
message = "{} {}".format(response.status, url)
logging.error(message)
raise Exception(message)
save_cookie(response)
response_body = response.read()
response_body = zlib.decompress(response_body, 47)
response_body = decode_response(response_body)
return find_allriscontainer(response_body, url)
def findall_calendars(allriscontainer):
"""Return a list of calendar links extracted from html content."""
for select in allriscontainer.getiterator('select'):
if select.get('id') and select.get('id') == 'GRA':
values = set()
for option in select.getiterator('option'):
if not option.get('class') == 'calWeek':
if not 'inaktiv' in option.text:
value = option.get('value')
value = int(value)
values.add(value)
base = allriscontainer.base_url
values = sorted(values)
return ['{}?GRA={}'.format(base, value) for value in values]
def date_range(months=3):
"""Return an URL query string."""
year_from, month_from, *_ = time.localtime()
year_to = year_from
month_to = month_from + months
while month_to > 12:
year_to += 1
month_to -= 12
template = 'YYV={}&MMV={}&YYB={}&MMB={}'
return template.format(year_from, month_from, year_to, month_to)
DATE_RANGE = date_range()
def find_borough_slug(url):
slug = url.split('/', 4)[3]
slug = slug[3:]
return slug
def find_committee_id(url):
query_pairs = url.split('?', 1)[1]
query_pairs = query_pairs.split('&')
for pair in query_pairs:
name, value = pair.split('=', 1)
if name == 'GRA' and value.isdigit():
return int(value)
def find_calendar_url(url):
calendar_url = url
calendar_url = calendar_url.split('&', 1)[0]
return calendar_url
def find_calendar_uid(url):
borough = find_borough_slug(url)
committee = find_committee_id(url)
calendar_uid = borough + '-' + '%03d' % committee
calendar_uid = '%s-%03d' % (borough, committee)
calendar_uid = '{}-{:03d}'.format(borough, committee)
return calendar_uid
def find_calendar_borough(url):
calendar_borough = url
calendar_borough = calendar_borough.split('/', 4)[3]
calendar_borough = BOROUGH_NAMES[calendar_borough]
return calendar_borough
def find_calendar_committee(allriscontainer):
cells = allriscontainer.getiterator('th')
for cell in cells:
if cell.get('colspan') == '6':
committee = cell.text_content()
committee = committee[23:].split(' im Zeitraum', 1)[0]
return committee
def findall_tablerows_zl1n(allriscontainer):
for table in allriscontainer.getiterator('table'):
if table.get('class') == 'tl1':
tablerows = []
for row in table.getiterator('tr'):
if row.get('class') == 'zl11' or row.get('class') == 'zl12':
tablerows.append(row)
return tablerows
def find_event_dtstart(row):
date_text = row[0].text[4:]
time_text = row[1].text[:5]
if date_text.strip() and time_text.strip():
dtstart = (
int(date_text[6:10]),
int(date_text[3:5]),
int(date_text[0:2]),
int(time_text[0:2]),
int(time_text[3:5]),
0,
0,
0,
0
)
elapsed_time = (time.time() - time.mktime(dtstart))
one_day = 60*60*24
if elapsed_time < 1*one_day:
dtstart = '{}{:02d}{:02d}T{:02d}{:02d}{:02d}'.format(*dtstart)
return dtstart
def find_event_description(row):
try:
return row[3][0].text
except IndexError:
return row[3].text
def find_event_url(row):
try:
href = row[3][0].get('href')
return 'https://{}{}'.format(HOST, href)
except IndexError:
return ''
def findall_events(allriscontainer):
events = []
base_url = allriscontainer.base_url
calendar_uid = find_calendar_uid(base_url)
committee_name = find_calendar_committee(allriscontainer)
rows = findall_tablerows_zl1n(allriscontainer)
for row in rows:
event = {
'dtstamp': DTSTAMP,
'dtstart': find_event_dtstart(row),
'summary': find_calendar_borough(base_url) + ': ' + committee_name,
'location': ''
}
if event.get('dtstart'):
event['url'] = find_event_url(row)
event['description'] = '{}\\n{}\\n-- \\nQuelle: {}\\nStand: {}'.format(
find_event_description(row),
event['url'],
base_url,
"{2:02d}.{1:02d}.{0}, {3:02d}:{4:02d} Uhr".format(
*time.localtime()
)
)
event['uid'] = '{}-{}'.format(calendar_uid, event['dtstart'])
events.append(event)
return events
def extract_vcalendar(allriscontainer):
"""Return a list of committee meetings extracted from html content."""
vcalendar = {
'vevents': findall_events(allriscontainer),
}
if vcalendar.get('vevents'):
base_url = allriscontainer.base_url
vcalendar['url'] = find_calendar_url(base_url)
vcalendar['uid'] = find_calendar_uid(base_url)
vcalendar['borough'] = find_calendar_borough(base_url)
vcalendar['committee'] = find_calendar_committee(allriscontainer)
vcalendar['name'] = '{}: {}'.format(
vcalendar['borough'],
vcalendar['committee']
)
return vcalendar
def fold_content_lines(content):
"""Fold lines of *content* string to a length of 75 octets.
"Lines of text SHOULD NOT be longer than 75 octets, excluding the line
break. Long content lines SHOULD be split into a multiple line
representations using a line "folding" technique. That is, a long
line can be split between any two characters by inserting a CRLF
immediately followed by a single linear white-space character"
https://tools.ietf.org/html/rfc5545#section-3.1
"""
content_lines = content.splitlines()
folded_content_lines = []
max_octets = 75
for line in content_lines:
while line:
characters = max_octets
encoded_line = line[:characters].encode('utf-8')
while len(encoded_line) > max_octets:
characters -= 1
encoded_line = line[:characters].encode('utf-8')
folded_content_lines.append(line[:characters])
line = line[characters:]
if line:
line = ' '+line
folded_content = '\n'.join(folded_content_lines)
return folded_content+'\n'
def write_vcalendar_file(vcalendar):
"""Create iCalendar data format strings and write them to files."""
if vcalendar.get('vevents'):
with open(os.path.join('templates', 'vevent.ics'), 'r') as icsfile:
vevent_template = icsfile.read()
with open(os.path.join('templates', 'vcalendar.ics'), 'r') as icsfile:
vcalendar_template = icsfile.read()
vevents_string = ''
for vevent in vcalendar['vevents']:
vevent_string = vevent_template.format(**vevent)
vevents_string += vevent_string
vcalendar['vevents'] = vevents_string+'\n'
vcalendar_string = vcalendar_template.format(**vcalendar)
vcalendar_string = fold_content_lines(vcalendar_string)
filename = '{}.ics'.format(vcalendar['uid'])
filename = os.path.join('calendars', filename)
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w', newline='\r\n') as icsfile:
icsfile.write(vcalendar_string)
def main():
"""The main function."""
with open('links.txt', 'r') as txtfile:
council_links = txtfile.read()
council_links = council_links.splitlines()
valid_links = {'http://www.berlin.de/ba-',
'https://www.berlin.de/ba'}
council_links = [l for l in council_links if l[:24] in valid_links]
for link in council_links:
try:
allriscontainer = get_allriscontainer(link)
except:
logging.warning('Skipping {}'.format(link))
continue
committee_links = findall_calendars(allriscontainer)
for link in committee_links:
link += '&' + DATE_RANGE
try:
allriscontainer = get_allriscontainer(link)
except:
logging.warning('Skipping {}'.format(link))
continue
vcalendar = extract_vcalendar(allriscontainer)
if vcalendar:
write_vcalendar_file(vcalendar)
SESSION.close()
if __name__ == '__main__':
main()