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.

This commit is contained in:
Reto Bollinger 2024-11-28 00:52:13 +01:00
parent 4bc9376c19
commit 855639a09d
6 changed files with 103 additions and 6 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 . /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

View file

@ -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

View file

@ -5,7 +5,7 @@ title: Willkommen beim PC Stammertal
html:
<h3>Unser nächster Anlass: </h3><br/>
Sonntag <strong>8. Dezember, Gangfischschiessen</strong> in Ermatingen<br/>
<div class="nextevent">Leider unbekannt, aber <strong>frag mal den Vorstand</strong> der müsste es wissen</div>
<div class="threecolumn">
<div>
<a href="termine/"><img src=" /images/termine_square.jpg" alt="Terminkalender"> All unsere Termine</a>
@ -18,4 +18,12 @@ Sonntag <strong>8. Dezember, Gangfischschiessen</strong> in Ermatingen<br/>
</div>
</div>
---
_template: page.html
_template:
page.html

View file

@ -28,6 +28,10 @@ body:
* <div>So. 12. Januar 2025</div>&nbsp;
* <div>&nbsp;</div>&nbsp;
* <div>&nbsp;</div>&nbsp;
* <div>Orientierungslauf Stammerberg (kein Schiessbetrieb!)</div>&nbsp;
* <div>Sa. 13. September 2025</div>&nbsp;
* <div>&nbsp;</div>&nbsp;
* <div>&nbsp;</div>&nbsp;
* <div>Schwaderlohschiessen</div>&nbsp;
* <div>Sa. 20. September 2025</div>&nbsp;
* <div>13:00</div>&nbsp;
@ -56,3 +60,4 @@ body:
---
_template: page.html

View file

@ -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 {} <ICS_URL>".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 "&nbsp;"
replacestr = "<div class=\"nextevent\">{} <strong> {}{}, {}</strong>{}</div>".format(weekday, date, time, name, location)
else:
replacestr = "<div class=\"nextevent\">Leider unbekannt, aber <strong>frag mal den Vorstand</strong> der müsste es wissen</div>"
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("<h3>Unser nächster Anlass: </h3><br/>")
print("<div class=\"nextevent\">Leider unbekannt, aber <strong>frag mal den Vorstand</strong> der müsste es wissen</div>")
print("<div class=\"threecolumn\">")
print(" <div>")
print(" <a href=\"termine/\"><img src=\" /images/termine_square.jpg\" alt=\"Terminkalender\"> All unsere Termine</a>")
print(" </div>")
print(" <div>")
print(" <a href=\"about/\"><img src=\"/images/about_square.jpg\" alt=\"Buch\"> Alle Infos über uns</a>")
print(" </div>")
print(" <div>")
print(" <a href=\"kontakt/\"><img src=\"/images/kontakt_square.jpg\" alt=\"Briefe\"> Kontaktiere uns</a>")
print(" </div>")
print("</div>")
print("---")
print("_template:")
print("")
print("page.html")
print("")
print("")

View file

@ -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 <strong>%s, %s %s Uhr</strong> in %s<br/>", orderstartdate, weekday, realstartdate, summarystring, starttime, location)
wholeline=sprintf("%s <div class=\"nextevent\">%s <strong>%s, %s %s Uhr</strong> in %s</div>", orderstartdate, weekday, realstartdate, summarystring, starttime, location)
gsub(/ 00:00 Uhr/, "", wholeline)
print wholeline
dtstart=""