-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_calendar.py
427 lines (304 loc) · 12.5 KB
/
parse_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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"""
parse_calendar.py
This file is responsible for parsing the calendar data from the icalendar format to a json format.
"""
import icalendar
import recurring_ical_events
from datetime import timedelta
import requests
import json
from credentials import *
import helper
def download_calendar(url):
"""
Downloads a calendar from the specified URL
Args: url (str): The URL of the calendar to download.
returns icalendar.Calendar object
"""
cal = icalendar.Calendar.from_ical(requests.get(url).text)
return cal
def get_events_today(cal) -> list:
"""
Retrieves a list of events happening today from the given calendar.
Args:
cal (Calendar): The calendar object to retrieve events from.
Returns:
list: A list of events happening today.
"""
check_date = helper.decide_day_check()
events_today = recurring_ical_events.of(cal).at(check_date)
if testing_date is not None:
events_today = recurring_ical_events.of(cal).at(testing_date)
return events_today
def boundary_checks(event):
"""
Perform boundary checks on the given event.
Args:
event (dict): The event to be checked.
Returns:
tuple: A tuple containing the modified event and a boolean indicating if the event is valid.
"""
valid = True
if hasattr(event['DTSTART'].dt, 'hour'):
# continue if event starts before 8am and stops before 8am
if event['DTSTART'].dt.hour < 8 and event['DTEND'].dt.hour <= 8:
valid = False
# continue if event starts after 9pm
if event['DTSTART'].dt.hour >= 21:
valid = False
# if event ends after the start date, set end to start date then set time to 9pm
#! NOTE this means multiday events w/ times only work on day 1
if event['DTEND'].dt.day > event['DTSTART'].dt.day:
event['DTEND'].dt = event['DTEND'].dt.replace(day=event['DTSTART'].dt.day)
event['DTEND'].dt = event['DTEND'].dt.replace(hour=21, minute=0, second=0)
# Set event end to 9pm if later
if event['DTEND'].dt.hour >= 21:
event['DTEND'].dt = event['DTEND'].dt.replace(hour=21, minute=0, second=0)
# If event starts before 8am but stops after 9am, set start to 8am
if event['DTSTART'].dt.hour < 8 and event['DTEND'].dt.hour >= 9:
event['DTSTART'].dt = event['DTSTART'].dt.replace(hour=8, minute=0, second=0)
return event, valid
def check_empty(json_data):
"""
If a user has no events, append a "No events" event to their list
"""
today = helper.getDateToday()
for user in json_data:
if len(user["events"]) == 0:
user["events"].append({
"name": "No events",
"start": str(today),
"end": str(today+timedelta(1)),
"location": "",
"description": "",
"duration": "1 day, 0:00:00"
})
return json_data
def determine_user_index(user: str) -> int:
match user:
case "Jayden":
user_index = 0
case "Joshua_T":
user_index = 1
case "Jacob":
user_index = 2
case "Joshua_AC":
user_index = 3
case "Everyone": #default
user_index = 4
case _: # Case of error/doesnt match
user_index = 4
return user_index
def parse_single_timetable_calendar(url: str) -> str:
"""
Parse users' timetable calendar into json format
This creates the 4 user jsons for the calendar,
preparing for further data to be added
"""
cal = download_calendar(url)
events_today = get_events_today(cal)
# Initialise empty json string
json_user_list = []
for event in events_today:
# print(event)
# print(event['SUMMARY'])
# print(event['DTSTART'].dt)
# print(event['DTEND'].dt)
# if 'LOCATION' in event:
# print(event['LOCATION'])
# if 'DESCRIPTION' in event:
# print(event['DESCRIPTION'])
# Run boundary checks for events during the day
event, valid = boundary_checks(event)
# If event is invalid, i.e. out of bounds, skip to next event
if valid == False:
continue
# Clean event summary by removing any text after a colon if present
# note we keep 1 word after the colon
if ":" in event['SUMMARY']:
event['SUMMARY'] = event['SUMMARY'].split(':')[0] + ': ' + event['SUMMARY'].split(':')[1].split()[0]
# Calculate duration of event
duration = event['DTEND'].dt - event['DTSTART'].dt
# prepare json data
event_data = {
"name": str(event['SUMMARY']),
"start": str(event['DTSTART'].dt),
"end": str(event['DTEND'].dt),
"location": str(event['LOCATION']) if 'LOCATION' in event else "",
"description": str(event['DESCRIPTION']) if 'DESCRIPTION' in event else "",
"duration": str(duration)
}
json_user_list.append(event_data)
return json_user_list
def parse_combined_calendar(url: str, json_data: list) -> list:
"""
Parses a combined calendar by downloading the calendar from the given URL,
extracting today's events, applying boundary checks, and organizing the events
based on the person associated with each event.
Args:
url (str): The URL of the calendar to download.
json_data (list): The JSON data containing the parsed events.
Returns:
list: The updated JSON data with the parsed events.
"""
cal = download_calendar(url)
events_today = get_events_today(cal)
# Loop through today's events
for event in events_today:
# print(event['SUMMARY'])
# Verify valid event, and apply boundaries
event, valid = boundary_checks(event)
# If event is invalid, i.e. out of bounds, skip to next event
if valid == False:
continue
# Strip leading and trailing whitespace
event['SUMMARY'] = event['SUMMARY'].strip()
#* Determine whose event this is
user_index_list = []
# Process initials
while True:
# Default
user = "Everyone"
# Loop through initials dictionary, checking the start of the event summary
for inits in initials:
if event['SUMMARY'].startswith(inits):
# Identify the user, then append this to the index list.
user = initials[inits]
# Remove those initials from event summary
event['SUMMARY'] = event['SUMMARY'][len(inits):].strip()
break
# Update list with user index
user_index_list.append(determine_user_index(user))
# Check if multiperson event
if event['SUMMARY'].startswith("&"):
# Remove "&" from event summary
event['SUMMARY'] = event['SUMMARY'][1:].strip()
# reruns the loop
else:
# No more initials to process
break
# Calculate duration of event
duration = event['DTEND'].dt - event['DTSTART'].dt
# print(user_index_list)
# Loop through user indexes for event
for user_index in user_index_list:
# Clean event summary, where format is JK - EVENT or JK: EVENT
if user_index != 4:
# if dash present, remove text before it. Note split once only
if "-" in event['SUMMARY']:
event['SUMMARY'] = event['SUMMARY'].split('-', 1)[1]
elif ":" in event['SUMMARY']:
event['SUMMARY'] = event['SUMMARY'].split(':', 1)[1]
# Strip leading and trailing whitespace
event['SUMMARY'] = event['SUMMARY'].strip()
# prepare json data
event_data = {
"name": str(event['SUMMARY']),
"start": str(event['DTSTART'].dt),
"end": str(event['DTEND'].dt),
"location": str(event['LOCATION']) if 'LOCATION' in event else "",
"description": str(event['DESCRIPTION']) if 'DESCRIPTION' in event else "",
"duration": str(duration)
}
### Attempt to update JSON data
# Check if user is "Everyone"
if user_index == 4:
user = "Everyone"
# Check for EVERYONE events. If event summary contains "[ALL]"
if "[ALL]" in event_data["name"]:
# [ALL] is removed in JS.
# Place into everyone's calendar
for user_data in json_data:
user_data["events"].append(event_data)
continue
#! Skips rest of the loop
else:
try:
json_data[4]
except IndexError:
# If "everyone" 5th element doesn't exist, create it
json_data.append({"user": "Everyone", "events": []})
else:
# If not everyone, get user from user index
user = list(initials.values())[user_index]
# Append event to json data
# print(user)
for user_data in json_data:
if user_data["user"] == user:
user_data["events"].append(event_data)
# Return updated json data.
return json_data
def parse_personal_calendars(json_data: list) -> list:
"""
Parse personal calendars into json format
"""
for url in personal_cals:
cal = download_calendar(url)
events_today = get_events_today(cal)
# Loop through today's events
for event in events_today:
# Verify valid event, and apply boundaries
event, valid = boundary_checks(event)
# If event is invalid, i.e. out of bounds, skip to next event
if valid == False:
continue
# Calculate duration of event
duration = event['DTEND'].dt - event['DTSTART'].dt
# Determine whose event this is
user = personal_cals[url]
# Check for EVERYONE events. If event summary ends in "EVERYONE"
# then set user to everyone
if event['SUMMARY'].endswith("EVERYONE"):
user = "Everyone"
# Remove "- EVERYONE" from end of event summary
# and remove leading/trailing whitespace
event['SUMMARY'] = event['SUMMARY'].replace("- EVERYONE", "").strip()
# If "everyone" element doesn't exist, create it
try:
json_data[4]
except IndexError:
json_data.append({"user": "Everyone", "events": []})
# prepare json data
event_data = {
"name": str(event['SUMMARY']),
"start": str(event['DTSTART'].dt),
"end": str(event['DTEND'].dt),
"location": str(event['LOCATION']) if 'LOCATION' in event else "",
"description": str(event['DESCRIPTION']) if 'DESCRIPTION' in event else "",
"duration": str(duration)
}
# Append event to json data
for user_data in json_data:
if user_data["user"] == user:
user_data["events"].append(event_data)
return json_data
def parse_all_calendars():
"""
Iterate through all calendars available in credentials.py and parse them into json format
Example format in example.json
"""
# init
json_data = []
# loop through url dictionary for timetables
for name in timetable_links:
# Construct each json
user_json_data = {
"user": name,
"events": parse_single_timetable_calendar(timetable_links[name])
}
# Append to json list
json_data.append(user_json_data)
# loop through additional calendars.
# Parse combined calendar and update json_data
json_data = parse_combined_calendar(combined_cal, json_data)
# Parse personal calendars and update json_data
json_data = parse_personal_calendars(json_data)
# Check if events are empty
json_data = check_empty(json_data)
# Create into json format
json_data = json.dumps(json_data)
# print(json_data)
return json_data
# Only run if this file is run directly
if __name__ == '__main__':
parse_all_calendars()