From 855639a09d5bc0716cdf5f0f60390f33b5c782b0 Mon Sep 17 00:00:00 2001 From: Reto Bollinger Date: Thu, 28 Nov 2024 00:52:13 +0100 Subject: [PATCH] Reworked second shell script to a python script. But requiring an odd quirk as Python cannot read from a Docker-volume when piping its output. --- lektor/docker/Dockerfile | 2 +- lektor/docker/entrypoint.sh | 4 +- lektor/lektordata/project/content/contents.lr | 14 +++- .../project/content/termine/contents.lr | 5 ++ .../scripts/calendar-fetcher-main.py | 82 +++++++++++++++++++ .../scripts/calendar-fetcher-main.sh | 2 +- 6 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 lektor/lektordata/scripts/calendar-fetcher-main.py diff --git a/lektor/docker/Dockerfile b/lektor/docker/Dockerfile index 684a847..4b358a8 100644 --- a/lektor/docker/Dockerfile +++ b/lektor/docker/Dockerfile @@ -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 +RUN . /opt/venv/bin/activate && pip install ics requests bs4 COPY entrypoint.sh /opt/entrypoint.sh RUN chmod +x /opt/entrypoint.sh diff --git a/lektor/docker/entrypoint.sh b/lektor/docker/entrypoint.sh index aa2ba68..d011900 100644 --- a/lektor/docker/entrypoint.sh +++ b/lektor/docker/entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/bash sh -c ". /opt/venv/bin/activate && exec python /opt/lektor/scripts/calendar-fetcher.py ${CALENDAR_URL} > /opt/lektor/project/content/termine/contents.lr" -/opt/lektor/scripts/calendar-fetcher-main.sh ${CALENDAR_URL} > /opt/lektor/project/content/contents.lr +# TODO Something with python and reading from docker mounts is broken but only when piping +cp /opt/lektor/project/content/contents.lr /tmp/myshadowcopyformisbehavingpython.txt +sh -c ". /opt/venv/bin/activate && exec python /opt/lektor/scripts/calendar-fetcher-main.py ${CALENDAR_URL} > /opt/lektor/project/content/contents.lr" /root/.local/bin/lektor --project /opt/lektor/project server --host 0.0.0.0 \ No newline at end of file diff --git a/lektor/lektordata/project/content/contents.lr b/lektor/lektordata/project/content/contents.lr index e39b1ea..ebab072 100644 --- a/lektor/lektordata/project/content/contents.lr +++ b/lektor/lektordata/project/content/contents.lr @@ -5,9 +5,9 @@ title: Willkommen beim PC Stammertal html:

Unser nächster Anlass:


-Sonntag 8. Dezember, Gangfischschiessen in Ermatingen
+
Leider unbekannt, aber frag mal den Vorstand der müsste es wissen
-
+
Terminkalender All unsere Termine
@@ -18,4 +18,12 @@ Sonntag 8. Dezember, Gangfischschiessen in Ermatingen
--- -_template: page.html +_template: + +page.html + + + + + + diff --git a/lektor/lektordata/project/content/termine/contents.lr b/lektor/lektordata/project/content/termine/contents.lr index 098836c..5da647e 100644 --- a/lektor/lektordata/project/content/termine/contents.lr +++ b/lektor/lektordata/project/content/termine/contents.lr @@ -28,6 +28,10 @@ body: *
So. 12. Januar 2025
  *
 
  *
 
  +*
Orientierungslauf Stammerberg (kein Schiessbetrieb!)
  + *
Sa. 13. September 2025
  + *
 
  + *
 
  *
Schwaderlohschiessen
  *
Sa. 20. September 2025
  *
13:00
  @@ -56,3 +60,4 @@ body: --- _template: page.html + diff --git a/lektor/lektordata/scripts/calendar-fetcher-main.py b/lektor/lektordata/scripts/calendar-fetcher-main.py new file mode 100644 index 0000000..276d341 --- /dev/null +++ b/lektor/lektordata/scripts/calendar-fetcher-main.py @@ -0,0 +1,82 @@ +from bs4 import BeautifulSoup +import sys +import requests +from ics import Calendar +import arrow +import locale + + +# TODO: Handling missing arguments is way more complex in Python :( +if len(sys.argv) != 2: + print("Usage: python3 {} ".format(sys.argv[0])) + +# this should actually be the following file. But since piping from a Python-script which reads from a Docker-volume is broken we use the entrypoint.sh script to copy the file into the dockercontainer before running and thus we have that od /tmp/ path +# sourcefile = "/opt/lektor/project/content/contents.lr" +sourcefile = "/tmp/myshadowcopyformisbehavingpython.txt" + +with open(sourcefile) as fp: + soup = BeautifulSoup(fp, 'html.parser') + +cols = soup.find_all('div', {'class' : 'nextevent'}) + +sourcestr = "" + +if len(cols) != 1 : + sourcestr = "" +else: + sourcestr = str(cols[0]) + +with open(sourcefile, 'r') as file: + file_contents = file.read() + + +url = sys.argv[1] + +#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) + +locale.setlocale(locale.LC_TIME, locale.normalize("de_DE.UTF-8")) + +colevents = list(c.timeline.start_after(arrow.now())) + +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 = "
{} {}{}, {}{}
".format(weekday, date, time, name, location) +else: + replacestr = "
Leider unbekannt, aber frag mal den Vorstand der müsste es wissen
" + +if len(sourcestr) > 0 and sourcestr in file_contents: + + updated_contents = file_contents.replace(sourcestr, replacestr) + print(updated_contents) +else: + + print("_model: htmlpage") + print("---") + print("title: Willkommen beim PC Stammertal") + print("---") + print("html:") + print("") + print("

Unser nächster Anlass:


") + print("
Leider unbekannt, aber frag mal den Vorstand der müsste es wissen
") + print("
") + print("
") + print(" \"Terminkalender\" All unsere Termine") + print("
") + print("
") + print(" \"Buch\" Alle Infos über uns") + print("
") + print("
") + print(" \"Briefe\" Kontaktiere uns") + print("
") + print("
") + print("---") + print("_template:") + print("") + print("page.html") + print("") + print("") diff --git a/lektor/lektordata/scripts/calendar-fetcher-main.sh b/lektor/lektordata/scripts/calendar-fetcher-main.sh index 3662199..6a6e63d 100755 --- a/lektor/lektordata/scripts/calendar-fetcher-main.sh +++ b/lektor/lektordata/scripts/calendar-fetcher-main.sh @@ -62,7 +62,7 @@ echo "$ICS_DATA" | awk 'BEGIN{FS=":"} starttime=sprintf("%02d:%02d", substr(dtstart,10,2), substr(dtstart,12,2)) summarystring=sprintf("%s", summary) locationstring=sprintf("%s", location) - wholeline=sprintf("%s %s %s, %s %s Uhr in %s
", orderstartdate, weekday, realstartdate, summarystring, starttime, location) + wholeline=sprintf("%s
%s %s, %s %s Uhr in %s
", orderstartdate, weekday, realstartdate, summarystring, starttime, location) gsub(/ 00:00 Uhr/, "", wholeline) print wholeline dtstart=""