t-r.de/utils/refresh-events.py

46 lines
1.4 KiB
Python
Executable File

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")