Compare commits

...

3 commits

3 changed files with 88 additions and 38 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 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,8 +1,9 @@
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import sys 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
@ -32,21 +33,41 @@ with open(sourcefile, 'r') as file:
url = sys.argv[1] url = sys.argv[1]
locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
#TODO: Handling HTTP request or pasring errors is currently completely missing (although I implemented such a nice handling if the script would just gracefully continue on exceptions) #TODO: Handling HTTP request or pasring errors is currently completely missing (although I implemented such a nice handling if the script would just gracefully continue on exceptions)
c = Calendar(requests.get(url).text) response = requests.get(url)
calendar = Calendar.from_ical(response.content)
locale.setlocale(locale.LC_TIME, locale.normalize("de_DE.UTF-8")) # Get recurring and non-recurring events
events = recurring_ical_events.of(calendar).after(datetime.now())
colevents = list(c.timeline.start_after(arrow.now())) try:
event = next(events)
start = event.get('dtstart').dt
out_summary = event.get('summary')
location = event.get('location', 'No location specified')
out_weekday = start.strftime("%A")
out_startdate = start.strftime("%-d. %B")
# Format output based on whether it's an all-day event
if isinstance(start, date) and not isinstance(start, datetime):
out_starttime = ""
else:
out_starttime = start.strftime(" %-H:%M")
if location != 'No location specified':
out_location = f", {location}"
else:
out_location = ""
replacestr = "<div class=\"nextevent\">{} <strong> {}{}, {}</strong>{}</div>".format(out_weekday, out_startdate, out_starttime, out_summary, out_location)
except StopIteration:
if len(colevents) > 0 :
weekday = colevents[0].begin.strftime("%A")
date = colevents[0].begin.strftime("%-d. %B")
time = colevents[0].begin.strftime(" %-H:%M") if not colevents[0].all_day else ""
name = colevents[0].name
location = " in " + colevents[0].location if colevents[0].location != None else ""
replacestr = "<div class=\"nextevent\">{} <strong> {}{}, {}</strong>{}</div>".format(weekday, date, time, name, location)
else:
replacestr = fallbackreplacestr replacestr = fallbackreplacestr
# Why should we check if sourcestring is in the file when it originates from there?!? # Why should we check if sourcestring is in the file when it originates from there?!?
@ -55,6 +76,7 @@ else:
# Also an empty string is a perfect match and matches in the wrong place, therefore we check for a minimal length > 0 # Also an empty string is a perfect match and matches in the wrong place, therefore we check for a minimal length > 0
# TODO: Most probably replacement could be done by BeautifulSoup too. # TODO: Most probably replacement could be done by BeautifulSoup too.
# But as this is a collection of StackOverflow roadkill rather than fine crafted menu of purest python ingredients, that's just what I could catch most easily. # But as this is a collection of StackOverflow roadkill rather than fine crafted menu of purest python ingredients, that's just what I could catch most easily.
if len(sourcestr) > 0 and sourcestr in file_contents: if len(sourcestr) > 0 and sourcestr in file_contents:
updated_contents = file_contents.replace(sourcestr, replacestr) updated_contents = file_contents.replace(sourcestr, replacestr)

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