-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: example of using with external library
- Loading branch information
Крылов Александр
committed
Jul 13, 2024
1 parent
2106cf5
commit 10b5e7b
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
if __name__ == "__main__": | ||
fetch_and_print() |