added fetch script for calendar events that should appear under "Termine"

This commit is contained in:
eclipse 2025-02-14 12:04:22 +01:00
parent c7d925cb05
commit 82683cf1f8
2 changed files with 46 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
output/
__pycache__/
*.code-workspace
utils/.caldav_pass

45
utils/refresh-events.py Executable file
View File

@ -0,0 +1,45 @@
import caldav
from datetime import datetime
import vobject
import yaml
server_url = "https://***REMOVED***
cal_user = "tobias"
cal_url = "https://***REMOVED***
cal_category = "Lesung/Veranstaltung"
metadata_keys = ["summary", "dtstart", "dtend", "description", "location"]
result_file = "../content/pages/termine.md"
# get passwort
with open(".caldav_pass", "r") as f:
cal_pass = f.read().strip()
# create vobject calendar
vcal = vobject.newFromBehavior("vcalendar")
# establish connection to caldav server
with caldav.DAVClient(url=server_url, username=cal_user, password=cal_pass) as dav_client:
# establish connection to calendar
dav_cal = dav_client.calendar(url=cal_url)
# put all events from caldav calendar into vobject calendar
for e in dav_cal.events():
for ev in e.instance.contents["vevent"]:
vcal.add(ev)
# we only want events belonging to a specific category
events = [e for e in vcal.getChildren() if "categories" in e.contents.keys() and cal_category in map(lambda x: x.value[0], e.contents["categories"])]
# convert events into a structure that Pelican can use
events = [{k: v[0].value for k, v in e.contents.items() if k in metadata_keys} for e in events]
# keep only future events
today = datetime.today().date()
events = [e for e in events if e["dtstart"].date() >= today]
# write data as YAML
with open(result_file, 'w') as f:
f.write("---\n")
yaml.dump({"termine": events}, f)
f.write("---\n")