forked from FelixArt/schaukasten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
395 lines (324 loc) · 16.7 KB
/
main.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import importlib.util
import subprocess
# Check if a library is installed
def library_installed(library_name):
spec = importlib.util.find_spec(library_name)
return spec is not None
# Install a library using pip
def install_library(library_name):
if library_name == "dateutil":
library_name = "python-dateutil"
subprocess.check_call(['pip', 'install', library_name])
# List of required libraries
required_libraries = ["os", "re", "datetime", "requests", "locale", "icalendar",
"reportlab", "dateutil", "random", "pytz"]
# Check and install missing libraries
missing_libraries = []
for library in required_libraries:
if not library_installed(library):
missing_libraries.append(library)
if missing_libraries:
print("Installing missing libraries...")
for library in missing_libraries:
install_library(library)
# Import the installed libraries
import os
import re
import datetime
import requests
import locale
import random
import pytz
from icalendar import Calendar
from reportlab.lib.pagesizes import landscape, A4
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import cm, mm
from reportlab.platypus.flowables import KeepInFrame
from dateutil.rrule import rrulestr
# Output directory and name
current_directory = os.getcwd()
current_week = datetime.datetime.now().strftime('%Y-%W')
# List of colors that are not set
tmp_colors = {}
for t in range(2):
# Define the output directory and filename
if t == 0:
try:
locale.setlocale(locale.LC_TIME, 'de_DE.utf8')
except locale.Error:
print("Unsupported locale setting, using default locale.")
output_filename = f'event_overview_{current_week}_de.pdf'
else:
try:
locale.setlocale(locale.LC_TIME, 'en_US.utf8')
except locale.Error:
print("Unsupported locale setting, using default locale.")
output_filename = f'event_overview_{current_week}_en.pdf'
output_path = os.path.join(current_directory, output_filename)
# Check if the output file already exists
if os.path.exists(output_path):
suffix = 1
base_name, extension = os.path.splitext(output_filename)
# Generate a new filename with an ascending suffix
while os.path.exists(output_path):
new_filename = f"{base_name}({suffix}){extension}"
output_path = os.path.join(current_directory, new_filename)
suffix += 1
rowamount = 0
# Fetch data from the iCal URL
ical_url = 'https://calendar.google.com/calendar/ical/queerreferat.aachen%40gmail.com/public/basic.ics'
response = requests.get(ical_url)
if response.status_code != 200:
print('Failed to fetch iCal data.')
exit()
# Set your local timezone
local_timezone = pytz.timezone('Europe/Berlin')
# Parse iCal data
calendar = Calendar.from_ical(response.text)
events_of_week = []
# Get the events of the current week
current_date = datetime.datetime.now().date()
start_of_week = current_date - datetime.timedelta(days=current_date.weekday())
end_of_week = start_of_week + datetime.timedelta(days=6)
# Iterate over the events in the calendar
for event in calendar.walk():
if event.name == 'VEVENT':
# Regular event
event_start = event.decoded('DTSTART')
if isinstance(event_start, datetime.datetime):
event_start = event_start.date()
event_end = event.decoded('DTEND')
if isinstance(event_end, datetime.datetime):
event_end = event_end.date()
if start_of_week <= event_start <= end_of_week or start_of_week <= event_end <= end_of_week:
events_of_week.append(event)
# Recurring event
if event.get('RRULE'):
rrule = event['RRULE'].to_ical().decode('utf-8')
recurring_events = []
# Create the recurrence rule object from the RRULE string
rule = rrulestr(rrule, dtstart=event_start, ignoretz=True)
# Convert start_of_week and end_of_week to datetime.datetime objects
start_of_week_datetime = datetime.datetime.combine(start_of_week, datetime.datetime.min.time())
end_of_week_datetime = datetime.datetime.combine(end_of_week, datetime.datetime.max.time())
# Generate the recurring dates within the specified week
recurring_dates = rule.between(start_of_week_datetime, end_of_week_datetime, inc=True)
for date in recurring_dates:
new_event = event.copy()
event_start_time = event.decoded('DTSTART')
if isinstance(event_start_time, datetime.datetime):
event_start_time = event_start_time.time()
event_end_time = event.decoded('DTEND')
if isinstance(event_end_time, datetime.datetime):
event_end_time = event_end_time.time()
# Calculate the adjusted start and end times based on the original event's duration
new_event_start = datetime.datetime.combine(date, event_start_time)
new_event_end = datetime.datetime.combine(date, event_end_time)
new_event['DTSTART'].dt = new_event_start
new_event['DTEND'].dt = new_event_end
# Convert UNTIL value to UTC if it is timezone-aware
if 'RRULE' in new_event and 'UNTIL' in new_event['RRULE']:
until_value = new_event['RRULE']['UNTIL']
if isinstance(until_value, list):
until_value = until_value[0]
if until_value.tzinfo is not None:
until_value = until_value.astimezone(pytz.UTC)
new_event['RRULE']['UNTIL'] = [until_value]
recurring_events.append(new_event)
events_of_week.extend(recurring_events)
# Prepare column Headers
header = []
dates = [start_of_week + datetime.timedelta(days=i) for i in range(7)]
header.extend(date.strftime('%A\n%d %b') for date in dates)
data = [header]
# Location Filter
location_variable = 'Queerreferat an den Aachener Hochschulen e.V., Gerlachstraße 20-22, 52064 Aachen, Deutschland'
# Create a dictionary to store events by date
events_by_date = {date: [] for date in dates}
# Filtering duplicate events:
filtered_events = []
processed_event_uids = set()
for event in events_of_week:
event_uid = event.get('UID')
if event_uid not in processed_event_uids:
filtered_events.append(event)
processed_event_uids.add(event_uid)
else:
existing_event_index = next(
(index for index, e in enumerate(filtered_events) if e.get('UID') == event_uid), None
)
if existing_event_index is not None:
existing_event = filtered_events[existing_event_index]
if event.decoded('LAST-MODIFIED') > existing_event.decoded('LAST-MODIFIED'):
filtered_events[existing_event_index] = event
events_of_week = filtered_events
# Group events by date
for event in events_of_week:
event_start = event.decoded('DTSTART')
if isinstance(event_start, datetime.datetime):
event_start = event_start.date()
# Filter events if needed
if event.decoded('SUMMARY') != bytes('', 'utf-8'):
events_by_date[event_start].append(event)
events_exist = True
# Find highest amount of events
maxevents = 0
for date in dates:
if len(events_by_date[date]) > maxevents:
maxevents = len(events_by_date[date])
rowamount = maxevents
if maxevents < 1:
events_exist = False
columnwidth = 110
# Create columns for the table
for j in range(rowamount):
data.append(['', '', '', '', '', '', ''])
for date in dates:
events = events_by_date[date]
k = 1
events = sorted(events, key=lambda e: e.decoded('DTSTART').astimezone(local_timezone))
sorted_events = []
for (index, ev) in enumerate(events):
if ev in sorted_events:
continue
if index != len(events) - 1 and ev.decoded('DTSTART').astimezone(local_timezone) == events[
index + 1].decoded('DTSTART').astimezone(local_timezone) and ev.get("SUMMARY") > events[index + 1].get(
"SUMMARY"):
sorted_events.append(events[index + 1])
sorted_events.append(ev)
else:
sorted_events.append(ev)
for event in sorted_events:
# Format event information
event_title = event.get('SUMMARY')
event_time = f"{event.decoded('DTSTART').astimezone(local_timezone).strftime('%H:%M')} - {event.decoded('DTEND').astimezone(local_timezone).strftime('%H:%M')}"
event_location = "<br/>" + event.get('LOCATION', '') if event.get('LOCATION',
'') != location_variable else ''
event_description = re.sub(r'<(?!br/).*?>', '', event.get('DESCRIPTION', ''))
if t == 0:
if "----" in event_description:
event_description = event_description.split("----")[0]
elif "_______________" in event_description:
event_description = event_description.split("_______________")[0]
elif "______________" in event_description:
event_description = event_description.split("______________")[0]
else:
if "----" in event_description:
event_description = event_description.split("----")[1]
elif "_______________" in event_description:
event_description = event_description.split("_______________")[1]
elif "______________" in event_description:
event_description = event_description.split("______________")[1]
styles = getSampleStyleSheet()
cell_style = styles["BodyText"]
cell_style.fontSize = 12
# Collect event infos
cell_contents = f"<b>{event_title}</b><br/>{event_time}<i>{event_location}</i><br/>{event_description}"
cell_content = Paragraph(cell_contents, cell_style)
event_start = event.decoded('DTSTART')
if isinstance(event_start, datetime.datetime):
event_start = event_start.date()
data[k][(event_start - start_of_week).days] = cell_content
k = k + 1
# Create table style
table_style = [
('BACKGROUND', (0, 0), (-1, 0), colors.white),
('TEXTCOLOR', (0, 0), (-1, 0), colors.black),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 14),
('BOTTOMPADDING', (0, 0), (-1, 0), 2),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.black),
('SPAN', (0, 1), (0, 2))
]
# Define event name to color mapping
event_color_mapping = {
'Filmabend': colors.HexColor("#E78080"),
'Queer Feminist Action': colors.HexColor("#88E780"),
'Queercafé': colors.HexColor("#E780DB"),
'Trans-Beratung': colors.HexColor("#80E7E1"),
'test²multiply': colors.HexColor("#F6A97C"),
'International Evening': colors.HexColor("#80E7A7"),
'Ace & Aro Abend': colors.HexColor("#E7E680"),
'Fesseltreff': colors.HexColor("#AA80E7"),
'Bi-Pan* and Friends': colors.HexColor("#E7C280"),
'FLINTA-Abend': colors.HexColor("#DF80E7"),
'Plenum': colors.HexColor("#8081E7"),
'Spieleabend': colors.HexColor("#E7D080"),
'TIN* Abend': colors.HexColor("#84D980"),
'Poly Abend': colors.HexColor("#D2D984"),
'Warm Up': colors.HexColor("#F05252"),
'Anime Abend (Film)': colors.HexColor("#f2966f"),
'Anime Abend Serie': colors.HexColor("#BDF370"),
'Bibliothekstreffen': colors.HexColor("#99FFFC"),
# Add more event names and corresponding colors as key-value pairs
}
spanned_cell_color = None # Initialize spanned_cell_color variable
# Add merged cell coordinates to table style
for row_index, row in enumerate(data):
for col_index, cell in enumerate(row):
# Extract the actual cell content from the KeepInFrame object
# cell_content = cell._content[0] if isinstance(cell, KeepInFrame) else cell
cell_content = cell
# Extract the first line (bolded) from the cell contents
cell_content_lines = re.findall(r"<b>(.*?)</b>", str(cell_content))
event_name = cell_content_lines[0].strip() if cell_content_lines else ''
if event_name not in event_color_mapping and event_name not in tmp_colors:
tmp_colors[event_name] = (random.uniform(0.7, 1), random.uniform(0.7, 1), random.uniform(0.7, 1))
rowheights = 470 / rowamount
color_to_use = event_color_mapping.get(event_name) if event_color_mapping.get(
event_name) else tmp_colors.get(event_name)
if row_index > 0 and row_index < rowamount:
if data[row_index][col_index] != '':
table_style.append(('BACKGROUND', (col_index, row_index), (col_index, row_index), color_to_use))
if data[row_index + 1][col_index] == '':
if row_index + 2 <= rowamount and data[row_index + 2][col_index] == '':
table_style.append(('SPAN', (col_index, row_index), (col_index, row_index + 2)))
rowheights = 3 * rowheights
if data[row_index][col_index] != '':
table_style.append(
('BACKGROUND', (col_index, row_index), (col_index, row_index + 2), color_to_use))
else:
table_style.append(('SPAN', (col_index, row_index), (col_index, row_index + 1)))
rowheights = 2 * rowheights
if data[row_index][col_index] != '':
table_style.append(
('BACKGROUND', (col_index, row_index), (col_index, row_index + 1), color_to_use))
elif row_index == rowamount and data[row_index][col_index] != '':
table_style.append(('BACKGROUND', (col_index, row_index), (col_index, row_index), color_to_use))
if type(cell_content) == Paragraph:
cell_content = KeepInFrame(columnwidth, rowheights, [cell_content])
data[row_index][col_index] = cell_content
elements = []
# Add title
title_style = getSampleStyleSheet()["Title"]
if t == 0:
title_text = f"Veranstaltungen der Woche vom {start_of_week.strftime('%d %b %Y')} bis {end_of_week.strftime('%d %b %Y')}"
else:
title_text = f"<i>Events of the week from {start_of_week.strftime('%d %b %Y')} to {end_of_week.strftime('%d %b %Y')}</i>"
title = Paragraph(title_text, title_style)
elements.append(title)
# Create table
if events_exist:
# Calculate cell heights based on content
row_heights = [cm * 1.5] + [rowheights] * rowamount
table = Table(data, colWidths=columnwidth, rowHeights=row_heights)
table.setStyle(TableStyle(table_style))
elements.append(table)
else:
msg_style = getSampleStyleSheet()["Heading1"]
msg_text = "Diese Woche keine Veranstaltungen<br/><i>No events this week</i>"
msg = Paragraph(msg_text, msg_style)
elements.append(Spacer(1, 2 * cm))
elements.append(msg)
# Create the PDF file
doc = SimpleDocTemplate(output_path, pagesize=landscape(A4), leftMargin=(6.35 * mm), rightMargin=(6.35 * mm),
topMargin=(6.35 * mm),
bottomMargin=(6.35 * mm))
# Build the document with the elements
doc.build(elements)
print(f'Event overview table generated: {output_path}')