mirror of
https://git.bolliret.ch/pcs/pcs-website
synced 2026-01-18 16:41:37 +01:00
Added a few comments to one python-script
This commit is contained in:
parent
43d90086d0
commit
83c4ba2a46
2 changed files with 12 additions and 91 deletions
|
|
@ -10,12 +10,13 @@ import locale
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 2:
|
||||||
print("Usage: python3 {} <ICS_URL>".format(sys.argv[0]))
|
print("Usage: python3 {} <ICS_URL>".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"
|
sourcefile = "/opt/lektor/project/content/contents.lr"
|
||||||
|
|
||||||
|
fallbackreplacestr = "<div class=\"nextevent\">Leider unbekannt, aber <strong>frag mal den Vorstand</strong> der müsste es wissen</div>"
|
||||||
|
|
||||||
with open(sourcefile) as fp:
|
with open(sourcefile) as fp:
|
||||||
soup = BeautifulSoup(fp, 'html.parser')
|
soup = BeautifulSoup(fp, 'html.parser')
|
||||||
|
|
||||||
cols = soup.find_all('div', {'class' : 'nextevent'})
|
cols = soup.find_all('div', {'class' : 'nextevent'})
|
||||||
|
|
||||||
sourcestr = ""
|
sourcestr = ""
|
||||||
|
|
@ -43,11 +44,17 @@ if len(colevents) > 0 :
|
||||||
date = colevents[0].begin.strftime("%-d. %B")
|
date = colevents[0].begin.strftime("%-d. %B")
|
||||||
time = colevents[0].begin.strftime(" %-H:%M") if not colevents[0].all_day else ""
|
time = colevents[0].begin.strftime(" %-H:%M") if not colevents[0].all_day else ""
|
||||||
name = colevents[0].name
|
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 = "<div class=\"nextevent\">{} <strong> {}{}, {}</strong>{}</div>".format(weekday, date, time, name, location)
|
replacestr = "<div class=\"nextevent\">{} <strong> {}{}, {}</strong>{}</div>".format(weekday, date, time, name, location)
|
||||||
else:
|
else:
|
||||||
replacestr = "<div class=\"nextevent\">Leider unbekannt, aber <strong>frag mal den Vorstand</strong> der müsste es wissen</div>"
|
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:
|
if len(sourcestr) > 0 and sourcestr in file_contents:
|
||||||
|
|
||||||
updated_contents = file_contents.replace(sourcestr, replacestr)
|
updated_contents = file_contents.replace(sourcestr, replacestr)
|
||||||
|
|
@ -61,7 +68,7 @@ else:
|
||||||
print("html:")
|
print("html:")
|
||||||
print("")
|
print("")
|
||||||
print("<h3>Unser nächster Anlass: </h3><br/>")
|
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(fallbackreplacestr)
|
||||||
print("<div class=\"threecolumn\">")
|
print("<div class=\"threecolumn\">")
|
||||||
print(" <div>")
|
print(" <div>")
|
||||||
print(" <a href=\"termine/\"><img src=\" /images/termine_square.jpg\" alt=\"Terminkalender\"> All unsere Termine</a>")
|
print(" <a href=\"termine/\"><img src=\" /images/termine_square.jpg\" alt=\"Terminkalender\"> All unsere Termine</a>")
|
||||||
|
|
|
||||||
|
|
@ -1,86 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
ICS_URL="$1"
|
|
||||||
|
|
||||||
if [ -z "$ICS_URL" ]; then
|
|
||||||
echo "Usage: $0 <ICS_URL>"
|
|
||||||
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 "<h3>Unser nächster Anlass: </h3><br/>"
|
|
||||||
#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 <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=""
|
|
||||||
summary=""
|
|
||||||
location=""
|
|
||||||
}' | awk -v today="$TODAY" 'substr($1,1,8) >= today' | sort | cut -d " " -f 2- | head -n 1
|
|
||||||
|
|
||||||
echo "<div class=\"threecolumn\">"
|
|
||||||
echo " <div>"
|
|
||||||
echo " <a href=\"termine/\"><img src=\" /images/termine_square.jpg\" alt=\"Terminkalender\"> All unsere Termine</a>"
|
|
||||||
echo " </div>"
|
|
||||||
echo " <div>"
|
|
||||||
echo " <a href=\"about/\"><img src=\"/images/about_square.jpg\" alt=\"Buch\"> Alle Infos über uns</a>"
|
|
||||||
echo " </div>"
|
|
||||||
echo " <div>"
|
|
||||||
echo " <a href=\"kontakt/\"><img src=\"/images/kontakt_square.jpg\" alt=\"Briefe\"> Kontaktiere uns</a>"
|
|
||||||
echo " </div>"
|
|
||||||
echo "</div>"
|
|
||||||
echo "---"
|
|
||||||
echo "_template: page.html"
|
|
||||||
echo ""
|
|
||||||
Loading…
Add table
Reference in a new issue