-
Notifications
You must be signed in to change notification settings - Fork 0
/
caldav-fetch.py
executable file
·75 lines (61 loc) · 2.09 KB
/
caldav-fetch.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
#!/usr/bin/env python3
import json
from os import environ as env
import caldav
username = env["CALDAV_USERNAME"]
password = env["CALDAV_PASSWORD"]
url = env["CALDAV_URL"]
caldav_url = f"https://{url}/{username}/"
headers = {}
def fetch_and_print():
with caldav.DAVClient(
url=caldav_url,
username=username,
password=password,
# Optional parameter to set HTTP headers on each request if needed
headers=headers,
) as client:
print_calendars_demo(client.principal().calendars())
def print_calendars_demo(calendars):
if not calendars:
return
events = []
for calendar in calendars:
for event in calendar.events():
for component in event.icalendar_instance.walk():
if component.name != "VEVENT" or component.get('STATUS') == 'CANCELLED':
continue
events.append(fill_event(component, calendar))
print(json.dumps(events, indent=2, ensure_ascii=False))
def fill_event(component, calendar) -> dict[str, str]:
cur = {}
cur["calendar"] = f"{calendar}"
cur["summary"] = component.get("summary")
cur["description"] = component.get("description")
cur["start"] = component.get("dtstart").dt.strftime("%m/%d/%Y %H:%M")
endDate = component.get("dtend")
if endDate and endDate.dt:
cur["end"] = endDate.dt.strftime("%m/%d/%Y %H:%M")
cur["datestamp"] = component.get("dtstamp").dt.strftime("%m/%d/%Y %H:%M")
duration = component.get("duration")
if duration:
cur["duration"] = str(duration.dt)
rrule = component.get('rrule')
if rrule:
cur["rrule"] = fill_rrule(rrule)
return cur
def fill_rrule(rrule) -> dict[str, str]:
res = dict()
res['freq'] = rrule.get('freq')
res['interval'] = rrule.get('interval')
until = rrule.get("until")
if until is not None:
res['until'] = until[0].strftime("%m/%d/%Y %H:%M")
days = rrule.get('byday')
if days:
res['byday'] = []
for d in days:
res['byday'].append(d)
return res
if __name__ == "__main__":
fetch_and_print()