mirror of
https://git.bolliret.ch/pcs/pcs-website
synced 2026-01-18 16:51:36 +01:00
Compare commits
3 commits
83c4ba2a46
...
73cee8a653
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73cee8a653 | ||
|
|
46f41aab66 | ||
|
|
b25ca0df2c |
3 changed files with 88 additions and 38 deletions
|
|
@ -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 . /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
|
||||
RUN chmod +x /opt/entrypoint.sh
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
from bs4 import BeautifulSoup
|
||||
import sys
|
||||
from datetime import datetime, date
|
||||
import recurring_ical_events
|
||||
import requests
|
||||
from ics import Calendar
|
||||
import arrow
|
||||
from icalendar import Calendar
|
||||
import locale
|
||||
|
||||
|
||||
|
|
@ -32,21 +33,41 @@ with open(sourcefile, 'r') as file:
|
|||
|
||||
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)
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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:
|
||||
|
||||
replacestr = fallbackreplacestr
|
||||
|
||||
# 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
|
||||
# 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.
|
||||
|
||||
if len(sourcestr) > 0 and sourcestr in file_contents:
|
||||
|
||||
updated_contents = file_contents.replace(sourcestr, replacestr)
|
||||
|
|
|
|||
|
|
@ -1,17 +1,49 @@
|
|||
import sys
|
||||
from datetime import datetime, date
|
||||
import recurring_ical_events
|
||||
import requests
|
||||
from ics import Calendar
|
||||
import arrow
|
||||
from icalendar import Calendar
|
||||
import locale
|
||||
import sys
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python3 {} <ICS_URL>".format(sys.argv[0]))
|
||||
def fetch_upcoming_events(ics_url):
|
||||
# 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')
|
||||
|
||||
out_startdate = start.strftime("%a. %-d. %B %Y")
|
||||
|
||||
# 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 = location
|
||||
else:
|
||||
out_location = " "
|
||||
|
||||
print(f"* <div>{out_summary}</div> ")
|
||||
print(f" * <div>{out_startdate}</div> ")
|
||||
print(f" * <div>{out_starttime}</div> ")
|
||||
print(f" * <div>{out_location}</div> ")
|
||||
|
||||
|
||||
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("---")
|
||||
|
|
@ -19,11 +51,7 @@ print("title: Termine")
|
|||
print("---")
|
||||
print("body:")
|
||||
print("")
|
||||
for event in list(c.timeline.start_after(arrow.now())):
|
||||
print("* <div>{}</div> ".format(event.name))
|
||||
print(" * <div>{}</div> ".format(event.begin.strftime("%a. %-d. %B %Y")))
|
||||
print(" * <div>{}</div> ".format(event.begin.strftime("%-H:%M") if not event.all_day else " "))
|
||||
print(" * <div>{}</div> ".format(event.location if event.location != None else " "))
|
||||
fetch_upcoming_events(ics_url)
|
||||
print("")
|
||||
print("---")
|
||||
print("_template: page.html")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue