mirror of
https://git.bolliret.ch/pcs/pcs-website
synced 2026-01-18 11:51:37 +01:00
Claude.ai refactored my script and optimized error handling along the way. ...looking for a new job now. ... ...me, not Claude!
This commit is contained in:
parent
73cee8a653
commit
f6d1d46dc9
1 changed files with 130 additions and 92 deletions
|
|
@ -1,110 +1,148 @@
|
|||
from bs4 import BeautifulSoup
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from datetime import datetime, date
|
||||
import recurring_ical_events
|
||||
from typing import Optional, NamedTuple
|
||||
from dataclasses import dataclass
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from icalendar import Calendar
|
||||
import recurring_ical_events
|
||||
import locale
|
||||
|
||||
@dataclass
|
||||
class EventDetails:
|
||||
weekday: str
|
||||
date: str
|
||||
time: str
|
||||
summary: str
|
||||
location: str
|
||||
|
||||
# TODO: Handling missing arguments is way more complex in Python :(
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python3 {} <ICS_URL>".format(sys.argv[0]))
|
||||
def to_html(self) -> str:
|
||||
return (
|
||||
f'<div class="nextevent">{self.weekday} '
|
||||
f'<strong>{self.date}{self.time}, {self.summary}</strong>'
|
||||
f'{self.location}</div>'
|
||||
)
|
||||
|
||||
# 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 = "<div class=\"nextevent\">Leider unbekannt, aber <strong>frag mal den Vorstand</strong> der müsste es wissen</div>"
|
||||
|
||||
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]
|
||||
|
||||
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)
|
||||
response = requests.get(url)
|
||||
calendar = Calendar.from_ical(response.content)
|
||||
|
||||
# Get recurring and non-recurring events
|
||||
events = recurring_ical_events.of(calendar).after(datetime.now())
|
||||
|
||||
try:
|
||||
event = next(events)
|
||||
|
||||
|
||||
start = event.get('dtstart').dt
|
||||
out_summary = event.get('summary')
|
||||
location = event.get('location', 'No location specified')
|
||||
class EventProcessor:
|
||||
def __init__(self, ics_url: str, content_file: str):
|
||||
self.ics_url = ics_url
|
||||
self.content_file = Path(content_file)
|
||||
self.fallback_html = (
|
||||
'<div class="nextevent">Leider unbekannt, aber '
|
||||
'<strong>frag mal den Vorstand</strong> der müsste es wissen</div>'
|
||||
)
|
||||
|
||||
out_weekday = start.strftime("%A")
|
||||
out_startdate = start.strftime("%-d. %B")
|
||||
@property
|
||||
def fallback_content(self) -> str:
|
||||
return f"""_model: htmlpage
|
||||
---
|
||||
title: Willkommen beim PC Stammertal
|
||||
---
|
||||
html:
|
||||
|
||||
# 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")
|
||||
<h3>Unser nächster Anlass: </h3><br/>
|
||||
{self.fallback_html}
|
||||
<div class="threecolumn">
|
||||
<div>
|
||||
<a href="termine/"><img src=" /images/termine_square.jpg" alt="Terminkalender"> All unsere Termine</a><!-- Fallback>
|
||||
</div>
|
||||
<div>
|
||||
<a href="about/"><img src="/images/about_square.jpg" alt="Buch"> Alle Infos über uns</a>
|
||||
</div>
|
||||
<div>
|
||||
<a href="kontakt/"><img src="/images/kontakt_square.jpg" alt="Briefe"> Kontaktiere uns</a>
|
||||
</div>
|
||||
</div>
|
||||
---
|
||||
_template:
|
||||
|
||||
page.html
|
||||
"""
|
||||
|
||||
if location != 'No location specified':
|
||||
out_location = f", {location}"
|
||||
else:
|
||||
out_location = ""
|
||||
def setup_locale(self) -> None:
|
||||
try:
|
||||
locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
|
||||
except locale.Error as e:
|
||||
print(f"Warning: Failed to set locale: {e}", file=sys.stderr)
|
||||
|
||||
replacestr = "<div class=\"nextevent\">{} <strong> {}{}, {}</strong>{}</div>".format(out_weekday, out_startdate, out_starttime, out_summary, out_location)
|
||||
def fetch_calendar(self) -> Optional[Calendar]:
|
||||
try:
|
||||
response = requests.get(self.ics_url, timeout=10)
|
||||
response.raise_for_status()
|
||||
return Calendar.from_ical(response.content)
|
||||
except (requests.RequestException, ValueError) as e:
|
||||
print(f"Error fetching calendar: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
except StopIteration:
|
||||
def format_event_time(self, start: datetime | date) -> str:
|
||||
return "" if isinstance(start, date) and not isinstance(start, datetime) else start.strftime(" %-H:%M")
|
||||
|
||||
replacestr = fallbackreplacestr
|
||||
def get_next_event(self, calendar: Calendar) -> Optional[EventDetails]:
|
||||
try:
|
||||
events = recurring_ical_events.of(calendar).after(datetime.now())
|
||||
event = next(events)
|
||||
start = event.get('dtstart').dt
|
||||
|
||||
return EventDetails(
|
||||
weekday=start.strftime("%A"),
|
||||
date=start.strftime("%-d. %B"),
|
||||
time=self.format_event_time(start),
|
||||
summary=event.get('summary', ''),
|
||||
location=f", {event.get('location')}" if event.get('location') else ""
|
||||
)
|
||||
|
||||
except (StopIteration, AttributeError) as e:
|
||||
print(f"No upcoming events found: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
# 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.
|
||||
def read_current_content(self) -> tuple[str, str]:
|
||||
try:
|
||||
content = self.content_file.read_text()
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
events = soup.find_all('div', {'class': 'nextevent'})
|
||||
return content, str(events[0]) if len(events) == 1 else ""
|
||||
|
||||
except (IOError, IndexError) as e:
|
||||
print(f"Error reading content file: {e}", file=sys.stderr)
|
||||
return "", ""
|
||||
|
||||
if len(sourcestr) > 0 and sourcestr in file_contents:
|
||||
def process(self) -> str:
|
||||
self.setup_locale()
|
||||
|
||||
content, source_str = self.read_current_content()
|
||||
if not content:
|
||||
return self.fallback_content
|
||||
|
||||
if len(source_str) > 0 and source_str in content:
|
||||
calendar = self.fetch_calendar()
|
||||
if not calendar:
|
||||
return content.replace(source_str, self.fallback_html).rstrip()
|
||||
|
||||
event = self.get_next_event(calendar)
|
||||
if not event:
|
||||
return content.replace(source_str, self.fallback_html).rstrip()
|
||||
|
||||
return content.replace(source_str, event.to_html()).rstrip()
|
||||
|
||||
return self.fallback_content
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) != 2:
|
||||
print(f"Usage: {sys.argv[0]} <ICS_URL>", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
processor = EventProcessor(
|
||||
ics_url=sys.argv[1],
|
||||
content_file="/opt/lektor/project/content/contents.lr"
|
||||
)
|
||||
|
||||
updated_contents = file_contents.replace(sourcestr, replacestr)
|
||||
print(updated_contents)
|
||||
else:
|
||||
print(processor.process(), end='')
|
||||
print("")
|
||||
print("")
|
||||
|
||||
print("_model: htmlpage")
|
||||
print("---")
|
||||
print("title: Willkommen beim PC Stammertal")
|
||||
print("---")
|
||||
print("html:")
|
||||
print("")
|
||||
print("<h3>Unser nächster Anlass: </h3><br/>")
|
||||
print(fallbackreplacestr)
|
||||
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("")
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Reference in a new issue