forked from uwekamper/c-calendar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertcal.py
executable file
·246 lines (201 loc) · 8.72 KB
/
convertcal.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
#!/usr/bin/env python3
# coding: utf-8
"""convertcal.py: Converts several ICS files and exports a Javascript array suitable for fullcalendar.io."""
__author__ = "Ricardo Band <[email protected]>, Uwe Kamper <[email protected]>, cketti <[email protected]>"
__copyright__ = "Copyright 2016-2017, Berlin, Germany"
import urllib.request
from copy import copy
from dateutil.rrule import rrulestr, rruleset
from datetime import datetime, timedelta, date
import pytz
import json
import os
import re
import sys
import traceback
from icalendar import Calendar
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
# Parameter dict used for timedeltas, e.g. timedelta(**INTO_PAST)
# From now, what is the oldest date that we want to list?
INTO_PAST = {
'days': 365
}
berlin = pytz.timezone('Europe/Amsterdam')
# Parameter dict used for timedeltas, e.g. timedelta(**INTO_FUTURE)
# From now, what is the furthest we want to expand recurring events into the future.
INTO_FUTURE = {
'days': 365
}
newcal = Calendar()
newcal.add('prodid', '-//' + 'c-base' + '//' + 'c-base.org' + '//')
newcal.add('version', '2.0')
newcal.add('x-wr-calname', 'c-base events')
def do_one_ics(ics, default_location):
events = []
global newcal
cal = Calendar.from_ical(ics)
oldest_non_recurring = datetime.utcnow().replace(tzinfo=pytz.utc) - timedelta(**INTO_PAST)
all_events = []
for ev_id, event in enumerate(cal.walk('vevent')):
newcal.add_component(event)
d = event.get('dtstart').dt
de = get_end_date(event, d)
title = clean_up_title(event.get('summary'))
if d.year == 2019 and d.month == 5:
print(repr(d))
print("---->", title, event.get("rrule"))
if not de:
print("Skipping event: %s" % title)
continue
uid = event.get('uid', '')
location = event.get('location', default_location)
description = event.get('description', '')
is_all_day = False
if not isinstance(d, datetime):
d = datetime(d.year, d.month, d.day, tzinfo=pytz.utc)
is_all_day = True
# If the event is a recurring event
if event.get('rrule'):
event_template = {
"id": ev_id,
"title": title,
"description": description,
"location": location,
"allDay": is_all_day,
"uid": uid,
}
events = get_events_from_rrule(event, event_template, d, de)
all_events.extend(events)
continue
# Ignore regular events that are older than one year.
if d < oldest_non_recurring:
# print("Skipping non-recurring event %s because it is too old (%s)" %(event.get('summary'), d.isoformat()))
continue
# This is just a regular event, just use it as is
current = {
"id": ev_id,
"title": title,
"start": d.isoformat(),
"description": description,
"location": location,
"uid": uid
}
if is_all_day == True:
current["allDay"] = True
current["end"] = de.isoformat()
else:
current["allDay"] = False
current["end"] = de.isoformat()
all_events.append(current)
return all_events
def clean_up_title(title):
if title == None:
return "Kein Titel"
return re.sub(r"\n", " ", title)
def get_events_from_rrule(ical_event, event_template, start_date, end_date):
events = []
ical_rrule = ical_event.get('rrule')
ical_rrule_str = ical_rrule.to_ical().decode('utf-8')
rrule = rrulestr(ical_rrule_str, ignoretz=True, dtstart=start_date.replace(tzinfo=None))
ruleset = rruleset()
ruleset.rrule(rrule)
exdates = get_exdates(ical_event)
for exdate in exdates:
for exdate_date in exdate.dts:
datetime_or_date = exdate_date.dt
if isinstance(datetime_or_date, datetime):
exclusion_datetime = datetime_or_date.replace(tzinfo=None)
elif isinstance(datetime_or_date, date):
exclusion_datetime = datetime(
year=datetime_or_date.year,
month=datetime_or_date.month,
day=datetime_or_date.day)
else:
continue
ruleset.exdate(exclusion_datetime)
after = datetime.utcnow() - timedelta(**INTO_PAST)
before = datetime.utcnow() + timedelta(**INTO_FUTURE)
rrule_instances = list(ruleset.between(after, before))
for num, rrule_instance in enumerate(rrule_instances, start = 1):
event = copy(event_template)
event['uid'] = "{}-{}".format(event_template['uid'], num)
event['start'] = berlin.localize(rrule_instance).isoformat()
if not event["allDay"]:
instance_end_date = datetime(rrule_instance.year, rrule_instance.month, rrule_instance.day,
end_date.hour, end_date.minute, end_date.second)
event["end"] = berlin.localize(instance_end_date).isoformat()
print(repr(event))
events.append(event)
return events
def get_exdates(ical_event):
exdate_ical = ical_event.get('exdate')
if not exdate_ical:
exdates = []
elif isinstance(exdate_ical, list):
exdates = exdate_ical
else:
exdates = [exdate_ical]
return exdates
def get_end_date(event, start_date):
dtend = event.get('dtend')
if dtend:
end_date = dtend.dt
else:
duration = event.get('duration')
if not duration:
return None
end_date = start_date + duration.dt
return end_date
export_name = os.path.join(os.path.dirname(__file__), 'html', 'exported', 'events.js')
export_json_name = os.path.join(os.path.dirname(__file__), 'html', 'exported', 'events.json')
merged_name = os.path.join(os.path.dirname(__file__), 'html', 'exported', 'c-base-events.ics')
error_name = os.path.join(os.path.dirname(__file__), 'html', 'exported', 'errors.js')
online_name = os.path.join(os.path.dirname(__file__), 'html', 'exported', 'online.js')
try:
events_ics = urllib.request.urlopen('https://c.c-base.org/calendar/events.ics').read()
c_base_events = do_one_ics(events_ics, 'mainhall')
regulars_ics = urllib.request.urlopen('https://c.c-base.org/calendar/regulars.ics').read()
regular_events = do_one_ics(regulars_ics, 'mainhall')
online_ics = urllib.request.urlopen('https://c.c-base.org/calendar/online.ics').read()
online_events = do_one_ics(online_ics, 'https://jitsi.c-base.org/mainhall')
url = "https://c.c-base.org/calendar/seminars.ics"
seminars_ics = urllib.request.urlopen(url).read()
seminar_events = do_one_ics(seminars_ics, "seminarraum")
# No errors happened. Make it known to the world!
with open(os.path.realpath(error_name), mode="w") as outfh:
outfh.write('window.c_base_errors = "";\n')
except Exception as e:
traceback.print_exc(file=sys.stdout)
print('### ERROR : ', str(e))
with open(os.path.realpath(error_name), mode="w") as outfh:
outfh.write('window.c_base_errors = ' + json.dumps(str(e)) + ";\n")
exit(1)
with open(os.path.realpath(export_name), mode="w") as outfh:
outfh.write("window.c_base_regulars = " + json.dumps(regular_events, indent=4, sort_keys=True) + ";\n")
outfh.write("window.c_base_events = " + json.dumps(c_base_events, indent=4, sort_keys=True) + ";\n")
outfh.write("window.c_base_seminars= " + json.dumps(seminar_events, indent=4, sort_keys=True) + ";\n")
outfh.write("window.c_base_online = " + json.dumps(online_events, indent=4, sort_keys=True) + ";\n")
outfh.write("window.lastUpdate = \"" + datetime.now().isoformat() +" UTC\";\n")
all_events = {
"c_base_regulars": regular_events,
"c_base_events": c_base_events,
"c_base_seminars": seminar_events,
"c_base_online": online_events,
"lastUpdate": datetime.now().isoformat() +" UTC"
}
with open(os.path.realpath(export_json_name), mode="w") as outfh:
outfh.write(json.dumps(all_events, indent=4, sort_keys=True) + "\n")
with open(os.path.join(os.path.dirname(__file__), 'html', 'exported', 'events.ics'), 'wb') as f:
f.write(events_ics)
with open(os.path.join(os.path.dirname(__file__), 'html', 'exported', 'regulars.ics'), 'wb') as f:
f.write(regulars_ics)
with open(os.path.join(os.path.dirname(__file__), 'html', 'exported', 'seminars.ics'), 'wb') as f:
f.write(seminars_ics)
with open(os.path.join(os.path.dirname(__file__), 'html', 'exported', 'online.ics'), 'wb') as f:
f.write(online_ics)
with open(os.path.realpath(merged_name) , 'wb') as f:
f.write(newcal.to_ical())
f.close()
with open(os.path.realpath(error_name), mode="w") as outfh:
outfh.write('window.c_base_errors = "None";\n')
exit(0)