From 83c4ba2a464dd0468b33abb533d6b9b850ce9e46 Mon Sep 17 00:00:00 2001 From: Reto Bollinger Date: Thu, 28 Nov 2024 21:28:39 +0100 Subject: [PATCH] Added a few comments to one python-script --- .../scripts/calendar-fetcher-main.py | 17 ++-- .../scripts/calendar-fetcher-main.sh | 86 ------------------- 2 files changed, 12 insertions(+), 91 deletions(-) delete mode 100755 lektor/lektordata/scripts/calendar-fetcher-main.sh diff --git a/lektor/lektordata/scripts/calendar-fetcher-main.py b/lektor/lektordata/scripts/calendar-fetcher-main.py index a517bcf..17d286f 100644 --- a/lektor/lektordata/scripts/calendar-fetcher-main.py +++ b/lektor/lektordata/scripts/calendar-fetcher-main.py @@ -10,12 +10,13 @@ import locale if len(sys.argv) != 2: print("Usage: python3 {} ".format(sys.argv[0])) -# TODO Reading from the same file as the output of this script is piped into leads to synchronization/buffering issues, we therefore have to do some quirks in entrypoint.sh +# TODO: Reading from the same file as the output of this script is piped into leads to synchronization/buffering issues, we therefore have to do some quirks in entrypoint.sh sourcefile = "/opt/lektor/project/content/contents.lr" +fallbackreplacestr = "
Leider unbekannt, aber frag mal den Vorstand der müsste es wissen
" + with open(sourcefile) as fp: soup = BeautifulSoup(fp, 'html.parser') - cols = soup.find_all('div', {'class' : 'nextevent'}) sourcestr = "" @@ -43,11 +44,17 @@ if len(colevents) > 0 : 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 " " + 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
" + replacestr = fallbackreplacestr +# Why should we check if sourcestring is in the file when it originates from there?!? +# Because theoretically it could be that BeautifulSoup extracts it slightly different (sepcial characters or such). +# And as here we do a simple replace it wouldn't catch anything if it isn't an exact match. +# 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) @@ -61,7 +68,7 @@ else: print("html:") print("") print("

Unser nächster Anlass:


") - print("
Leider unbekannt, aber frag mal den Vorstand der müsste es wissen
") + print(fallbackreplacestr) print("
") print("
") print(" \"Terminkalender\" All unsere Termine") diff --git a/lektor/lektordata/scripts/calendar-fetcher-main.sh b/lektor/lektordata/scripts/calendar-fetcher-main.sh deleted file mode 100755 index 6a6e63d..0000000 --- a/lektor/lektordata/scripts/calendar-fetcher-main.sh +++ /dev/null @@ -1,86 +0,0 @@ -#!/bin/bash - -ICS_URL="$1" - -if [ -z "$ICS_URL" ]; then - echo "Usage: $0 " - exit 1 -fi - -ICS_DATA=$(curl -s "$ICS_URL") - -TODAY=$(date +%Y%m%d) - -echo "_model: htmlpage" -echo "---" -echo "title: Willkommen beim PC Stammertal" -echo "---" -echo "html:" -echo "" -echo "

Unser nächster Anlass:


" -#original statement: -#awk 'BEGIN{FS=":"}/^DTSTART/{dtstart=$2}/^SUMMARY/{summary=$2}/^END:VEVENT/{print substr(dtstart,7,2)"/"substr(dtstart,5,2)"/"substr(dtstart,1,4),sprintf("%02d",substr(dtstart,10,2)+3)":"substr(dtstart,12,2),summary}' file.txt -#from here: https://stackoverflow.com/questions/74111401/parse-ics-and-create-output - -#OK what the heck are we doing here? TBH I even do not know exactly myself -# First we iterate over the ICS items looking for DTSTART, SUMMARY, LOCATION and END:VEVENT -# as soon as we found an END:VEVENT we consider that an event and start processing it -# if we havent found DTSTART, SUMMARY OR LOCATION for that event (actually rather just since last itartion step, that's why at the end we empty those variables) -# we insert "19700101T000000" for dtstart and " " for the other variables. -# As whole day events have a starting time of exactly 00:00 we consider this starting time equivalent to a whole day event. -# Yes, that' means we can't have all those midnight parties as those turn into whole day events. -# Once we have this all collected we print that into the markdown representation of a list item with a sublist for the subsequent fields -# But! Beware: we are for one not printing the newlines (except for one at the end of each event) and for the other our lines start with a YYYYMMDD representation of the date -# So we have for each event a line starting with YYYYMMDD followed by the markdown representation of a list item containing a sublist with all fields terminated with a newline -# Thats the first awk command -# Second awk command filters all lines which are in the past (yes, most probably this could have been done in the first awk call as well) -# Now we sort those remaining lines as they come in absolute random order (that's the reason for having an event squeezed on one line, and starting the line with YYYYMMDD representation of te event date) -# Now we no longer need the leading timestamp so we cut it off -# As we only want the next event (which is the one on top of our now sorted list) we just grab the first line -echo "$ICS_DATA" | awk 'BEGIN{FS=":"} -/^DTSTART/{dtstart=$2} -/^SUMMARY/{summary=$2} -/^LOCATION/{location=$2} -/^END:VEVENT/{ - gsub(/\r/, "", summary) - gsub(/\r/, "", location) - if (dtstart == "") dtstart = "19700101T000000" - if (summary == "") summary = " " - if (location == "") location = " " - - date_str = substr(dtstart, 1, 4) "-" substr(dtstart, 5, 2) "-" substr(dtstart, 7, 2) - cmd = "LC_ALL=de_DE.UTF-8 date -d \"" date_str "\" +%A" - cmd | getline weekday - close(cmd) - - cmd = "LC_ALL=de_DE.UTF-8 date -d \"" date_str "\" +%B" - cmd | getline month - close(cmd) - - orderstartdate=sprintf("%s", substr(dtstart,1,8)) - realstartdate=sprintf("%d. %s", substr(dtstart,7,2), month) - 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) - gsub(/ 00:00 Uhr/, "", wholeline) - print wholeline - dtstart="" - summary="" - location="" -}' | awk -v today="$TODAY" 'substr($1,1,8) >= today' | sort | cut -d " " -f 2- | head -n 1 - -echo "
" -echo "
" -echo " \"Terminkalender\" All unsere Termine" -echo "
" -echo "
" -echo " \"Buch\" Alle Infos über uns" -echo "
" -echo "
" -echo " \"Briefe\" Kontaktiere uns" -echo "
" -echo "
" -echo "---" -echo "_template: page.html" -echo ""