From b5803fff63464a6e02c216fd24ca06dfc4d02f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D1=80=D1=8B=D0=BB=D0=BE=D0=B2=20=D0=90=D0=BB=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Thu, 11 Jul 2024 23:39:42 +0300 Subject: [PATCH] feat: example of using with external library --- examples/get_events_example.py | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/get_events_example.py diff --git a/examples/get_events_example.py b/examples/get_events_example.py new file mode 100644 index 0000000..9dc1e7e --- /dev/null +++ b/examples/get_events_example.py @@ -0,0 +1,52 @@ +#!/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": + 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") + return cur + + +if __name__ == "__main__": + fetch_and_print()