-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (61 loc) · 2.35 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
from datetime import datetime, timedelta
from icalendar import Calendar
CAL_IN_PATH = 'path/to/in_cal.ics'
CAL_OUT_PATH = 'path/to/out_cal.ics'
def timedelta_from_frequ(freq_str, cnt):
if freq_str == 'DAILY':
return timedelta(days=cnt)
elif freq_str == 'WEEKLY':
return timedelta(weeks=cnt)
elif freq_str == 'MONTHLY':
return timedelta(months=cnt)
else:
raise ValueError(f"Frequency {freq_str} not recognised")
def happens_after_today(dt):
dt_date = datetime(dt.year, dt.month, dt.day)
return dt_date > datetime.today()
def get_ongoing_events_of_calendar():
g = open(CAL_IN_PATH, 'r')
cal = Calendar.from_ical(g.read())
events = []
event_uids_to_keep = set()
for component in cal.walk():
if component.name == "VEVENT":
# recurring event
if component.get('RRULE') is not None:
if 'UNTIL' in component.get('RRULE') and not happens_after_today(component.get('RRULE')['UNTIL'][0]):
continue
if 'COUNT' in component.get('RRULE'):
count = component.get('RRULE')['COUNT'][0]
freq_str = component.get('RRULE')['FREQ'][0]
end_date = component.get('DTEND').dt
last_date = end_date + timedelta_from_frequ(freq_str, count)
if not happens_after_today(last_date):
continue
elif not happens_after_today(component.get('DTEND').dt):
continue
ev_uid = component.get('UID')
event_uids_to_keep.add(ev_uid)
print(component.get('summary'))
events.append(component)
elif component.name == "VALARM":
ev_uid = component.get('UID')
if ev_uid not in event_uids_to_keep:
continue
event_uids_to_keep.remove(ev_uid)
events.append(component)
else:
# component of other type
pass
print(len(event_uids_to_keep))
return events
def create_new_calendar_from_events(events):
cal = Calendar()
for event in events:
cal.add_component(event)
f = open(CAL_OUT_PATH, 'wb')
f.write(cal.to_ical())
f.close()
if __name__ == '__main__':
events = get_ongoing_events_of_calendar()
create_new_calendar_from_events(events)