From f6d1d46dc9af67796ef05b417a530b122965e606 Mon Sep 17 00:00:00 2001 From: Reto Bollinger Date: Fri, 29 Nov 2024 21:18:22 +0100 Subject: [PATCH] Claude.ai refactored my script and optimized error handling along the way. ...looking for a new job now. ... ...me, not Claude! --- .../scripts/calendar-fetcher-main.py | 222 ++++++++++-------- 1 file changed, 130 insertions(+), 92 deletions(-) diff --git a/lektor/lektordata/scripts/calendar-fetcher-main.py b/lektor/lektordata/scripts/calendar-fetcher-main.py index 8169a0f..c7ec997 100644 --- a/lektor/lektordata/scripts/calendar-fetcher-main.py +++ b/lektor/lektordata/scripts/calendar-fetcher-main.py @@ -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 {} ".format(sys.argv[0])) + def to_html(self) -> str: + return ( + f'
{self.weekday} ' + f'{self.date}{self.time}, {self.summary}' + f'{self.location}
' + ) -# 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 = "" - -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 = ( + '
Leider unbekannt, aber ' + 'frag mal den Vorstand der müsste es wissen
' + ) - 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") +

Unser nächster Anlass:


+{self.fallback_html} +