Reworked script for termine to use modules recurring-ical-events and icalendar for dealing with reccuring events

This commit is contained in:
Reto Bollinger 2024-11-29 15:51:54 +01:00
parent 83c4ba2a46
commit b25ca0df2c
2 changed files with 53 additions and 25 deletions

View file

@ -8,7 +8,7 @@ RUN sed -i '/de_DE.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
RUN python3 -m venv /opt/venv RUN python3 -m venv /opt/venv
RUN . /opt/venv/bin/activate && pip install ics requests bs4 RUN . /opt/venv/bin/activate && pip install ics requests bs4 recurring_ical_events icalendar
COPY entrypoint.sh /opt/entrypoint.sh COPY entrypoint.sh /opt/entrypoint.sh
RUN chmod +x /opt/entrypoint.sh RUN chmod +x /opt/entrypoint.sh

View file

@ -1,31 +1,59 @@
import sys from datetime import datetime, date
import recurring_ical_events
import requests import requests
from ics import Calendar from icalendar import Calendar
import arrow
import locale import locale
import sys
if len(sys.argv) != 2: def fetch_upcoming_events(ics_url):
print("Usage: python3 {} <ICS_URL>".format(sys.argv[0])) # Set German locale
locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
response = requests.get(ics_url)
calendar = Calendar.from_ical(response.content)
# Get recurring and non-recurring events
events = recurring_ical_events.of(calendar).after(datetime.now())
for event in events:
start = event.get('dtstart').dt
out_summary = event.get('summary')
location = event.get('location', 'No location specified')
out_startdate = start.strftime("%a. %-d. %B %Y")
url = sys.argv[1] # Format output based on whether it's an all-day event
if isinstance(start, date) and not isinstance(start, datetime):
out_starttime = "&nbsp;"
else:
out_starttime = start.strftime('%-H:%M')
if location != 'No location specified':
out_location = location
else:
out_location = "&nbsp;"
c = Calendar(requests.get(url).text) print(f"* <div>{out_summary}</div>&nbsp;")
print(f" * <div>{out_startdate}</div>&nbsp;")
print(f" * <div>{out_starttime}</div>&nbsp;")
print(f" * <div>{out_location}</div>&nbsp;")
locale.setlocale(locale.LC_TIME, locale.normalize("de_DE.UTF-8"))
print("_model: page") if __name__ == "__main__":
print("---")
print("title: Termine") ics_url = sys.argv[1]
print("---")
print("body:") # ics_url = "https://backoffice.pc-stammertal.ch/remote.php/dav/public-calendars/RqLX5wj25aY6cpnP?export"
print("")
for event in list(c.timeline.start_after(arrow.now())): print("_model: page")
print("* <div>{}</div>&nbsp;".format(event.name)) print("---")
print(" * <div>{}</div>&nbsp;".format(event.begin.strftime("%a. %-d. %B %Y"))) print("title: Termine")
print(" * <div>{}</div>&nbsp;".format(event.begin.strftime("%-H:%M") if not event.all_day else "&nbsp;")) print("---")
print(" * <div>{}</div>&nbsp;".format(event.location if event.location != None else "&nbsp;")) print("body:")
print("") print("")
print("---") fetch_upcoming_events(ics_url)
print("_template: page.html") print("")
print("") print("---")
print("") print("_template: page.html")
print("")
print("")