mirror of
https://git.bolliret.ch/pcs/pcs-website
synced 2026-01-18 15:51:36 +01:00
Refactored second script. -> Docker-Rebuild required
This commit is contained in:
parent
b25ca0df2c
commit
46f41aab66
2 changed files with 36 additions and 14 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 python3 -m venv /opt/venv
|
||||||
|
|
||||||
RUN . /opt/venv/bin/activate && pip install ics requests bs4 recurring_ical_events icalendar
|
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
|
||||||
|
|
|
||||||
|
|
@ -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" in {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)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue