-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuwr_calendar.py
executable file
·335 lines (260 loc) · 9.84 KB
/
uwr_calendar.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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
#
# A little python-script that produces automatically the weekly needed
# calendar for the UWR.
# (see http://wiki.ubuntuusers.de/ubuntuusers/Ikhayateam/UWR)
#
# Version 2.0 (2015-08-14)
# written by chris34 (http://ubuntuusers.de/user/chris34/)
#
# licensed under the
#
## DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
## Version 2, December 2004
##
## Copyright (C) 2004 Sam Hocevar <[email protected]>
##
## Everyone is permitted to copy and distribute verbatim or modified
## copies of this license document, and changing it is allowed as long
## as the name is changed.
##
## DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
## TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
##
## 0. You just DO WHAT THE FUCK YOU WANT TO.
import datetime
from html.parser import HTMLParser
from html.entities import name2codepoint
from re import sub
import sys
import time
import urllib.request, urllib.error, urllib.parse
class uuCalendarMonthOverviewParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.calendar_table = False
self.parsed_urls = []
def handle_starttag(self, tag, attrs):
if tag == "table" and attrs[0][1] == "calendar_month":
self.calendar_table = True
if self.calendar_table and tag == "a" and attrs[-1][1] == "event_link":
self.parsed_urls.append(attrs[0][1])
def get_parsed_urls(self):
return self.parsed_urls
class uuCalendarEventParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.event_data = {"name": "", # h3 without any attribute
"ort": "", # span with class="location"
"datum": "", # from table class="vevent" -> tbody -> first tr -> second td; allerdings z.B. von Juli 3, 2013 19:30 bis Juli 3, 2013 22:00
}
self.name_found = False
self.ort_found = False
self.datum_table_found = False
self.datum_table_tr_counter = 0
self.datum_found = False
def _convert_entity(self, string):
return chr(name2codepoint[string])
def _convert_charref(self, string):
if string[0] == "x":
return chr(int(string[1:], 16))
else:
return chr(int(string))
def _prepare_name(self):
name_string = self.event_data["name"][len("Veranstaltung")+1:]
name_string = name_string.replace("„", "") # remove „
name_string = name_string.replace("“", "") # remove ”
self.event_data["name"] = name_string
def _prepare_datum(self, data):
'''extracts the beginning of the event'''
datum = sub(r"\s+", " ", data)
if "bis" in datum:
datum = datum[:datum.index("bis")]
if "morgen" in datum:
time_start = datum.index("morgen") + len("morgen") + 1 # 1 → whitespace
time_end = time_start + 5 # f.e. 19:00
time = datum[time_start:time_end]
time_str = time.split(":")
try:
time = datetime.time(int(time_str[0]), int(time_str[1]))
datum_obj = datetime.datetime.combine(datetime.date.today() + datetime.timedelta(1), time)
except ValueError:
datum_obj = datetime.date.today() + datetime.timedelta(1)
elif "heute" in datum or "gestern" in datum:
# only parsed but (hopefully) never in output
datum_obj = datetime.datetime.now()
else:
# month strings (2013-10-21)
to_replace = (
("\n", ""),
("Januar", 1),
("Februar", 2),
("März", 3),
("April", 4),
("Mai", 5),
("Juni", 6),
("Juli", 7),
("August", 8),
("September", 9),
("Oktober", 10),
("November", 11),
("Dezember", 12),
)
for i in to_replace:
datum = datum.replace(i[0], str(i[1]))
datum = datum[5:-1]
try:
datum_obj = datetime.datetime.strptime(datum, "%d. %m %Y %H:%M")
except ValueError:
datum_obj = datetime.datetime.strptime(datum, "%d. %m %Y")
return datum_obj
def _prepare_ort(self):
ort = self.event_data["ort"]
ort = sub(r"\s+", " ", ort)
ort = ort.replace("\n", "")
self.event_data["ort"] = ort.strip()
def handle_starttag(self, tag, attrs):
# event-name detection
if tag == "h3" and len(attrs) == 0:
self.name_found = True
# Ort detection
if tag == "td" and self.datum_table_tr_counter == 2:
self.ort_found = True
# datum detection
# from table class="vevent" -> tbody -> first tr -> second td
# f.e. von Juli 3, 2013 19:30 bis Juli 3, 2013 22:00
if tag == "table" and len(attrs) > 0:
if "vevent" in attrs[0][1]:
self.datum_table_found = True
if self.datum_table_found and tag == "tr":
self.datum_table_tr_counter += 1
if self.datum_table_tr_counter == 1 and tag == "td":
self.datum_found = True
def handle_data(self, data):
if self.name_found:
self.event_data["name"] += data
if self.ort_found and data != "":
self.event_data["ort"] += data
if self.datum_found:
self.event_data["datum"] = self._prepare_datum(data)
def handle_endtag(self, tag):
if tag == "h3" and self.name_found:
self.name_found = False
if (tag == "span" or tag == "td") and self.ort_found:
self.ort_found = False
if tag == "table":
self.datum_table_found = False
if self.datum_found and tag == "td":
self.datum_found = False
def get_parsed_data(self):
self._prepare_ort()
self._prepare_name()
return self.event_data
def handle_entityref(self, name):
if self.name_found:
self.event_data["name"] += self._convert_entity(name)
if self.ort_found:
self.event_data["ort"] += self._convert_entity(name)
def handle_charref(self, name):
if self.name_found:
self.event_data["name"] += self._convert_charref(name)
if self.ort_found:
self.event_data["ort"] += self._convert_charref(name)
def generate_url(date_obj):
url_base = "https://ubuntuusers.de/calendar"
sep = "/"
year = str(date_obj.year)
month = str(date_obj.month)
return sep.join([url_base, year, month])
def download_page(url):
response = urllib.request.urlopen(url).read()
return str(response, "utf-8")
def collect_information(pages):
info_array = pages
for i in range(0, len(info_array)):
parser = uuCalendarEventParser()
parser.feed(download_page(info_array[i]))
event_data = parser.get_parsed_data()
info_array[i] = {
"url": info_array[i],
"name": event_data["name"],
"ort": event_data["ort"],
"datum": event_data["datum"],
}
return info_array
def delete_unused_unichr(text):
'''Some unicode-characters caused an invalid RSS-Feed. Thus, they will be filtered/deleted.
characters in detail: decimal 0-31, 127-159; see http://unicode-table.com/ for more information
'''
new_text = text
unused_unichr_range = list(range(0, 32))
unused_unichr_range.extend(list(range(127, 160)))
for chr_code in unused_unichr_range:
new_text = new_text.replace(chr(chr_code), "")
return new_text
def main(date=None):
if date == None:
today = datetime.date.today()
else:
today = date
# find next Tuesday
if today.weekday() == 0:
calendar_begin = today + datetime.timedelta(1)
else:
calendar_begin = today + datetime.timedelta(8-today.weekday())
calendar_end = calendar_begin + datetime.timedelta(13)
month_pages = [ [generate_url(calendar_begin)],
[generate_url(calendar_end)],
]
if month_pages[0][0] == month_pages[1][0]:
month_pages.pop(-1)
pages = []
for i in month_pages:
parser = uuCalendarMonthOverviewParser()
parser.feed(download_page(i[0]))
pages.extend(parser.get_parsed_urls())
infos = collect_information(pages)
# correct date for events that last for more than one day
duplicate = []
for i in infos:
if infos.count(i) > 1 and i not in duplicate:
duplicate.append(i)
for e in duplicate:
duplicateIndex = 0
for i in infos:
if e["url"] == i["url"]:
i["datum"] += datetime.timedelta(duplicateIndex)
duplicateIndex += 1
## create_table
table = """## generated on %s
{{{#!vorlage Tabelle
<-4 rowclass="titel"-4> Termine vom %s bis %s
+++
<rowclass="kopf">Name
Ort
Datum
Uhrzeit
""" %( datetime.datetime.today().ctime(),
calendar_begin.strftime("%d.%m.%Y"),
calendar_end.strftime("%d.%m.%Y")
)
highlight = False
for i in infos:
if calendar_end >= i["datum"].date() >= calendar_begin:
table += "+++\n"
name = delete_unused_unichr(i["name"])
ort = delete_unused_unichr(i["ort"])
ordered_infos = [ "[calendar:" + i["url"][32:-1] + ":" + name + "]",
ort,
i["datum"].strftime("%a, %d.%m.%Y\n%H:%M") + " Uhr"]
if highlight: # highlight every second row
ordered_infos[0] = '<rowclass="highlight">' + ordered_infos[0]
highlight = not(highlight)
table += "\n".join(ordered_infos) + "\n"
table += "}}}"
print(table)
if __name__ == "__main__":
import locale
locale.setlocale(locale.LC_ALL, "")
main()