- feed fetch with ETag/Last-Modified caching - iCalendar parsing/expansion with TEC series detection - posting-window scheduler with 3-day reminders and day-of fallback - image attachments from ATTACH, downscaled via Pillow - SQLite outbox with idempotent deliveries - watchdog alerts to Note to Self - Docker Compose with signal-cli-rest-api
113 lines
3.2 KiB
Python
113 lines
3.2 KiB
Python
"""Render an event in the group's posting format."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import calendar as calendar_module
|
|
import re
|
|
from datetime import datetime
|
|
from zoneinfo import ZoneInfo
|
|
|
|
from .config import FormatConfig
|
|
from .expand import Event
|
|
|
|
# Loose UK postcode matcher; deliberately permissive because feeds vary.
|
|
UK_POSTCODE_RE = re.compile(r"\b([A-Z]{1,2}\d[A-Z\d]?)\s*(\d[A-Z]{2})\b", re.IGNORECASE)
|
|
|
|
|
|
def find_postcode(*texts: str) -> str:
|
|
for text in texts:
|
|
if not text:
|
|
continue
|
|
match = UK_POSTCODE_RE.search(text)
|
|
if match:
|
|
return f"{match.group(1).upper()} {match.group(2).upper()}"
|
|
return ""
|
|
|
|
|
|
def _clean(text: str) -> str:
|
|
return re.sub(r"\s+", " ", text or "").strip()
|
|
|
|
|
|
def _first_line(text: str) -> str:
|
|
for line in (text or "").splitlines():
|
|
if line.strip():
|
|
return line.strip()
|
|
return ""
|
|
|
|
|
|
def venue_from_location(location: str) -> str:
|
|
"""The first line of a TEC LOCATION value is the venue name."""
|
|
return _clean(_first_line(location)).rstrip(",")
|
|
|
|
|
|
def venue_from_description(description: str) -> str:
|
|
"""Best-effort fallback: a line in the description that carries a postcode."""
|
|
for line in (description or "").splitlines():
|
|
match = UK_POSTCODE_RE.search(line)
|
|
if not match:
|
|
continue
|
|
before = line[: match.start()].strip(" ,-\u2013")
|
|
parts = [part.strip() for part in re.split(r"[,;]", before) if part.strip()]
|
|
if parts:
|
|
return _clean(parts[0])[:60]
|
|
return ""
|
|
|
|
|
|
def _clock(moment: datetime) -> tuple[str, str]:
|
|
hour = moment.hour % 12 or 12
|
|
meridiem = "am" if moment.hour < 12 else "pm"
|
|
return f"{hour}.{moment.minute:02d}", meridiem
|
|
|
|
|
|
def format_time_range(start: datetime, end: datetime) -> str:
|
|
start_text, start_meridiem = _clock(start)
|
|
end_text, end_meridiem = _clock(end)
|
|
if not end or end <= start:
|
|
return f"{start_text}{start_meridiem}"
|
|
if start_meridiem == end_meridiem:
|
|
return f"{start_text}-{end_text}{end_meridiem}"
|
|
return f"{start_text}{start_meridiem}-{end_text}{end_meridiem}"
|
|
|
|
|
|
def format_hashtags(start: datetime) -> str:
|
|
month = calendar_module.month_name[start.month]
|
|
return f"#{month} #{month}{start.day}"
|
|
|
|
|
|
def render_post(
|
|
event: Event,
|
|
config: FormatConfig,
|
|
local_tz: ZoneInfo,
|
|
*,
|
|
venue: str = "",
|
|
postcode: str = "",
|
|
link: str = "",
|
|
) -> str:
|
|
start = event.start.astimezone(local_tz)
|
|
end = event.end.astimezone(local_tz)
|
|
|
|
lines: list[str] = []
|
|
|
|
if config.show_title:
|
|
title = _clean(event.summary)
|
|
if title:
|
|
lines.append(title)
|
|
|
|
if config.show_hashtags:
|
|
lines.append(format_hashtags(start))
|
|
|
|
if config.show_time:
|
|
lines.append("All day" if event.all_day else format_time_range(start, end))
|
|
|
|
if config.show_location:
|
|
name = _clean(venue) or venue_from_location(event.location)
|
|
code = postcode or find_postcode(event.location, event.description)
|
|
place = " ".join(part for part in (name, code) if part).strip()
|
|
if place:
|
|
lines.append(place)
|
|
|
|
if config.show_link and link:
|
|
lines.append(link)
|
|
|
|
return "\n".join(lines)
|