diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 92b3fea..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,17 +0,0 @@ -module.exports = { - "env": { - "browser": true, - "es6": true - }, - "extends": "eslint:recommended", - "globals": { - "Atomics": "readonly", - "SharedArrayBuffer": "readonly" - }, - "parserOptions": { - "ecmaVersion": 14, - "sourceType": "module" - }, - "rules": { - } -}; diff --git a/.gitignore b/.gitignore index 87339fa..cf41dea 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,4 @@ __pycache__/ __pycache__ .mypy_cache config -.hypothesis -personal-data -static/bootstrap5 -static/leaflet* -static/es-module-shims +.hypothesis/ diff --git a/AGENTS.md b/AGENTS.md deleted file mode 100644 index fdd8f72..0000000 --- a/AGENTS.md +++ /dev/null @@ -1,67 +0,0 @@ -# Development Guidelines - -## Project Overview -This is a personal agenda web application built with Flask that tracks various events and important dates: -- Events: birthdays, holidays, travel itineraries, conferences, waste collection schedules -- Space launches, meteor showers, astronomical events -- Financial information (FX rates, stock market) -- UK-specific features (holidays, waste collection, railway schedules) -- Authentication via UniAuth -- Frontend uses Bootstrap 5, Leaflet for maps, FullCalendar for calendar views - -## Python Environment -- Always use `python3` directly, never `python` -- All Python code should include type annotations -- Use `typing.Any` instead of `Any` in type hints (import from typing module) -- Run `mypy --strict` (fix any type errors in the file) and `black` on modified code after creating or modifying Python files -- Avoid running `black .` -- Main entry point: `python3 web_view.py` (Flask app on port 5000) -- Tests: Use `pytest` (tests in `/tests/` directory) - -## Project Structure -- `agenda/` - Main Python package with modules for different event types -- `web_view.py` - Flask web application entry point -- `templates/` - Jinja2 HTML templates -- `static/` - CSS, JS, and frontend assets -- `config/` - Configuration files -- `personal-data/` - User's personal data (not in git) - -## Git Workflow -- Avoid committing unrelated untracked files (e.g., `node_modules/`, build artifacts) -- Only commit relevant project files -- Personal data directory (`personal-data/`) is excluded from git - -## Conference attendance fields -Conferences in `conferences.yaml` support optional `attend_start` and `attend_end` fields for when you arrive late or leave early. Both accept a plain date or a datetime with time and timezone (YAML datetime syntax). When present, the trip page shows the attendance dates instead of the official conference dates. The official `start`/`end` fields are always kept for context. - -```yaml -- name: FOSDEM - start: 2023-02-04 - end: 2023-02-05 - attend_end: 2023-02-04 # left after day 1 - attend_start: 2023-02-04 14:00:00+00:00 # or with time -``` - -## Notes -- Trip stats new-country badges come from `agenda.stats.calculate_yearly_stats` via `year_stats.new_countries` (first-visit year, excluding `PREVIOUSLY_VISITED`). -- Trip stats are calculated in `agenda/stats.py`: - - `travel_legs()` extracts airlines, airports, and stations from individual trip travel legs - - `calculate_yearly_stats()` aggregates stats per year including flight/train counts, airlines, airports, stations - - `calculate_overall_stats()` aggregates yearly stats into overall totals for the summary section - -## Travel type patterns -Transport types: `flight`, `train`, `ferry`, `coach`, `bus`. - -**Road transport (bus and coach)** share a common loader `load_road_transport()` in `trip.py`. `load_coaches` and `load_buses` are thin wrappers that pass type name, YAML filenames, and CO2 factor (coach: 0.027 kg/km, bus: 0.1 kg/km). Both use `from_station`/`to_station` fields and support scalar or dict-keyed GeoJSON route filenames in the stop/station data. - -**Ferry** is loaded separately: uses `from_terminal`/`to_terminal` fields and GeoJSON routes come from the terminal's `routes` dict (always a dict, not scalar). - -**Route rendering** (`get_trip_routes`): bus and coach are handled in a single combined block (they use the same pattern — `from_station`/`to_station`, `{type}_routes/` folder). Ferry always has a geojson file and renders as type `"train"` for the map renderer. - -**Trip elements** (`Trip.elements()` in `types.py`): bus and coach are handled in a single combined block using `item["type"] in ("coach", "bus")`. Ferry is separate because it uses `from_terminal`/`to_terminal` and always requires `arrive` (not optional). - -**Location collection** (`get_locations`): bus → `bus_stop`, coach → `coach_station`, ferry → `ferry_terminal` (all separate map pin types). - -**CO2 factors** (kg CO2e per passenger per km): train 0.037, coach 0.027, ferry 0.02254, bus 0.1. - -**Schengen tracking**: ferry journeys are tracked for Schengen compliance; bus and coach are not. diff --git a/agenda/__init__.py b/agenda/__init__.py index 0428f0c..baf8813 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -2,7 +2,6 @@ from datetime import date, datetime, time -import pycountry import pytz uk_tz = pytz.timezone("Europe/London") @@ -11,34 +10,3 @@ uk_tz = pytz.timezone("Europe/London") def uk_time(d: date, t: time) -> datetime: """Combine time and date for UK timezone.""" return uk_tz.localize(datetime.combine(d, t)) - - -def format_list_with_ampersand(items: list[str]) -> str: - """Join a list of strings with commas and an ampersand.""" - if len(items) > 1: - return ", ".join(items[:-1]) + " & " + items[-1] - elif items: - return items[0] - return "" - - -def get_country(alpha_2: str | None) -> pycountry.db.Country | None: - """Lookup country by alpha-2 country code.""" - if not alpha_2: - return None - if alpha_2.count(",") > 3: # ESA - return pycountry.db.Country(flag="🇪🇺", name="ESA") - if not alpha_2: - return None - if alpha_2 == "xk": - return pycountry.db.Country( - flag="\U0001f1fd\U0001f1f0", name="Kosovo", alpha_2="xk" - ) - - country: pycountry.db.Country | None = None - if len(alpha_2) == 2: - country = pycountry.countries.get(alpha_2=alpha_2.upper()) - elif len(alpha_2) == 3: - country = pycountry.countries.get(alpha_3=alpha_2.upper()) - - return country diff --git a/agenda/accommodation.py b/agenda/accommodation.py index 59e9d64..3ce4b9e 100644 --- a/agenda/accommodation.py +++ b/agenda/accommodation.py @@ -1,19 +1,20 @@ -"""Accommodation.""" +"""Accomodation""" import yaml -from .event import Event +from .types import Event def get_events(filepath: str) -> list[Event]: - """Get accommodation from YAML.""" + """Get accomodation from YAML.""" with open(filepath) as f: return [ Event( date=item["from"], end_date=item["to"], name="accommodation", - title=( + title="🧳" + + ( f'{item["location"]} Airbnb' if item.get("operator") == "airbnb" else item["name"] diff --git a/agenda/add_new_conference.py b/agenda/add_new_conference.py deleted file mode 100644 index 3b041b4..0000000 --- a/agenda/add_new_conference.py +++ /dev/null @@ -1,799 +0,0 @@ -"""Helpers for adding conferences to the YAML data file.""" - -import configparser -import json -import os -import re -import sys -import typing -from datetime import date, datetime, time, timezone -from urllib.parse import parse_qs, urlparse - -import html2text -import lxml.html -import openai -import pycountry -import requests -import yaml - -from agenda.conference import ConferenceSeries, conference_date_fields, load_series - -USER_AGENT = "add-new-conference/0.1" -COORDINATE_PATTERNS = ( - re.compile(r"@(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)"), - re.compile(r"[?&]q=(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)"), - re.compile(r"[?&](?:ll|center)=(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)"), - re.compile(r"!3d(-?\d+(?:\.\d+)?)!4d(-?\d+(?:\.\d+)?)"), - re.compile(r"[?&]destination=(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)"), -) - - -def read_api_key() -> str: - """Read API key from ~/.config/openai/config.""" - config_path = os.path.expanduser("~/.config/openai/config") - parser = configparser.ConfigParser() - parser.read(config_path) - return parser["openai"]["api_key"] - - -def conference_yaml_format_description() -> str: - """Return the conference YAML format description for LLM prompts.""" - return """ -Use this YAML format for one conference entry. - -Required fields: -- `name`: event name. -- `topic`: topic/category. -- `location`: city or location label. Use `TBC` if the page confirms a future - event but not a city. -- Date information in nested `dates`. - -Preferred date shape: -- `dates.status`: one of `exact`, `tentative`, or `approximate`. -- For `exact`: use when the page confirms specific dates/times. Include - `dates.start` and `dates.end` as YAML dates or timezone-aware datetimes. -- For `tentative`: use when specific dates are guessed or explicitly - unconfirmed. Include `dates.start`, `dates.end`, and preferably `dates.label` - and `dates.basis`. -- For `approximate`: use when only a broad date phrase is known. Include - `dates.label`, `dates.earliest`, and `dates.latest`. Examples: `March 2027` - should become earliest `2027-03-01`, latest `2027-03-31`; `mid-April 2027` - should become a sensible bounded range such as `2027-04-11` to `2027-04-20`. - -Important date rule: -- If the source page contains exact dates, output `dates.status: exact` even if - the existing agenda entry or conference announcement previously had only - approximate dates. -- Always include an end date for `exact` and `tentative`. For a single-day - event, `dates.end` can be the same as `dates.start`. -- Do not output legacy top-level `start`, `end`, or `date_status`. - -Common optional fields: -- `series`: a key from the known conference series list, when this event belongs - to a listed series. -- `country`: valid ISO 3166-1 alpha-2 country code in lowercase, for example - `ca`, `gb`, `us`. Do not output country names. -- `venue`, `address`, `latitude`, `longitude`, `url`, `cfp_url`, `cfp_end`, - `hashtag`, `description`. -- For an address written in a non-Latin script, output a conventional - Latin-script rendering only when it can be derived confidently. Do not invent - or loosely translate an address. If no reliable rendering is available, omit - `address`; coordinates are preferable to an unreadable or inaccurate address. -- `free`, `price`, `currency`, `hackathon`, `online`, `attendees`. -- When `free: true`, omit `price` and `currency` because they are redundant. -- Do not include `going`, `registered`, `accommodation_booked`, - `transport_booked`, or `trip` unless the source explicitly says they apply to - my attendance. -""" - - -def yaml_example_text() -> str: - """Return examples of the conference YAML format.""" - return """ -- name: Geomob London - series: geomob-london - topic: Maps - location: London - country: gb - dates: - status: exact - start: 2026-01-28 18:00:00+00:00 - end: 2026-01-28 22:00:00+00:00 - url: https://thegeomob.com/post/jan-28th-2026-geomoblon-details - venue: Geovation Hub - address: Sutton Yard, 65 Goswell Rd, London EC1V 7EN - latitude: 51.5242464 - longitude: -0.0997024 - free: true - hashtag: '#geomobLON' - -- name: DebConf 25 - series: debconf - topic: Debian - location: Plouzane - country: fr - dates: - status: exact - start: 2025-07-07 - end: 2025-07-20 - url: https://wiki.debian.org/DebConf/25 - cfp_url: https://debconf25.debconf.org/talks/new/ - venue: Ecole nationale superieure Mines-Telecom Atlantique Bretagne Pays de la Loire - campus de Brest - latitude: 48.35934 - longitude: -4.569889 - -- name: Wikimedia Hackathon - series: wikimedia-hackathon - topic: Wikimedia - location: Albania - country: al - dates: - status: approximate - label: mid-April 2027 - earliest: 2027-04-11 - latest: 2027-04-20 - url: https://www.mediawiki.org/wiki/Wikimedia_Hackathon_2027 - hackathon: true - -- name: PyCascades - series: pycascades - topic: Python - location: Seattle, Washington - country: us - dates: - status: approximate - label: March 2027 - earliest: 2027-03-01 - latest: 2027-03-31 -""" - - -def series_prompt_text(series: dict[str, ConferenceSeries]) -> str: - """Return compact known series text for the LLM prompt.""" - if not series: - return "No known conference series loaded." - - lines = ["Known conference series IDs:"] - for series_id, item in sorted(series.items()): - details = [item["name"]] - if topic := item.get("topic"): - details.append(f"topic: {topic}") - if location := item.get("usual_location"): - details.append(f"usual location: {location}") - if country := item.get("country"): - details.append(f"country: {country}") - lines.append(f"- {series_id}: " + "; ".join(details)) - return "\n".join(lines) - - -def build_prompt( - url: str, - source_text: str, - detected_coordinates: tuple[float, float] | None, - series: dict[str, ConferenceSeries] | None = None, -) -> str: - """Build prompt with embedded YAML format details and examples.""" - coordinate_note = "" - if detected_coordinates is not None: - coordinate_note = ( - "\nDetected venue coordinates from a map link on the page:\n" - f"latitude: {detected_coordinates[0]}\n" - f"longitude: {detected_coordinates[1]}\n" - ) - - prompt = f""" -I keep a record of interesting conferences in a YAML file. - -Format rules: - -{conference_yaml_format_description()} - -{series_prompt_text(series or {})} - -Here are some examples of the format I use: - -{yaml_example_text()} - -Now here is a new conference of interest: - -Conference URL: {url} - -Return the YAML representation for this conference following the same style and -keys as the examples. Only include keys if the information is available. Do not -invent details. - -Important: if this is a Geomob event, use a `dates.end` datetime of 22:00 local -time on the event date unless the page explicitly provides a different end time. -{coordinate_note} - -Wrap your answer in a JSON object with a single key "yaml". -=== -{source_text} -""" - return prompt - - -def get_from_open_ai(prompt: str, model: str = "gpt-5.4") -> dict[str, str]: - """Pass prompt to OpenAI and get reply.""" - client = openai.OpenAI(api_key=read_api_key()) - - response = client.chat.completions.create( - messages=[{"role": "user", "content": prompt}], - model=model, - response_format={"type": "json_object"}, - ) - - reply = response.choices[0].message.content - assert isinstance(reply, str) - return typing.cast(dict[str, str], json.loads(reply)) - - -def fetch_webpage(url: str) -> lxml.html.HtmlElement: - """Fetch webpage HTML and parse it.""" - response = requests.get(url, headers={"User-Agent": USER_AGENT}) - response.raise_for_status() - return lxml.html.fromstring(response.content) - - -def webpage_to_text(root: lxml.html.HtmlElement) -> str: - """Convert parsed HTML into readable text content.""" - metadata = extract_event_metadata(root) - root_copy = lxml.html.fromstring(lxml.html.tostring(root)) - - for script_or_style in root_copy.xpath("//script|//style"): - script_or_style.drop_tree() - - text_maker = html2text.HTML2Text() - text_maker.ignore_links = True - text_maker.ignore_images = True - page_text = text_maker.handle(lxml.html.tostring(root_copy, encoding="unicode")) - if not metadata: - return page_text - - metadata_text = "\n".join(f"- {key}: {value}" for key, value in metadata.items()) - return f"Structured event metadata:\n{metadata_text}\n\n{page_text}" - - -def extract_event_metadata(root: lxml.html.HtmlElement) -> dict[str, str]: - """Extract Schema.org Event microdata, including visually hidden values.""" - event_nodes = root.xpath( - '//*[@itemscope and contains(@itemtype, "schema.org/Event")]' - ) - if not event_nodes: - return {} - - metadata: dict[str, str] = {} - for element in event_nodes[0].xpath(".//*[@itemprop]"): - key = str(element.get("itemprop", "")).strip() - value = str(element.get("content", "")).strip() - if key and value and key not in metadata: - metadata[key] = value - return metadata - - -def apply_event_metadata_dates( - conf: dict[str, typing.Any], root: lxml.html.HtmlElement -) -> None: - """Fill missing generated dates from Schema.org Event microdata.""" - dates = conf.get("dates") - if isinstance(dates, dict) and dates.get("start") is not None: - return - if conf.get("start") is not None: - return - - metadata = extract_event_metadata(root) - start = parse_yaml_date_value(metadata.get("startDate")) - if start is None: - return - end = parse_yaml_date_value(metadata.get("endDate")) or start - conf["dates"] = {"status": "exact", "start": start, "end": end} - - -def parse_osm_url(url: str) -> tuple[float, float] | None: - """Extract latitude/longitude from an OpenStreetMap URL.""" - parsed = urlparse(url) - query = parse_qs(parsed.query) - - mlat = query.get("mlat") - mlon = query.get("mlon") - if mlat and mlon: - return float(mlat[0]), float(mlon[0]) - - if parsed.fragment.startswith("map="): - parts = parsed.fragment.split("/") - if len(parts) >= 3: - return float(parts[-2]), float(parts[-1]) - - return None - - -def extract_google_maps_latlon(url: str) -> tuple[float, float] | None: - """Extract latitude/longitude from a Google Maps URL.""" - for pattern in COORDINATE_PATTERNS: - match = pattern.search(url) - if match: - return float(match.group(1)), float(match.group(2)) - - return None - - -def latlon_from_google_maps_url( - url: str, timeout: int = 10 -) -> tuple[float, float] | None: - """Resolve a Google Maps URL and extract latitude/longitude.""" - response = requests.get( - url, - allow_redirects=True, - timeout=timeout, - headers={"User-Agent": "lookup.py/1.0"}, - ) - response.raise_for_status() - - coordinates = extract_google_maps_latlon(response.url) - if coordinates is not None: - return coordinates - - return extract_google_maps_latlon(response.text) - - -def parse_coordinates_from_url(url: str) -> tuple[float, float] | None: - """Extract latitude/longitude from a supported map URL.""" - lower_url = url.lower() - - if "openstreetmap.org" in lower_url: - return parse_osm_url(url) - - if "google." in lower_url or "maps.app.goo.gl" in lower_url: - coordinates = extract_google_maps_latlon(url) - if coordinates is not None: - return coordinates - - try: - return latlon_from_google_maps_url(url) - except requests.RequestException: - return None - - return None - - -def detect_page_coordinates(root: lxml.html.HtmlElement) -> tuple[float, float] | None: - """Detect venue coordinates from Google Maps or OSM links.""" - for link in root.xpath("//a[@href]"): - href = str(link.get("href", "")).strip() - if not href: - continue - - coordinates = parse_coordinates_from_url(href) - if coordinates is not None: - return coordinates - - return None - - -def parse_date(date_str: str) -> datetime: - """Parse ISO date or datetime into a naive datetime (UTC if tz-aware).""" - try: - dt = datetime.fromisoformat(date_str) - except ValueError: - dt = datetime.fromisoformat(date_str.split("T")[0]) - - if dt.tzinfo is not None: - dt = dt.astimezone(timezone.utc).replace(tzinfo=None) - - return dt - - -def data_dir_from_conferences_path(yaml_path: str) -> str: - """Return personal-data directory from a conferences.yaml path.""" - return os.path.dirname(os.path.abspath(yaml_path)) - - -def url_has_year_component(url: str) -> bool: - """Return True if the URL contains a year or edition path component.""" - parsed = urlparse(url) - components = [part for part in parsed.path.split("/") if part] - if parsed.netloc: - components.extend(part for part in parsed.netloc.split(".") if part) - - for component in components: - if re.fullmatch(r"20\d{2}", component): - return True - if re.search(r"(?:^|[-_/])20\d{2}(?:$|[-_/])", component): - return True - if re.fullmatch(r"\d{1,2}x", component, flags=re.IGNORECASE): - return True - return False - - -def insert_sorted( - conferences: list[dict[str, typing.Any]], new_conf: dict[str, typing.Any] -) -> list[dict[str, typing.Any]]: - """Insert a conference sorted by start date and skip duplicate URLs.""" - new_url = new_conf.get("url") - new_start = conference_sort_datetime(new_conf) - new_year = new_start.year - update_idx = find_inexact_existing_conference(conferences, new_conf) - if update_idx is not None: - existing = conferences.pop(update_idx) - merged = dict(existing) - merged.update(new_conf) - print(f"Updating inexact conference entry: {existing.get('name')}") - return insert_sorted(conferences, merged) - - if new_url: - for conf in conferences: - if conf.get("url") == new_url: - existing_start = conference_sort_datetime(conf) - existing_year = existing_start.year - - if url_has_year_component(new_url): - print(f"⚠️ Conference with URL {new_url} already exists, skipping.") - return conferences - if existing_year == new_year: - print( - f"⚠️ Conference already exists in YAML " - f"(url={new_url}, year={existing_year}), skipping." - ) - return conferences - - for idx, conf in enumerate(conferences): - existing_start = conference_sort_datetime(conf) - if new_start < existing_start: - conferences.insert(idx, new_conf) - return conferences - conferences.append(new_conf) - return conferences - - -def date_ranges_overlap( - first: dict[str, typing.Any], second: dict[str, typing.Any] -) -> bool: - """Return True if two conference date ranges overlap.""" - first_fields = conference_date_fields(first) - second_fields = conference_date_fields(second) - return typing.cast(date, first_fields["start_date"]) <= typing.cast( - date, second_fields["end_date"] - ) and typing.cast(date, second_fields["start_date"]) <= typing.cast( - date, first_fields["end_date"] - ) - - -def same_conference_identity( - existing: dict[str, typing.Any], new_conf: dict[str, typing.Any] -) -> bool: - """Return True if two entries appear to represent the same conference.""" - existing_url = existing.get("url") - new_url = new_conf.get("url") - if existing_url and new_url and existing_url == new_url: - return True - - existing_series = existing.get("series") - new_series = new_conf.get("series") - if existing_series and new_series and existing_series == new_series: - return date_ranges_overlap(existing, new_conf) - - return str(existing.get("name", "")).casefold() == str( - new_conf.get("name", "") - ).casefold() and date_ranges_overlap(existing, new_conf) - - -def find_inexact_existing_conference( - conferences: list[dict[str, typing.Any]], new_conf: dict[str, typing.Any] -) -> int | None: - """Return index of an inexact existing entry that exact new data can update.""" - new_fields = conference_date_fields(new_conf) - if new_fields["date_status"] != "exact": - return None - - for idx, existing in enumerate(conferences): - existing_fields = conference_date_fields(existing) - if existing_fields["date_status"] == "exact": - continue - if same_conference_identity(existing, new_conf): - return idx - - return None - - -def conference_sort_datetime(conf: dict[str, typing.Any]) -> datetime: - """Return conference sort date as a datetime.""" - sort_date = conference_date_fields(conf)["sort_date"] - if isinstance(sort_date, datetime): - return sort_date - return datetime.combine(sort_date, time()) - - -def validate_country(conf: dict[str, typing.Any]) -> None: - """Ensure country is a valid ISO 3166-1 alpha-2 code, normalise if possible.""" - country = conf.get("country") - if not country: - return - - country = country.strip() - if len(country) == 2: - if pycountry.countries.get(alpha_2=country.upper()): - conf["country"] = country.lower() - return - raise ValueError(f"❌ Invalid ISO 3166-1 code '{country}'") - - match = pycountry.countries.get(name=country) - if not match: - try: - match = pycountry.countries.search_fuzzy(country)[0] - except LookupError as exc: - raise ValueError( - f"❌ Country '{country}' not recognised as ISO 3166-1" - ) from exc - - conf["country"] = match.alpha_2.lower() - - -def validate_series( - conf: dict[str, typing.Any], series: dict[str, ConferenceSeries] -) -> None: - """Ensure a generated series ID exists in conference_series.yaml.""" - series_id = conf.get("series") - if series_id is None: - return - if not isinstance(series_id, str) or series_id not in series: - raise ValueError( - f"Generated conference uses unknown series {series_id!r}. " - "Add it to conference_series.yaml first or remove the series field." - ) - - -def normalize_free_event_fields(conf: dict[str, typing.Any]) -> None: - """Remove redundant pricing fields from free conferences.""" - if conf.get("free") is True: - conf.pop("price", None) - conf.pop("currency", None) - - -def parse_yaml_datetime(value: typing.Any) -> datetime | None: - """Convert YAML date/datetime values to a datetime.""" - if isinstance(value, datetime): - return value - - if isinstance(value, date): - return datetime.combine(value, time()) - - if isinstance(value, str): - try: - return datetime.fromisoformat(value) - except ValueError: - return datetime.combine(date.fromisoformat(value.split("T")[0]), time()) - - return None - - -def parse_yaml_date_value(value: typing.Any) -> date | datetime | None: - """Convert YAML date/datetime strings to date-like values.""" - if isinstance(value, datetime): - return value - - if isinstance(value, date): - return value - - if not isinstance(value, str): - return None - - try: - if " " in value or "T" in value: - return datetime.fromisoformat(value) - return date.fromisoformat(value) - except ValueError: - return None - - -def normalize_date_values(conf: dict[str, typing.Any]) -> None: - """Normalize quoted ISO date/datetime values produced by the LLM.""" - dates = conf.get("dates") - if isinstance(dates, dict): - for field in ("start", "end", "earliest", "latest"): - if field in dates: - parsed = parse_yaml_date_value(dates[field]) - if parsed is not None: - dates[field] = parsed - - for field in ("start", "end"): - if field in conf: - parsed = parse_yaml_date_value(conf[field]) - if parsed is not None: - conf[field] = parsed - - -def same_type_as_start( - start_value: typing.Any, - new_dt: datetime, - keep_timezone: bool = True, - prefer_datetime: bool = False, -) -> typing.Any: - """Return end value shaped like the start value when possible.""" - if isinstance(start_value, datetime): - if keep_timezone: - return new_dt - return new_dt.replace(tzinfo=None) - - if isinstance(start_value, date): - if prefer_datetime: - return new_dt - return new_dt.date() - - if isinstance(start_value, str): - if prefer_datetime or " " in start_value or "T" in start_value: - return new_dt.isoformat(sep=" ") - return new_dt.date().isoformat() - - return new_dt - - -def normalize_dates_field(conf: dict[str, typing.Any]) -> None: - """Move legacy top-level date fields into the nested dates mapping.""" - normalize_date_values(conf) - raw_dates = conf.get("dates") - dates = raw_dates if isinstance(raw_dates, dict) else None - - if dates is None and ("start" in conf or "end" in conf): - start = conf.pop("start", None) - end = conf.pop("end", start) - status = str(conf.pop("date_status", "exact")) - conf["dates"] = {"status": status, "start": start, "end": end} - return - - if dates is not None: - if "start" in conf and "start" not in dates: - dates["start"] = conf["start"] - if "end" in conf and "end" not in dates: - dates["end"] = conf["end"] - if "date_status" in conf and "status" not in dates: - dates["status"] = conf["date_status"] - - conf.pop("start", None) - conf.pop("end", None) - conf.pop("date_status", None) - normalize_date_values(conf) - - -def validate_generated_conference(conf: dict[str, typing.Any]) -> None: - """Validate generated conference YAML before inserting it.""" - try: - conference_date_fields(conf) - except ValueError as exc: - generated_yaml = yaml.dump(conf, sort_keys=False, allow_unicode=True).strip() - raise ValueError( - "Generated conference YAML is missing valid date information. " - "Expected nested `dates:` with exact/tentative start/end or " - f"approximate earliest/latest.\n\nGenerated YAML:\n{generated_yaml}" - ) from exc - - -def maybe_extract_explicit_end_time(source_text: str) -> int | None: - """Extract an explicit 12-hour clock end time for Geomob-style pages.""" - lowered = source_text.lower() - - if "10pm" in lowered or "10 pm" in lowered or "22:00" in lowered: - return 22 - - if "11pm" in lowered or "11 pm" in lowered or "23:00" in lowered: - return 23 - - return None - - -def normalise_end_field(new_conf: dict[str, typing.Any], source_text: str) -> None: - """Ensure an end value exists, with a Geomob-specific fallback.""" - dates = new_conf.get("dates") - nested_dates = dates if isinstance(dates, dict) else None - start_value = ( - nested_dates.get("start") if nested_dates is not None else new_conf.get("start") - ) - if start_value is None: - return - - start_dt = parse_yaml_datetime(start_value) - if start_dt is None: - return - - name = str(new_conf.get("name", "")) - url = str(new_conf.get("url", "")) - is_geomob = "geomob" in name.lower() or "thegeomob.com" in url.lower() - - if is_geomob: - end_hour = maybe_extract_explicit_end_time(source_text) - if end_hour is None: - end_hour = 22 - - geomob_end = start_dt.replace(hour=end_hour, minute=0, second=0, microsecond=0) - end_value = same_type_as_start(start_value, geomob_end, prefer_datetime=True) - if nested_dates is not None: - nested_dates["end"] = end_value - else: - new_conf["end"] = end_value - return - - if nested_dates is not None: - if "end" not in nested_dates: - nested_dates["end"] = same_type_as_start(start_value, start_dt) - return - - if "end" not in new_conf: - new_conf["end"] = same_type_as_start(start_value, start_dt) - - -def load_conferences(yaml_path: str) -> list[dict[str, typing.Any]]: - """Load conference YAML.""" - with open(yaml_path) as file: - loaded = yaml.safe_load(file) - assert isinstance(loaded, list) - return typing.cast(list[dict[str, typing.Any]], loaded) - - -def load_conference_series_for_path(yaml_path: str) -> dict[str, ConferenceSeries]: - """Load conference series next to the target conferences YAML file.""" - return load_series(data_dir_from_conferences_path(yaml_path)) - - -def dump_conferences(yaml_path: str, conferences: list[dict[str, typing.Any]]) -> None: - """Write conference YAML.""" - with open(yaml_path, "w") as file: - text = yaml.dump(conferences, sort_keys=False, allow_unicode=True) - text = text.replace("\n- name:", "\n\n- name:") - file.write(text.lstrip()) - - -def add_new_conference(url: str, yaml_path: str) -> bool: - """Fetch, generate and insert a conference into the YAML file.""" - conferences = load_conferences(yaml_path) - - if url_has_year_component(url): - for conf in conferences: - if conf.get("url") == url: - fields = conference_date_fields(conf) - if fields["date_status"] != "exact": - continue - print( - "⚠️ Conference already exists in YAML " - + f"(url={url}), skipping before API call." - ) - return False - - soup = fetch_webpage(url) - source_text = webpage_to_text(soup) - detected_coordinates = detect_page_coordinates(soup) - series = load_conference_series_for_path(yaml_path) - prompt = build_prompt(url, source_text, detected_coordinates, series) - new_yaml_text = get_from_open_ai(prompt)["yaml"] - - new_conf = yaml.safe_load(new_yaml_text) - if isinstance(new_conf, list): - new_conf = new_conf[0] - assert isinstance(new_conf, dict) - - validate_country(new_conf) - validate_series(new_conf, series) - normalize_free_event_fields(new_conf) - apply_event_metadata_dates(new_conf, soup) - normalize_dates_field(new_conf) - normalise_end_field(new_conf, source_text) - normalize_dates_field(new_conf) - validate_generated_conference(new_conf) - - if detected_coordinates is not None: - new_conf["latitude"] = detected_coordinates[0] - new_conf["longitude"] = detected_coordinates[1] - - updated = insert_sorted(conferences, new_conf) - dump_conferences(yaml_path, updated) - return True - - -def main(argv: list[str] | None = None) -> int: - """CLI entrypoint.""" - args = argv if argv is not None else sys.argv[1:] - if not args: - raise SystemExit("Usage: add-new-conference URL") - - yaml_path = os.path.expanduser("~/src/personal-data/conferences.yaml") - add_new_conference(args[0], yaml_path) - return 0 diff --git a/agenda/airbnb.py b/agenda/airbnb.py deleted file mode 100644 index dfbf3c2..0000000 --- a/agenda/airbnb.py +++ /dev/null @@ -1,190 +0,0 @@ -"""Library for parsing Airbnb booking HTML files.""" - -import json -import re -import typing -from datetime import datetime -from typing import Any -from zoneinfo import ZoneInfo - -import lxml.html -import pycountry - -StrDict = dict[str, typing.Any] - - -def build_datetime(date_str: str, time_str: str, tz_name: str) -> datetime: - """ - Combine an ISO date string, HH:MM time string, and a timezone name - into a timezone-aware datetime in the specified timezone. - """ - dt_str = f"{date_str}T{time_str}" - naive_dt = datetime.fromisoformat(dt_str) - return naive_dt.replace(tzinfo=ZoneInfo(tz_name)) - - -def list_to_dict(items: list[typing.Any]) -> dict[str, typing.Any]: - """Convert a flat list to a dict, assuming alternating keys and values.""" - return {items[i]: items[i + 1] for i in range(0, len(items), 2)} - - -def extract_country_code(address: str) -> str | None: - """Return ISO 3166-1 alpha-2 country code from a free-text address.""" - address_lower = address.lower() - for country in pycountry.countries: - if country.name.lower() in address_lower: - return str(country.alpha_2.lower()) - if ( - hasattr(country, "official_name") - and country.official_name.lower() in address_lower - ): - return str(country.alpha_2.lower()) - return None - - -def get_json_blob(tree: Any) -> str: - data_id = "data-injector-instances" - js_string = tree.xpath(f'//*[@id="{data_id}"]/text()')[0] - return str(js_string) - - -def get_ui_state(tree: Any) -> StrDict: - data_id = "data-injector-instances" - js_string = tree.xpath(f'//*[@id="{data_id}"]/text()')[0] - big_blob = json.loads(str(js_string)) - ui_state = walk_tree(big_blob, "uiState") - return list_to_dict(ui_state[0]) - - -def get_reservation_data(ui_state: StrDict) -> StrDict: - return { - row["id"]: row for row in ui_state["reservation"]["scheduled_event"]["rows"] - } - - -def get_room_url(tree: Any) -> str | None: - for e in tree.xpath('//a[@data-testid="reservation-destination-link"]'): - href = e.get("href") - assert isinstance(href, str) - if not href.startswith("/room"): - continue - return "https://www.airbnb.co.uk" + href - return None - - -def get_price_from_reservation(reservation: StrDict) -> str: - price = reservation["payment_summary"]["subtitle"] - assert isinstance(price, str) - tc = "Total cost: " - if price.startswith(tc): - price = price[len(tc) :] - assert price[0] == "£" - return price[1:] - - -def extract_booking_from_html(html_file: str) -> StrDict: - """Extract booking information from Airbnb HTML file.""" - - with open(html_file, "r", encoding="utf-8") as f: - text_content = f.read() - - confirmation_match = re.search( - r"/trips/v1/reservation-details/ro/RESERVATION2_CHECKIN/([A-Z0-9]+)", - text_content, - ) - if confirmation_match is None: - raise ValueError("Could not find confirmation code in HTML") - confirmation_code = confirmation_match.group(1) - - tree = lxml.html.parse(html_file) - root = tree.getroot() - try: - ui_state = get_ui_state(tree) - except Exception: - print(html_file) - raise - - reservation = get_reservation_data(ui_state) - m_guests = re.match(r"^(\d+) guests?$", reservation["guests"]["subtitle"]) - if m_guests is None: - raise ValueError("Could not parse number of guests") - number_of_adults = int(m_guests.group(1)) - - price = get_price_from_reservation(reservation) - metadata = ui_state["reservation"]["metadata"] - country_code = metadata["country"].lower() - - title = reservation["dynamic_marquee_title_image_v3"]["title"] - location = title.rpartition(" in ")[2] - - checkin_checkout = reservation["checkin_checkout_arrival_guide"] - check_in_time = checkin_checkout["leading_subtitle"] - check_out_time = checkin_checkout["trailing_subtitle"] - - check_in = build_datetime( - metadata["check_in_date"], check_in_time, metadata["timezone"] - ) - - check_out = build_datetime( - metadata["check_out_date"], check_out_time, metadata["timezone"] - ) - - address = reservation["map"]["address"] if "map" in reservation else None - - if "header_action.pdp" in reservation: - name = reservation["header_action.pdp"]["subtitle"] - else: - name = root.findtext(".//h1") - - booking = { - "type": "apartment", - "operator": "airbnb", - "name": name, - "location": location, - "booking_reference": confirmation_code, - "booking_url": f"https://www.airbnb.co.uk/trips/v1/reservation-details/ro/RESERVATION2_CHECKIN/{confirmation_code}", - "country": country_code, - "latitude": metadata["lat"], - "longitude": metadata["lng"], - "timezone": metadata["timezone"], - "from": check_in, - "to": check_out, - "price": price, - "currency": "GBP", - "number_of_adults": number_of_adults, - } - if address: - booking["address"] = address - - room_url = get_room_url(tree) - if room_url is not None: - booking["url"] = room_url - - return booking - - -def walk_tree(data: Any, want_key: str) -> Any: - """Recursively search for a dict containing 'reservation' and return its value.""" - if isinstance(data, dict): - if want_key in data: - return data[want_key] - for key, value in data.items(): - result = walk_tree(value, want_key) - if result is not None: - return result - elif isinstance(data, list): - for item in data: - result = walk_tree(item, want_key) - if result is not None: - return result - return None - - -def parse_multiple_files(filenames: list[str]) -> list[StrDict]: - """Parse multiple Airbnb HTML files and return a list of booking dictionaries.""" - bookings = [] - for html_file in sorted(filenames): - booking = extract_booking_from_html(html_file) - assert booking - bookings.append(booking) - return bookings diff --git a/agenda/birthday.py b/agenda/birthday.py index 6fdfd21..fdebd5d 100644 --- a/agenda/birthday.py +++ b/agenda/birthday.py @@ -4,7 +4,7 @@ from datetime import date import yaml -from .event import Event +from .types import Event YEAR_NOT_KNOWN = 1900 @@ -42,7 +42,7 @@ def get_birthdays(from_date: date, filepath: str) -> list[Event]: Event( date=bday.replace(year=bday.year + offset), name="birthday", - title=f'{entity["label"]} ({display_age})', + title=f'🎈 {entity["label"]} ({display_age})', ) ) diff --git a/agenda/bristol_waste.py b/agenda/bristol_waste.py deleted file mode 100644 index db8f80f..0000000 --- a/agenda/bristol_waste.py +++ /dev/null @@ -1,131 +0,0 @@ -"""Waste collection schedules.""" - -import json -import os -import typing -from collections import defaultdict -from datetime import date, datetime, timedelta - -import httpx - -from .event import Event -from .utils import make_waste_dir - -ttl_hours = 12 - - -BristolSchedule = list[dict[str, typing.Any]] - - -async def get(start_date: date, data_dir: str, uprn: str, cache: str) -> list[Event]: - """Get waste collection schedule from Bristol City Council.""" - by_date: defaultdict[date, list[str]] = defaultdict(list) - for item in await get_data(data_dir, uprn, cache): - service = get_service(item) - for d in collections(item): - if d < start_date and service not in by_date[d]: - by_date[d].append(service) - - return [ - Event(name="waste_schedule", date=d, title="Bristol: " + ", ".join(services)) - for d, services in by_date.items() - ] - - -async def get_data(data_dir: str, uprn: str, cache: str) -> BristolSchedule: - """Get Bristol Waste schedule, with cache.""" - now = datetime.now() - waste_dir = os.path.join(data_dir, "waste") - - make_waste_dir(data_dir) - - existing_data = os.listdir(waste_dir) - existing = [f for f in existing_data if f.endswith(f"_{uprn}.json")] - if existing: - recent_filename = max(existing) - recent = datetime.strptime(recent_filename, f"%Y-%m-%d_%H:%M_{uprn}.json") - delta = now - recent - - def get_from_recent() -> BristolSchedule: - json_data = json.load(open(os.path.join(waste_dir, recent_filename))) - return typing.cast(BristolSchedule, json_data["data"]) - - if ( - cache != "refresh" - and existing - and (cache == "force" or delta < timedelta(hours=ttl_hours)) - ): - return get_from_recent() - - try: - r = await get_web_data(uprn) - except httpx.ReadTimeout: - return get_from_recent() - - with open(f'{waste_dir}/{now.strftime("%Y-%m-%d_%H:%M")}_{uprn}.json', "wb") as out: - out.write(r.content) - - return typing.cast(BristolSchedule, r.json()["data"]) - - -async def get_web_data(uprn: str) -> httpx.Response: - """Get JSON from Bristol City Council.""" - UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" - HEADERS = { - "Accept": "*/*", - "Accept-Language": "en-GB,en;q=0.9", - "Connection": "keep-alive", - "Ocp-Apim-Subscription-Key": "47ffd667d69c4a858f92fc38dc24b150", - "Ocp-Apim-Trace": "true", - "Origin": "https://bristolcouncil.powerappsportals.com", - "Referer": "https://bristolcouncil.powerappsportals.com/", - "Sec-Fetch-Dest": "empty", - "Sec-Fetch-Mode": "cors", - "Sec-Fetch-Site": "cross-site", - "Sec-GPC": "1", - "User-Agent": UA, - } - - _uprn = str(uprn).zfill(12) - - async with httpx.AsyncClient(timeout=20) as client: - # Initialise form - payload = {"servicetypeid": "7dce896c-b3ba-ea11-a812-000d3a7f1cdc"} - response = await client.get( - "https://bristolcouncil.powerappsportals.com/completedynamicformunauth/", - headers=HEADERS, - params=payload, - ) - - host = "bcprdapidyna002.azure-api.net" - - # Set the search criteria - payload = {"Uprn": "UPRN" + _uprn} - response = await client.post( - f"https://{host}/bcprdfundyna001-llpg/DetailedLLPG", - headers=HEADERS, - json=payload, - ) - - # Retrieve the schedule - payload = {"uprn": _uprn} - response = await client.post( - f"https://{host}/bcprdfundyna001-alloy/NextCollectionDates", - headers=HEADERS, - json=payload, - ) - - return response - - -def get_service(item: dict[str, typing.Any]) -> str: - """Bristol waste service name.""" - service: str = item["containerName"] - return "Recycling" if "Recycling" in service else service.partition(" ")[2] - - -def collections(item: dict[str, typing.Any]) -> typing.Iterable[date]: - """Bristol dates from collections.""" - for collection in item["collection"]: - for collection_date_key in ["nextCollectionDate", "lastCollectionDate"]: - yield date.fromisoformat(collection[collection_date_key][:10]) diff --git a/agenda/build_place_yaml.py b/agenda/build_place_yaml.py deleted file mode 100644 index bc346f4..0000000 --- a/agenda/build_place_yaml.py +++ /dev/null @@ -1,257 +0,0 @@ -"""Build airport and station YAML entries from Wikidata.""" - -import argparse -import typing -from pathlib import Path - -import requests -import yaml - -API_URL = "https://www.wikidata.org/w/api.php" -PERSONAL_DATA_DIR = Path("~/src/personal-data").expanduser() -USER_AGENT = "agenda-build-place-yaml/0.1" - -Entity = dict[str, typing.Any] -Entities = dict[str, Entity] - - -class WikidataClient: - """Small Wikidata API client for place lookups.""" - - def __init__(self) -> None: - """Create a Wikidata API session.""" - self.session = requests.Session() - self.session.headers.update({"User-Agent": USER_AGENT}) - - def get_json(self, params: dict[str, str]) -> dict[str, typing.Any]: - """Fetch JSON from Wikidata.""" - response = self.session.get(API_URL, params=params) - response.raise_for_status() - return typing.cast(dict[str, typing.Any], response.json()) - - def get_alpha2_country_code(self, qid: str) -> str: - """Query the Wikidata API for alpha-2 country code.""" - data = self.get_json( - { - "action": "wbgetclaims", - "entity": qid, - "property": "P297", - "format": "json", - } - ) - p297 = data["claims"]["P297"] - return typing.cast(str, p297[0]["mainsnak"]["datavalue"]["value"]).lower() - - def search_entities(self, query: str) -> Entities: - """Search Wikidata and return detailed entities.""" - search_data = self.get_json( - { - "action": "query", - "list": "search", - "format": "json", - "srsearch": query, - } - ) - search_results = search_data["query"]["search"] - if not search_results: - return {} - - ids = [result["title"] for result in search_results] - entity_data = self.get_json( - { - "action": "wbgetentities", - "format": "json", - "ids": "|".join(ids), - } - ) - return typing.cast(Entities, entity_data["entities"]) - - -def entity_names(entity: Entity) -> set[str]: - """Return labels and aliases for a Wikidata entity.""" - names: set[str] = {lang["value"] for lang in entity.get("labels", {}).values()} - for alias_list in entity.get("aliases", {}).values(): - for alias in alias_list: - names.add(alias["value"]) - return names - - -def exact_station_match(station_name: str, entity: Entity) -> bool: - """Return whether the entity names match the requested station name.""" - names = entity_names(entity) - return station_name in names or f"{station_name} station" in names - - -def entity_claim_value( - entity: Entity, property_id: str, default: typing.Any = None -) -> typing.Any: - """Return the first Wikidata claim value for a property.""" - claims = entity.get("claims", {}) - if property_id not in claims: - return default - return claims[property_id][0]["mainsnak"]["datavalue"]["value"] - - -def build_station_info( - client: WikidataClient, station_name: str, entity_id: str, entity: Entity -) -> dict[str, typing.Any]: - """Build a stations.yaml entry.""" - coords = entity_claim_value(entity, "P625") - country_value = entity_claim_value(entity, "P17") - uic = entity_claim_value(entity, "P722") - uk_station_code = entity_claim_value(entity, "P4755") - - station_info: dict[str, typing.Any] = { - "name": station_name, - "latitude": coords["latitude"], - "longitude": coords["longitude"], - "country": client.get_alpha2_country_code(country_value["id"]), - "wikidata": entity_id, - "routes": {}, - } - - if uic is not None: - station_info["uic"] = uic - if uk_station_code is not None: - station_info["alpha3"] = uk_station_code - - return station_info - - -def search_for_station( - client: WikidataClient, station_name: str -) -> dict[str, typing.Any]: - """Search for a station and return a stations.yaml entry.""" - haswbstatement = "P31=Q55488|P31=Q18543139|P31=Q1147171" - entities = client.search_entities(f"{station_name} haswbstatement:{haswbstatement}") - - for entity_id, entity in entities.items(): - if exact_station_match(station_name, entity): - return build_station_info(client, station_name, entity_id, entity) - - if entities: - entity_id, entity = next(iter(entities.items())) - return build_station_info(client, station_name, entity_id, entity) - - raise ValueError(f"No Wikidata station found for {station_name!r}") - - -def build_airport_info( - client: WikidataClient, iata: str, entity_id: str, entity: Entity -) -> dict[str, typing.Any]: - """Build an airports.yaml entry.""" - label = entity["labels"]["en"]["value"] - claims = entity["claims"] - coords = claims["P625"][0]["mainsnak"]["datavalue"]["value"] - country_qid = claims["P17"][0]["mainsnak"]["datavalue"]["value"]["id"] - - info: dict[str, typing.Any] = { - "iata": iata, - "name": label, - "city": label, - "country": client.get_alpha2_country_code(country_qid), - "latitude": coords["latitude"], - "longitude": coords["longitude"], - "qid": entity_id, - } - - website = entity_claim_value(entity, "P856") - if website is not None: - info["website"] = website - - return info - - -def search_for_airport(client: WikidataClient, iata: str) -> dict[str, typing.Any]: - """Search for an airport by IATA code and return an airports.yaml entry.""" - entities = client.search_entities(f"haswbstatement:P238={iata.upper()}") - if not entities: - raise ValueError(f"No Wikidata airport found for IATA code {iata!r}") - - entity_id, entity = next(iter(entities.items())) - return build_airport_info(client, iata.upper(), entity_id, entity) - - -def load_yaml(path: Path) -> typing.Any: - """Load a YAML file.""" - return yaml.safe_load(path.read_text()) - - -def dump_yaml(data: typing.Any) -> str: - """Dump YAML using the local personal-data style.""" - return yaml.dump(data, sort_keys=False, allow_unicode=True) - - -def dump_yaml_list_with_blank_lines(items: list[dict[str, typing.Any]]) -> str: - """Dump a YAML list with a blank line between top-level items.""" - text = dump_yaml(items).lstrip() - return text.replace("\n- ", "\n\n- ") - - -def upsert_station(data_dir: Path, station_info: dict[str, typing.Any]) -> bool: - """Add or replace a station entry. Return True when an existing entry changed.""" - path = data_dir / "stations.yaml" - stations = typing.cast(list[dict[str, typing.Any]], load_yaml(path)) - - for index, station in enumerate(stations): - if station.get("name") == station_info["name"]: - stations[index] = station_info - path.write_text(dump_yaml_list_with_blank_lines(stations)) - return True - - stations.append(station_info) - path.write_text(dump_yaml_list_with_blank_lines(stations)) - return False - - -def upsert_airport(data_dir: Path, airport_info: dict[str, typing.Any]) -> bool: - """Add or replace an airport entry. Return True when an existing entry changed.""" - path = data_dir / "airports.yaml" - airports = typing.cast(dict[str, dict[str, typing.Any]], load_yaml(path)) - iata = typing.cast(str, airport_info["iata"]) - existed = iata in airports - airports[iata] = airport_info - path.write_text(dump_yaml(airports)) - return existed - - -def station_main(argv: list[str] | None = None) -> int: - """CLI entrypoint for building and importing a station YAML entry.""" - parser = argparse.ArgumentParser( - description="Add or update a station in personal-data/stations.yaml." - ) - parser.add_argument("station_name") - parser.add_argument("--data-dir", default=str(PERSONAL_DATA_DIR)) - parser.add_argument("--print-only", action="store_true") - args = parser.parse_args(argv) - - station_info = search_for_station(WikidataClient(), args.station_name) - if args.print_only: - print(dump_yaml_list_with_blank_lines([station_info]).strip()) - return 0 - - replaced = upsert_station(Path(args.data_dir), station_info) - action = "Updated" if replaced else "Added" - print(f"{action} station {station_info['name']} in {args.data_dir}/stations.yaml") - return 0 - - -def airport_main(argv: list[str] | None = None) -> int: - """CLI entrypoint for building and importing an airport YAML entry.""" - parser = argparse.ArgumentParser( - description="Add or update an airport in personal-data/airports.yaml." - ) - parser.add_argument("iata") - parser.add_argument("--data-dir", default=str(PERSONAL_DATA_DIR)) - parser.add_argument("--print-only", action="store_true") - args = parser.parse_args(argv) - - airport_info = search_for_airport(WikidataClient(), args.iata) - if args.print_only: - print(dump_yaml({airport_info["iata"]: airport_info}).strip()) - return 0 - - replaced = upsert_airport(Path(args.data_dir), airport_info) - action = "Updated" if replaced else "Added" - print(f"{action} airport {airport_info['iata']} in {args.data_dir}/airports.yaml") - return 0 diff --git a/agenda/busy.py b/agenda/busy.py deleted file mode 100644 index 7dc744c..0000000 --- a/agenda/busy.py +++ /dev/null @@ -1,668 +0,0 @@ -"""Identify busy events and gaps when nothing is scheduled.""" - -import itertools -import typing -from datetime import date, datetime, timedelta, timezone - -import flask -import pycountry - -from . import events_yaml, get_country, travel -from .event import Event -from .types import StrDict, Trip - - -def busy_event(e: Event) -> bool: - """Busy.""" - if e.name not in { - "event", - "accommodation", - "conference", - "transport", - "meetup", - "party", - "trip", - "hackathon", - }: - return False - - if e.title in ("IA UK board meeting", "Mill Road Winter Fair"): - return False - - if e.name == "conference" and not e.going: - return False - if not e.title: - return True - if e.title == "LHG Run Club" or "Third Thursday Social" in e.title: - return False - - lc_title = e.title.lower() - return ( - "rebels" not in lc_title - and "south west data social" not in lc_title - and "dorkbot" not in lc_title - ) - - -def get_busy_events( - start: date, config: flask.config.Config, trips: list[Trip] -) -> list[Event]: - """Find busy events from a year ago to two years in the future.""" - last_year = start - timedelta(days=365) - next_year = start + timedelta(days=2 * 365) - - my_data = config["PERSONAL_DATA"] - events = events_yaml.read(my_data, last_year, next_year, skip_trips=True) - - for trip in trips: - event_type = "trip" - if trip.events and not trip.conferences: - event_type = trip.events[0]["name"] - elif len(trip.conferences) == 1 and trip.conferences[0].get("hackathon"): - event_type = "hackathon" - events.append( - Event( - name=event_type, - title=trip.title + " " + trip.country_flags, - date=trip.start, - end_date=trip.end, - url=flask.url_for("trip_page", start=trip.start.isoformat()), - ) - ) - - busy_events = [ - e - for e in sorted(events, key=lambda e: e.as_date) - if (e.as_date >= start or (e.end_date and e.end_as_date >= start)) - and e.as_date < next_year - and busy_event(e) - ] - - return busy_events - - -def _parse_datetime_field(datetime_obj: datetime | date | str) -> tuple[datetime, date]: - """Parse a datetime field that could be datetime object or string.""" - if isinstance(datetime_obj, datetime): - dt = datetime_obj - elif isinstance(datetime_obj, date): - dt = datetime.combine(datetime_obj, datetime.min.time(), tzinfo=timezone.utc) - elif isinstance(datetime_obj, str): - dt = datetime.fromisoformat(datetime_obj.replace("Z", "+00:00")) - else: - raise ValueError(f"Invalid datetime format: {datetime_obj}") - - if dt.tzinfo is None: - dt = dt.replace(tzinfo=timezone.utc) - else: - dt = dt.astimezone(timezone.utc) - - return dt, dt.date() - - -def _get_accommodation_location( - acc: StrDict, on_trip: bool = False -) -> tuple[str | None, pycountry.db.Country]: - """Get location from accommodation data.""" - c = get_country(acc["country"]) - assert c - assert isinstance(acc["location"], str) - return (acc["location"] if on_trip else None, c) - - -def _find_most_recent_travel_within_trip( - trip: Trip, - target_date: date, -) -> tuple[str | None, pycountry.db.Country | None] | None: - """Find the most recent travel location within a trip.""" - uk_airports = {"LHR", "LGW", "STN", "LTN", "BRS", "BHX", "MAN", "EDI", "GLA"} - - trip_most_recent_date: date | None = None - trip_most_recent_location: tuple[str | None, pycountry.db.Country | None] | None = ( - None - ) - trip_most_recent_datetime: datetime | None = None - - # Check flights within trip period - for travel_item in trip.travel: - if travel_item["type"] == "flight" and "arrive" in travel_item: - arrive_datetime, arrive_date = _parse_datetime_field(travel_item["arrive"]) - - # Only consider flights within this trip and before target date - if not (trip.start <= arrive_date <= target_date): - continue - # Compare both date and time to handle same-day flights correctly - if ( - trip_most_recent_date is None - or arrive_date > trip_most_recent_date - or ( - arrive_date == trip_most_recent_date - and ( - trip_most_recent_datetime is None - or arrive_datetime > trip_most_recent_datetime - ) - ) - ): - trip_most_recent_date = arrive_date - trip_most_recent_datetime = arrive_datetime - destination_airport = travel_item["to"] - assert "to_airport" in travel_item - airport_info = travel_item["to_airport"] - airport_country = airport_info["country"] - if airport_country == "gb": - if destination_airport in uk_airports: - # UK airport while on trip - show actual location - location_name = airport_info.get( - "city", airport_info.get("name", "London") - ) - trip_most_recent_location = ( - location_name, - get_country("gb"), - ) - else: - trip_most_recent_location = (None, get_country("gb")) - else: - location_name = airport_info.get( - "city", airport_info.get("name", destination_airport) - ) - trip_most_recent_location = ( - location_name, - get_country(airport_country), - ) - - # Check accommodations within trip period - for acc in trip.accommodation: - if "from" in acc: - try: - _, acc_date = _parse_datetime_field(acc["from"]) - except ValueError: - continue - - # Only consider accommodations within this trip and before/on target date - if trip.start <= acc_date <= target_date: - # Accommodation takes precedence over flights on the same date - # or if it's genuinely more recent - if ( - trip_most_recent_date is None - or acc_date > trip_most_recent_date - or acc_date == trip_most_recent_date - ): - trip_most_recent_date = acc_date - trip_most_recent_location = _get_accommodation_location( - acc, on_trip=True - ) - - # Check trains within trip period - for travel_item in trip.travel: - if travel_item["type"] == "train": - for leg in travel_item.get("legs", []): - if "arrive" in leg: - try: - arrive_datetime, arrive_date = _parse_datetime_field( - leg["arrive"] - ) - except ValueError: - continue - - # Only consider trains within this trip and before target date - if trip.start <= arrive_date <= target_date: - # Compare both date and time to handle same-day arrivals correctly - if ( - trip_most_recent_date is None - or arrive_date > trip_most_recent_date - or ( - arrive_date == trip_most_recent_date - and ( - trip_most_recent_datetime is None - or arrive_datetime > trip_most_recent_datetime - ) - ) - ): - trip_most_recent_date = arrive_date - trip_most_recent_datetime = arrive_datetime - # For trains, we can get station info from to_station if available - destination = leg.get("to") - assert "to_station" in leg - station_info = leg["to_station"] - station_country = station_info["country"] - if station_country == "gb": - trip_most_recent_location = ( - destination, - get_country("gb"), - ) - else: - trip_most_recent_location = ( - destination, - get_country(station_country), - ) - - # Check ferries within trip period - for travel_item in trip.travel: - if travel_item["type"] == "ferry" and "arrive" in travel_item: - try: - arrive_datetime, arrive_date = _parse_datetime_field( - travel_item["arrive"] - ) - except ValueError: - continue - - # Only consider ferries within this trip and before target date - if trip.start <= arrive_date <= target_date: - # Compare both date and time to handle same-day arrivals correctly - if ( - trip_most_recent_date is None - or arrive_date > trip_most_recent_date - or ( - arrive_date == trip_most_recent_date - and ( - trip_most_recent_datetime is None - or arrive_datetime > trip_most_recent_datetime - ) - ) - ): - trip_most_recent_date = arrive_date - trip_most_recent_datetime = arrive_datetime - # For ferries, we can get terminal info from to_terminal if available - destination = travel_item.get("to") - assert "to_terminal" in travel_item - terminal_info = travel_item["to_terminal"] - terminal_country = terminal_info.get("country", "gb") - terminal_city = terminal_info.get("city", destination) - if terminal_country == "gb": - trip_most_recent_location = ( - terminal_city, - get_country("gb"), - ) - else: - trip_most_recent_location = ( - terminal_city, - get_country(terminal_country), - ) - - return trip_most_recent_location - - -def _get_trip_location_by_progression( - trip: Trip, target_date: date -) -> tuple[str | None, pycountry.db.Country | None] | None: - """Determine location based on trip progression and date.""" - locations = trip.locations() - if not locations: - return None - - # If only one location, use it (when on a trip, always show the location) - if len(locations) == 1: - city, country = locations[0] - return (city, country) - - # Multiple locations: use progression through the trip - if not trip.end: - city, country = locations[-1] - return (city, country) - trip_duration = (trip.end - trip.start).days + 1 - days_into_trip = (target_date - trip.start).days - - # Simple progression: first half at first location, second half at last location - if days_into_trip <= trip_duration // 2: - city, country = locations[0] - else: - city, country = locations[-1] - - return (city, country) - - -def _find_most_recent_travel_before_date( - target_date: date, - trips: list[Trip], -) -> tuple[str | None, pycountry.db.Country | None] | None: - """Find the most recent travel location before a given date.""" - uk_airports = {"LHR", "LGW", "STN", "LTN", "BRS", "BHX", "MAN", "EDI", "GLA"} - - most_recent_location: tuple[str | None, pycountry.db.Country | None] | None = None - most_recent_date: date | None = None - most_recent_datetime: datetime | None = None - - # Check all travel across all trips - for trip in trips: - # Check flights - for travel_item in trip.travel: - if travel_item["type"] == "flight" and "arrive" in travel_item: - try: - arrive_datetime, arrive_date = _parse_datetime_field( - travel_item["arrive"] - ) - except ValueError: - continue - - if arrive_date <= target_date: - # Compare both date and time to handle same-day flights correctly - if ( - most_recent_date is None - or arrive_date > most_recent_date - or ( - arrive_date == most_recent_date - and ( - most_recent_datetime is None - or arrive_datetime > most_recent_datetime - ) - ) - ): - most_recent_date = arrive_date - most_recent_datetime = arrive_datetime - destination_airport = travel_item["to"] - # For flights, determine if we're "on trip" based on whether this is within any trip period - on_trip = any( - t.start <= arrive_date <= (t.end or t.start) for t in trips - ) - - if "to_airport" in travel_item: - airport_info = travel_item["to_airport"] - airport_country = airport_info.get("country", "gb") - if airport_country == "gb": - if not on_trip: - # When not on a trip, UK airports mean home - most_recent_location = (None, get_country("gb")) - else: - # When on a trip, show the actual location even for UK airports - location_name = airport_info.get( - "city", airport_info.get("name", "London") - ) - most_recent_location = ( - location_name, - get_country("gb"), - ) - else: - location_name = airport_info.get( - "city", - airport_info.get("name", destination_airport), - ) - most_recent_location = ( - location_name, - get_country(airport_country), - ) - else: - most_recent_location = ( - destination_airport, - get_country("gb"), - ) - - # Check trains - elif travel_item["type"] == "train": - for leg in travel_item.get("legs", []): - if "arrive" in leg: - try: - arrive_datetime, arrive_date = _parse_datetime_field( - leg["arrive"] - ) - except ValueError: - continue - - if arrive_date <= target_date: - # Compare both date and time to handle same-day arrivals correctly - if ( - most_recent_date is None - or arrive_date > most_recent_date - or ( - arrive_date == most_recent_date - and ( - most_recent_datetime is None - or arrive_datetime > most_recent_datetime - ) - ) - ): - most_recent_date = arrive_date - most_recent_datetime = arrive_datetime - destination = leg.get("to") - on_trip = any( - t.start <= arrive_date <= (t.end or t.start) - for t in trips - ) - - if "to_station" in leg: - station_info = leg["to_station"] - station_country = station_info.get("country", "gb") - if station_country == "gb": - if not on_trip: - most_recent_location = ( - None, - get_country("gb"), - ) - else: - most_recent_location = ( - destination, - get_country("gb"), - ) - else: - most_recent_location = ( - destination, - get_country(station_country), - ) - else: - most_recent_location = ( - destination, - get_country("gb"), - ) - - # Check ferries - elif travel_item["type"] == "ferry" and "arrive" in travel_item: - try: - arrive_datetime, arrive_date = _parse_datetime_field( - travel_item["arrive"] - ) - except ValueError: - continue - - if arrive_date <= target_date: - # Compare both date and time to handle same-day arrivals correctly - if ( - most_recent_date is None - or arrive_date > most_recent_date - or ( - arrive_date == most_recent_date - and ( - most_recent_datetime is None - or arrive_datetime > most_recent_datetime - ) - ) - ): - most_recent_date = arrive_date - most_recent_datetime = arrive_datetime - destination = travel_item.get("to") - on_trip = any( - t.start <= arrive_date <= (t.end or t.start) for t in trips - ) - - if "to_terminal" in travel_item: - terminal_info = travel_item["to_terminal"] - terminal_country = terminal_info.get("country", "gb") - terminal_city = terminal_info.get("city", destination) - if terminal_country == "gb": - if not on_trip: - most_recent_location = (None, get_country("gb")) - else: - most_recent_location = ( - terminal_city, - get_country("gb"), - ) - else: - most_recent_location = ( - terminal_city, - get_country(terminal_country), - ) - else: - most_recent_location = (destination, get_country("gb")) - - # Check accommodation - only override if accommodation is more recent - for acc in trip.accommodation: - if "from" in acc: - try: - _, acc_date = _parse_datetime_field(acc["from"]) - except ValueError: - continue - - if acc_date <= target_date: - # Only update if this accommodation is more recent than existing result - if most_recent_date is None or acc_date > most_recent_date: - most_recent_date = acc_date - on_trip = any( - t.start <= acc_date <= (t.end or t.start) for t in trips - ) - most_recent_location = _get_accommodation_location( - acc, on_trip=on_trip - ) - - return most_recent_location - - -def _check_return_home_heuristic( - target_date: date, trips: list[Trip] -) -> tuple[str | None, pycountry.db.Country | None] | None: - """Check if should return home based on recent trips that have ended.""" - for trip in trips: - if trip.end and trip.end < target_date: - locations = trip.locations() - if locations: - final_city, final_country = locations[-1] - final_alpha_2 = final_country.alpha_2 - days_since_trip = (target_date - trip.end).days - - # If trip ended in UK, you should be home now - if hasattr(final_country, "alpha_2") and final_country.alpha_2 == "GB": - return (None, get_country("gb")) - - return None - - -def get_location_for_date( - target_date: date, - trips: list[Trip], -) -> tuple[str | None, pycountry.db.Country | None]: - """Get location (city, country) for a specific date using travel history.""" - # First check if currently on a trip - for trip in trips: - if not (trip.start <= target_date <= (trip.end or trip.start)): - continue - # For trips, find the most recent travel within the trip period - trip_location = _find_most_recent_travel_within_trip( - trip, - target_date, - ) - if trip_location: - return trip_location - - # Fallback: determine location based on trip progression and date - progression_location = _get_trip_location_by_progression(trip, target_date) - if progression_location: - return progression_location - - # Find most recent travel before this date - recent_travel = _find_most_recent_travel_before_date(target_date, trips) - - # Check for recent trips that have ended - prioritize this over individual travel data - # This handles cases where you're traveling home after a trip (e.g. stopovers, connections) - return_home = _check_return_home_heuristic(target_date, trips) - if return_home: - return return_home - - # Return most recent location or default to home - if recent_travel: - return recent_travel - - return (None, get_country("gb")) - - -def weekends( - start: date, busy_events: list[Event], trips: list[Trip], data_dir: str -) -> typing.Sequence[StrDict]: - """Next ten weekends.""" - weekday = start.weekday() - - # Calculate the difference to the next or previous Saturday - if weekday == 6: # Sunday - start_date = start - timedelta(days=1) - else: - start_date = start + timedelta(days=(5 - weekday)) - - weekends_info = [] - for i in range(52): - saturday = start_date + timedelta(weeks=i) - sunday = saturday + timedelta(days=1) - - saturday_events = [ - event - for event in busy_events - if event.end_date and event.as_date <= saturday <= event.end_as_date - ] - sunday_events = [ - event - for event in busy_events - if event.end_date and event.as_date <= sunday <= event.end_as_date - ] - - saturday_location = get_location_for_date( - saturday, - trips, - ) - sunday_location = get_location_for_date( - sunday, - trips, - ) - - weekends_info.append( - { - "date": saturday, - "saturday": saturday_events, - "sunday": sunday_events, - "saturday_location": saturday_location, - "sunday_location": sunday_location, - } - ) - - return weekends_info - - -def find_gaps(events: list[Event], min_gap_days: int = 3) -> list[StrDict]: - """Gaps of at least `min_gap_days` between events in a list of events.""" - # Sort events by start date - - gaps: list[tuple[date, date]] = [] - previous_event_end = None - - by_start_date = { - d: list(on_day) - for d, on_day in itertools.groupby(events, key=lambda e: e.as_date) - } - - by_end_date = { - d: list(on_day) - for d, on_day in itertools.groupby(events, key=lambda e: e.end_as_date) - } - - for event in events: - # Use start date for current event - start_date = event.as_date - - # If previous event exists, calculate the gap - if previous_event_end: - gap_days = (start_date - previous_event_end).days - if gap_days >= (min_gap_days + 2): - start_end = ( - previous_event_end + timedelta(days=1), - start_date - timedelta(days=1), - ) - gaps.append(start_end) - - # Update previous event end date - end = event.end_as_date - if not previous_event_end or end > previous_event_end: - previous_event_end = end - - return [ - { - "start": gap_start, - "end": gap_end, - "after": by_start_date[gap_end + timedelta(days=1)], - "before": by_end_date[gap_start - timedelta(days=1)], - } - for gap_start, gap_end in gaps - ] diff --git a/agenda/calendar.py b/agenda/calendar.py index 0a66b6c..4d2a8d8 100644 --- a/agenda/calendar.py +++ b/agenda/calendar.py @@ -1,115 +1,79 @@ """Calendar.""" import typing -import uuid from datetime import timedelta -from .event import Event +from .types import Event -# A map to associate event types with a specific calendar ID -event_type_calendar_map = { - "bank_holiday": "uk_holidays", - "conference": "conferences", - "us_holiday": "us_holidays", - "birthday": "birthdays", - "waste_schedule": "home", - "accommodation": "travel", - "market": "markets", +event_type_color_map = { + "bank_holiday": "success-subtle", + "conference": "primary-subtle", + "us_holiday": "secondary-subtle", + "birthday": "info-subtle", + "waste_schedule": "danger-subtle", } -# Define the calendars (categories) for TOAST UI -# These will be passed to the frontend to configure colors and names -toastui_calendars = [ - {"id": "default", "name": "General", "backgroundColor": "#00a9ff"}, - {"id": "uk_holidays", "name": "UK Bank Holiday", "backgroundColor": "#28a745"}, - {"id": "us_holidays", "name": "US Holiday", "backgroundColor": "#6c757d"}, - {"id": "conferences", "name": "Conference", "backgroundColor": "#007bff"}, - {"id": "birthdays", "name": "Birthday", "backgroundColor": "#17a2b8"}, - {"id": "home", "name": "Home", "backgroundColor": "#dc3545"}, - {"id": "travel", "name": "Travel", "backgroundColor": "#ffc107", "color": "#000"}, - {"id": "markets", "name": "Markets", "backgroundColor": "#e2e3e5", "color": "#000"}, -] +colors = { + "primary-subtle": "#cfe2ff", + "secondary-subtle": "#e2e3e5", + "success-subtle": "#d1e7dd", + "info-subtle": "#cff4fc", + "warning-subtle": "#fff3cd", + "danger-subtle": "#f8d7da", +} -def build_toastui_events(events: list[Event]) -> list[dict[str, typing.Any]]: - """Build a list of event objects for TOAST UI Calendar.""" +def build_events(events: list[Event]) -> list[dict[str, typing.Any]]: + """Build list of events for FullCalendar.""" items: list[dict[str, typing.Any]] = [] + one_day = timedelta(days=1) for e in events: if e.name == "today": continue - - # Determine the calendar ID for the event, defaulting if not mapped - calendar_id = event_type_calendar_map.get(e.name, "default") - - # Handle special case for 'accommodation' if e.name == "accommodation": assert e.title and e.end_date - # All-day event for the duration of the stay - items.append( - { - "id": str(uuid.uuid4()), - "calendarId": calendar_id, - "title": e.title_with_emoji, - "start": e.as_date.isoformat(), - "end": (e.end_as_date + one_day).isoformat(), - "isAllday": True, - "raw": {"url": e.url}, - } - ) - # Timed event for check-in - items.append( - { - "id": str(uuid.uuid4()), - "calendarId": calendar_id, - "title": f"Check-in: {e.title}", - "start": e.date.isoformat(), - "end": (e.date + timedelta(hours=1)).isoformat(), - "isAllday": False, - "raw": {"url": e.url}, - } - ) - # Timed event for check-out - items.append( - { - "id": str(uuid.uuid4()), - "calendarId": calendar_id, - "title": f"Checkout: {e.title}", - "start": e.end_date.isoformat(), - "end": (e.end_date + timedelta(hours=1)).isoformat(), - "isAllday": False, - "raw": {"url": e.url}, - } - ) + item = { + "allDay": True, + "title": e.display_title, + "start": e.as_date.isoformat(), + "end": (e.end_as_date + one_day).isoformat(), + "url": e.url, + } + items.append(item) + + item = { + "allDay": False, + "title": "checkin: " + e.title, + "start": e.date.isoformat(), + "url": e.url, + } + items.append(item) + item = { + "allDay": False, + "title": "checkout: " + e.title, + "start": e.end_date.isoformat(), + "url": e.url, + } + items.append(item) + continue - # Handle all other events - start_iso = e.date.isoformat() if e.has_time: - end_iso = (e.end_date or e.date + timedelta(minutes=30)).isoformat() + end = e.end_date or e.date + timedelta(hours=1) else: - # For all-day events, the end date is exclusive. - # For a single-day event, the end date should be the same as the start date. - # For a multi-day event, a day is added to make the range inclusive. - if e.end_date: - # This is a multi-day event, so add one day to the end. - end_date = e.end_as_date + one_day - else: - # This is a single-day event. The end date is the same as the start date. - end_date = e.as_date - end_iso = end_date.isoformat() - - item: dict[str, typing.Any] = { - "id": str(uuid.uuid4()), - "calendarId": calendar_id, - "title": e.title_with_emoji, - "start": start_iso, - "end": end_iso, - "isAllday": not e.has_time, + end = (e.end_as_date if e.end_date else e.as_date) + one_day + item = { + "allDay": not e.has_time, + "title": e.display_title, + "start": e.date.isoformat(), + "end": end.isoformat(), } + if e.name in event_type_color_map: + item["color"] = colors[event_type_color_map[e.name]] + item["textColor"] = "black" if e.url: - item["raw"] = {"url": e.url} + item["url"] = e.url items.append(item) - return items diff --git a/agenda/car_journey_timeline.py b/agenda/car_journey_timeline.py deleted file mode 100644 index d3518ee..0000000 --- a/agenda/car_journey_timeline.py +++ /dev/null @@ -1,474 +0,0 @@ -"""Import recorded car journeys from a Google Timeline export.""" - -from __future__ import annotations - -import argparse -import json -import typing -from collections import defaultdict -from dataclasses import dataclass -from datetime import date, datetime, timedelta -from pathlib import Path - -from geopy.distance import geodesic # type: ignore - -from . import car_journey_yaml, trip as trip_module, utils - -PERSONAL_DATA_DIR = Path("~/src/personal-data").expanduser() -VEHICLE_ACTIVITY_TYPES = frozenset({"IN_PASSENGER_VEHICLE", "IN_VEHICLE"}) -ACTIVITY_SPLIT_GAP = timedelta(hours=2) - -LatLon = tuple[float, float] -LonLat = tuple[float, float] -StrDict = dict[str, typing.Any] - - -@dataclass(frozen=True) -class TimelinePoint: - """A single Google Timeline breadcrumb point.""" - - time: datetime - location: LatLon - - @property - def lonlat(self) -> LonLat: - """Return point as GeoJSON lon/lat.""" - return (self.location[1], self.location[0]) - - -@dataclass(frozen=True) -class VehicleActivity: - """A vehicle activity segment plus its breadcrumb points.""" - - index: int - mode: str - start_time: datetime - end_time: datetime - start: LatLon - end: LatLon - points: tuple[TimelinePoint, ...] - - @property - def path(self) -> tuple[LatLon, ...]: - """Return a deduplicated path.""" - coords = [point.location for point in self.points] - if not coords: - coords = [self.start, self.end] - elif coords[0] != self.start: - coords.insert(0, self.start) - if coords[-1] != self.end: - coords.append(self.end) - return tuple(dedupe_consecutive(coords)) - - @property - def depart_time(self) -> datetime: - """Prefer the first breadcrumb time when available.""" - return self.points[0].time if self.points else self.start_time - - @property - def arrive_time(self) -> datetime: - """Prefer the last breadcrumb time when available.""" - return self.points[-1].time if self.points else self.end_time - - @property - def leading_path(self) -> tuple[LatLon, ...]: - """Return the first few coordinates for start matching.""" - return self.path[: min(3, len(self.path))] - - @property - def trailing_path(self) -> tuple[LatLon, ...]: - """Return the last few coordinates for end matching.""" - return self.path[-min(3, len(self.path)) :] - - -@dataclass(frozen=True) -class TimelineCarJourney: - """A YAML-ready car journey row with a GeoJSON path.""" - - trip: date - depart: datetime - arrive: datetime - route: str - from_label: str - to_label: str - path: tuple[LatLon, ...] - mode: str - - def as_yaml_item(self) -> StrDict: - """Return the YAML mapping.""" - return { - "trip": self.trip, - "depart": self.depart, - "arrive": self.arrive, - "route": self.route, - "from": self.from_label, - "to": self.to_label, - } - - def as_geojson(self) -> StrDict: - """Return a GeoJSON feature for this journey.""" - return { - "type": "Feature", - "properties": { - "source": "google_timeline", - "mode": self.mode, - "start_time": self.depart.isoformat(), - "end_time": self.arrive.isoformat(), - }, - "geometry": { - "type": "LineString", - "coordinates": [[lon, lat] for lat, lon in self.path], - }, - } - - -def parse_latlng(value: str) -> LatLon: - """Parse Google's ``lat°, lon°`` format.""" - latitude, longitude = value.replace("°", "").split(",", 1) - return (float(latitude.strip()), float(longitude.strip())) - - -def days_spanned(start: datetime, end: datetime) -> list[date]: - """Return all dates touched by a datetime range.""" - day = start.date() - days = [day] - while day < end.date(): - day += timedelta(days=1) - days.append(day) - return days - - -def dedupe_consecutive(coords: list[LatLon]) -> list[LatLon]: - """Drop repeated adjacent coordinates.""" - deduped: list[LatLon] = [] - for coord in coords: - if deduped and deduped[-1] == coord: - continue - deduped.append(coord) - return deduped - - -def load_timeline_path_points(data: StrDict) -> dict[date, list[TimelinePoint]]: - """Group all timelinePath points by local date.""" - points_by_day: dict[date, list[TimelinePoint]] = defaultdict(list) - for segment in data.get("semanticSegments", []): - timeline_path = segment.get("timelinePath") - if not isinstance(timeline_path, list): - continue - for item in timeline_path: - if not isinstance(item, dict): - continue - point = item.get("point") - time_value = item.get("time") - if not isinstance(point, str) or not isinstance(time_value, str): - continue - breadcrumb = TimelinePoint( - time=datetime.fromisoformat(time_value), - location=parse_latlng(point), - ) - points_by_day[breadcrumb.time.date()].append(breadcrumb) - for breadcrumbs in points_by_day.values(): - breadcrumbs.sort(key=lambda item: item.time) - return points_by_day - - -def activity_points( - start: datetime, - end: datetime, - points_by_day: dict[date, list[TimelinePoint]], -) -> tuple[TimelinePoint, ...]: - """Return breadcrumb points within an activity window.""" - selected: list[TimelinePoint] = [] - for day in days_spanned(start, end): - for point in points_by_day.get(day, []): - if start <= point.time <= end: - selected.append(point) - selected.sort(key=lambda item: item.time) - return tuple(selected) - - -def load_vehicle_activities(timeline_path: Path) -> list[VehicleActivity]: - """Load vehicle activities and attach breadcrumb points.""" - data = typing.cast(StrDict, json.loads(timeline_path.read_text())) - points_by_day = load_timeline_path_points(data) - activities: list[VehicleActivity] = [] - next_index = 0 - for index, segment in enumerate(data.get("semanticSegments", [])): - activity = segment.get("activity") - if not isinstance(activity, dict): - continue - mode = activity.get("topCandidate", {}).get("type") - start_location = activity.get("start", {}).get("latLng") - end_location = activity.get("end", {}).get("latLng") - start_time = segment.get("startTime") - end_time = segment.get("endTime") - if ( - mode not in VEHICLE_ACTIVITY_TYPES - or not isinstance(start_location, str) - or not isinstance(end_location, str) - or not isinstance(start_time, str) - or not isinstance(end_time, str) - ): - continue - - depart = datetime.fromisoformat(start_time) - arrive = datetime.fromisoformat(end_time) - start_latlon = parse_latlng(start_location) - end_latlon = parse_latlng(end_location) - activity = VehicleActivity( - index=index, - mode=typing.cast(str, mode), - start_time=depart, - end_time=arrive, - start=start_latlon, - end=end_latlon, - points=activity_points( - depart, - arrive, - points_by_day, - ), - ) - for split_activity in split_activity_on_gaps(activity): - activities.append( - VehicleActivity( - index=next_index, - mode=split_activity.mode, - start_time=split_activity.start_time, - end_time=split_activity.end_time, - start=split_activity.start, - end=split_activity.end, - points=split_activity.points, - ) - ) - next_index += 1 - return activities - - -def activity_matches_date(activity: VehicleActivity, target: date) -> bool: - """Return whether an activity overlaps a date.""" - return target in days_spanned(activity.start_time, activity.end_time) - - -def split_activity_on_gaps(activity: VehicleActivity) -> list[VehicleActivity]: - """Split an activity when breadcrumbs contain long pauses.""" - if len(activity.points) < 2: - return [activity] - - chunks: list[list[TimelinePoint]] = [[]] - for point in activity.points: - current = chunks[-1] - if current and point.time - current[-1].time > ACTIVITY_SPLIT_GAP: - chunks.append([]) - current = chunks[-1] - current.append(point) - - if len(chunks) == 1: - return [activity] - - split: list[VehicleActivity] = [] - for chunk in chunks: - split.append( - VehicleActivity( - index=activity.index, - mode=activity.mode, - start_time=chunk[0].time, - end_time=chunk[-1].time, - start=chunk[0].location, - end=chunk[-1].location, - points=tuple(chunk), - ) - ) - return split - - -def distance_to_coords_km(coords: tuple[LatLon, ...], target: LatLon) -> float: - """Return the closest coordinate distance to a target coordinate.""" - return min(float(geodesic(coord, target).km) for coord in coords) - - -def select_activity_slice( - activities: list[VehicleActivity], - start_target: LatLon, - end_target: LatLon, -) -> list[VehicleActivity]: - """Select the contiguous activity slice that best matches a planned leg.""" - if not activities: - raise ValueError("no vehicle activities available for route matching") - - best_slice: tuple[float, int, int] | None = None - for start_index, activity in enumerate(activities): - start_distance = distance_to_coords_km(activity.leading_path, start_target) - for end_index in range(start_index, len(activities)): - end_distance = distance_to_coords_km( - activities[end_index].trailing_path, - end_target, - ) - score = start_distance + end_distance - if ( - best_slice is None - or score < best_slice[0] - or ( - score == best_slice[0] - and end_index - start_index < best_slice[2] - best_slice[1] - ) - ): - best_slice = (score, start_index, end_index) - - assert best_slice is not None - return activities[best_slice[1] : best_slice[2] + 1] - - -def stop_labels( - from_label: str, - to_label: str, - count: int, -) -> list[tuple[str, str]]: - """Return start/end labels for a split route.""" - if count == 1: - return [(from_label, to_label)] - labels: list[tuple[str, str]] = [] - for index in range(count): - start = from_label if index == 0 else f"Drive stop {index}" - end = to_label if index == count - 1 else f"Drive stop {index + 1}" - labels.append((start, end)) - return labels - - -def route_name(activity: VehicleActivity) -> str: - """Return a stable route filename stem for an imported activity.""" - return "timeline_" + activity.depart_time.strftime("%Y%m%d_%H%M%S") - - -def build_timeline_journeys( - timeline_path: Path, - data_dir: Path, - today: date | None = None, -) -> list[TimelineCarJourney]: - """Generate split past car journeys from timeline data.""" - if today is None: - today = date.today() - - activities = load_vehicle_activities(timeline_path) - existing = car_journey_yaml.load_yaml_list(data_dir / "car_journeys.yaml") - generated: list[TimelineCarJourney] = [] - - for item in existing: - trip_date = utils.as_date(item["trip"]) - if trip_date >= today: - continue - - route = item.get("route") - if not isinstance(route, str): - raise ValueError(f"car journey route must be a string: {item!r}") - - route_path = car_journey_yaml.car_route_path(data_dir, route) - existing_geojson = car_journey_yaml.read_geojson(route_path) - endpoints = trip_module.geojson_route_endpoints(existing_geojson) - if endpoints is None: - raise ValueError(f"could not read route endpoints from {route_path}") - - from_label, to_label = trip_module.car_route_labels(item) - if not isinstance(from_label, str) or not isinstance(to_label, str): - raise ValueError(f"could not determine car route labels for {item!r}") - - journey_date = utils.as_date(item["depart"]) - matches = [ - activity - for activity in activities - if activity_matches_date(activity, journey_date) - ] - selected = select_activity_slice(matches, endpoints[0], endpoints[1]) - labels = stop_labels(from_label, to_label, len(selected)) - for activity, (start_label, end_label) in zip(selected, labels, strict=True): - generated.append( - TimelineCarJourney( - trip=trip_date, - depart=activity.depart_time, - arrive=activity.arrive_time, - route=route_name(activity), - from_label=start_label, - to_label=end_label, - path=activity.path, - mode=activity.mode, - ) - ) - - return generated - - -def write_timeline_routes( - data_dir: Path, - journeys: list[TimelineCarJourney], -) -> int: - """Write GeoJSON files for generated journeys.""" - route_dir = data_dir / "car_routes" - route_dir.mkdir(parents=True, exist_ok=True) - written = 0 - for journey in journeys: - path = route_dir / f"{journey.route}.geojson" - path.write_text(json.dumps(journey.as_geojson(), separators=(",", ":")) + "\n") - written += 1 - return written - - -def rewrite_car_journeys_yaml( - data_dir: Path, - journeys: list[TimelineCarJourney], - today: date | None = None, -) -> tuple[int, int]: - """Replace past YAML rows with timeline-derived journeys.""" - if today is None: - today = date.today() - - path = data_dir / "car_journeys.yaml" - existing = car_journey_yaml.load_yaml_list(path) - future_rows = [item for item in existing if utils.as_date(item["trip"]) >= today] - generated_rows = [journey.as_yaml_item() for journey in journeys] - combined = generated_rows + future_rows - combined.sort(key=car_journey_yaml.journey_key) - path.write_text(car_journey_yaml.dump_yaml_list_with_blank_lines(combined)) - replaced = len(existing) - len(future_rows) - return (replaced, len(generated_rows)) - - -def import_from_timeline( - timeline_path: Path, - data_dir: Path = PERSONAL_DATA_DIR, - today: date | None = None, -) -> tuple[int, int, int]: - """Write route files and replace past YAML rows.""" - journeys = build_timeline_journeys(timeline_path, data_dir, today=today) - routes_written = write_timeline_routes(data_dir, journeys) - rows_replaced, rows_written = rewrite_car_journeys_yaml( - data_dir, journeys, today=today - ) - return (routes_written, rows_replaced, rows_written) - - -def parse_date(value: str) -> date: - """Parse an ISO date.""" - return date.fromisoformat(value) - - -def main(argv: list[str] | None = None) -> int: - """CLI entrypoint.""" - parser = argparse.ArgumentParser( - description=( - "Replace past car_journeys.yaml rows with routes split from a " - "Google Timeline export." - ) - ) - parser.add_argument("timeline", type=Path) - parser.add_argument("--data-dir", type=Path, default=PERSONAL_DATA_DIR) - parser.add_argument("--today", type=parse_date, default=None) - args = parser.parse_args(argv) - - routes_written, rows_replaced, rows_written = import_from_timeline( - args.timeline, - data_dir=args.data_dir, - today=args.today, - ) - print(f"Wrote {routes_written} route GeoJSON file(s)") - print(f"Replaced {rows_replaced} past car journey row(s)") - print(f"Wrote {rows_written} split timeline journey row(s)") - return 0 diff --git a/agenda/car_journey_yaml.py b/agenda/car_journey_yaml.py deleted file mode 100644 index e8cb1f9..0000000 --- a/agenda/car_journey_yaml.py +++ /dev/null @@ -1,762 +0,0 @@ -"""Generate car journey YAML and routes for trips.""" - -from __future__ import annotations - -import argparse -import json -import os -import re -import typing -import unicodedata -from dataclasses import dataclass -from datetime import date, datetime -from pathlib import Path - -import yaml - -try: - import openrouteservice # type: ignore[import-not-found] -except ImportError: # pragma: no cover - optional dependency in tests - openrouteservice = None - -from . import trip as trip_module -from . import utils - -PERSONAL_DATA_DIR = Path("~/src/personal-data").expanduser() -DEFAULT_HOME_LABEL = "PCH" -DEFAULT_PROFILE = "driving-car" -DEFAULT_SNAP_RADIUS_METRES = 5000 - -LatLon = tuple[float, float] -LonLat = tuple[float, float] -GeoJSON = dict[str, typing.Any] -StrDict = dict[str, typing.Any] -RouteFetcher = typing.Callable[[LonLat, LonLat], GeoJSON] - - -class NoAliasDumper(yaml.SafeDumper): - """YAML dumper that avoids anchors for repeated scalar values.""" - - def ignore_aliases(self, data: typing.Any) -> bool: - """Disable aliases for all values.""" - return True - - -class TripLike(typing.Protocol): - """Trip fields needed for car journey generation.""" - - start: date - accommodation: list[StrDict] - travel: list[StrDict] - - -@dataclass(frozen=True) -class CarJourney: - """A car journey ready to write to YAML and GeoJSON.""" - - trip: date - depart: date - arrive: date - route: str - start: LonLat - end: LonLat - - def as_yaml_item(self) -> StrDict: - """Return the YAML mapping for this journey.""" - return { - "trip": self.trip, - "depart": self.depart, - "arrive": self.arrive, - "route": self.route, - } - - -def latlon_to_lonlat(coord: LatLon) -> LonLat: - """Convert a lat/lon pair to lon/lat for routing APIs.""" - return (coord[1], coord[0]) - - -def route_label(value: str) -> str: - """Return a filesystem-safe route label.""" - ascii_value = ( - unicodedata.normalize( - "NFKD", - value.strip() - .replace("æ", "ae") - .replace("Æ", "AE") - .replace("ð", "d") - .replace("Ð", "D") - .replace("þ", "th") - .replace("Þ", "Th"), - ) - .encode("ascii", "ignore") - .decode("ascii") - ) - label = re.sub(r"[^A-Za-z0-9]+", "_", ascii_value).strip("_") - if not label: - raise ValueError(f"empty route label from {value!r}") - return label - - -def load_yaml_list(path: Path) -> list[StrDict]: - """Load a YAML list, returning an empty list when the file does not exist.""" - if not path.exists(): - return [] - loaded = yaml.safe_load(path.read_text()) - if loaded is None: - return [] - if isinstance(loaded, list) and all(isinstance(item, dict) for item in loaded): - return typing.cast(list[StrDict], loaded) - raise ValueError(f"{path} must contain a YAML list") - - -def dump_yaml_list_with_blank_lines(items: list[StrDict]) -> str: - """Dump a top-level YAML list using the personal-data spacing style.""" - if not items: - return "---\n" - text = yaml.dump( - items, sort_keys=False, allow_unicode=True, Dumper=NoAliasDumper - ).lstrip() - return "---\n" + text.replace("\n- ", "\n\n- ") - - -def car_route_path(data_dir: Path, route: str) -> Path: - """Return the GeoJSON path for a car route name.""" - route_filename = trip_module.route_filename_without_extension(route) - return data_dir / "car_routes" / f"{route_filename}.geojson" - - -def read_geojson(path: Path) -> GeoJSON: - """Read a GeoJSON file.""" - return typing.cast(GeoJSON, json.loads(path.read_text())) - - -def home_latlon_from_existing_routes(data_dir: Path, home_label: str) -> LatLon: - """Find the home coordinate from existing car routes.""" - route_dir = data_dir / "car_routes" - if not route_dir.exists(): - raise ValueError(f"missing car route directory: {route_dir}") - - home_route_prefix = f"{home_label}_to_" - home_route_suffix = f"_to_{home_label}" - - for path in sorted(route_dir.glob("*.geojson")): - geojson_data = read_geojson(path) - endpoints = trip_module.geojson_route_endpoints(geojson_data) - if endpoints is None: - continue - route_name = path.stem - if route_name.startswith(home_route_prefix): - return endpoints[0] - if route_name.endswith(home_route_suffix): - return endpoints[1] - - raise ValueError( - f"could not find home coordinate for {home_label!r} in {route_dir}" - ) - - -def trip_for_date(data_dir: Path, trip_date: date) -> TripLike: - """Return the trip with the given start date.""" - trips = typing.cast( - list[TripLike], trip_module.build_trip_list(data_dir=str(data_dir)) - ) - for item in trips: - if item.start == trip_date: - return item - raise ValueError(f"could not find trip starting {trip_date}") - - -def primary_accommodation(trip: TripLike) -> StrDict: - """Return the first accommodation for a trip.""" - if not trip.accommodation: - raise ValueError(f"trip {trip.start} has no accommodation") - return min(trip.accommodation, key=lambda item: utils.as_datetime(item["from"])) - - -def accommodation_label(accommodation: StrDict) -> str: - """Return the destination label for an accommodation.""" - label = accommodation.get("location") or accommodation.get("name") - if not isinstance(label, str): - raise ValueError("accommodation has no location or name") - return route_label(label) - - -def accommodation_latlon(accommodation: StrDict) -> LatLon: - """Return accommodation coordinates.""" - latitude = accommodation.get("latitude") - longitude = accommodation.get("longitude") - if not isinstance(latitude, (int, float)) or not isinstance( - longitude, (int, float) - ): - raise ValueError("accommodation is missing latitude/longitude") - return (float(latitude), float(longitude)) - - -def sorted_accommodation(trip: TripLike) -> list[StrDict]: - """Return trip accommodation sorted by check-in time.""" - return sorted(trip.accommodation, key=lambda item: utils.as_datetime(item["from"])) - - -def airport_latlon(airport: StrDict) -> LatLon: - """Return airport coordinates.""" - latitude = airport.get("latitude") - longitude = airport.get("longitude") - if not isinstance(latitude, (int, float)) or not isinstance( - longitude, (int, float) - ): - raise ValueError("airport is missing latitude/longitude") - return (float(latitude), float(longitude)) - - -def flight_departure_datetime(item: StrDict) -> typing.Any: - """Return flight departure value for sorting.""" - return item["depart"] - - -def trip_flights(trip: TripLike) -> list[StrDict]: - """Return trip flights sorted by departure time.""" - flights = [item for item in trip.travel if item.get("type") == "flight"] - if not flights: - raise ValueError(f"trip {trip.start} has no flights") - return sorted( - flights, key=lambda item: utils.as_datetime(flight_departure_datetime(item)) - ) - - -def trip_ferries(trip: TripLike) -> list[StrDict]: - """Return trip ferries sorted by departure time.""" - ferries = [item for item in trip.travel if item.get("type") == "ferry"] - if not ferries: - raise ValueError(f"trip {trip.start} has no ferries") - return sorted(ferries, key=lambda item: utils.as_datetime(item["depart"])) - - -def location_latlon(location: StrDict, label: str) -> LatLon: - """Return coordinates from a loaded travel location.""" - latitude = location.get("latitude") - longitude = location.get("longitude") - if not isinstance(latitude, (int, float)) or not isinstance( - longitude, (int, float) - ): - raise ValueError(f"{label} is missing latitude/longitude") - return (float(latitude), float(longitude)) - - -def location_name(location: StrDict, fallback: str) -> str: - """Return a route label source for a loaded travel location.""" - name = location.get("name") or fallback - if not isinstance(name, str): - raise ValueError(f"location name must be a string: {location!r}") - return name - - -def car_journeys_for_accommodation( - trip: TripLike, - data_dir: Path, - home_label: str = DEFAULT_HOME_LABEL, - destination_label: str | None = None, -) -> list[CarJourney]: - """Build outbound and return car journeys for a trip's primary accommodation.""" - accommodation = primary_accommodation(trip) - home = latlon_to_lonlat(home_latlon_from_existing_routes(data_dir, home_label)) - destination = latlon_to_lonlat(accommodation_latlon(accommodation)) - destination_route_label = destination_label or accommodation_label(accommodation) - outbound_route = f"{route_label(home_label)}_to_{destination_route_label}" - return_route = f"{destination_route_label}_to_{route_label(home_label)}" - - check_in = utils.as_date(accommodation["from"]) - check_out = utils.as_date(accommodation["to"]) - - return [ - CarJourney( - trip=trip.start, - depart=check_in, - arrive=check_in, - route=outbound_route, - start=home, - end=destination, - ), - CarJourney( - trip=trip.start, - depart=check_out, - arrive=check_out, - route=return_route, - start=destination, - end=home, - ), - ] - - -def car_journeys_for_ferry( - trip: TripLike, - data_dir: Path, - home_label: str = DEFAULT_HOME_LABEL, - destination_label: str | None = None, -) -> list[CarJourney]: - """Build car journeys around an outbound and return car ferry crossing.""" - ferries = trip_ferries(trip) - outbound_ferry = ferries[0] - return_ferry = ferries[-1] - accommodation = primary_accommodation(trip) - - home = latlon_to_lonlat(home_latlon_from_existing_routes(data_dir, home_label)) - accommodation_coord = latlon_to_lonlat(accommodation_latlon(accommodation)) - accommodation_route_label = destination_label or accommodation_label(accommodation) - - outbound_from_terminal = outbound_ferry.get("from_terminal") - outbound_to_terminal = outbound_ferry.get("to_terminal") - return_from_terminal = return_ferry.get("from_terminal") - return_to_terminal = return_ferry.get("to_terminal") - terminals = ( - outbound_from_terminal, - outbound_to_terminal, - return_from_terminal, - return_to_terminal, - ) - if not all(isinstance(terminal, dict) for terminal in terminals): - raise ValueError("ferry is missing terminal details") - - outbound_from = typing.cast(StrDict, outbound_from_terminal) - outbound_to = typing.cast(StrDict, outbound_to_terminal) - return_from = typing.cast(StrDict, return_from_terminal) - return_to = typing.cast(StrDict, return_to_terminal) - - home_route_label = route_label(home_label) - outbound_from_label = route_label( - location_name(outbound_from, typing.cast(str, outbound_ferry.get("from", ""))) - ) - outbound_to_label = route_label( - location_name(outbound_to, typing.cast(str, outbound_ferry.get("to", ""))) - ) - return_from_label = route_label( - location_name(return_from, typing.cast(str, return_ferry.get("from", ""))) - ) - return_to_label = route_label( - location_name(return_to, typing.cast(str, return_ferry.get("to", ""))) - ) - - outbound_from_coord = latlon_to_lonlat( - location_latlon(outbound_from, outbound_from_label) - ) - outbound_to_coord = latlon_to_lonlat( - location_latlon(outbound_to, outbound_to_label) - ) - return_from_coord = latlon_to_lonlat( - location_latlon(return_from, return_from_label) - ) - return_to_coord = latlon_to_lonlat(location_latlon(return_to, return_to_label)) - - return [ - CarJourney( - trip=trip.start, - depart=utils.as_date(outbound_ferry["depart"]), - arrive=utils.as_date(outbound_ferry["depart"]), - route=f"{home_route_label}_to_{outbound_from_label}", - start=home, - end=outbound_from_coord, - ), - CarJourney( - trip=trip.start, - depart=utils.as_date(outbound_ferry["arrive"]), - arrive=utils.as_date(outbound_ferry["arrive"]), - route=f"{outbound_to_label}_to_{accommodation_route_label}", - start=outbound_to_coord, - end=accommodation_coord, - ), - CarJourney( - trip=trip.start, - depart=utils.as_date(return_ferry["depart"]), - arrive=utils.as_date(return_ferry["depart"]), - route=f"{accommodation_route_label}_to_{return_from_label}", - start=accommodation_coord, - end=return_from_coord, - ), - CarJourney( - trip=trip.start, - depart=utils.as_date(return_ferry["arrive"]), - arrive=utils.as_date(return_ferry["arrive"]), - route=f"{return_to_label}_to_{home_route_label}", - start=return_to_coord, - end=home, - ), - ] - - -def car_journeys_for_airport( - trip: TripLike, - data_dir: Path, - home_label: str = DEFAULT_HOME_LABEL, -) -> list[CarJourney]: - """Build outbound and return car journeys for the trip's UK airport.""" - flights = trip_flights(trip) - outbound_flight = flights[0] - return_flight = flights[-1] - outbound_airport = outbound_flight.get("from_airport") - return_airport = return_flight.get("to_airport") - if not isinstance(outbound_airport, dict) or not isinstance(return_airport, dict): - raise ValueError("flight is missing airport details") - - outbound_iata = outbound_airport.get("iata") - return_iata = return_airport.get("iata") - if not isinstance(outbound_iata, str) or not isinstance(return_iata, str): - raise ValueError("flight airport is missing IATA code") - - home = latlon_to_lonlat(home_latlon_from_existing_routes(data_dir, home_label)) - outbound_destination = latlon_to_lonlat(airport_latlon(outbound_airport)) - return_start = latlon_to_lonlat(airport_latlon(return_airport)) - home_route_label = route_label(home_label) - outbound_route_label = route_label(outbound_iata) - return_route_label = route_label(return_iata) - - return [ - CarJourney( - trip=trip.start, - depart=utils.as_date(outbound_flight["depart"]), - arrive=utils.as_date(outbound_flight["depart"]), - route=f"{home_route_label}_to_{outbound_route_label}", - start=home, - end=outbound_destination, - ), - CarJourney( - trip=trip.start, - depart=utils.as_date(return_flight["arrive"]), - arrive=utils.as_date(return_flight["arrive"]), - route=f"{return_route_label}_to_{home_route_label}", - start=return_start, - end=home, - ), - ] - - -def country_code(item: StrDict) -> str | None: - """Return a lower-case country code from a YAML item.""" - country = item.get("country") - return country.casefold() if isinstance(country, str) else None - - -def accommodation_route_label(accommodation: StrDict) -> str: - """Return the route label for an accommodation.""" - return accommodation_label(accommodation) - - -def airport_iata(airport: StrDict) -> str: - """Return an airport IATA code.""" - iata = airport.get("iata") - if not isinstance(iata, str): - raise ValueError(f"airport missing IATA code: {airport!r}") - return iata - - -def flight_airport(flight: StrDict, field: str) -> StrDict: - """Return airport detail for a flight field.""" - airport = flight.get(field) - if not isinstance(airport, dict): - raise ValueError(f"flight missing {field}") - return typing.cast(StrDict, airport) - - -def car_journeys_for_fly_drive( - trip: TripLike, - data_dir: Path, - home_label: str = DEFAULT_HOME_LABEL, -) -> list[CarJourney]: - """Build car journeys for home-airport-hotel plus destination rental car legs.""" - flights = trip_flights(trip) - accommodations = sorted_accommodation(trip) - if not accommodations: - raise ValueError(f"trip {trip.start} has no accommodation") - - outbound_flight = flights[0] - return_flight = flights[-1] - outbound_destination_airport = flight_airport(outbound_flight, "to_airport") - return_origin_airport = flight_airport(return_flight, "from_airport") - return_destination_airport = flight_airport(return_flight, "to_airport") - home = latlon_to_lonlat(home_latlon_from_existing_routes(data_dir, home_label)) - home_route_label = route_label(home_label) - - preflight_accommodation = next( - ( - accommodation - for accommodation in accommodations - if country_code(accommodation) == "gb" - and utils.as_date(accommodation["from"]) - <= utils.as_date(outbound_flight["depart"]) - ), - None, - ) - destination_country = country_code(outbound_destination_airport) - rental_accommodations = [ - accommodation - for accommodation in accommodations - if country_code(accommodation) == destination_country - ] - if not rental_accommodations: - raise ValueError("trip has no destination-country accommodation") - - journeys: list[CarJourney] = [] - if preflight_accommodation is not None: - preflight_label = accommodation_route_label(preflight_accommodation) - preflight_coord = latlon_to_lonlat( - accommodation_latlon(preflight_accommodation) - ) - journeys.append( - CarJourney( - trip=trip.start, - depart=utils.as_date(preflight_accommodation["from"]), - arrive=utils.as_date(preflight_accommodation["from"]), - route=f"{home_route_label}_to_{preflight_label}", - start=home, - end=preflight_coord, - ) - ) - - destination_airport_label = route_label(airport_iata(outbound_destination_airport)) - destination_airport_coord = latlon_to_lonlat( - airport_latlon(outbound_destination_airport) - ) - first_accommodation = rental_accommodations[0] - first_label = accommodation_route_label(first_accommodation) - first_coord = latlon_to_lonlat(accommodation_latlon(first_accommodation)) - journeys.append( - CarJourney( - trip=trip.start, - depart=utils.as_date(outbound_flight["arrive"]), - arrive=utils.as_date(outbound_flight["arrive"]), - route=f"{destination_airport_label}_to_{first_label}", - start=destination_airport_coord, - end=first_coord, - ) - ) - - for previous, current in zip(rental_accommodations, rental_accommodations[1:]): - previous_label = accommodation_route_label(previous) - current_label = accommodation_route_label(current) - journeys.append( - CarJourney( - trip=trip.start, - depart=utils.as_date(previous["to"]), - arrive=utils.as_date(previous["to"]), - route=f"{previous_label}_to_{current_label}", - start=latlon_to_lonlat(accommodation_latlon(previous)), - end=latlon_to_lonlat(accommodation_latlon(current)), - ) - ) - - last_accommodation = rental_accommodations[-1] - return_origin_airport_label = route_label(airport_iata(return_origin_airport)) - journeys.append( - CarJourney( - trip=trip.start, - depart=utils.as_date(return_flight["depart"]), - arrive=utils.as_date(return_flight["depart"]), - route=f"{accommodation_route_label(last_accommodation)}_to_{return_origin_airport_label}", - start=latlon_to_lonlat(accommodation_latlon(last_accommodation)), - end=latlon_to_lonlat(airport_latlon(return_origin_airport)), - ) - ) - - return_destination_airport_label = route_label( - airport_iata(return_destination_airport) - ) - journeys.append( - CarJourney( - trip=trip.start, - depart=utils.as_date(return_flight["arrive"]), - arrive=utils.as_date(return_flight["arrive"]), - route=f"{return_destination_airport_label}_to_{home_route_label}", - start=latlon_to_lonlat(airport_latlon(return_destination_airport)), - end=home, - ) - ) - - return journeys - - -def car_journeys_for_trip( - trip: TripLike, - data_dir: Path, - destination: str, - home_label: str = DEFAULT_HOME_LABEL, - destination_label: str | None = None, -) -> list[CarJourney]: - """Build outbound and return car journeys for a trip.""" - if destination == "accommodation": - return car_journeys_for_accommodation( - trip, - data_dir, - home_label=home_label, - destination_label=destination_label, - ) - if destination == "airport": - return car_journeys_for_airport(trip, data_dir, home_label=home_label) - if destination == "ferry": - return car_journeys_for_ferry( - trip, - data_dir, - home_label=home_label, - destination_label=destination_label, - ) - if destination == "fly-drive": - return car_journeys_for_fly_drive(trip, data_dir, home_label=home_label) - raise ValueError(f"unknown car journey destination {destination!r}") - - -def openrouteservice_fetcher(api_key: str) -> RouteFetcher: - """Return a route fetcher backed by openrouteservice.""" - if openrouteservice is None: - raise ValueError("openrouteservice must be installed") - client = openrouteservice.Client(key=api_key) - - def fetch(start: LonLat, end: LonLat) -> GeoJSON: - return typing.cast( - GeoJSON, - client.directions( - [start, end], - profile=DEFAULT_PROFILE, - format="geojson", - instructions=False, - radiuses=[DEFAULT_SNAP_RADIUS_METRES, DEFAULT_SNAP_RADIUS_METRES], - ), - ) - - return fetch - - -def write_route_files( - data_dir: Path, - journeys: list[CarJourney], - fetch_route: RouteFetcher, - overwrite_routes: bool = False, -) -> int: - """Write missing route GeoJSON files and return count written.""" - route_dir = data_dir / "car_routes" - route_dir.mkdir(parents=True, exist_ok=True) - written = 0 - - for journey in journeys: - path = car_route_path(data_dir, journey.route) - if path.exists() and not overwrite_routes: - continue - route_geojson = fetch_route(journey.start, journey.end) - path.write_text(json.dumps(route_geojson, separators=(",", ":")) + "\n") - written += 1 - - return written - - -def journey_key(item: StrDict) -> tuple[date, datetime, datetime, str]: - """Return a stable key for duplicate detection and sorting.""" - route = item.get("route") - if not isinstance(route, str): - raise ValueError(f"car journey route must be a string: {item!r}") - return ( - utils.as_date(item["trip"]), - utils.as_datetime(item["depart"]), - utils.as_datetime(item["arrive"]), - trip_module.route_filename_without_extension(route), - ) - - -def import_car_journeys(data_dir: Path, journeys: list[CarJourney]) -> int: - """Insert car journeys into car_journeys.yaml and return count added.""" - path = data_dir / "car_journeys.yaml" - existing = load_yaml_list(path) - existing_keys = {journey_key(item) for item in existing} - new_items = [ - journey.as_yaml_item() - for journey in journeys - if journey_key(journey.as_yaml_item()) not in existing_keys - ] - if not new_items: - return 0 - - combined = existing + new_items - combined.sort(key=journey_key) - path.write_text(dump_yaml_list_with_blank_lines(combined)) - return len(new_items) - - -def add_car_journeys_for_trip( - trip_date: date, - data_dir: Path = PERSONAL_DATA_DIR, - destination: str = "accommodation", - home_label: str = DEFAULT_HOME_LABEL, - destination_label: str | None = None, - fetch_route: RouteFetcher | None = None, - overwrite_routes: bool = False, - dry_run: bool = False, -) -> tuple[list[CarJourney], int, int]: - """Add car journeys and routes for a trip.""" - trip = trip_for_date(data_dir, trip_date) - journeys = car_journeys_for_trip( - trip, - data_dir, - destination=destination, - home_label=home_label, - destination_label=destination_label, - ) - - if dry_run: - return (journeys, 0, 0) - - if fetch_route is None: - api_key = os.environ.get("ORS_API_KEY") - if not api_key: - raise ValueError("ORS_API_KEY must be set") - fetch_route = openrouteservice_fetcher(api_key) - - routes_written = write_route_files( - data_dir, journeys, fetch_route, overwrite_routes=overwrite_routes - ) - journeys_added = import_car_journeys(data_dir, journeys) - return (journeys, routes_written, journeys_added) - - -def parse_date(value: str) -> date: - """Parse an ISO date.""" - return date.fromisoformat(value) - - -def main(argv: list[str] | None = None) -> int: - """CLI entrypoint.""" - parser = argparse.ArgumentParser( - description=( - "Add car journeys from home to trip accommodation and back, " - "using openrouteservice for GeoJSON routes." - ) - ) - parser.add_argument("trip_date", type=parse_date) - parser.add_argument( - "--destination", - choices=("accommodation", "airport", "ferry", "fly-drive"), - default="accommodation", - help=( - "Route to accommodation check-in/out, the trip airport, " - "around an outbound/return car ferry, or a fly-drive chain." - ), - ) - parser.add_argument("--data-dir", default=str(PERSONAL_DATA_DIR)) - parser.add_argument("--home-label", default=DEFAULT_HOME_LABEL) - parser.add_argument("--destination-label") - parser.add_argument("--overwrite-routes", action="store_true") - parser.add_argument("--dry-run", action="store_true") - args = parser.parse_args(argv) - - journeys, routes_written, journeys_added = add_car_journeys_for_trip( - args.trip_date, - data_dir=Path(args.data_dir).expanduser(), - destination=args.destination, - home_label=args.home_label, - destination_label=args.destination_label, - overwrite_routes=args.overwrite_routes, - dry_run=args.dry_run, - ) - - for journey in journeys: - print(f"{journey.depart}: {journey.route}") - if args.dry_run: - return 0 - print(f"Wrote {routes_written} route file(s)") - print(f"Added {journeys_added} car journey YAML item(s)") - return 0 diff --git a/agenda/carnival.py b/agenda/carnival.py deleted file mode 100644 index 7f6f2f4..0000000 --- a/agenda/carnival.py +++ /dev/null @@ -1,33 +0,0 @@ -"""Calculate the date for carnival.""" - -from datetime import date, timedelta - -from dateutil.easter import easter - -from .event import Event - - -def rio_carnival_events(start_date: date, end_date: date) -> list[Event]: - """List of events for Rio Carnival for each year between start_date and end_date.""" - events = [] - for year in range(start_date.year, end_date.year + 1): - easter_date = easter(year) - carnival_start = easter_date - timedelta(days=51) - carnival_end = easter_date - timedelta(days=46) - - # Only include the carnival if it falls within the specified date range - if ( - start_date <= carnival_start <= end_date - or start_date <= carnival_end <= end_date - ): - events.append( - Event( - name="carnival", - title="Rio Carnival", - date=carnival_start, - end_date=carnival_end, - url="https://en.wikipedia.org/wiki/Rio_Carnival", - ) - ) - - return events diff --git a/agenda/conference.py b/agenda/conference.py index 1d90d96..da697fe 100644 --- a/agenda/conference.py +++ b/agenda/conference.py @@ -2,31 +2,11 @@ import dataclasses import decimal -import os -import typing from datetime import date, datetime import yaml -from . import utils -from .event import Event -from .types import DateOrDateTime, StrDict - -MAX_CONF_DAYS = 20 -DATE_STATUSES = {"exact", "approximate", "tentative"} -DATED_STATUSES = {"exact", "tentative"} - - -class ConferenceSeries(typing.TypedDict, total=False): - """Conference series metadata.""" - - name: str - topic: str - url: str - notes: str - cadence: str - usual_location: str - country: str +from .types import Event @dataclasses.dataclass @@ -36,11 +16,8 @@ class Conference: name: str topic: str location: str - series: str | None = None - start: date | datetime | None = None - end: date | datetime | None = None - trip: date | None = None - country: str | None = None + start: date | datetime + end: date | datetime venue: str | None = None address: str | None = None url: str | None = None @@ -52,19 +29,6 @@ class Conference: online: bool = False price: decimal.Decimal | None = None currency: str | None = None - latitude: float | None = None - longitude: float | None = None - attend_start: date | datetime | None = None - attend_end: date | datetime | None = None - cfp_end: date | None = None - cfp_url: str | None = None - free: bool | None = None - hackathon: bool | None = None - ticket_type: str | None = None - attendees: int | None = None - hashtag: str | None = None - description: str | None = None - dates: StrDict | None = None @property def display_name(self) -> str: @@ -76,157 +40,19 @@ class Conference: ) -def _date_range_label(start: date, end: date) -> str: - """Format conference date range for display.""" - if start == end: - return start.strftime("%a %-d %b %Y") - if start.year == end.year and start.month == end.month: - return f"{start.strftime('%a %-d')}-{end.strftime('%-d %b %Y')}" - return f"{start.strftime('%a %-d %b')}-{end.strftime('%-d %b %Y')}" - - -def _require_date_value(value: typing.Any, field_name: str) -> DateOrDateTime: - """Return date-like field value or raise ValueError.""" - if isinstance(value, (date, datetime)): - return value - raise ValueError(f"conference dates field {field_name!r} must be a date/datetime") - - -def _require_date_only(value: typing.Any, field_name: str) -> date: - """Return field value as a date or raise ValueError.""" - return utils.as_date(_require_date_value(value, field_name)) - - -def conference_date_fields(item: StrDict) -> StrDict: - """Return derived date fields for a conference YAML item.""" - raw_dates = item.get("dates") - if raw_dates is None: - status = typing.cast(str, item.get("date_status", "exact")) - if status not in DATE_STATUSES: - raise ValueError(f"unknown conference date status {status!r}") - start = _require_date_value(item.get("start"), "start") - end = _require_date_value(item.get("end", start), "end") - start_date = utils.as_date(start) - end_date = utils.as_date(end) - return { - "date_status": status, - "start": start, - "end": end, - "start_date": start_date, - "end_date": end_date, - "sort_date": start_date, - "latest_date": end_date, - "display_date": _date_range_label(start_date, end_date), - "has_exact_dates": status == "exact", - } - - if not isinstance(raw_dates, dict): - raise ValueError("conference dates must be a mapping") - - status_value = raw_dates.get("status", "exact") - if not isinstance(status_value, str) or status_value not in DATE_STATUSES: - raise ValueError(f"unknown conference date status {status_value!r}") - - if status_value in DATED_STATUSES: - start = _require_date_value(raw_dates.get("start", item.get("start")), "start") - end = _require_date_value(raw_dates.get("end", item.get("end", start)), "end") - start_date = utils.as_date(start) - end_date = utils.as_date(end) - label = raw_dates.get("label") - display_date = ( - label if isinstance(label, str) else _date_range_label(start_date, end_date) - ) - return { - "date_status": status_value, - "start": start, - "end": end, - "start_date": start_date, - "end_date": end_date, - "sort_date": start_date, - "latest_date": end_date, - "display_date": display_date, - "has_exact_dates": status_value == "exact", - } - - earliest = _require_date_only(raw_dates.get("earliest"), "earliest") - latest = _require_date_only(raw_dates.get("latest"), "latest") - label = raw_dates.get("label") - display_date = ( - label if isinstance(label, str) else _date_range_label(earliest, latest) - ) - return { - "date_status": status_value, - "start_date": earliest, - "end_date": latest, - "sort_date": earliest, - "latest_date": latest, - "display_date": display_date, - "has_exact_dates": False, - } - - -def validate_conference_date_fields(item: StrDict) -> StrDict: - """Validate conference date fields and return derived values.""" - fields = conference_date_fields(item) - if fields["start_date"] > fields["end_date"]: - raise ValueError("conference ends before it starts") - - if fields["date_status"] in DATED_STATUSES: - duration = (fields["end_date"] - fields["start_date"]).days - if duration >= MAX_CONF_DAYS: - raise ValueError( - f"conference is {duration} days; maximum is {MAX_CONF_DAYS - 1}" - ) - return fields - - -def load_series(data_dir: str) -> dict[str, ConferenceSeries]: - """Load conference series metadata.""" - filepath = os.path.join(data_dir, "conference_series.yaml") - if not os.path.exists(filepath): - return {} - - loaded = yaml.safe_load(open(filepath, "r")) - if loaded is None: - return {} - if not isinstance(loaded, dict): - raise ValueError("conference_series.yaml must be a mapping") - return typing.cast(dict[str, ConferenceSeries], loaded) - - def get_list(filepath: str) -> list[Event]: """Read conferences from a YAML file and return a list of Event objects.""" - events: list[Event] = [] - for item in yaml.safe_load(open(filepath, "r")): - try: - fields = validate_conference_date_fields(item) - except ValueError as exc: - raise AssertionError(str(exc)) from exc - normalized_item = dict(item) - if "start" in fields: - normalized_item["start"] = fields["start"] - normalized_item["end"] = fields["end"] - conf = Conference(**normalized_item) - - if fields["has_exact_dates"]: - assert conf.start is not None and conf.end is not None - event = Event( - name="conference", - date=conf.start, - end_date=conf.end, - title=conf.display_name, - url=conf.url, - going=conf.going, - ) - events.append(event) - if not conf.cfp_end: - continue - cfp_end_event = Event( - name="cfp_end", - date=conf.cfp_end, - title="CFP end: " + conf.display_name, - url=conf.cfp_url or conf.url, + return [ + Event( + name="conference", + date=conf.start, + end_date=conf.end, + title=f"🎤 {conf.display_name}", + url=conf.url, + going=conf.going, ) - events.append(cfp_end_event) - - return events + for conf in ( + Conference(**conf) + for conf in yaml.safe_load(open(filepath, "r"))["conferences"] + ) + ] diff --git a/agenda/data.py b/agenda/data.py index 5517fc3..a508c39 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -1,50 +1,51 @@ """Agenda data.""" import asyncio +import collections +import itertools import os import typing -from datetime import date, datetime, timedelta, timezone -from time import time +from datetime import date, datetime, timedelta import dateutil.rrule import dateutil.tz import flask -import lxml # type: ignore[import-untyped] +import holidays +import isodate # type: ignore +import lxml import pytz +import yaml from . import ( accommodation, birthday, - bristol_waste, - busy, - carnival, + calendar, conference, domains, economist, - events_yaml, - gandi, + fx, gwr, hn, - holidays, meetup, - n_somerset_waste, stock_market, subscription, sun, thespacedevs, travel, uk_holiday, + uk_tz, + waste_schedule, ) -from .event import Event -from .types import StrDict -from .utils import time_function +from .types import Event, Holiday + +StrDict = dict[str, typing.Any] here = dateutil.tz.tzlocal() # deadline to file tax return # credit card expiry dates # morzine ski lifts -# chalet availability calendar +# chalet availablity calendar # starlink visible @@ -61,124 +62,298 @@ def timezone_transition( ] -async def n_somerset_waste_collection_events( - data_dir: str, postcode: str, uprn: str, force_cache: bool = False +def us_holidays(start_date: date, end_date: date) -> list[Holiday]: + """Get US holidays.""" + found: list[Holiday] = [] + for year in range(start_date.year, end_date.year + 1): + hols = holidays.country_holidays("US", years=year, language="en") + found += [ + Holiday(date=hol_date, name=title, country="us") + for hol_date, title in hols.items() + if start_date < hol_date < end_date + ] + + extra = [] + for h in found: + if h.name != "Thanksgiving": + continue + extra += [ + Holiday(date=h.date + timedelta(days=1), name="Black Friday", country="us"), + Holiday(date=h.date + timedelta(days=4), name="Cyber Monday", country="us"), + ] + + return found + extra + + +def get_nyse_holidays( + start_date: date, end_date: date, us_hols: list[Holiday] ) -> list[Event]: + """NYSE holidays.""" + known_us_hols = {(h.date, h.name) for h in us_hols} + found: list[Event] = [] + rename = {"Thanksgiving Day": "Thanksgiving"} + for year in range(start_date.year, end_date.year + 1): + hols = holidays.financial_holidays("NYSE", years=year) + found += [ + Event( + name="holiday", + date=hol_date, + title=rename.get(title, title), + ) + for hol_date, title in hols.items() + if start_date < hol_date < end_date + ] + found = [hol for hol in found if (hol.date, hol.title) not in known_us_hols] + for hol in found: + assert hol.title + hol.title += " (NYSE)" + return found + + +def get_holidays(country: str, start_date: date, end_date: date) -> list[Holiday]: + """Get holidays.""" + found: list[Holiday] = [] + for year in range(start_date.year, end_date.year + 1): + hols = holidays.country_holidays(country.upper(), years=year, language="en_US") + found += [ + Holiday( + date=hol_date, + name=title, + country=country.lower(), + ) + for hol_date, title in hols.items() + if start_date < hol_date < end_date + ] + + return found + + +def midnight(d: date) -> datetime: + """Convert from date to midnight on that day.""" + return datetime.combine(d, datetime.min.time()) + + +def dates_from_rrule( + rrule: str, start: date, end: date +) -> typing.Sequence[datetime | date]: + """Generate events from an RRULE between start_date and end_date.""" + all_day = not any(param in rrule for param in ["BYHOUR", "BYMINUTE", "BYSECOND"]) + + return [ + i.date() if all_day else uk_tz.localize(i) + for i in dateutil.rrule.rrulestr(rrule, dtstart=midnight(start)).between( + midnight(start), midnight(end) + ) + ] + + +async def waste_collection_events(data_dir: str) -> list[Event]: """Waste colllection events.""" - html = await n_somerset_waste.get_html(data_dir, postcode, uprn, force_cache) + postcode = "BS48 3HG" + uprn = "24071046" + + html = await waste_schedule.get_html(data_dir, postcode, uprn) root = lxml.html.fromstring(html) - events = n_somerset_waste.parse(root) - return typing.cast(list[Event], events) + events = waste_schedule.parse(root) + return events async def bristol_waste_collection_events( - data_dir: str, start_date: date, uprn: str, force_cache: bool = False + data_dir: str, start_date: date ) -> list[Event]: """Waste colllection events.""" - cache = "force" if force_cache else "recent" - return typing.cast( - list[Event], await bristol_waste.get(start_date, data_dir, uprn, cache) - ) + uprn = "358335" + + return await waste_schedule.get_bristol_gov_uk(start_date, data_dir, uprn) -def find_events_during_stay( - accommodation_events: list[Event], markets: list[Event] -) -> list[Event]: - """Market events that happen during accommodation stays.""" - overlapping_markets = [] - for market in markets: - market_date = market.as_date - assert isinstance(market_date, date) - for e in accommodation_events: - start, end = e.as_date, e.end_as_date - assert start and end and all(isinstance(i, date) for i in (start, end)) - # Check if the market date is within the accommodation dates. - if start <= market_date <= end: - overlapping_markets.append(market) - break # Breaks the inner loop if overlap is found. - return overlapping_markets +def combine_holidays(holidays: list[Holiday]) -> list[Event]: + """Combine UK and US holidays with the same date and title.""" + all_countries = {h.country for h in holidays} -def hide_markets_while_away( - events: list[Event], accommodation_events: list[Event] -) -> None: - """Hide markets that happen while away.""" - optional = [ - e - for e in events - if e.name == "market" or (e.title and "LHG Run Club" in e.title) - ] - going = [e for e in events if e.going] + standard_name = { + (1, 1): "New Year's Day", + (1, 6): "Epiphany", + (5, 1): "Labour Day", + (8, 15): "Assumption Day", + (12, 8): "Immaculate conception", + (12, 25): "Christmas Day", + (12, 26): "Boxing Day", + } - overlapping_markets = find_events_during_stay( - accommodation_events + going, optional - ) - for market in overlapping_markets: - events.remove(market) + combined: collections.defaultdict[ + tuple[date, str], set[str] + ] = collections.defaultdict(set) + for h in holidays: + assert isinstance(h.name, str) and isinstance(h.date, date) -class AgendaData(typing.TypedDict, total=False): - """Agenda Data.""" + event_key = (h.date, standard_name.get((h.date.month, h.date.day), h.name)) + combined[event_key].add(h.country) - now: datetime - stock_markets: list[str] - rockets: list[thespacedevs.Summary] - gwr_advance_tickets: date | None - data_gather_seconds: float - stock_market_times_seconds: float - timings: list[tuple[str, float]] - events: list[Event] - accommodation_events: list[Event] - gaps: list[StrDict] - sunrise: datetime - sunset: datetime - last_week: date - two_weeks_ago: date - errors: list[tuple[str, Exception]] - - -def rocket_launch_events(rockets: list[thespacedevs.Summary]) -> list[Event]: - """Rocket launch events.""" events: list[Event] = [] - for launch in rockets: - dt = None - - net_precision = launch["net_precision"] - skip = {"Year", "Month", "Quarter", "Fiscal Year"} - if net_precision == "Day": - dt = datetime.strptime(launch["net"], "%Y-%m-%dT%H:%M:%SZ").date() - elif ( - net_precision - and net_precision not in skip - and "Year" not in net_precision - and launch["t0_time"] - ): - dt = pytz.utc.localize( - datetime.strptime(launch["net"], "%Y-%m-%dT%H:%M:%SZ") + for (d, name), countries in combined.items(): + if len(countries) == len(all_countries): + country_list = "" + elif len(countries) < len(all_countries) / 2: + country_list = ", ".join(sorted(country.upper() for country in countries)) + else: + country_list = "not " + ", ".join( + sorted(country.upper() for country in all_countries - set(countries)) ) - if not dt: - continue - - rocket_name = ( - f'{launch["rocket"]["full_name"]}: ' - + f'{launch["mission_name"] or "[no mission]"}' + e = Event( + name="holiday", + date=d, + title=f"{name} ({country_list})" if country_list else name, ) - e = Event(name="rocket", date=dt, title=rocket_name) events.append(e) return events -def event_sort_datetime(event: Event) -> datetime: - """Return a timezone-normalized datetime suitable for sorting events.""" - dt = typing.cast(datetime, event.as_datetime) - if dt.tzinfo is None or dt.utcoffset() is None: - return dt - return dt.astimezone(timezone.utc).replace(tzinfo=None) +def get_yaml_event_date_field(item: dict[str, str]) -> str: + """Event date field name.""" + return ( + "end_date" + if item["name"] == "travel_insurance" + else ("start_date" if "start_date" in item else "date") + ) -async def get_data(now: datetime, config: flask.config.Config) -> AgendaData: +def get_yaml_event_end_date_field(item: dict[str, str]) -> str: + """Event date field name.""" + return ( + "end_date" + if item["name"] == "travel_insurance" + else ("start_date" if "start_date" in item else "date") + ) + + +def read_events_yaml(data_dir: str, start: date, end: date) -> list[Event]: + """Read eventes from YAML file.""" + events: list[Event] = [] + for item in yaml.safe_load(open(os.path.join(data_dir, "events.yaml"))): + duration = ( + isodate.parse_duration(item["duration"]) if "duration" in item else None + ) + dates = ( + dates_from_rrule(item["rrule"], start, end) + if "rrule" in item + else [item[get_yaml_event_date_field(item)]] + ) + for dt in dates: + e = Event( + name=item["name"], + date=dt, + end_date=( + dt + duration + if duration + else ( + item.get("end_date") + if item["name"] != "travel_insurance" + else None + ) + ), + title=item.get("title"), + url=item.get("url"), + ) + events.append(e) + return events + + +def find_markets_during_stay( + accommodation_events: list[Event], markets: list[Event] +) -> list[Event]: + """Market events that happen during accommodation stays.""" + overlapping_markets = [] + for market in markets: + for e in accommodation_events: + # Check if the market date is within the accommodation dates. + if e.as_date <= market.as_date <= e.end_as_date: + overlapping_markets.append(market) + break # Breaks the inner loop if overlap is found. + return overlapping_markets + + +def find_gaps(events: list[Event], min_gap_days: int = 3) -> list[StrDict]: + """Gaps of at least `min_gap_days` between events in a list of events.""" + # Sort events by start date + + gaps: list[tuple[date, date]] = [] + previous_event_end = None + + by_start_date = { + d: list(on_day) + for d, on_day in itertools.groupby(events, key=lambda e: e.as_date) + } + + by_end_date = { + d: list(on_day) + for d, on_day in itertools.groupby(events, key=lambda e: e.end_as_date) + } + + for event in events: + # Use start date for current event + start_date = event.as_date + + # If previous event exists, calculate the gap + if previous_event_end: + gap_days = (start_date - previous_event_end).days + if gap_days >= (min_gap_days + 2): + start_end = ( + previous_event_end + timedelta(days=1), + start_date - timedelta(days=1), + ) + gaps.append(start_end) + + # Update previous event end date + end = event.end_as_date + if not previous_event_end or end > previous_event_end: + previous_event_end = end + + return [ + { + "start": gap_start, + "end": gap_end, + "after": by_start_date[gap_end + timedelta(days=1)], + "before": by_end_date[gap_start - timedelta(days=1)], + } + for gap_start, gap_end in gaps + ] + + +def busy_event(e: Event) -> bool: + """Busy.""" + if e.name not in { + "event", + "accommodation", + "conference", + "dodainville", + "transport", + "meetup", + "party", + }: + return False + + if e.title in ("IA UK board meeting", "Mill Road Winter Fair"): + return False + + if e.name == "conference" and not e.going: + return False + if not e.title: + return True + if e.title == "LHG Run Club" or "Third Thursday Social" in e.title: + return False + + lc_title = e.title.lower() + return "rebels" not in lc_title and "south west data social" not in lc_title + + +async def get_data( + now: datetime, config: flask.config.Config +) -> typing.Mapping[str, str | object]: """Get data to display on agenda dashboard.""" data_dir = config["DATA_DIR"] @@ -192,51 +367,28 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData: minus_365 = now - timedelta(days=365) plus_365 = now + timedelta(days=365) - t0 = time() - offline_mode = bool(config.get("OFFLINE_MODE")) - result_list = await asyncio.gather( - time_function( - "gwr_advance_tickets", gwr.advance_ticket_date, data_dir, offline_mode - ), - time_function( - "backwell_bins", - n_somerset_waste_collection_events, - data_dir, - config["BACKWELL_POSTCODE"], - config["BACKWELL_UPRN"], - offline_mode, - ), - time_function( - "bristol_bins", - bristol_waste_collection_events, - data_dir, - today, - config["BRISTOL_UPRN"], - offline_mode, - ), + ( + gbpusd, + gwr_advance_tickets, + bank_holiday, + rockets, + backwell_bins, + bristol_bins, + ) = await asyncio.gather( + fx.get_gbpusd(config), + gwr.advance_ticket_date(data_dir), + uk_holiday.bank_holiday_list(last_year, next_year, data_dir), + thespacedevs.get_launches(rocket_dir, limit=40), + waste_collection_events(data_dir), + bristol_waste_collection_events(data_dir, today), ) - rockets = thespacedevs.read_cached_launches(rocket_dir) - results = {call[0]: call[1] for call in result_list} - - errors = [(call[0], call[3]) for call in result_list if call[3]] - - gwr_advance_tickets = results["gwr_advance_tickets"] - - data_gather_seconds = time() - t0 - t0 = time() - - stock_market_times = stock_market.open_and_close() - stock_market_times_seconds = time() - t0 - - reply: AgendaData = { + reply: dict[str, typing.Any] = { "now": now, - "stock_markets": stock_market_times, + "gbpusd": gbpusd, + "stock_markets": stock_market.open_and_close(), "rockets": rockets, "gwr_advance_tickets": gwr_advance_tickets, - "data_gather_seconds": data_gather_seconds, - "stock_market_times_seconds": stock_market_times_seconds, - "timings": [(call[0], call[2]) for call in result_list], } my_data = config["PERSONAL_DATA"] @@ -253,44 +405,85 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData: if gwr_advance_tickets: events.append(Event(name="gwr_advance_tickets", date=gwr_advance_tickets)) - us_hols = holidays.us_holidays(last_year, next_year) - events += holidays.get_nyse_holidays(last_year, next_year, us_hols) + us_hols = us_holidays(last_year, next_year) + + holidays: list[Holiday] = bank_holiday + us_hols + for country in ( + "at", + "be", + "br", + "ch", + "cz", + "de", + "dk", + "ee", + "es", + "fi", + "fr", + "gr", + "it", + "ke", + "nl", + "pl", + ): + holidays += get_holidays(country, last_year, next_year) + + events += get_nyse_holidays(last_year, next_year, us_hols) accommodation_events = accommodation.get_events( os.path.join(my_data, "accommodation.yaml") ) - holiday_list = holidays.get_all(last_year, next_year, data_dir) - events += holidays.combine_holidays(holiday_list) - events += holidays.get_school_holidays(last_year, next_year, data_dir) - if flask.g.user.is_authenticated: - events += birthday.get_birthdays( - last_year, os.path.join(my_data, "entities.yaml") - ) - events += domains.renewal_dates(my_data) + events += combine_holidays(holidays) + events += birthday.get_birthdays(last_year, os.path.join(my_data, "entities.yaml")) events += accommodation_events events += travel.all_events(my_data) events += conference.get_list(os.path.join(my_data, "conferences.yaml")) - for key in "backwell_bins", "bristol_bins": - if results[key]: - events += results[key] - events += events_yaml.read(my_data, last_year, next_year) + events += backwell_bins + bristol_bins + events += read_events_yaml(my_data, last_year, next_year) events += subscription.get_events(os.path.join(my_data, "subscriptions.yaml")) - events += gandi.get_events(data_dir) events += economist.publication_dates(last_week, next_year) events += meetup.get_events(my_data) events += hn.whoishiring(last_year, next_year) - events += carnival.rio_carnival_events(last_year, next_year) - events += rocket_launch_events(rockets) + + events += domains.renewal_dates(my_data) + + # hide markets that happen while away + markets = [e for e in events if e.name == "market"] + going = [e for e in events if e.going] + + overlapping_markets = find_markets_during_stay( + accommodation_events + going, markets + ) + for market in overlapping_markets: + events.remove(market) + + for launch in rockets: + dt = None + + if launch["net_precision"] == "Day": + dt = datetime.strptime(launch["net"], "%Y-%m-%dT00:00:00Z").date() + elif launch["t0_time"]: + dt = pytz.utc.localize( + datetime.strptime(launch["net"], "%Y-%m-%dT%H:%M:%SZ") + ) + + if not dt: + continue + + rocket_name = f'🚀{launch["rocket"]}: {launch["mission_name"] or "[no mission]"}' + e = Event(name="rocket", date=dt, title=rocket_name) + events.append(e) + events += [Event(name="today", date=today)] busy_events = [ e for e in sorted(events, key=lambda e: e.as_date) - if e.as_date > today and e.as_date < next_year and busy.busy_event(e) + if e.as_date > today and e.as_date < next_year and busy_event(e) ] - gaps = busy.find_gaps(busy_events) + gaps = find_gaps(busy_events) events += [ Event(name="gap", date=gap["start"], end_date=gap["end"]) for gap in gaps @@ -300,7 +493,7 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData: # at the top of the list for today. This is achieved by sorting first by # the datetime attribute, and then ensuring that events with the name # "today" are ordered before others on the same date. - events.sort(key=lambda e: (event_sort_datetime(e), e.name != "today")) + events.sort(key=lambda e: (e.as_datetime, e.name != "today")) reply["gaps"] = gaps @@ -308,10 +501,9 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData: reply["sunrise"] = sun.sunrise(observer) reply["sunset"] = sun.sunset(observer) reply["events"] = events - reply["accommodation_events"] = accommodation_events reply["last_week"] = last_week reply["two_weeks_ago"] = two_weeks_ago - reply["errors"] = errors + reply["fullcalendar_events"] = calendar.build_events(events) return reply diff --git a/agenda/domains.py b/agenda/domains.py index 5b03bff..99c614e 100644 --- a/agenda/domains.py +++ b/agenda/domains.py @@ -1,10 +1,10 @@ -"""Domain renewal dates.""" +"""Accomodation.""" import csv import os from datetime import datetime -from .event import Event +from .types import Event url = "https://admin.gandi.net/domain/01578ef0-a84b-11e7-bdf3-00163e6dc886/" diff --git a/agenda/economist.py b/agenda/economist.py index 21a8199..4438b98 100644 --- a/agenda/economist.py +++ b/agenda/economist.py @@ -5,7 +5,7 @@ from datetime import date, time, timedelta from dateutil.relativedelta import TH, relativedelta from . import uk_time -from .event import Event +from .types import Event def publication_dates(start_date: date, end_date: date) -> list[Event]: diff --git a/agenda/event.py b/agenda/event.py deleted file mode 100644 index b975625..0000000 --- a/agenda/event.py +++ /dev/null @@ -1,149 +0,0 @@ -"""Types.""" - -import datetime -from dataclasses import dataclass - -from . import utils -from .types import DateOrDateTime, StrDict - -emojis = { - "market": "🧺", - "us_presidential_election": "🗳️🇺🇸", - "bus_route_closure": "🚌❌", - "meetup": "👥", - "dinner": "🍷", - "party": "🍷", - "ba_voucher": "✈️", - "accommodation": "🏨", # alternative: 🧳 - "flight": "✈️", - "conference": "🎤", - "rocket": "🚀", - "birthday": "🎈", - "waste_schedule": "🗑️", - "economist": "📰", - "running": "🏃", - "critical_mass": "🚴", - "trip": "🧳", - "hackathon": "💻", -} - - -@dataclass -class Event: - """Event.""" - - name: str - date: DateOrDateTime - end_date: DateOrDateTime | None = None - title: str | None = None - url: str | None = None - going: bool | None = None - - @property - def as_datetime(self) -> datetime.datetime: - """Date/time of event.""" - return utils.as_datetime(self.date) - - @property - def has_time(self) -> bool: - """Event has a time associated with it.""" - return isinstance(self.date, datetime.datetime) - - @property - def as_date(self) -> datetime.date: - """Date of event.""" - return ( - self.date.date() if isinstance(self.date, datetime.datetime) else self.date - ) - - @property - def end_as_date(self) -> datetime.date: - """Date of event.""" - return ( - ( - self.end_date.date() - if isinstance(self.end_date, datetime.datetime) - else self.end_date - ) - if self.end_date - else self.as_date - ) - - @property - def display_time(self) -> str | None: - """Time for display on web page.""" - return ( - self.date.strftime("%H:%M") - if isinstance(self.date, datetime.datetime) - else None - ) - - @property - def display_timezone(self) -> str | None: - """Timezone for display on web page.""" - return ( - self.date.strftime("%z") - if isinstance(self.date, datetime.datetime) - else None - ) - - def display_duration(self) -> str | None: - """Duration for display.""" - if self.end_as_date != self.as_date or not self.has_time: - return None - - assert isinstance(self.date, datetime.datetime) - assert isinstance(self.end_date, datetime.datetime) - - secs: int = int((self.end_date - self.date).total_seconds()) - - hours: int = secs // 3600 - mins: int = (secs % 3600) // 60 - - if mins == 0: - return f"{hours:d}h" - if hours == 0: - return f"{mins:d} mins" - - return f"{hours:d}h {mins:02d} mins" - - def delta_days(self, today: datetime.date) -> str: - """Return number of days from today as a string.""" - delta = (self.as_date - today).days - - match delta: - case 0: - return "today" - case 1: - return "1 day" - case _: - return f"{delta:,d} days" - - @property - def display_date(self) -> str: - """Date for display on web page.""" - if isinstance(self.date, datetime.datetime): - return self.date.strftime("%a, %d, %b %Y %H:%M %z") - else: - return self.date.strftime("%a, %d, %b %Y") - - @property - def display_title(self) -> str: - """Name for display.""" - return self.title or self.name - - @property - def emoji(self) -> str | None: - """Emoji.""" - if self.title == "LHG Run Club": - return "🏃🍻" - return emojis.get(self.name) - - @property - def title_with_emoji(self) -> str | None: - """Title with optional emoji at the start.""" - title = self.title or self.name - if title is None: - return None - emoji = self.emoji - return f"{emoji} {title}" if emoji else title diff --git a/agenda/events_yaml.py b/agenda/events_yaml.py deleted file mode 100644 index 8678d64..0000000 --- a/agenda/events_yaml.py +++ /dev/null @@ -1,85 +0,0 @@ -"""Read events from YAML.""" - -import os -import typing -from datetime import date, datetime - -import dateutil.rrule -import isodate # type: ignore -import yaml - -from . import uk_tz -from .event import Event - - -def midnight(d: date) -> datetime: - """Convert from date to midnight on that day.""" - return datetime.combine(d, datetime.min.time()) - - -def dates_from_rrule( - rrule: str, start: date, end: date -) -> typing.Sequence[datetime | date]: - """Generate events from an RRULE between start_date and end_date.""" - all_day = not any(param in rrule for param in ["BYHOUR", "BYMINUTE", "BYSECOND"]) - - return [ - i.date() if all_day else uk_tz.localize(i) - for i in dateutil.rrule.rrulestr(rrule, dtstart=midnight(start)).between( - midnight(start), midnight(end) - ) - ] - - -def get_yaml_event_date_field(item: dict[str, str]) -> str: - """Event date field name.""" - return ( - "end_date" - if item["name"] == "travel_insurance" - else ("start_date" if "start_date" in item else "date") - ) - - -def get_yaml_event_end_date_field(item: dict[str, str]) -> str: - """Event date field name.""" - return ( - "end_date" - if item["name"] == "travel_insurance" - else ("start_date" if "start_date" in item else "date") - ) - - -def read( - data_dir: str, start: date, end: date, skip_trips: bool = False -) -> list[Event]: - """Read eventes from YAML file.""" - events: list[Event] = [] - for item in yaml.safe_load(open(os.path.join(data_dir, "events.yaml"))): - if "trip" in item and skip_trips: - continue - duration = ( - isodate.parse_duration(item["duration"]) if "duration" in item else None - ) - dates = ( - dates_from_rrule(item["rrule"], start, end) - if "rrule" in item - else [item[get_yaml_event_date_field(item)]] - ) - for dt in dates: - e = Event( - name=item["name"], - date=dt, - end_date=( - dt + duration - if duration - else ( - item.get("end_date") - if item["name"] != "travel_insurance" - else None - ) - ), - title=item.get("title"), - url=item.get("url"), - ) - events.append(e) - return events diff --git a/agenda/fx.py b/agenda/fx.py index 9a65338..1d48e49 100644 --- a/agenda/fx.py +++ b/agenda/fx.py @@ -9,10 +9,6 @@ from decimal import Decimal import flask import httpx -DEFAULT_FX_CACHE_TTL_HOURS = 24 -DEFAULT_FX_FAILURE_RETRY_HOURS = 24 -FRANKFURTER_CACHE_PREFIX = "frankfurter" - async def get_gbpusd(config: flask.config.Config) -> Decimal: """Get the current value for GBPUSD, with caching.""" @@ -46,235 +42,3 @@ async def get_gbpusd(config: flask.config.Config) -> Decimal: data = json.loads(r.text, parse_float=Decimal) return typing.cast(Decimal, 1 / data["quotes"]["USDGBP"]) - - -def read_cached_rates( - filename: str | None, currencies: list[str] -) -> dict[str, Decimal]: - """Read FX rates from cache.""" - if filename is None: - return {} - - with open(filename) as file: - data = json.load(file, parse_float=Decimal) - - frankfurter_rates = _frankfurter_rates(data, currencies) - if frankfurter_rates: - return frankfurter_rates - - if isinstance(data, list): - return {} - - if not isinstance(data, dict): - return {} - - rates = data.get("rates") - if isinstance(rates, dict): - return {cur: Decimal(rates[cur]) for cur in currencies if cur in rates} - - quotes = data.get("quotes") - if isinstance(quotes, dict): - return { - cur: Decimal(quotes[f"GBP{cur}"]) - for cur in currencies - if f"GBP{cur}" in quotes - } - - return {} - - -def _frankfurter_rates(data: typing.Any, currencies: list[str]) -> dict[str, Decimal]: - """Extract rates from Frankfurter's response format.""" - if not isinstance(data, list): - return {} - - rates: dict[str, Decimal] = {} - for item in data: - if not isinstance(item, dict): - continue - - quote = item.get("quote") - rate = item.get("rate") - if isinstance(quote, str) and quote in currencies and rate is not None: - rates[quote] = Decimal(rate) - - return rates - - -def _fx_cache_datetime(filename: str) -> datetime: - """Extract the cache timestamp from an FX cache filename.""" - return datetime.strptime(filename[:16], "%Y-%m-%d_%H:%M") - - -def _has_required_quotes(filename: str, currencies: list[str]) -> bool: - """Return true if the cache file contains the requested GBP quotes.""" - try: - return len(read_cached_rates(filename, currencies)) == len(currencies) - except (OSError, json.JSONDecodeError): - return False - - -def _latest_file( - fx_dir: str, filenames: list[str], currencies: list[str], *, valid_only: bool -) -> str | None: - """Return the newest matching FX cache filename.""" - matching_files = [ - filename - for filename in filenames - if not valid_only - or _has_required_quotes(os.path.join(fx_dir, filename), currencies) - ] - - return max(matching_files) if matching_files else None - - -def get_rates_exchangerate_host(config: flask.config.Config) -> dict[str, Decimal]: - """Get exchange rates from exchangerate.host. - - Kept as a fallback implementation in case we decide to switch back from - Frankfurter. - """ - currencies = config["CURRENCIES"] - access_key = config["EXCHANGERATE_ACCESS_KEY"] - data_dir = config["DATA_DIR"] - cache_ttl_hours = int(config.get("FX_CACHE_TTL_HOURS", DEFAULT_FX_CACHE_TTL_HOURS)) - failure_retry_hours = int( - config.get("FX_FAILURE_RETRY_HOURS", DEFAULT_FX_FAILURE_RETRY_HOURS) - ) - - now = datetime.now() - now_str = now.strftime("%Y-%m-%d_%H:%M") - fx_dir = os.path.join(data_dir, "fx") - os.makedirs(fx_dir, exist_ok=True) # Ensure the directory exists - - currency_string = ",".join(sorted(currencies)) - file_suffix = f"{currency_string}_to_GBP.json" - existing_data = os.listdir(fx_dir) - existing_files = [f for f in existing_data if f.endswith(file_suffix)] - - latest_attempt = _latest_file(fx_dir, existing_files, currencies, valid_only=False) - latest_valid = _latest_file(fx_dir, existing_files, currencies, valid_only=True) - latest_valid_path = ( - os.path.join(fx_dir, latest_valid) if latest_valid is not None else None - ) - - if latest_valid is not None: - recent = _fx_cache_datetime(latest_valid) - delta = now - recent - - if delta < timedelta(hours=cache_ttl_hours) or config["OFFLINE_MODE"]: - return read_cached_rates(latest_valid_path, currencies) - - if latest_attempt is not None: - recent_attempt = _fx_cache_datetime(latest_attempt) - attempt_delta = now - recent_attempt - if attempt_delta < timedelta(hours=failure_retry_hours): - return read_cached_rates(latest_valid_path, currencies) - - url = "http://api.exchangerate.host/live" - params = {"currencies": currency_string, "source": "GBP", "access_key": access_key} - - filename = f"{now_str}_{file_suffix}" - try: - with httpx.Client() as client: - response = client.get(url, params=params, timeout=10) - except (httpx.ConnectError, httpx.ReadTimeout): - return read_cached_rates(latest_valid_path, currencies) - - try: - data = json.loads(response.text, parse_float=Decimal) - except json.decoder.JSONDecodeError: - return read_cached_rates(latest_valid_path, currencies) - - if not data.get("success", True) or not isinstance(data.get("quotes"), dict): - with open(os.path.join(fx_dir, filename), "w") as file: - file.write(response.text) - return read_cached_rates(latest_valid_path, currencies) - - with open(os.path.join(fx_dir, filename), "w") as file: - file.write(response.text) - - return { - cur: Decimal(data["quotes"][f"GBP{cur}"]) - for cur in currencies - if f"GBP{cur}" in data["quotes"] - } - - -def get_rates(config: flask.config.Config) -> dict[str, Decimal]: - """Get current values of exchange rates for a list of currencies against GBP.""" - currencies = config["CURRENCIES"] - data_dir = config["DATA_DIR"] - cache_ttl_hours = int(config.get("FX_CACHE_TTL_HOURS", DEFAULT_FX_CACHE_TTL_HOURS)) - failure_retry_hours = int( - config.get("FX_FAILURE_RETRY_HOURS", DEFAULT_FX_FAILURE_RETRY_HOURS) - ) - - now = datetime.now() - now_str = now.strftime("%Y-%m-%d_%H:%M") - fx_dir = os.path.join(data_dir, "fx") - os.makedirs(fx_dir, exist_ok=True) # Ensure the directory exists - - currency_string = ",".join(sorted(currencies)) - legacy_file_suffix = f"{currency_string}_to_GBP.json" - frankfurter_file_suffix = f"{FRANKFURTER_CACHE_PREFIX}_{legacy_file_suffix}" - existing_data = os.listdir(fx_dir) - valid_cache_files = [ - f - for f in existing_data - if f.endswith(legacy_file_suffix) or f.endswith(frankfurter_file_suffix) - ] - attempt_files = [f for f in existing_data if f.endswith(frankfurter_file_suffix)] - - latest_attempt = _latest_file(fx_dir, attempt_files, currencies, valid_only=False) - latest_source_valid = _latest_file( - fx_dir, attempt_files, currencies, valid_only=True - ) - latest_valid = _latest_file(fx_dir, valid_cache_files, currencies, valid_only=True) - latest_valid_path = ( - os.path.join(fx_dir, latest_valid) if latest_valid is not None else None - ) - - if config["OFFLINE_MODE"]: - return read_cached_rates(latest_valid_path, currencies) - - if latest_source_valid is not None: - recent = _fx_cache_datetime(latest_source_valid) - delta = now - recent - - if delta < timedelta(hours=cache_ttl_hours): - return read_cached_rates( - os.path.join(fx_dir, latest_source_valid), currencies - ) - - if latest_attempt is not None: - recent_attempt = _fx_cache_datetime(latest_attempt) - attempt_delta = now - recent_attempt - if attempt_delta < timedelta(hours=failure_retry_hours): - return read_cached_rates(latest_valid_path, currencies) - - url = "https://api.frankfurter.dev/v2/rates" - params = {"base": "GBP", "quotes": currency_string} - - filename = f"{now_str}_{frankfurter_file_suffix}" - try: - with httpx.Client() as client: - response = client.get(url, params=params, timeout=10) - except (httpx.ConnectError, httpx.ReadTimeout): - return read_cached_rates(latest_valid_path, currencies) - - try: - data = json.loads(response.text, parse_float=Decimal) - except json.decoder.JSONDecodeError: - return read_cached_rates(latest_valid_path, currencies) - - frankfurter_rates = _frankfurter_rates(data, currencies) - if not frankfurter_rates: - with open(os.path.join(fx_dir, filename), "w") as file: - file.write(response.text) - return read_cached_rates(latest_valid_path, currencies) - - with open(os.path.join(fx_dir, filename), "w") as file: - file.write(response.text) - - return frankfurter_rates diff --git a/agenda/gandi.py b/agenda/gandi.py deleted file mode 100644 index b5f3278..0000000 --- a/agenda/gandi.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Gandi domain renewal dates.""" - -import os -from .event import Event -from datetime import datetime - -import json - - -def get_events(data_dir: str) -> list[Event]: - """Get subscription renewal dates.""" - filename = os.path.join(data_dir, "gandi_domains.json") - - with open(filename) as f: - items = json.load(f) - - assert isinstance(items, list) - assert all(item["fqdn"] and item["dates"]["registry_ends_at"] for item in items) - - return [ - Event( - date=datetime.fromisoformat(item["dates"]["registry_ends_at"]).date(), - name="domain", - title=item["fqdn"] + " renewal", - ) - for item in items - ] diff --git a/agenda/generate_booking_yaml.py b/agenda/generate_booking_yaml.py deleted file mode 100644 index 3ce8ddd..0000000 --- a/agenda/generate_booking_yaml.py +++ /dev/null @@ -1,483 +0,0 @@ -"""Generate travel booking YAML from booking text or a booking URL.""" - -import argparse -import configparser -import importlib -import json -import os -import sys -import typing -from dataclasses import dataclass -from datetime import date, datetime -from pathlib import Path - -import html2text -import lxml.html -import openai -import requests -import yaml - -USER_AGENT = "generate-booking-yaml/0.1" -REPO_ROOT = Path(__file__).resolve().parent.parent -SPEC_PATH = REPO_ROOT / "docs" / "personal-data-yaml.md" -PERSONAL_DATA_DIR = Path("~/src/personal-data").expanduser() - - -class TripLike(typing.Protocol): - """Trip attributes needed for booking import matching.""" - - start: date - end: date | None - - -@dataclass(frozen=True) -class BookingConfig: - """Configuration for one travel booking YAML generator.""" - - booking_type: str - yaml_filename: str - spec_heading: str - start_field: str - excluded_top_level_keys: tuple[str, ...] - json_key: str = "booking" - - -BOOKING_CONFIGS: dict[str, BookingConfig] = { - "flight": BookingConfig( - booking_type="flight", - yaml_filename="flights.yaml", - spec_heading="flights.yaml", - start_field="depart", - excluded_top_level_keys=("trip",), - ), - "train": BookingConfig( - booking_type="train", - yaml_filename="trains.yaml", - spec_heading="trains.yaml", - start_field="depart", - excluded_top_level_keys=("trip",), - ), - "accommodation": BookingConfig( - booking_type="accommodation", - yaml_filename="accommodation.yaml", - spec_heading="accommodation.yaml", - start_field="from", - excluded_top_level_keys=("trip", "latitude", "longitude"), - ), -} - - -def read_api_key() -> str: - """Read API key from ~/.config/openai/config.""" - config_path = os.path.expanduser("~/.config/openai/config") - parser = configparser.ConfigParser() - parser.read(config_path) - return parser["openai"]["api_key"] - - -def read_markdown_section(markdown_text: str, heading: str) -> str: - """Return one second-level markdown section by heading text.""" - section_headings = (f"## `{heading}`", f"## {heading}") - start = -1 - matched_heading = "" - for section_heading in section_headings: - start = markdown_text.find(section_heading) - if start != -1: - matched_heading = section_heading - break - if start == -1: - raise ValueError(f"Could not find section for heading {heading!r}") - - next_heading = markdown_text.find("\n## ", start + len(matched_heading)) - if next_heading == -1: - return markdown_text[start:].strip() - return markdown_text[start:next_heading].strip() - - -def yaml_format_description(config: BookingConfig) -> str: - """Return relevant personal-data YAML documentation for the prompt.""" - spec_text = SPEC_PATH.read_text() - sections = [ - read_markdown_section(spec_text, "General Rules"), - read_markdown_section(spec_text, "Cross-File References"), - read_markdown_section(spec_text, config.spec_heading), - ] - return "\n\n".join(sections) - - -def read_existing_bookings( - config: BookingConfig, data_dir: Path = PERSONAL_DATA_DIR -) -> str: - """Read the existing YAML file for examples and local style.""" - path = data_dir / config.yaml_filename - return path.read_text() - - -def build_prompt( - booking_text: str, - config: BookingConfig, - current_bookings: str | None = None, -) -> str: - """Build prompt to pass to the LLM.""" - bookings = current_bookings - if bookings is None: - bookings = read_existing_bookings(config) - excluded_keys = ", ".join(f'"{key}"' for key in config.excluded_top_level_keys) - - return f""" -I keep a record of all my {config.booking_type} bookings in a YAML file. - -Use this YAML format specification: - -{yaml_format_description(config)} - -Here's my current list of bookings for examples of local style and known -references. -=== -{bookings} -=== -Here's a new booking I just made. - -Return the YAML representation for this booking using the documented format and -the same local style as my existing bookings. - -Rules: -- Wrap the response in a JSON object with a single key "{config.json_key}" that - contains the booking in YAML. -- The value of "{config.json_key}" must be YAML text, not JSON. -- Exclude these top-level keys from the YAML: {excluded_keys}. -- Do not invent details that are not present in the booking text. -- Quote prices and identifiers that might otherwise be parsed as numbers. - -=== -{booking_text} -""" - - -def get_from_open_ai(prompt: str, model: str = "gpt-5.4") -> dict[str, str]: - """Pass prompt to OpenAI and get reply.""" - client = openai.OpenAI(api_key=read_api_key()) - - response = client.chat.completions.create( - messages=[{"role": "user", "content": prompt}], - model=model, - response_format={"type": "json_object"}, - ) - - reply = response.choices[0].message.content - assert isinstance(reply, str) - return typing.cast(dict[str, str], json.loads(reply)) - - -def fetch_webpage(url: str) -> lxml.html.HtmlElement: - """Fetch webpage HTML and parse it.""" - response = requests.get(url, headers={"User-Agent": USER_AGENT}) - response.raise_for_status() - return lxml.html.fromstring(response.content) - - -def webpage_to_text(root: lxml.html.HtmlElement) -> str: - """Convert parsed HTML into readable text content.""" - root_copy = lxml.html.fromstring(lxml.html.tostring(root)) - - for script_or_style in root_copy.xpath("//script|//style"): - script_or_style.drop_tree() - - text_maker = html2text.HTML2Text() - text_maker.ignore_links = False - text_maker.ignore_images = True - return text_maker.handle(lxml.html.tostring(root_copy, encoding="unicode")) - - -def url_to_booking_text(url: str) -> str: - """Fetch a URL and convert it to source text for the model.""" - return webpage_to_text(fetch_webpage(url)) - - -def booking_text_from_args(args: list[str]) -> str: - """Return booking text from a URL argument or stdin.""" - if args: - if len(args) != 1: - raise SystemExit("Usage: generate-BOOKING-booking-yaml [URL]") - return url_to_booking_text(args[0]) - return sys.stdin.read() - - -def generate_booking_yaml( - booking_text: str, config: BookingConfig, model: str = "gpt-5.4" -) -> str: - """Generate booking YAML from source text.""" - prompt = build_prompt(booking_text, config) - return get_from_open_ai(prompt, model=model)[config.json_key] - - -def datetime_from_yaml_value(value: typing.Any) -> datetime: - """Convert a YAML date/datetime/string value into a datetime.""" - if isinstance(value, datetime): - return value - if isinstance(value, date): - return datetime.combine(value, datetime.min.time()) - if isinstance(value, str): - parsed = datetime.fromisoformat(value) - return parsed - raise TypeError(f"Unsupported departure value: {value!r}") - - -def first_departure(booking: dict[str, typing.Any], config: BookingConfig) -> datetime: - """Return the first departure datetime for a generated booking.""" - if config.booking_type == "flight": - flights = booking["flights"] - assert isinstance(flights, list) - first_flight = flights[0] - assert isinstance(first_flight, dict) - return datetime_from_yaml_value(first_flight["depart"]) - - return datetime_from_yaml_value(booking[config.start_field]) - - -def comparable_departure( - booking: dict[str, typing.Any], config: BookingConfig -) -> datetime: - """Return a timezone-naive departure datetime for sorting.""" - return first_departure(booking, config).replace(tzinfo=None) - - -def generated_bookings_from_yaml(yaml_text: str) -> list[dict[str, typing.Any]]: - """Parse generated booking YAML into a list of booking mappings.""" - loaded = yaml.safe_load(yaml_text) - if isinstance(loaded, dict): - return [typing.cast(dict[str, typing.Any], loaded)] - if isinstance(loaded, list) and all(isinstance(item, dict) for item in loaded): - return typing.cast(list[dict[str, typing.Any]], loaded) - raise ValueError("Generated booking YAML must be a mapping or list of mappings.") - - -def trip_key_position(booking: dict[str, typing.Any], config: BookingConfig) -> int: - """Return the preferred insertion position for the top-level trip key.""" - keys = list(booking) - if config.booking_type == "flight": - if "booking_reference" in booking: - return keys.index("booking_reference") + 1 - return 0 - - if config.booking_type == "accommodation": - if "country" in booking: - return keys.index("country") + 1 - if "location" in booking: - return keys.index("location") + 1 - return 0 - - if "to" in booking: - return keys.index("to") + 1 - if "from" in booking: - return keys.index("from") + 1 - return 0 - - -def set_trip_key( - booking: dict[str, typing.Any], config: BookingConfig, trip_date: date -) -> dict[str, typing.Any]: - """Set trip in the usual top-level position while preserving other key order.""" - without_trip = {key: value for key, value in booking.items() if key != "trip"} - keys = list(without_trip) - position = trip_key_position(without_trip, config) - reordered: dict[str, typing.Any] = {} - - for index, key in enumerate(keys): - if index == position: - reordered["trip"] = trip_date - reordered[key] = without_trip[key] - - if "trip" not in reordered: - reordered["trip"] = trip_date - - booking.clear() - booking.update(reordered) - return booking - - -def build_trips(data_dir: Path) -> list[TripLike]: - """Build trips from personal data without importing agenda.trip at module load.""" - trip_module = importlib.import_module("agenda.trip") - build_trip_list = typing.cast( - typing.Callable[..., list[TripLike]], trip_module.build_trip_list - ) - return build_trip_list(data_dir=str(data_dir)) - - -def matching_trip_date(depart: datetime, data_dir: Path = PERSONAL_DATA_DIR) -> date: - """Find the trip grouping date for a departure, falling back to departure date.""" - depart_date = depart.date() - matching_starts: list[date] = [] - - for trip in build_trips(data_dir): - trip_end = trip.end or trip.start - if trip.start <= depart_date <= trip_end: - matching_starts.append(trip.start) - - if matching_starts: - return max(matching_starts) - return depart_date - - -def add_trip_dates( - bookings: list[dict[str, typing.Any]], - config: BookingConfig, - data_dir: Path = PERSONAL_DATA_DIR, -) -> None: - """Add the top-level trip key to generated booking mappings.""" - for booking in bookings: - trip_date = matching_trip_date(first_departure(booking, config), data_dir) - set_trip_key(booking, config, trip_date) - - -def dump_generated_bookings(bookings: list[dict[str, typing.Any]]) -> str: - """Dump only generated bookings for insertion into an existing YAML list.""" - text = yaml.dump(bookings, sort_keys=False, allow_unicode=True) - return text.lstrip() - - -def join_yaml_list_blocks(preamble: str, blocks: list[str], trailing: str = "") -> str: - """Join top-level YAML list blocks with a blank line between items.""" - body = "\n\n".join(block.rstrip("\n") for block in blocks) - return preamble + body + "\n" + trailing - - -def split_yaml_list_blocks(text: str) -> tuple[str, list[str], str]: - """Split a top-level YAML list into preamble, item blocks, and trailing text.""" - lines = text.splitlines(keepends=True) - first_item = next( - (index for index, line in enumerate(lines) if line.startswith("- ")), None - ) - if first_item is None: - return text, [], "" - - item_starts = [ - index - for index, line in enumerate(lines[first_item:], start=first_item) - if line.startswith("- ") - ] - blocks = [ - "".join(lines[start:end]) - for start, end in zip(item_starts, item_starts[1:] + [len(lines)]) - ] - return "".join(lines[:first_item]), blocks, "" - - -def existing_bookings_from_blocks(blocks: list[str]) -> list[dict[str, typing.Any]]: - """Parse split YAML item blocks into booking mappings.""" - bookings = [] - for block in blocks: - loaded = yaml.safe_load(block) - if not isinstance(loaded, list) or len(loaded) != 1: - raise ValueError("Could not parse existing booking block.") - item = loaded[0] - if not isinstance(item, dict): - raise ValueError("Existing booking block is not a mapping.") - bookings.append(typing.cast(dict[str, typing.Any], item)) - return bookings - - -def insertion_index( - existing_bookings: list[dict[str, typing.Any]], - new_bookings: list[dict[str, typing.Any]], - config: BookingConfig, -) -> int: - """Return the chronological insertion index for generated bookings.""" - new_depart = min(comparable_departure(booking, config) for booking in new_bookings) - for index, booking in enumerate(existing_bookings): - if comparable_departure(booking, config) > new_depart: - return index - return len(existing_bookings) - - -def insert_booking_text( - existing_text: str, - new_yaml_text: str, - config: BookingConfig, -) -> str: - """Insert generated booking YAML into an existing top-level YAML list.""" - preamble, blocks, trailing = split_yaml_list_blocks(existing_text) - new_bookings = generated_bookings_from_yaml(new_yaml_text) - _, new_blocks, _ = split_yaml_list_blocks(dump_generated_bookings(new_bookings)) - if len(new_blocks) != len(new_bookings): - raise ValueError("Could not split generated booking YAML into item blocks.") - - existing_bookings = existing_bookings_from_blocks(blocks) - - new_items = sorted( - zip(new_bookings, new_blocks), - key=lambda item: comparable_departure(item[0], config), - ) - for booking, block in new_items: - insert_at = insertion_index(existing_bookings, [booking], config) - existing_bookings.insert(insert_at, booking) - blocks.insert(insert_at, block) - - return join_yaml_list_blocks(preamble, blocks, trailing) - - -def import_booking_yaml( - generated_yaml: str, - config: BookingConfig, - data_dir: Path = PERSONAL_DATA_DIR, -) -> int: - """Add generated booking YAML to the configured personal-data file.""" - bookings = generated_bookings_from_yaml(generated_yaml) - add_trip_dates(bookings, config, data_dir) - new_yaml = dump_generated_bookings(bookings) - - yaml_path = data_dir / config.yaml_filename - existing_text = yaml_path.read_text() - updated_text = insert_booking_text(existing_text, new_yaml, config) - yaml_path.write_text(updated_text) - return len(bookings) - - -def main_for_type(booking_type: str, argv: list[str] | None = None) -> int: - """CLI entrypoint for a specific booking type.""" - parser = argparse.ArgumentParser( - description=( - f"Generate {booking_type} booking YAML from stdin or a URL and import it." - ) - ) - parser.add_argument("url", nargs="?", help="Booking URL to fetch") - parser.add_argument("--model", default=os.environ.get("OPENAI_MODEL", "gpt-5.4")) - parser.add_argument( - "--data-dir", - default=str(PERSONAL_DATA_DIR), - help="Directory containing personal-data YAML files.", - ) - parser.add_argument( - "--print-only", - action="store_true", - help="Print generated YAML instead of editing the personal-data file.", - ) - parsed = parser.parse_args(argv) - - config = BOOKING_CONFIGS[booking_type] - args = [parsed.url] if parsed.url else [] - booking_text = booking_text_from_args(args) - new_yaml = generate_booking_yaml(booking_text, config, model=parsed.model) - if parsed.print_only: - print(new_yaml) - return 0 - - count = import_booking_yaml(new_yaml, config, data_dir=Path(parsed.data_dir)) - print(f"Imported {count} {booking_type} booking(s).") - return 0 - - -def train_main(argv: list[str] | None = None) -> int: - """CLI entrypoint for train booking YAML generation.""" - return main_for_type("train", argv) - - -def flight_main(argv: list[str] | None = None) -> int: - """CLI entrypoint for flight booking YAML generation.""" - return main_for_type("flight", argv) - - -def accommodation_main(argv: list[str] | None = None) -> int: - """CLI entrypoint for accommodation booking YAML generation.""" - return main_for_type("accommodation", argv) diff --git a/agenda/geomob.py b/agenda/geomob.py deleted file mode 100644 index f7383b3..0000000 --- a/agenda/geomob.py +++ /dev/null @@ -1,110 +0,0 @@ -"""Geomob events.""" - -import os -from dataclasses import dataclass -from datetime import date, datetime -from typing import List - -import dateutil.parser -import flask -import lxml.html -import requests - -import agenda.mail -import agenda.utils - - -@dataclass(frozen=True) -class GeomobEvent: - """Geomob event.""" - - date: date - href: str - hashtag: str - - -def extract_events( - tree: lxml.html.HtmlElement, -) -> List[GeomobEvent]: - """Extract upcoming events from the HTML content.""" - events = [] - - for event in tree.xpath('//ol[@class="event-list"]/li/a'): - date_str, _, hashtag = event.text_content().strip().rpartition(" ") - events.append( - GeomobEvent( - date=dateutil.parser.parse(date_str).date(), - href=event.get("href"), - hashtag=hashtag, - ) - ) - - return events - - -def find_new_events( - prev: list[GeomobEvent], cur: list[GeomobEvent] -) -> list[GeomobEvent]: - """Find new events that appear in cur but not in prev.""" - return list(set(cur) - set(prev)) - - -def geomob_email(new_events: list[GeomobEvent], base_url: str) -> tuple[str, str]: - """Generate email subject and body for new events. - - Args: - new_events (List[Event]): List of new events. - base_url (str): The base URL of the website. - - Returns: - tuple[str, str]: Email subject and body. - """ - assert new_events - - subject = f"{len(new_events)} New Geomob Event(s) Announced" - - body_lines = ["Hello,\n", "Here are the new Geomob events:\n"] - for event in new_events: - url = base_url + event.href - # Check for double slashes in the path part only (after protocol) - if "://" in url: - protocol, rest = url.split("://", 1) - assert "//" not in rest, f"Double slash found in URL path: {url}" - else: - assert "//" not in url, f"Double slash found in URL: {url}" - event_details = f"Date: {event.date}\nURL: {url}\nHashtag: {event.hashtag}\n" - body_lines.append(event_details) - body_lines.append("-" * 40) - - body = "\n".join(body_lines) - return (subject, body) - - -def get_cached_upcoming_events_list(geomob_dir: str) -> list[GeomobEvent]: - """Get known geomob events.""" - filename = agenda.utils.get_most_recent_file(geomob_dir, "html") - return extract_events(lxml.html.parse(filename).getroot()) if filename else [] - - -def update(config: flask.config.Config) -> None: - """Get upcoming Geomob events and report new ones.""" - geomob_dir = os.path.join(config["DATA_DIR"], "geomob") - - prev_events = get_cached_upcoming_events_list(geomob_dir) - r = requests.get("https://thegeomob.com/") - cur_events = extract_events(lxml.html.fromstring(r.content)) - - if cur_events == prev_events: - return # no change - - now = datetime.now() - new_filename = os.path.join(geomob_dir, now.strftime("%Y-%m-%d_%H:%M:%S.html")) - open(new_filename, "w").write(r.text) - - new_events = list(set(cur_events) - set(prev_events)) - if not new_events: - return - - base_url = "https://thegeomob.com" - subject, body = geomob_email(new_events, base_url) - agenda.mail.send_mail(config, subject, body) diff --git a/agenda/gwr.py b/agenda/gwr.py index cc7e968..5806939 100644 --- a/agenda/gwr.py +++ b/agenda/gwr.py @@ -10,30 +10,6 @@ import httpx url = "https://www.gwr.com/your-tickets/choosing-your-ticket/advance-tickets" -def parse_date_string(date_str: str) -> date: - """Parse date string from HTML.""" - if not date_str[-1].isdigit(): # If the year is missing, use the current year - date_str += f" {date.today().year}" - - return datetime.strptime(date_str, "%A %d %B %Y").date() - - -def extract_dates(html: str) -> None | dict[str, date]: - """Extract dates from HTML.""" - pattern = re.compile( - r"\s*(Weekdays|Saturdays|Sundays)*" - + r"\s*(.*?)(?:\*\*)?\s*", - ) - - if not pattern.search(html): - return None - - return { - match.group(1): parse_date_string(match.group(2)) - for match in pattern.finditer(html) - } - - def extract_weekday_date(html: str) -> date | None: """Furthest date of GWR advance ticket booking.""" # Compile a regular expression pattern to match the relevant table row @@ -42,19 +18,22 @@ def extract_weekday_date(html: str) -> date | None: ) # Search the HTML for the pattern - if match := pattern.search(html): - return parse_date_string(match.group(1)) - else: + if not (match := pattern.search(html)): return None + date_str = match.group(1) + + # If the year is missing, use the current year + if not date_str[-1].isdigit(): + date_str += f" {date.today().year}" + + return datetime.strptime(date_str, "%A %d %B %Y").date() -async def advance_tickets_page_html( - data_dir: str, ttl: int = 60 * 60 * 6, force_cache: bool = False -) -> str: +async def advance_tickets_page_html(data_dir: str, ttl: int = 60 * 60 * 6) -> str: """Get advance-tickets web page HTML with cache.""" filename = os.path.join(data_dir, "advance-tickets.html") mtime = os.path.getmtime(filename) if os.path.exists(filename) else 0 - if force_cache or (time() - mtime) < ttl: # use cache + if (time() - mtime) < ttl: # use cache return open(filename).read() async with httpx.AsyncClient() as client: r = await client.get(url) @@ -63,7 +42,7 @@ async def advance_tickets_page_html( return html -async def advance_ticket_date(data_dir: str, force_cache: bool = False) -> date | None: +async def advance_ticket_date(data_dir: str) -> date | None: """Get GWR advance tickets date with cache.""" - html = await advance_tickets_page_html(data_dir, force_cache=force_cache) + html = await advance_tickets_page_html(data_dir) return extract_weekday_date(html) diff --git a/agenda/hn.py b/agenda/hn.py index 52bdb0a..9f11e91 100644 --- a/agenda/hn.py +++ b/agenda/hn.py @@ -5,7 +5,7 @@ from datetime import date, datetime, time, timedelta import pytz from dateutil.relativedelta import relativedelta -from .event import Event +from .types import Event eastern_time = pytz.timezone("America/New_York") diff --git a/agenda/holidays.py b/agenda/holidays.py deleted file mode 100644 index 175e798..0000000 --- a/agenda/holidays.py +++ /dev/null @@ -1,184 +0,0 @@ -"""Holidays.""" - -import collections -from datetime import date, timedelta - -import flask - -import agenda.uk_holiday -import holidays -from agenda.uk_school_holiday import school_holiday_list - -from .event import Event -from .types import Holiday, Trip - - -def get_trip_holidays(trip: Trip) -> list[Holiday]: - """Get holidays happening during trip.""" - if not trip.end: - return [] - countries = {c.alpha_2 for c in trip.countries} - return sorted( - ( - hol - for hol in get_all( - trip.start, trip.end, flask.current_app.config["DATA_DIR"] - ) - if hol.country.upper() in countries - ), - key=lambda item: (item.date, item.country), - ) - - -def get_school_holidays(start_date: date, end_date: date, data_dir: str) -> list[Event]: - """Get UK school holidays from cache.""" - return school_holiday_list(start_date, end_date, data_dir) - - -def get_trip_school_holidays(trip: Trip) -> list[Event]: - """Get UK school holidays happening during trip.""" - if not trip.end: - return [] - - return get_school_holidays( - trip.start, - trip.end, - flask.current_app.config["DATA_DIR"], - ) - - -def us_holidays(start_date: date, end_date: date) -> list[Holiday]: - """Get US holidays.""" - found: list[Holiday] = [] - for year in range(start_date.year, end_date.year + 1): - hols = holidays.country_holidays("US", years=year, language="en") - found += [ - Holiday(date=hol_date, name=title, country="us") - for hol_date, title in hols.items() - if start_date < hol_date < end_date - ] - - extra = [] - for h in found: - if h.name != "Thanksgiving": - continue - extra += [ - Holiday(date=h.date + timedelta(days=1), name="Black Friday", country="us"), - Holiday(date=h.date + timedelta(days=4), name="Cyber Monday", country="us"), - ] - - return found + extra - - -def get_nyse_holidays( - start_date: date, end_date: date, us_hols: list[Holiday] -) -> list[Event]: - """NYSE holidays.""" - known_us_hols = {(h.date, h.name) for h in us_hols} - found: list[Event] = [] - rename = {"Thanksgiving Day": "Thanksgiving"} - for year in range(start_date.year, end_date.year + 1): - hols = holidays.financial_holidays("NYSE", years=year) - found += [ - Event( - name="holiday", - date=hol_date, - title=rename.get(title, title), - ) - for hol_date, title in hols.items() - if start_date <= hol_date <= end_date - ] - found = [hol for hol in found if (hol.date, hol.title) not in known_us_hols] - for hol in found: - assert hol.title - hol.title += " (NYSE)" - return found - - -def get_holidays(country: str, start_date: date, end_date: date) -> list[Holiday]: - """Get holidays.""" - found: list[Holiday] = [] - uc_country = country.upper() - - holiday_country = getattr(holidays, uc_country) - default_language = holiday_country.default_language - - def skip_holiday(holiday_name: str) -> bool: - """Skip holiday.""" - return country == "se" and holiday_name == "Sunday" - - for year in range(start_date.year, end_date.year + 1): - en_hols = holidays.country_holidays(uc_country, years=year, language="en_US") - local_lang = holidays.country_holidays( - uc_country, years=year, language=default_language - ) - found += [ - Holiday( - date=hol_date, - name=title, - local_name=local_lang[hol_date], - country=country.lower(), - ) - for hol_date, title in en_hols.items() - if start_date <= hol_date <= end_date and not skip_holiday(title) - ] - - return found - - -def combine_holidays(holidays: list[Holiday]) -> list[Event]: - """Combine UK and US holidays with the same date and title.""" - all_countries = {h.country for h in holidays} - - standard_name = { - (1, 1): "New Year's Day", - (1, 6): "Epiphany", - (5, 1): "Labour Day", - (8, 15): "Assumption Day", - (12, 8): "Immaculate conception", - (12, 25): "Christmas Day", - (12, 26): "Boxing Day", - } - - combined: collections.defaultdict[tuple[date, str], set[str]] = ( - collections.defaultdict(set) - ) - - for h in holidays: - assert isinstance(h.name, str) and isinstance(h.date, date) - - event_key = (h.date, standard_name.get((h.date.month, h.date.day), h.name)) - combined[event_key].add(h.country) - - events: list[Event] = [] - for (d, name), countries in combined.items(): - if len(countries) == len(all_countries): - country_list = "" - elif len(countries) < len(all_countries) / 2: - country_list = ", ".join(sorted(country.upper() for country in countries)) - else: - country_list = "not " + ", ".join( - sorted(country.upper() for country in all_countries - set(countries)) - ) - - e = Event( - name="holiday", - date=d, - title=f"{name} ({country_list})" if country_list else name, - ) - events.append(e) - - return events - - -def get_all(last_year: date, next_year: date, data_dir: str) -> list[Holiday]: - """Get holidays for various countries and return as a list.""" - us_hols = us_holidays(last_year, next_year) - - bank_holidays = agenda.uk_holiday.bank_holiday_list(last_year, next_year, data_dir) - - holiday_list: list[Holiday] = bank_holidays + us_hols - for country in flask.current_app.config["HOLIDAY_COUNTRIES"]: - holiday_list += get_holidays(country, last_year, next_year) - - return holiday_list diff --git a/agenda/ical.py b/agenda/ical.py deleted file mode 100644 index f180cc5..0000000 --- a/agenda/ical.py +++ /dev/null @@ -1,53 +0,0 @@ -"""Shared helpers for generating iCalendar feeds.""" - -from __future__ import annotations - -from datetime import date, datetime, timezone -from typing import Iterable - - -def escape_text(value: str) -> str: - """Escape text for safer ICS output.""" - return ( - value.replace("\\", "\\\\") - .replace(";", "\\;") - .replace(",", "\\,") - .replace("\n", "\\n") - ) - - -def _fold_line(value: str) -> Iterable[str]: - """Yield RFC5545 folded lines.""" - if len(value) <= 75: - yield value - return - - remaining = value - first = True - while remaining: - segment = remaining[:75] - remaining = remaining[75:] - if not first: - segment = " " + segment - yield segment - first = False - - -def append_property(lines: list[str], name: str, value: str) -> None: - """Append a folded property line to the ICS output.""" - for line in _fold_line(f"{name}:{value}"): - lines.append(line) - - -def format_datetime_utc(dt_value: datetime) -> str: - """Return datetime formatted in UTC for ICS.""" - if dt_value.tzinfo is None: - dt_value = dt_value.replace(tzinfo=timezone.utc) - else: - dt_value = dt_value.astimezone(timezone.utc) - return dt_value.strftime("%Y%m%dT%H%M%SZ") - - -def format_date(date_value: date) -> str: - """Return date formatted for all-day ICS events.""" - return date_value.strftime("%Y%m%d") diff --git a/agenda/mail.py b/agenda/mail.py deleted file mode 100644 index ac629eb..0000000 --- a/agenda/mail.py +++ /dev/null @@ -1,28 +0,0 @@ -"""Send e-mail.""" - -import smtplib -from email.message import EmailMessage -from email.utils import formatdate, make_msgid - -import flask - - -def send_mail(config: flask.config.Config, subject: str, body: str) -> None: - """Send an e-mail.""" - msg = EmailMessage() - - msg["Subject"] = subject - msg["To"] = f"{config['NAME']} <{config['MAIL_TO']}>" - msg["From"] = f"{config['NAME']} <{config['MAIL_FROM']}>" - msg["Date"] = formatdate() - msg["Message-ID"] = make_msgid() - - # Add extra mail headers - for header, value in config["MAIL_HEADERS"]: - msg[header] = value - - msg.set_content(body) - - s = smtplib.SMTP(config["SMTP_HOST"]) - s.sendmail(config["MAIL_TO"], [config["MAIL_TO"]], msg.as_string()) - s.quit() diff --git a/agenda/meetup.py b/agenda/meetup.py index df52a23..ab80d16 100644 --- a/agenda/meetup.py +++ b/agenda/meetup.py @@ -4,7 +4,7 @@ import json import os.path from datetime import datetime -from .event import Event +from .types import Event def get_events(data_dir: str) -> list[Event]: @@ -21,7 +21,7 @@ def get_events(data_dir: str) -> list[Event]: date=start, end_date=end, name="meetup", - title=item_event["title"], + title="👥" + item_event["title"], url=item_event["eventUrl"], ) events.append(e) diff --git a/agenda/meteors.py b/agenda/meteors.py deleted file mode 100644 index 5b6e5cb..0000000 --- a/agenda/meteors.py +++ /dev/null @@ -1,270 +0,0 @@ -"""Meteor shower data calculations.""" - -import typing -from datetime import datetime, timedelta - -import ephem # type: ignore - -MeteorShower = dict[str, typing.Any] - - -# Meteor shower definitions with parent comet orbital elements -METEOR_SHOWERS = { - "Quadrantids": { - "name": "Quadrantids", - "radiant_ra": "15h20m", # Right ascension at peak - "radiant_dec": "+49.5°", # Declination at peak - "peak_solar_longitude": 283.16, # Solar longitude at peak - "activity_start": 283.16 - 10, # Activity period - "activity_end": 283.16 + 10, - "rate_max": 120, - "rate_min": 50, - "parent_body": "2003 EH1", - "visibility": "Northern Hemisphere", - "description": "The year kicks off with the Quadrantids, known for their brief but intense peak lasting only about 4 hours.", - "velocity_kms": 41, - }, - "Lyrids": { - "name": "Lyrids", - "radiant_ra": "18h04m", - "radiant_dec": "+32.32°", - "peak_solar_longitude": 32.32, - "activity_start": 32.32 - 10, - "activity_end": 32.32 + 10, - "rate_max": 18, - "rate_min": 10, - "parent_body": "C/1861 G1 (Thatcher)", - "visibility": "Both hemispheres", - "description": "The Lyrids are one of the oldest recorded meteor showers, with observations dating back 2,700 years.", - "velocity_kms": 49, - }, - "Eta Aquariids": { - "name": "Eta Aquariids", - "radiant_ra": "22h32m", - "radiant_dec": "-1.0°", - "peak_solar_longitude": 45.5, - "activity_start": 45.5 - 15, - "activity_end": 45.5 + 15, - "rate_max": 60, - "rate_min": 30, - "parent_body": "1P/Halley", - "visibility": "Southern Hemisphere (best)", - "description": "Created by debris from Halley's Comet, these meteors are fast and often leave glowing trails.", - "velocity_kms": 66, - }, - "Perseids": { - "name": "Perseids", - "radiant_ra": "03h04m", - "radiant_dec": "+58.0°", - "peak_solar_longitude": 140.0, - "activity_start": 140.0 - 15, - "activity_end": 140.0 + 15, - "rate_max": 100, - "rate_min": 50, - "parent_body": "109P/Swift-Tuttle", - "visibility": "Northern Hemisphere", - "description": "One of the most popular meteor showers, viewing conditions vary by year based on moon phase.", - "velocity_kms": 59, - }, - "Orionids": { - "name": "Orionids", - "radiant_ra": "06h20m", - "radiant_dec": "+16.0°", - "peak_solar_longitude": 208.0, - "activity_start": 208.0 - 15, - "activity_end": 208.0 + 15, - "rate_max": 25, - "rate_min": 15, - "parent_body": "1P/Halley", - "visibility": "Both hemispheres", - "description": "Another shower created by Halley's Comet debris, known for their speed and brightness.", - "velocity_kms": 66, - }, - "Geminids": { - "name": "Geminids", - "radiant_ra": "07h28m", - "radiant_dec": "+32.0°", - "peak_solar_longitude": 262.2, - "activity_start": 262.2 - 10, - "activity_end": 262.2 + 10, - "rate_max": 120, - "rate_min": 60, - "parent_body": "3200 Phaethon", - "visibility": "Both hemispheres", - "description": "The best shower of most years with the highest rates. Unusual for being caused by an asteroid rather than a comet.", - "velocity_kms": 35, - }, - "Ursids": { - "name": "Ursids", - "radiant_ra": "14h28m", - "radiant_dec": "+75.0°", - "peak_solar_longitude": 270.7, - "activity_start": 270.7 - 5, - "activity_end": 270.7 + 5, - "rate_max": 10, - "rate_min": 5, - "parent_body": "8P/Tuttle", - "visibility": "Northern Hemisphere", - "description": "A minor shower that closes out the year, best viewed from dark locations away from city lights.", - "velocity_kms": 33, - }, -} - - -def calculate_solar_longitude_date(year: int, target_longitude: float) -> datetime: - """Calculate the date when the Sun reaches a specific longitude for a given year.""" - # Start from beginning of year - start_date = datetime(year, 1, 1) - - # Use PyEphem to calculate solar longitude - observer = ephem.Observer() - observer.lat = "0" # Equator - observer.lon = "0" # Greenwich - observer.date = start_date - - sun = ephem.Sun(observer) - - # Search for the date when solar longitude matches target - # Solar longitude 0° = Spring Equinox (around March 20) - # We need to find when sun reaches the target longitude - - # Approximate: start search from reasonable date based on longitude - if target_longitude < 90: # Spring (Mar-Jun) - search_start = datetime(year, 3, 1) - elif target_longitude < 180: # Summer (Jun-Sep) - search_start = datetime(year, 6, 1) - elif target_longitude < 270: # Fall (Sep-Dec) - search_start = datetime(year, 9, 1) - else: # Winter (Dec-Mar) - search_start = datetime(year, 12, 1) - - observer.date = search_start - - # Search within a reasonable range (±60 days) - for day_offset in range(-60, 61): - test_date = search_start + timedelta(days=day_offset) - observer.date = test_date - sun.compute(observer) - - # Convert ecliptic longitude to degrees - sun_longitude = float(sun.hlon) * 180 / ephem.pi - - # Check if we're close to target longitude (within 0.5 degrees) - if abs(sun_longitude - target_longitude) < 0.5: - return test_date - - # Fallback: return approximation based on solar longitude - # Rough approximation: solar longitude increases ~1° per day - days_from_equinox = target_longitude - equinox_date = datetime(year, 3, 20) # Approximate spring equinox - return equinox_date + timedelta(days=days_from_equinox) - - -def calculate_moon_phase(date_obj: datetime) -> tuple[float, str]: - """Calculate moon phase for a given date.""" - observer = ephem.Observer() - observer.date = date_obj - - moon = ephem.Moon(observer) - moon.compute(observer) - - # Moon phase (0 = new moon, 0.5 = full moon, 1 = new moon again) - phase = moon.phase / 100.0 - - # Determine moon phase name and viewing quality - if phase < 0.1: - phase_name = "New Moon" - viewing_quality = "excellent" - elif phase < 0.3: - phase_name = "Waxing Crescent" - viewing_quality = "good" - elif phase < 0.7: - phase_name = "First Quarter" if phase < 0.5 else "Waxing Gibbous" - viewing_quality = "moderate" - elif phase < 0.9: - phase_name = "Full Moon" - viewing_quality = "poor" - else: - phase_name = "Waning Crescent" - viewing_quality = "good" - - return phase, f"{phase_name} ({viewing_quality} viewing)" - - -def calculate_meteor_shower_data(year: int) -> list[MeteorShower]: - """Calculate meteor shower data for a given year using astronomical calculations.""" - meteor_data = [] - - for shower_id, shower_info in METEOR_SHOWERS.items(): - # Calculate peak date based on solar longitude - peak_longitude = shower_info["peak_solar_longitude"] - assert isinstance(peak_longitude, (int, float)) - peak_date = calculate_solar_longitude_date(year, peak_longitude) - - # Calculate activity period - start_longitude = shower_info["activity_start"] - assert isinstance(start_longitude, (int, float)) - activity_start = calculate_solar_longitude_date(year, start_longitude) - - end_longitude = shower_info["activity_end"] - assert isinstance(end_longitude, (int, float)) - activity_end = calculate_solar_longitude_date(year, end_longitude) - - # Calculate moon phase at peak - moon_illumination, moon_phase_desc = calculate_moon_phase(peak_date) - - # Format dates - peak_formatted = peak_date.strftime("%B %d") - if peak_date.day != (peak_date + timedelta(days=1)).day: - peak_formatted += f"-{(peak_date + timedelta(days=1)).strftime('%d')}" - - active_formatted = ( - f"{activity_start.strftime('%B %d')} - {activity_end.strftime('%B %d')}" - ) - - # Determine viewing quality based on moon phase - viewing_quality = ( - "excellent" - if moon_illumination < 0.3 - else ( - "good" - if moon_illumination < 0.7 - else "moderate" if moon_illumination < 0.9 else "poor" - ) - ) - - meteor_shower = { - "name": shower_info["name"], - "peak": peak_formatted, - "active": active_formatted, - "rate": f"{shower_info['rate_min']}-{shower_info['rate_max']} meteors per hour", - "radiant": str(shower_info["radiant_ra"]).split("h")[0] - + "h " - + str(shower_info["radiant_dec"]), - "moon_phase": moon_phase_desc, - "visibility": shower_info["visibility"], - "description": shower_info["description"], - "peak_date": peak_date.strftime("%Y-%m-%d"), - "start_date": activity_start.strftime("%Y-%m-%d"), - "end_date": activity_end.strftime("%Y-%m-%d"), - "rate_min": shower_info["rate_min"], - "rate_max": shower_info["rate_max"], - "moon_illumination": moon_illumination, - "viewing_quality": viewing_quality, - "parent_body": shower_info["parent_body"], - "velocity_kms": shower_info["velocity_kms"], - } - - meteor_data.append(meteor_shower) - - # Sort by peak date - meteor_data.sort(key=lambda x: datetime.strptime(str(x["peak_date"]), "%Y-%m-%d")) - - return meteor_data - - -def get_meteor_data(year: int | None = None) -> list[MeteorShower]: - """Get meteor shower data for a specific year using astronomical calculations.""" - if year is None: - year = datetime.now().year - return calculate_meteor_shower_data(year) diff --git a/agenda/n_somerset_waste.py b/agenda/n_somerset_waste.py deleted file mode 100644 index 2d9cddd..0000000 --- a/agenda/n_somerset_waste.py +++ /dev/null @@ -1,93 +0,0 @@ -"""Waste collection schedules.""" - -import os -import re -from collections import defaultdict -from datetime import date, datetime, time, timedelta - -import httpx -import lxml.html - -from . import uk_time -from .event import Event -from .utils import make_waste_dir - -ttl_hours = 12 - - -async def get_html( - data_dir: str, postcode: str, uprn: str, force_cache: bool = False -) -> str: - """Get waste schedule.""" - now = datetime.now() - waste_dir = os.path.join(data_dir, "waste") - - make_waste_dir(data_dir) - - existing_data = os.listdir(waste_dir) - existing = [f for f in existing_data if f.endswith(".html")] - if existing: - recent_filename = max(existing) - recent = datetime.strptime(recent_filename, "%Y-%m-%d_%H:%M.html") - delta = now - recent - - if existing and (force_cache or delta < timedelta(hours=ttl_hours)): - return open(os.path.join(waste_dir, recent_filename)).read() - - now_str = now.strftime("%Y-%m-%d_%H:%M") - filename = f"{waste_dir}/{now_str}.html" - - forms_base_url = "https://forms.n-somerset.gov.uk" - url = "https://forms.n-somerset.gov.uk/Waste/CollectionSchedule" - async with httpx.AsyncClient() as client: - r = await client.post( - url, - data={ - "PreviousHouse": "", - "PreviousPostcode": "-", - "Postcode": postcode, - "SelectedUprn": uprn, - }, - ) - form_post_html = r.text - pattern = r'

Object moved to here<\/a>\.<\/h2>' - m = re.search(pattern, form_post_html) - if m: - r = await client.get(forms_base_url + m.group(1)) - html = r.text - open(filename, "w").write(html) - return html - - -def parse_waste_schedule_date(day_and_month: str) -> date: - """Parse waste schedule date.""" - today = date.today() - fmt = "%A %d %B %Y" - d = datetime.strptime(f"{day_and_month} {today.year}", fmt).date() - if d < today: - d = datetime.strptime(f"{day_and_month} {today.year + 1}", fmt).date() - return d - - -def parse(root: lxml.html.HtmlElement) -> list[Event]: - """Parse waste schedule.""" - tbody = root.find(".//table/tbody") - assert tbody is not None - by_date = defaultdict(list) - for e_service, e_next_date, e_following in tbody: - assert e_service.text and e_next_date.text and e_following.text - service = e_service.text - next_date = parse_waste_schedule_date(e_next_date.text) - following_date = parse_waste_schedule_date(e_following.text) - - by_date[next_date].append(service) - by_date[following_date].append(service) - - return [ - Event( - name="waste_schedule", - date=uk_time(d, time(6, 30)), - title="Backwell: " + ", ".join(services), - ) - for d, services in by_date.items() - ] diff --git a/agenda/schengen.py b/agenda/schengen.py deleted file mode 100644 index 7f1d355..0000000 --- a/agenda/schengen.py +++ /dev/null @@ -1,299 +0,0 @@ -"""Schengen area rolling time calculator for travel tracking.""" - -from datetime import date, datetime, timedelta - -from .types import SchengenCalculation, SchengenStay, StrDict -from .utils import depart_datetime - -# Schengen Area countries as of 2025 -SCHENGEN_COUNTRIES = { - # EU countries in Schengen - "at", # Austria - "be", # Belgium - "bg", # Bulgaria (joined January 2025) - "hr", # Croatia - "cz", # Czech Republic - "dk", # Denmark - "ee", # Estonia - "fi", # Finland - "fr", # France - "de", # Germany - "gr", # Greece - "hu", # Hungary - "it", # Italy - "lv", # Latvia - "lt", # Lithuania - "lu", # Luxembourg - "mt", # Malta - "nl", # Netherlands - "pl", # Poland - "pt", # Portugal - "ro", # Romania (joined January 2025) - "sk", # Slovakia - "si", # Slovenia - "es", # Spain - "se", # Sweden - # Non-EU countries in Schengen - "is", # Iceland - "li", # Liechtenstein - "no", # Norway - "ch", # Switzerland -} - - -def is_schengen_country(country_code: str) -> bool: - """Check if a country is in the Schengen area.""" - if not country_code or not isinstance(country_code, str): - return False - return country_code.lower() in SCHENGEN_COUNTRIES - - -def extract_schengen_stays_from_travel( - travel_items: list[StrDict], -) -> list[SchengenStay]: - """Extract Schengen stays from travel items.""" - stays: list[SchengenStay] = [] - current_location = None - entry_date = None - - # Handle empty travel items - if not travel_items: - return stays - - # Sort travel items by departure date, filtering out items without depart date - sorted_items = sorted( - [item for item in travel_items if item.get("depart")], - key=lambda x: depart_datetime(x), - ) - - for item in sorted_items: - from_country = None - to_country = None - travel_date = item.get("depart") - - if not travel_date: - continue - - # Extract travel date - if isinstance(travel_date, datetime): - travel_date = travel_date.date() - elif not isinstance(travel_date, date): - # Skip items with invalid travel dates - continue - - # Determine origin and destination countries - if item.get("type") == "flight": - from_airport = item.get("from_airport", {}) - to_airport = item.get("to_airport", {}) - from_country = from_airport.get("country") - to_country = to_airport.get("country") - elif item.get("type") == "train": - from_station = item.get("from_station", {}) - to_station = item.get("to_station", {}) - from_country = from_station.get("country") - to_country = to_station.get("country") - elif item.get("type") == "ferry": - from_terminal = item.get("from_terminal", {}) - to_terminal = item.get("to_terminal", {}) - from_country = from_terminal.get("country") - to_country = to_terminal.get("country") - - # Handle entering/exiting Schengen - if current_location and is_schengen_country(current_location): - # Currently in Schengen - if to_country and not is_schengen_country(to_country): - # Exiting Schengen - use departure date - if entry_date: - stays.append( - SchengenStay( - entry_date=entry_date, - exit_date=travel_date, - country=current_location, - days=0, # Will be calculated in __post_init__ - ) - ) - entry_date = None - else: - # Currently outside Schengen - if to_country and is_schengen_country(to_country): - # Entering Schengen - use arrival date for long-haul flights - arrive_date = item.get("arrive") - assert arrive_date - if isinstance(arrive_date, datetime): - arrive_date = arrive_date.date() - assert isinstance(arrive_date, date) - entry_date = arrive_date - - current_location = to_country - - # Handle case where still in Schengen - if current_location and is_schengen_country(current_location) and entry_date: - stays.append( - SchengenStay( - entry_date=entry_date, - exit_date=None, # Still in Schengen - country=current_location, - days=0, # Will be calculated in __post_init__ - ) - ) - - return stays - - -def calculate_schengen_time( - travel_items: list[StrDict], calculation_date: date | None = None -) -> SchengenCalculation: - """ - Calculate Schengen rolling time compliance. - - Args: - travel_items: List of travel items from the trip system - calculation_date: Date to calculate from (defaults to today) - - Returns: - SchengenCalculation with compliance status and details - """ - if calculation_date is None: - calculation_date = date.today() - - # Extract Schengen stays from travel data - stays = extract_schengen_stays_from_travel(travel_items) - - # Calculate 180-day window (ending on calculation_date) - window_start = calculation_date - timedelta(days=179) - window_end = calculation_date - - # Find stays that overlap with the 180-day window - relevant_stays = [] - total_days = 0 - - for stay in stays: - # Check if stay overlaps with our 180-day window - stay_start = stay.entry_date - stay_end = stay.exit_date or calculation_date - - if stay_end >= window_start and stay_start <= window_end: - # Calculate overlap with window - overlap_start = max(stay_start, window_start) - overlap_end = min(stay_end, window_end) - overlap_days = (overlap_end - overlap_start).days + 1 - - if overlap_days > 0: - # Create a new stay object for the overlapping period - overlapping_stay = SchengenStay( - entry_date=overlap_start, - exit_date=overlap_end if overlap_end != calculation_date else None, - country=stay.country, - days=overlap_days, - ) - relevant_stays.append(overlapping_stay) - total_days += overlap_days - - # Calculate compliance - is_compliant = total_days <= 90 - days_remaining = max(0, 90 - total_days) - - # Calculate next reset date (when earliest stay in window expires) - next_reset_date = None - if relevant_stays: - earliest_stay = min(relevant_stays, key=lambda s: s.entry_date) - next_reset_date = earliest_stay.entry_date + timedelta(days=180) - - return SchengenCalculation( - total_days_used=total_days, - days_remaining=days_remaining, - is_compliant=is_compliant, - current_180_day_period=(window_start, window_end), - stays_in_period=relevant_stays, - next_reset_date=next_reset_date, - ) - - -def format_schengen_report(calculation: SchengenCalculation) -> str: - """Format a human-readable Schengen compliance report.""" - report = [] - - # Header - report.append("=== SCHENGEN AREA COMPLIANCE REPORT ===") - report.append( - f"Calculation period: {calculation.current_180_day_period[0]} to {calculation.current_180_day_period[1]}" - ) - report.append("") - - # Summary - status = "✅ COMPLIANT" if calculation.is_compliant else "❌ NON-COMPLIANT" - report.append(f"Status: {status}") - report.append(f"Days used: {calculation.total_days_used}/90") - - if calculation.is_compliant: - report.append(f"Days remaining: {calculation.days_remaining}") - else: - report.append(f"Days over limit: {calculation.days_over_limit}") - - if calculation.next_reset_date: - report.append(f"Next reset date: {calculation.next_reset_date}") - - report.append("") - - # Detailed stays - if calculation.stays_in_period: - report.append("Stays in current 180-day period:") - for stay in calculation.stays_in_period: - exit_str = ( - stay.exit_date.strftime("%Y-%m-%d") if stay.exit_date else "ongoing" - ) - report.append( - f" • {stay.entry_date.strftime('%Y-%m-%d')} to {exit_str} " - f"({stay.country.upper()}): {stay.days} days" - ) - else: - report.append("No Schengen stays in current 180-day period.") - - return "\n".join(report) - - -def get_schengen_countries_list() -> list[str]: - """Get a list of all Schengen area country codes.""" - return sorted(list(SCHENGEN_COUNTRIES)) - - -def predict_future_compliance( - travel_items: list[StrDict], - future_travel: list[tuple[date, date, str]], # (entry_date, exit_date, country) -) -> list[SchengenCalculation]: - """ - Predict future Schengen compliance with planned travel. - - Args: - travel_items: Existing travel history - future_travel: List of planned trips as (entry_date, exit_date, country) - - Returns: - List of SchengenCalculation objects for each planned trip - """ - predictions = [] - - # Create mock travel items for future trips - extended_travel = travel_items.copy() - - for entry_date, exit_date, country in future_travel: - # Add entry - extended_travel.append( - {"type": "flight", "depart": entry_date, "to_airport": {"country": country}} - ) - - # Add exit - extended_travel.append( - { - "type": "flight", - "depart": exit_date, - "from_airport": {"country": country}, - "to_airport": {"country": "gb"}, # Assuming return to UK - } - ) - - # Calculate compliance at the exit date - calculation = calculate_schengen_time(extended_travel, exit_date) - predictions.append(calculation) - - return predictions diff --git a/agenda/stats.py b/agenda/stats.py deleted file mode 100644 index 91493c9..0000000 --- a/agenda/stats.py +++ /dev/null @@ -1,153 +0,0 @@ -"""Trip statistic functions.""" - -from collections import defaultdict -import typing -from typing import TYPE_CHECKING, Counter, Mapping - -import agenda - -if TYPE_CHECKING: - from agenda.types import Trip - -StrDict = dict[str, typing.Any] - - -def travel_legs(trip: Trip, stats: StrDict) -> None: - """Calculate stats for travel legs.""" - for leg in trip.travel: - stats.setdefault("co2_kg", 0) - stats.setdefault("co2_by_transport_type", {}) - if "co2_kg" in leg: - stats["co2_kg"] += leg["co2_kg"] - transport_type = leg["type"] - stats["co2_by_transport_type"].setdefault(transport_type, 0) - stats["co2_by_transport_type"][transport_type] += leg["co2_kg"] - if leg["type"] == "flight": - from agenda.types import airport_label - - stats.setdefault("flight_count", 0) - stats.setdefault("airlines", Counter()) - stats.setdefault("airports", Counter()) - stats["flight_count"] += 1 - stats["airlines"][leg["airline_detail"]["name"]] += 1 - for field in ("from_airport", "to_airport"): - airport = leg.get(field) - if airport: - country = agenda.get_country(airport.get("country")) - label = airport_label(airport) - display = f"{country.flag} {label}" if country else label - stats["airports"][display] += 1 - if leg["type"] == "train": - stats.setdefault("train_count", 0) - stats["train_count"] += 1 - stats.setdefault("stations", Counter()) - train_legs = leg.get("legs", []) - if train_legs: - for train_leg in train_legs: - for field in ("from_station", "to_station"): - station = train_leg.get(field) - if station: - country = agenda.get_country(station.get("country")) - label = station["name"] - display = f"{country.flag} {label}" if country else label - stats["stations"][display] += 1 - else: - for field in ("from_station", "to_station"): - station = leg.get(field) - if station: - country = agenda.get_country(station.get("country")) - label = station["name"] - display = f"{country.flag} {label}" if country else label - stats["stations"][display] += 1 - - -def conferences(trip: Trip, yearly_stats: Mapping[int, StrDict]) -> None: - """Calculate conference stats.""" - for c in trip.conferences: - yearly_stats[c["start"].year].setdefault("conferences", 0) - yearly_stats[c["start"].year]["conferences"] += 1 - - -def calculate_overall_stats(yearly_stats: dict[int, StrDict]) -> StrDict: - """Aggregate yearly stats into overall stats for airlines, airports, stations.""" - overall: StrDict = { - "airlines": Counter(), - "airports": Counter(), - "stations": Counter(), - "flight_count": 0, - "train_count": 0, - "co2_kg": 0.0, - "co2_by_transport_type": {}, - } - - for year_stats in yearly_stats.values(): - if "airlines" in year_stats: - overall["airlines"] += year_stats["airlines"] - if "airports" in year_stats: - overall["airports"] += year_stats["airports"] - if "stations" in year_stats: - overall["stations"] += year_stats["stations"] - overall["flight_count"] += year_stats.get("flight_count", 0) - overall["train_count"] += year_stats.get("train_count", 0) - overall["co2_kg"] += year_stats.get("co2_kg", 0) - for transport_type, co2_kg in year_stats.get( - "co2_by_transport_type", {} - ).items(): - overall["co2_by_transport_type"].setdefault(transport_type, 0.0) - overall["co2_by_transport_type"][transport_type] += co2_kg - - return overall - - -def calculate_yearly_stats( - trips: list[Trip], previously_visited: set[str] | None = None -) -> dict[int, StrDict]: - """Calculate total distance and distance by transport type grouped by year.""" - yearly_stats: defaultdict[int, StrDict] = defaultdict(dict) - first_visit_year: dict[str, int] = {} - excluded_new: set[str] = previously_visited or set() - - for trip in trips: - year = trip.start.year - for country in trip.countries: - if country.alpha_2 == "GB": - continue - alpha_2 = country.alpha_2 - if alpha_2 not in first_visit_year or year < first_visit_year[alpha_2]: - first_visit_year[alpha_2] = year - - for trip in trips: - year = trip.start.year - dist = trip.total_distance() - yearly_stats[year].setdefault("count", 0) - yearly_stats[year]["count"] += 1 - - conferences(trip, yearly_stats) - - if dist: - yearly_stats[year]["total_distance"] = ( - yearly_stats[year].get("total_distance", 0) + trip.total_distance() - ) - - for transport_type, distance in trip.distances_by_transport_type(): - yearly_stats[year].setdefault("distances_by_transport_type", {}) - yearly_stats[year]["distances_by_transport_type"][transport_type] = ( - yearly_stats[year]["distances_by_transport_type"].get(transport_type, 0) - + distance - ) - - for country in trip.countries: - if country.alpha_2 == "GB": - continue - yearly_stats[year].setdefault("countries", set()) - yearly_stats[year]["countries"].add(country) - if ( - first_visit_year.get(country.alpha_2) == year - and country.alpha_2 not in excluded_new - ): - yearly_stats[year].setdefault("new_countries", set()) - yearly_stats[year]["new_countries"].add(country) - - travel_legs(trip, yearly_stats[year]) - - return dict(yearly_stats) diff --git a/agenda/stock_market.py b/agenda/stock_market.py index 60b8c7a..60fb5a4 100644 --- a/agenda/stock_market.py +++ b/agenda/stock_market.py @@ -3,14 +3,26 @@ from datetime import timedelta, timezone import dateutil.tz -import exchange_calendars # type: ignore +import exchange_calendars import pandas -from . import utils - here = dateutil.tz.tzlocal() +def timedelta_display(delta: timedelta) -> str: + """Format timedelta as a human readable string.""" + total_seconds = int(delta.total_seconds()) + days, remainder = divmod(total_seconds, 24 * 60 * 60) + hours, remainder = divmod(remainder, 60 * 60) + mins, secs = divmod(remainder, 60) + + return " ".join( + f"{v:>3} {label}" + for v, label in ((days, "days"), (hours, "hrs"), (mins, "mins")) + if v + ) + + def open_and_close() -> list[str]: """Stock markets open and close times.""" # The trading calendars code is slow, maybe there is a faster way to do this @@ -28,11 +40,11 @@ def open_and_close() -> list[str]: if cal.is_open_on_minute(now_local): next_close = cal.next_close(now).tz_convert(here) next_close = next_close.replace(minute=round(next_close.minute, -1)) - delta_close = utils.timedelta_display(next_close - now_local) + delta_close = timedelta_display(next_close - now_local) prev_open = cal.previous_open(now).tz_convert(here) prev_open = prev_open.replace(minute=round(prev_open.minute, -1)) - delta_open = utils.timedelta_display(now_local - prev_open) + delta_open = timedelta_display(now_local - prev_open) msg = ( f"{label:>6} market opened {delta_open} ago, " @@ -42,7 +54,7 @@ def open_and_close() -> list[str]: ts = cal.next_open(now) ts = ts.replace(minute=round(ts.minute, -1)) ts = ts.tz_convert(here) - delta = utils.timedelta_display(ts - now_local) + delta = timedelta_display(ts - now_local) msg = f"{label:>6} market opens in {delta}" + ( f" ({ts:%H:%M})" if (ts - now_local) < timedelta(days=1) else "" ) diff --git a/agenda/subscription.py b/agenda/subscription.py index a9f8ccc..9c2036c 100644 --- a/agenda/subscription.py +++ b/agenda/subscription.py @@ -2,7 +2,7 @@ import yaml -from .event import Event +from .types import Event def get_events(filepath: str) -> list[Event]: diff --git a/agenda/sun.py b/agenda/sun.py index 17995bb..72adc69 100644 --- a/agenda/sun.py +++ b/agenda/sun.py @@ -3,7 +3,7 @@ import typing from datetime import datetime -import ephem # type: ignore +import ephem def bristol() -> ephem.Observer: diff --git a/agenda/thespacedevs.py b/agenda/thespacedevs.py index 648d4ea..6ece23c 100644 --- a/agenda/thespacedevs.py +++ b/agenda/thespacedevs.py @@ -5,212 +5,33 @@ import os import typing from datetime import datetime -import requests - -from .types import StrDict -from .utils import filename_timestamp, get_most_recent_file +import httpx Launch = dict[str, typing.Any] Summary = dict[str, typing.Any] -ttl = 60 * 60 * 2 # two hours -LIMIT = 500 -ACTIVE_CREWED_FLIGHTS_CACHE_FILE = "active_crewed_flights.json" - - -def next_launch_api_data(rocket_dir: str, limit: int = LIMIT) -> StrDict | None: +async def next_launch_api(rocket_dir: str, limit: int = 200) -> list[Launch]: """Get the next upcoming launches from the API.""" now = datetime.now() filename = os.path.join(rocket_dir, now.strftime("%Y-%m-%d_%H:%M:%S.json")) url = "https://ll.thespacedevs.com/2.2.0/launch/upcoming/" params: dict[str, str | int] = {"limit": limit} - r = requests.get(url, params=params) - try: - data: StrDict = r.json() - except requests.exceptions.JSONDecodeError: - return None - # Only persist valid launch payloads; rate-limit / error responses must not - # overwrite the cache or they become the "most recent" file. - if isinstance(data.get("results"), list): - open(filename, "w").write(r.text) - return data - - -def next_launch_api(rocket_dir: str, limit: int = LIMIT) -> list[Summary] | None: - """Get the next upcoming launches from the API.""" - data = next_launch_api_data(rocket_dir, limit) - if not data: - return None + async with httpx.AsyncClient() as client: + r = await client.get(url, params=params) + open(filename, "w").write(r.text) + data = r.json() return [summarize_launch(launch) for launch in data["results"]] -def parse_api_datetime(value: typing.Any) -> datetime | None: - """Parse API datetime strings into datetime objects.""" - if not isinstance(value, str): - return None +def filename_timestamp(filename: str) -> tuple[datetime, str] | None: + """Get datetime from filename.""" try: - return datetime.fromisoformat(value.replace("Z", "+00:00")) + ts = datetime.strptime(filename, "%Y-%m-%d_%H:%M:%S.json") except ValueError: return None - - -def is_crewed_spaceflight(flight: Launch) -> bool: - """Return True when a spaceflight is crewed/human-rated.""" - spacecraft_config = get_nested(flight, ["spacecraft", "spacecraft_config"]) - if isinstance(spacecraft_config, dict) and spacecraft_config.get("human_rated"): - return True - - mission_type = get_nested(flight, ["launch", "mission", "type"]) - if isinstance(mission_type, str) and "human" in mission_type.lower(): - return True - - return False - - -def is_active_crewed_spaceflight(flight: Launch, now: datetime) -> bool: - """Return True when a crewed spaceflight is active (mission not yet ended).""" - launch = flight.get("launch") - if not isinstance(launch, dict): - return False - - if not is_crewed_spaceflight(flight): - return False - - launch_net = parse_api_datetime(launch.get("net")) - if launch_net and launch_net > now: - return False - - mission_end = parse_api_datetime(flight.get("mission_end")) - if mission_end and mission_end <= now: - return False - - spacecraft_in_space = get_nested(flight, ["spacecraft", "in_space"]) - if spacecraft_in_space is True: - return True - - if mission_end is None: - landing_success = get_nested(flight, ["landing", "success"]) - if landing_success is True: - return False - return True - - return True - - -def active_crewed_flights_api(limit: int = LIMIT) -> list[Summary] | None: - """ - Get active crewed spaceflights from the SpaceDevs API. - - The API does not reliably expose a direct filter for active flights, so this - paginates through results and applies local filtering. - """ - now = datetime.now().astimezone() - url = "https://ll.thespacedevs.com/2.2.0/spacecraft/flight/" - params: dict[str, str | int] = {"limit": limit} - launches: list[Summary] = [] - seen_slugs: set[str] = set() - page = 0 - max_pages = 20 - - while url and page < max_pages: - r = requests.get(url, params=params if page == 0 else None, timeout=30) - if not r.ok: - return None - try: - data: StrDict = r.json() - except requests.exceptions.JSONDecodeError: - return None - - results = data.get("results") - if not isinstance(results, list): - break - - for flight in results: - if not isinstance(flight, dict): - continue - if not is_active_crewed_spaceflight(flight, now): - continue - - launch = flight.get("launch") - if not isinstance(launch, dict): - continue - - launch_summary = summarize_launch(typing.cast(Launch, launch)) - slug = launch_summary.get("slug") - if not isinstance(slug, str): - continue - if slug in seen_slugs: - continue - seen_slugs.add(slug) - launches.append(launch_summary) - - next_url = data.get("next") - url = next_url if isinstance(next_url, str) else "" - params = {} - page += 1 - - return launches - - -def get_active_crewed_flights_cache_filename(rocket_dir: str) -> str: - """Path of the active crewed flights cache file.""" - return os.path.join(rocket_dir, ACTIVE_CREWED_FLIGHTS_CACHE_FILE) - - -def load_active_crewed_flights_cache( - rocket_dir: str, -) -> tuple[datetime, list[Summary]] | None: - """Load active crewed flights cache file.""" - filename = get_active_crewed_flights_cache_filename(rocket_dir) - if not os.path.exists(filename): - return None - - try: - cache_data = json.load(open(filename)) - except (json.JSONDecodeError, OSError): - return None - - updated_str = cache_data.get("updated") - updated = parse_api_datetime(updated_str) - results = cache_data.get("results") - if not updated or not isinstance(results, list): - return None - - summary_results: list[Summary] = [r for r in results if isinstance(r, dict)] - return (updated.replace(tzinfo=None), summary_results) - - -def write_active_crewed_flights_cache(rocket_dir: str, launches: list[Summary]) -> None: - """Write active crewed flights cache file.""" - filename = get_active_crewed_flights_cache_filename(rocket_dir) - payload: StrDict = { - "updated": datetime.now().isoformat(), - "results": launches, - } - with open(filename, "w") as f: - json.dump(payload, f) - - -def get_active_crewed_flights( - rocket_dir: str, refresh: bool = False -) -> list[Summary] | None: - """Get active crewed flights with cache and API fallback.""" - now = datetime.now() - cached = load_active_crewed_flights_cache(rocket_dir) - if cached and not refresh and (now - cached[0]).seconds <= ttl: - return cached[1] - - try: - active_flights = active_crewed_flights_api() - if active_flights is not None: - write_active_crewed_flights_cache(rocket_dir, active_flights) - return active_flights - except Exception: - pass - - return cached[1] if cached else [] + return (ts, filename) def format_time(time_str: str, net_precision: str) -> tuple[str, str | None]: @@ -238,8 +59,6 @@ def format_time(time_str: str, net_precision: str) -> tuple[str, str | None]: include_time = True case _ if net_precision and net_precision.startswith("Quarter "): time_format = f"Q{net_precision[-1]} %Y" - case _ if net_precision and net_precision.startswith("Year Half "): - time_format = f"H{net_precision[-1]} %Y" case _: time_format = None @@ -297,7 +116,6 @@ def summarize_launch(launch: Launch) -> Summary: return { "name": launch.get("name"), - "slug": launch["slug"], "status": launch.get("status"), "net": launch.get("net"), "net_precision": net_precision, @@ -308,7 +126,7 @@ def summarize_launch(launch: Launch) -> Summary: "launch_provider": launch_provider, "launch_provider_abbrev": launch_provider_abbrev, "launch_provider_type": get_nested(launch, ["launch_service_provider", "type"]), - "rocket": launch["rocket"]["configuration"], + "rocket": launch["rocket"]["configuration"]["full_name"], "mission": launch.get("mission"), "mission_name": get_nested(launch, ["mission", "name"]), "pad_name": launch["pad"]["name"], @@ -316,239 +134,24 @@ def summarize_launch(launch: Launch) -> Summary: "location": launch["pad"]["location"]["name"], "country_code": launch["pad"]["country_code"], "orbit": get_nested(launch, ["mission", "orbit"]), - "probability": launch["probability"], - "weather_concerns": launch["weather_concerns"], - "image": launch.get("image"), } -def is_launches_cache_fresh(rocket_dir: str) -> bool: - """Return True if the launches cache is younger than the TTL.""" - now = datetime.now() - existing = [ - x for x in (filename_timestamp(f, "json") for f in os.listdir(rocket_dir)) if x - ] - if not existing: - return False - existing.sort(reverse=True) - return (now - existing[0][0]).total_seconds() <= ttl - - -def load_cached_launches(rocket_dir: str) -> StrDict | None: - """Read the most recent cache of launches.""" - filename = get_most_recent_file(rocket_dir, "json") - return typing.cast(StrDict, json.load(open(filename))) if filename else None - - -def read_cached_launches(rocket_dir: str) -> list[Summary]: - """Read cached launches.""" - data = load_cached_launches(rocket_dir) - if not data or not isinstance(data.get("results"), list): - return [] - return [summarize_launch(launch) for launch in data["results"]] - - -def get_launches( - rocket_dir: str, limit: int = LIMIT, refresh: bool = False -) -> list[Summary] | None: +async def get_launches(rocket_dir: str, limit: int = 200) -> list[Summary]: """Get rocket launches with caching.""" now = datetime.now() - existing = [ - x for x in (filename_timestamp(f, "json") for f in os.listdir(rocket_dir)) if x - ] + existing = [x for x in (filename_timestamp(f) for f in os.listdir(rocket_dir)) if x] existing.sort(reverse=True) - if refresh or not existing or (now - existing[0][0]).seconds > ttl: + if not existing or (now - existing[0][0]).seconds > 3600: # one hour try: - upcoming = next_launch_api(rocket_dir, limit=limit) - if upcoming is None: - raise RuntimeError("unable to fetch upcoming launches") - active_crewed = get_active_crewed_flights(rocket_dir, refresh=refresh) or [] - by_slug = { - typing.cast(str, launch["slug"]): launch - for launch in upcoming - if isinstance(launch.get("slug"), str) - } - for launch in active_crewed: - slug = launch.get("slug") - if isinstance(slug, str) and slug not in by_slug: - by_slug[slug] = launch - return sorted(by_slug.values(), key=lambda launch: str(launch.get("net"))) - except Exception: - pass # fallback to cached version + return await next_launch_api(rocket_dir, limit=limit) + except httpx.ReadTimeout: + pass - # Find the most recent cache file that contains a valid results list. - # Older files without "results" (e.g. stale rate-limit responses) are skipped. - data = None - for _, f in existing: - filename = os.path.join(rocket_dir, f) - try: - candidate = json.load(open(filename)) - except (json.JSONDecodeError, OSError): - continue - if isinstance(candidate.get("results"), list): - data = candidate - break - if not data: - return [] - upcoming = [summarize_launch(launch) for launch in data["results"]] - active_crewed = get_active_crewed_flights(rocket_dir, refresh=refresh) or [] - by_slug = { - typing.cast(str, launch["slug"]): launch - for launch in upcoming - if isinstance(launch.get("slug"), str) - } - for launch in active_crewed: - slug = launch.get("slug") - if isinstance(slug, str) and slug not in by_slug: - by_slug[slug] = launch - return sorted(by_slug.values(), key=lambda launch: str(launch.get("net"))) + f = existing[0][1] - -def format_date(dt: datetime) -> str: - """Human readable date.""" - return dt.strftime("%d %b %Y at %H:%M UTC") - - -def format_datetime_change(field_name: str, old_val: str, new_val: str) -> str: - """Format a datetime field change with proper error handling.""" - try: - old_dt = datetime.fromisoformat(old_val.replace("Z", "+00:00")) - new_dt = datetime.fromisoformat(new_val.replace("Z", "+00:00")) - return ( - f"{field_name} changed from {format_date(old_dt)} to {format_date(new_dt)}" - ) - except (ValueError, AttributeError): - return f"{field_name} changed from {old_val} to {new_val}" - - -def format_datetime_update(field_name: str, new_val: str) -> str: - """Format a datetime field update (showing only the new value).""" - try: - new_dt = datetime.fromisoformat(new_val.replace("Z", "+00:00")) - return f"{field_name}: {format_date(new_dt)}" - except (ValueError, AttributeError): - return f"{field_name}: {new_val}" - - -def format_probability_change(old_val: int, new_val: int) -> str: - """Format probability field changes.""" - if old_val is None: - return f"Launch probability set to {new_val}%" - elif new_val is None: - return "Launch probability removed" - else: - return f"Launch probability changed from {old_val}% to {new_val}%" - - -def format_launch_changes(differences: StrDict) -> str: - """Convert deepdiff output to human-readable format.""" - changes: list[str] = [] - processed_paths: set[str] = set() - - SKIP_FIELDS = { - "agency_launch_attempt_count", - "agency_launch_attempt_count_year", - "location_launch_attempt_count", - "location_launch_attempt_count_year", - "pad_launch_attempt_count", - "pad_launch_attempt_count_year", - "orbital_launch_attempt_count", - "orbital_launch_attempt_count_year", - } - - # --- 1. Handle Special Group Value Changes --- - # Process high-level, user-friendly summaries first. - values = differences.get("values_changed", {}) - if "root['status']['name']" in values: - old_val = values["root['status']['name']"]["old_value"] - new_val = values["root['status']['name']"]["new_value"] - changes.append(f"Status changed from '{old_val}' to '{new_val}'") - processed_paths.add("root['status']") - - if "root['net_precision']['name']" in values: - old_val = values["root['net_precision']['name']"]["old_value"] - new_val = values["root['net_precision']['name']"]["new_value"] - changes.append(f"Launch precision changed from '{old_val}' to '{new_val}'") - processed_paths.add("root['net_precision']") - - # --- 2. Handle Type Changes --- - # This is often more significant than a value change (e.g., probability becoming None). - if "type_changes" in differences: - for path, change in differences["type_changes"].items(): - if any(path.startswith(p) for p in processed_paths): - continue - - field = path.replace("root['", "").replace("']", "").replace("root.", "") - - if field == "probability": - # Use custom formatter only for meaningful None transitions. - if change["old_type"] is type(None) or change["new_type"] is type(None): - changes.append( - format_probability_change( - change["old_value"], change["new_value"] - ) - ) - else: # For other type changes (e.g., int to str), use the generic message. - changes.append( - f"{field.replace('_', ' ').title()} type changed " - + f"from {change['old_type'].__name__} to {change['new_type'].__name__}" - ) - else: - changes.append( - f"{field.replace('_', ' ').title()} type changed " - + f"from {change['old_type'].__name__} to {change['new_type'].__name__}" - ) - processed_paths.add(path) - - # --- 3. Handle Remaining Value Changes --- - for path, change in values.items(): - if any(path.startswith(p) for p in processed_paths): - continue - - field = path.replace("root['", "").replace("']", "").replace("root.", "") - if field in SKIP_FIELDS: - continue - - old_val = change["old_value"] - new_val = change["new_value"] - - match field: - case "net": - changes.append(format_datetime_change("Launch time", old_val, new_val)) - case "window_start": - changes.append( - format_datetime_change("Launch window start", old_val, new_val) - ) - case "window_end": - changes.append( - format_datetime_change("Launch window end", old_val, new_val) - ) - case "last_updated": - changes.append(format_datetime_update("Last updated", new_val)) - case "name": - changes.append(f"Mission name changed from '{old_val}' to '{new_val}'") - case "probability": - changes.append(format_probability_change(old_val, new_val)) - case _: - changes.append(f"{field} changed from '{old_val}' to '{new_val}'") - processed_paths.add(path) - - # --- 4. Handle Added/Removed Fields --- - if "dictionary_item_added" in differences: - for path in differences["dictionary_item_added"]: - field = path.replace("root['", "").replace("']", "").replace("root.", "") - changes.append(f"New field added: {field.replace('_', ' ').title()}") - - if "dictionary_item_removed" in differences: - for path in differences["dictionary_item_removed"]: - field = path.replace("root['", "").replace("']", "").replace("root.", "") - changes.append(f"Field removed: {field.replace('_', ' ').title()}") - - # Sort changes for deterministic output in tests - return ( - "\n".join(f"• {change}" for change in sorted(changes)) - if changes - else "No specific changes detected" - ) + filename = os.path.join(rocket_dir, f) + data = json.load(open(filename)) + return [summarize_launch(launch) for launch in data["results"]] diff --git a/agenda/travel.py b/agenda/travel.py index 440e6af..4825c9c 100644 --- a/agenda/travel.py +++ b/agenda/travel.py @@ -1,86 +1,36 @@ """Travel.""" -import decimal -import json import os import typing -import flask import yaml -from geopy.distance import geodesic # type: ignore -from .event import Event -from .types import StrDict +from .types import Event Leg = dict[str, str] TravelList = list[dict[str, typing.Any]] -RouteDistances = dict[tuple[str, str], float] - - -def coords(airport: StrDict) -> tuple[float, float]: - """Longitude / Latitude as coordinate tuples.""" - # return (airport["longitude"], airport["latitude"]) - return (airport["latitude"], airport["longitude"]) - - -def flight_distance(f: StrDict) -> float: - """Distance of flight.""" - return float(geodesic(coords(f["from_airport"]), coords(f["to_airport"])).km) - - -def route_distances_as_json(route_distances: RouteDistances) -> str: - """Format route distances as JSON string.""" - return ( - "[\n" - + ",\n".join( - " " + json.dumps([s1, s2, dist]) - for (s1, s2), dist in route_distances.items() - ) - + "\n]" - ) - def parse_yaml(travel_type: str, data_dir: str) -> TravelList: """Parse flights YAML and return list of travel.""" filepath = os.path.join(data_dir, travel_type + ".yaml") - items: TravelList = yaml.safe_load(open(filepath)) - if not all(isinstance(item, dict) for item in items): - return items - - for item in items: - price = item.get("price") - if price: - item["price"] = decimal.Decimal(price) - - return items + return typing.cast(TravelList, yaml.safe_load(open(filepath))) def get_flights(data_dir: str) -> list[Event]: """Get travel events.""" - bookings = parse_yaml("flights", data_dir) - airlines = parse_yaml("airlines", data_dir) - by_iata = {a["iata"]: a for a in airlines} - events = [] - for booking in bookings: - for item in booking["flights"]: - if not item["depart"].date(): - continue - airline = by_iata[item["airline"]] - item["airline_code"] = airline[ - "iata" if not airline.get("flight_number_prefer_icao") else "icao" - ] - - e = Event( - date=item["depart"], - end_date=item.get("arrive"), - name="transport", - title=f'✈️ {item["from"]} to {item["to"]} ({flight_number(item)})', - url=(item.get("url") if flask.g.user.is_authenticated else None), - ) - events.append(e) - return events + return [ + Event( + date=item["depart"], + end_date=item.get("arrive"), + name="transport", + title=f'✈️ {item["from"]} to {item["to"]} ({flight_number(item)})', + url=item.get("url"), + ) + for item in parse_yaml("flights", data_dir) + if item["depart"].date() + ] def get_trains(data_dir: str) -> list[Event]: @@ -93,7 +43,7 @@ def get_trains(data_dir: str) -> list[Event]: end_date=leg["arrive"], name="transport", title=f'🚆 {leg["from"]} to {leg["to"]}', - url=(item.get("url") if flask.g.user.is_authenticated else None), + url=item.get("url"), ) for leg in item["legs"] ] @@ -102,7 +52,7 @@ def get_trains(data_dir: str) -> list[Event]: def flight_number(flight: Leg) -> str: """Flight number.""" - airline_code = flight["airline_code"] + airline_code = flight["airline"] # make sure this is the airline code, not the airline name assert " " not in airline_code and not any(c.islower() for c in airline_code) @@ -112,43 +62,3 @@ def flight_number(flight: Leg) -> str: def all_events(data_dir: str) -> list[Event]: """Get all flights and rail journeys.""" return get_trains(data_dir) + get_flights(data_dir) - - -def train_leg_distance(geojson_data: StrDict) -> float: - """Calculate the total length of a LineString in kilometers from GeoJSON data.""" - # Extract coordinates - first_object = geojson_data["features"][0]["geometry"] - assert first_object["type"] in ("LineString", "MultiLineString") - - if first_object["type"] == "LineString": - coord_list = [first_object["coordinates"]] - else: - first_object["type"] == "MultiLineString" - coord_list = first_object["coordinates"] - - total_length_km = 0.0 - - for coordinates in coord_list: - total_length_km += sum( - float(geodesic(coordinates[i], coordinates[i + 1]).km) - for i in range(len(coordinates) - 1) - ) - - return total_length_km - - -def load_route_distances(data_dir: str) -> RouteDistances: - """Load cache of route distances.""" - route_distances: RouteDistances = {} - with open(os.path.join(data_dir, "route_distances.json")) as f: - for s1, s2, dist in json.load(f): - route_distances[(s1, s2)] = dist - - return route_distances - - -def add_leg_route_distance(leg: StrDict, route_distances: RouteDistances) -> None: - s1, s2 = sorted([leg["from"], leg["to"]]) - dist = route_distances.get((s1, s2)) - if dist: - leg["distance"] = dist diff --git a/agenda/trip.py b/agenda/trip.py deleted file mode 100644 index 6de8e4d..0000000 --- a/agenda/trip.py +++ /dev/null @@ -1,1355 +0,0 @@ -"""Trips.""" - -import decimal -import hashlib -import json -import os -import typing -import unicodedata -from datetime import date, datetime, time, timedelta, timezone - -import flask -import pycountry -import yaml -from geopy.distance import geodesic # type: ignore - -from agenda import conference, ical, travel, trip_schengen -from agenda.types import StrDict, Trip, TripElement -from agenda.utils import as_date, as_datetime, depart_datetime - -TRAIN_CO2_KG_PER_KM = 0.037 -COACH_CO2_KG_PER_KM = 0.027 -FERRY_CO2_KG_PER_KM = 0.02254 -BUS_CO2_KG_PER_KM = 0.1 -CAR_CO2_KG_PER_KM = 0.218 -NOON = time(12) - - -class Airline(typing.TypedDict, total=False): - """Airline.""" - - iata: str - icao: str - name: str - - -def load_flight_destination_rules( - data_dir: str, -) -> list[tuple[str, set[str]]]: - """Load flight destination rules from personal data. - - YAML schema: - - origin: BRS - airline: U2 - destinations: [AGP, ALC] - """ - filename = os.path.join(data_dir, "flight_destinations.yaml") - if not os.path.exists(filename): - return [] - - raw = yaml.safe_load(open(filename)) - if not isinstance(raw, list): - return [] - - rules: list[tuple[str, set[str]]] = [] - for item in raw: - if not isinstance(item, dict): - continue - from_iata = item.get("origin") - airline = item.get("airline") - destinations = item.get("destinations") - if ( - not isinstance(from_iata, str) - or not isinstance(airline, str) - or not isinstance(destinations, list) - ): - continue - destination_set = {d.upper() for d in destinations if isinstance(d, str)} - if destination_set: - rules.append((from_iata.upper(), destination_set)) - return rules - - -def normalize_place_name(value: str) -> str: - """Normalize place names for case/diacritics-insensitive matching.""" - normalized = unicodedata.normalize("NFKD", value) - no_marks = "".join(ch for ch in normalized if not unicodedata.combining(ch)) - return " ".join(no_marks.casefold().split()) - - -def preferred_airport_iata(stop: StrDict, airports: dict[str, StrDict]) -> str | None: - """Return preferred airport IATA for a stop from airport data.""" - location = stop.get("location") - country = stop.get("country") - if not isinstance(location, str) or not isinstance(country, str): - return None - - location_normalized = normalize_place_name(location) - country_lower = country.casefold() - match_scores: list[tuple[int, str]] = [] - for iata, airport in airports.items(): - airport_country = airport.get("country") - if ( - not isinstance(airport_country, str) - or airport_country.casefold() != country_lower - ): - continue - - score = 0 - city = airport.get("city") - if isinstance(city, str) and normalize_place_name(city) == location_normalized: - score = max(score, 3) - alt_name = airport.get("alt_name") - if ( - isinstance(alt_name, str) - and normalize_place_name(alt_name) == location_normalized - ): - score = max(score, 2) - name = airport.get("name") - if isinstance(name, str) and normalize_place_name(name) == location_normalized: - score = max(score, 1) - if score: - match_scores.append((score, iata)) - - if not match_scores: - return None - match_scores.sort(key=lambda item: (-item[0], item[1])) - return match_scores[0][1] - - -def get_unbooked_flight_origin_iata( - destination_iata: str | None, origin_rules: list[tuple[str, set[str]]] -) -> str: - """Choose origin airport for an unbooked flight.""" - if destination_iata: - for from_iata, destinations in origin_rules: - if destination_iata in destinations: - return from_iata - return "LHR" - - -UNBOOKED_RAIL_DESTINATIONS: dict[tuple[str, str], tuple[str, str]] = { - ("fr", "paris"): ("London St Pancras", "Paris Gare du Nord"), -} -EUROSTAR_LONDON_STATION = "London St Pancras" -EUROSTAR_HOME_FEEDER_ROUTE = ("Bristol Temple Meads", "London Paddington") - - -def load_station_lookup(data_dir: str) -> dict[str, StrDict]: - """Load stations keyed by station name.""" - stations = travel.parse_yaml("stations", data_dir) - return {station["name"]: station for station in stations} - - -def get_unbooked_rail_route(item: StrDict, data_dir: str) -> StrDict | None: - """Return an assumed rail route for a conference without booked travel.""" - location = item.get("location") - country = item.get("country") - if not isinstance(location, str) or not isinstance(country, str): - return None - - station_names = UNBOOKED_RAIL_DESTINATIONS.get( - (country.casefold(), normalize_place_name(location)) - ) - if station_names is None: - return None - - stations = load_station_lookup(data_dir) - from_station = stations.get(station_names[0]) - to_station = stations.get(station_names[1]) - if from_station is None or to_station is None: - return None - - key = "_".join(["train"] + sorted([from_station["name"], to_station["name"]])) - route: StrDict = {"type": "train", "key": key} - geojson_filename = from_station.get("routes", {}).get(to_station["name"]) - if geojson_filename: - route["geojson_filename"] = os.path.join("train_routes", geojson_filename) - else: - route["from"] = latlon_tuple(from_station) - route["to"] = latlon_tuple(to_station) - return route - - -def build_train_route( - train_from: StrDict, train_to: StrDict, seen_geojson: set[str] -) -> StrDict | None: - """Build a map route between two stations.""" - geojson_filename = train_from.get("routes", {}).get(train_to["name"]) - key = "_".join(["train"] + sorted([train_from["name"], train_to["name"]])) - if not geojson_filename: - return { - "type": "train", - "key": key, - "from": latlon_tuple(train_from), - "to": latlon_tuple(train_to), - } - - if geojson_filename in seen_geojson: - return None - seen_geojson.add(geojson_filename) - - return { - "type": "train", - "key": key, - "geojson_filename": os.path.join("train_routes", geojson_filename), - } - - -def train_operator_is_eurostar(item: StrDict) -> bool: - """Return True when a train item is operated by Eurostar.""" - operator = item.get("operator") - return isinstance(operator, str) and operator.casefold() == "eurostar" - - -def is_eurostar_departure_from_london(train: StrDict) -> bool: - """Return True when a train journey departs London St Pancras on Eurostar.""" - if train.get("from") == EUROSTAR_LONDON_STATION and train_operator_is_eurostar( - train - ): - return True - - for leg in train.get("legs", []): - if leg.get("from") == EUROSTAR_LONDON_STATION and train_operator_is_eurostar( - leg - ): - return True - return False - - -def get_eurostar_home_feeder_route( - trip: Trip, data_dir: str, seen_geojson: set[str] -) -> StrDict | None: - """Return the assumed Bristol to Paddington route for London Eurostar trips.""" - if not any( - t["type"] == "train" and is_eurostar_departure_from_london(t) - for t in trip.travel - ): - return None - - stations = load_station_lookup(data_dir) - from_station = stations.get(EUROSTAR_HOME_FEEDER_ROUTE[0]) - to_station = stations.get(EUROSTAR_HOME_FEEDER_ROUTE[1]) - if from_station is None or to_station is None: - return None - - return build_train_route(from_station, to_station, seen_geojson) - - -def load_travel(travel_type: str, plural: str, data_dir: str) -> list[StrDict]: - """Read flight and train journeys.""" - items: list[StrDict] = travel.parse_yaml(plural, data_dir) - for item in items: - item["type"] = travel_type - return items - - -def add_station_objects(item: StrDict, by_name: dict[str, StrDict]) -> None: - """Lookup stations and add to train or leg.""" - item["from_station"] = by_name[item["from"]] - item["to_station"] = by_name[item["to"]] - - -def load_trains( - data_dir: str, route_distances: travel.RouteDistances | None = None -) -> list[StrDict]: - """Load trains.""" - trains = load_travel("train", "trains", data_dir) - stations = travel.parse_yaml("stations", data_dir) - by_name = {station["name"]: station for station in stations} - - for train in trains: - add_station_objects(train, by_name) - for leg in train["legs"]: - add_station_objects(leg, by_name) - if route_distances: - travel.add_leg_route_distance(leg, route_distances) - - if "distance" in leg: - leg["co2_kg"] = leg["distance"] * TRAIN_CO2_KG_PER_KM - - if all("distance" in leg for leg in train["legs"]): - train["distance"] = sum(leg["distance"] for leg in train["legs"]) - train["co2_kg"] = sum(leg["co2_kg"] for leg in train["legs"]) - - return trains - - -def load_ferries( - data_dir: str, route_distances: travel.RouteDistances | None = None -) -> list[StrDict]: - """Load ferries.""" - ferries = load_travel("ferry", "ferries", data_dir) - terminals = travel.parse_yaml("ferry_terminals", data_dir) - by_name = {terminal["name"]: terminal for terminal in terminals} - - for item in ferries: - assert item["from"] in by_name and item["to"] in by_name - from_terminal, to_terminal = by_name[item["from"]], by_name[item["to"]] - item["from_terminal"] = from_terminal - item["to_terminal"] = to_terminal - - if route_distances: - travel.add_leg_route_distance(item, route_distances) - - if "distance" in item: - item["co2_kg"] = item["distance"] * FERRY_CO2_KG_PER_KM - - geojson = from_terminal["routes"].get(item["to"]) - if geojson: - item["geojson_filename"] = geojson - - return ferries - - -def load_road_transport( - travel_type: str, - plural: str, - stops_yaml: str, - data_dir: str, - co2_factor: float, - route_distances: travel.RouteDistances | None = None, -) -> list[StrDict]: - """Load road transport (bus or coach).""" - items = load_travel(travel_type, plural, data_dir) - stops = travel.parse_yaml(stops_yaml, data_dir) - by_name = {stop["name"]: stop for stop in stops} - - for item in items: - add_station_objects(item, by_name) - if route_distances: - travel.add_leg_route_distance(item, route_distances) - if "distance" in item: - item["co2_kg"] = item["distance"] * co2_factor - from_station = item.get("from_station") - to_station = item.get("to_station") - if from_station and to_station: - # Support scalar or mapping routes: string or dict of stop name -> geojson filename - routes_val = from_station.get("routes", {}) - if isinstance(routes_val, str): - geo: str | None = routes_val - else: - geo = routes_val.get(to_station.get("name")) - if geo: - item["geojson_filename"] = geo - - return items - - -def load_coaches( - data_dir: str, route_distances: travel.RouteDistances | None = None -) -> list[StrDict]: - """Load coaches.""" - return load_road_transport( - "coach", - "coaches", - "coach_stations", - data_dir, - COACH_CO2_KG_PER_KM, - route_distances, - ) - - -def load_buses( - data_dir: str, route_distances: travel.RouteDistances | None = None -) -> list[StrDict]: - """Load buses.""" - return load_road_transport( - "bus", "buses", "bus_stops", data_dir, BUS_CO2_KG_PER_KM, route_distances - ) - - -def route_filename_without_extension(route: str) -> str: - """Return route filename without a GeoJSON extension.""" - return route.removesuffix(".geojson") - - -def line_strings_from_geojson(geojson_data: StrDict) -> list[list[list[float]]]: - """Extract LineString coordinate arrays from GeoJSON.""" - if geojson_data["type"] == "FeatureCollection": - features = typing.cast(list[StrDict], geojson_data["features"]) - return [ - line for feature in features for line in line_strings_from_geojson(feature) - ] - - if geojson_data["type"] == "Feature": - return line_strings_from_geojson(typing.cast(StrDict, geojson_data["geometry"])) - - if geojson_data["type"] == "LineString": - return [typing.cast(list[list[float]], geojson_data["coordinates"])] - - if geojson_data["type"] == "MultiLineString": - return typing.cast(list[list[list[float]]], geojson_data["coordinates"]) - - return [] - - -def geojson_lonlat_to_latlon(coord: list[float]) -> tuple[float, float]: - """Convert a GeoJSON lon/lat coordinate to a Leaflet lat/lon tuple.""" - return (coord[1], coord[0]) - - -def geojson_route_endpoints( - geojson_data: StrDict, -) -> tuple[tuple[float, float], tuple[float, float]] | None: - """Return the first and last points of a GeoJSON route.""" - lines = line_strings_from_geojson(geojson_data) - populated_lines = [line for line in lines if line] - if not populated_lines: - return None - return ( - geojson_lonlat_to_latlon(populated_lines[0][0]), - geojson_lonlat_to_latlon(populated_lines[-1][-1]), - ) - - -def geojson_route_distance_km(geojson_data: StrDict) -> float: - """Calculate the length of a GeoJSON route in kilometres.""" - total = 0.0 - for line in line_strings_from_geojson(geojson_data): - for i in range(len(line) - 1): - total += float( - geodesic( - geojson_lonlat_to_latlon(line[i]), - geojson_lonlat_to_latlon(line[i + 1]), - ).km - ) - return total - - -def car_endpoint_type(name: str) -> str: - """Choose a marker type for a car route endpoint.""" - return ( - "home" - if name.casefold() in {"home", "pch", "picture house court"} - else "car_stop" - ) - - -def car_route_labels(item: StrDict) -> tuple[str | None, str | None]: - """Return car route from/to labels from YAML or the route filename.""" - from_label = item.get("from") - to_label = item.get("to") - if isinstance(from_label, str) and isinstance(to_label, str): - return (from_label, to_label) - - route = item.get("route") - if not isinstance(route, str): - return ( - from_label if isinstance(from_label, str) else None, - to_label if isinstance(to_label, str) else None, - ) - - route_stem = os.path.basename(route_filename_without_extension(route)) - if "_to_" not in route_stem: - return ( - from_label if isinstance(from_label, str) else None, - to_label if isinstance(to_label, str) else None, - ) - - start, end = route_stem.split("_to_", 1) - return ( - from_label if isinstance(from_label, str) else start.replace("_", " "), - to_label if isinstance(to_label, str) else end.replace("_", " "), - ) - - -def car_endpoint_marker_type(item: StrDict, direction: str, label: str) -> str: - """Return the marker type for a car route endpoint.""" - marker_type = item.get(direction + "_type") - return marker_type if isinstance(marker_type, str) else car_endpoint_type(label) - - -def show_car_endpoint_marker(item: StrDict, direction: str, label: str) -> bool: - """Return whether to show a car endpoint marker on the map.""" - direction_marker = item.get(direction + "_show_marker") - if isinstance(direction_marker, bool): - return direction_marker - - show_markers = item.get("show_markers") - if isinstance(show_markers, bool): - return show_markers - - return car_endpoint_type(label) == "home" - - -def load_cars(data_dir: str) -> list[StrDict]: - """Load car journeys.""" - filename = os.path.join(data_dir, "car_journeys.yaml") - if not os.path.exists(filename): - return [] - - items = load_travel("car", "car_journeys", data_dir) - for item in items: - route = item.get("route") - if not isinstance(route, str): - continue - - route_filename = route_filename_without_extension(route) - item["geojson_filename"] = route_filename - - with open( - os.path.join(data_dir, "car_routes", route_filename + ".geojson") - ) as f: - geojson_data = typing.cast(StrDict, json.load(f)) - - if "distance" not in item: - item["distance"] = geojson_route_distance_km(geojson_data) - if "distance" in item and "co2_kg" not in item: - item["co2_kg"] = item["distance"] * CAR_CO2_KG_PER_KM - - endpoints = geojson_route_endpoints(geojson_data) - from_label, to_label = car_route_labels(item) - if endpoints is None: - continue - - for direction, field, label, endpoint in ( - ("from", "from_location", from_label, endpoints[0]), - ("to", "to_location", to_label, endpoints[1]), - ): - if not label or not show_car_endpoint_marker(item, direction, label): - continue - item[field] = { - "name": label, - "type": car_endpoint_marker_type(item, direction, label), - "latitude": endpoint[0], - "longitude": endpoint[1], - } - - if from_label: - item["from"] = from_label - if to_label: - item["to"] = to_label - - return items - - -def process_flight( - flight: StrDict, by_iata: dict[str, Airline], airports: list[StrDict] -) -> None: - """Add airport detail, airline name and distance to flight.""" - if flight["from"] in airports: - flight["from_airport"] = airports[flight["from"]] - if flight["to"] in airports: - flight["to_airport"] = airports[flight["to"]] - if "airline" in flight: - airline = by_iata[flight["airline"]] - flight["airline_detail"] = airline - flight["airline_code"] = airline[ - "iata" if not airline.get("flight_number_prefer_icao") else "icao" - ] - - flight["distance"] = travel.flight_distance(flight) - - -def load_flight_bookings(data_dir: str) -> list[StrDict]: - """Load flight bookings.""" - bookings = load_travel("flight", "flights", data_dir) - airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml"))) - by_iata = {a["iata"]: a for a in airlines} - airports = travel.parse_yaml("airports", data_dir) - for booking in bookings: - for flight in booking["flights"]: - process_flight(flight, by_iata, airports) - return bookings - - -def load_flights(flight_bookings: list[StrDict]) -> list[StrDict]: - """Load flights.""" - flights = [] - for booking in flight_bookings: - for flight in booking["flights"]: - for f in "type", "trip", "booking_reference", "price", "currency": - if f in booking: - flight[f] = booking[f] - flights.append(flight) - return flights - - -def collect_travel_items( - flight_bookings: list[StrDict], - data_dir: str | None = None, - route_distances: travel.RouteDistances | None = None, -) -> list[StrDict]: - """Generate list of trips.""" - if data_dir is None: - data_dir = flask.current_app.config["PERSONAL_DATA"] - - return sorted( - load_flights(load_flight_bookings(data_dir)) - + load_trains(data_dir, route_distances=route_distances) - + load_ferries(data_dir, route_distances=route_distances) - + load_coaches(data_dir, route_distances=route_distances) - + load_buses(data_dir, route_distances=route_distances) - + load_cars(data_dir), - key=depart_datetime, - ) - - -def group_travel_items_into_trips( - data: StrDict, yaml_trip_list: list[StrDict] -) -> list[Trip]: - """Group travel items into trips.""" - trips: dict[date, Trip] = {} - yaml_trip_lookup = {item["trip"]: item for item in yaml_trip_list} - for key, item_list in data.items(): - assert isinstance(item_list, list) - for item in item_list: - if not (start := item.get("trip")): - continue - if start not in trips: - from_yaml = yaml_trip_lookup.get(start, {}) - trips[start] = Trip( - start=start, **{k: v for k, v in from_yaml.items() if k != "trip"} - ) - getattr(trips[start], key).append(item) - - return [trip for _, trip in sorted(trips.items())] - - -def build_trip_list( - data_dir: str | None = None, - route_distances: travel.RouteDistances | None = None, -) -> list[Trip]: - """Generate list of trips.""" - if data_dir is None: - data_dir = flask.current_app.config["PERSONAL_DATA"] - - yaml_trip_list = travel.parse_yaml("trips", data_dir) - - flight_bookings = load_flight_bookings(data_dir) - conferences = travel.parse_yaml("conferences", data_dir) - for conf in conferences: - date_fields = conference.conference_date_fields(conf) - if "start" in date_fields: - conf["start"] = date_fields["start"] - conf["end"] = date_fields["end"] - - data = { - "flight_bookings": flight_bookings, - "travel": collect_travel_items(flight_bookings, data_dir, route_distances), - "accommodation": travel.parse_yaml("accommodation", data_dir), - "conferences": conferences, - "events": travel.parse_yaml("events", data_dir), - } - - for item in data["accommodation"]: - price = item.get("price") - if price: - item["price"] = decimal.Decimal(price) - - return group_travel_items_into_trips(data, yaml_trip_list) - - -def add_coordinates_for_unbooked_flights( - routes: list[StrDict], coordinates: list[StrDict], data_dir: str -) -> None: - """Add coordinates for flights that haven't been booked yet.""" - if not any(route["type"] == "unbooked_flight" for route in routes): - return - - airports = typing.cast(dict[str, StrDict], travel.parse_yaml("airports", data_dir)) - existing_airport_names = { - typing.cast(str, pin["name"]) - for pin in coordinates - if pin.get("type") == "airport" and isinstance(pin.get("name"), str) - } - iata_codes: set[str] = set() - for route in routes: - if route["type"] != "unbooked_flight": - continue - iata_codes.add(typing.cast(str, route.get("from_iata", "LHR"))) - to_iata = route.get("to_iata") - if isinstance(to_iata, str): - iata_codes.add(to_iata) - - for iata in sorted(iata_codes): - airport = airports.get(iata) - if not airport: - continue - airport_name = typing.cast(str, airport["name"]) - if airport_name in existing_airport_names: - continue - coordinates.append( - { - "name": airport_name, - "type": "airport", - "latitude": airport["latitude"], - "longitude": airport["longitude"], - } - ) - existing_airport_names.add(airport_name) - - -def stations_from_travel(t: StrDict) -> list[StrDict]: - """Stations from train journey.""" - station_list = [t["from_station"], t["to_station"]] - for leg in t["legs"]: - station_list.append(leg["from_station"]) - station_list.append(leg["to_station"]) - - return station_list - - -def process_station_list(station_list: list[StrDict]) -> StrDict: - """Proess sation list.""" - stations = {} - for s in station_list: - if s["name"] in stations: - continue - stations[s["name"]] = s - return stations - - -def get_locations(trip: Trip) -> dict[str, StrDict]: - """Collect locations of all travel locations in trip.""" - locations: dict[str, StrDict] = { - "station": {}, - "airport": {}, - "ferry_terminal": {}, - "coach_station": {}, - "bus_stop": {}, - "home": {}, - "car_stop": {}, - } - - station_list = [] - for t in trip.travel: - match t["type"]: - case "train": - station_list += stations_from_travel(t) - case "coach": - for field in ("from_station", "to_station"): - s = t[field] - locations["coach_station"][s["name"]] = s - case "bus": - for field in ("from_station", "to_station"): - s = t[field] - locations["bus_stop"][s["name"]] = s - case "flight": - for field in "from_airport", "to_airport": - if field in t: - locations["airport"][t[field]["iata"]] = t[field] - case "ferry": - for field in "from_terminal", "to_terminal": - terminal = t[field] - locations["ferry_terminal"][terminal["name"]] = terminal - case "car": - for field in ("from_location", "to_location"): - location = t.get(field) - if not location: - continue - location_type = location.get("type", "car_stop") - locations[location_type][location["name"]] = location - - locations["station"] = process_station_list(station_list) - return locations - - -def coordinate_dict(item: StrDict, coord_type: str) -> StrDict: - """Build coordinate dict for item.""" - return { - "name": item["name"], - "type": coord_type, - "latitude": item["latitude"], - "longitude": item["longitude"], - } - - -def coordinate_key(item: StrDict) -> tuple[str, str]: - """Return the de-duplication key for a coordinate item.""" - return (item["type"], item["name"]) - - -def add_coordinate_if_missing(coordinates: list[StrDict], item: StrDict) -> None: - """Append a coordinate item if the same type/name is not already present.""" - key = coordinate_key(item) - if any(coordinate_key(existing) == key for existing in coordinates): - return - coordinates.append(item) - - -def add_coordinates_for_eurostar_home_feeder( - trip: Trip, coordinates: list[StrDict], data_dir: str -) -> None: - """Add the assumed Bristol station pin for London Eurostar feeder travel.""" - if not any( - t["type"] == "train" and is_eurostar_departure_from_london(t) - for t in trip.travel - ): - return - - stations = load_station_lookup(data_dir) - from_station = stations.get(EUROSTAR_HOME_FEEDER_ROUTE[0]) - if from_station is None: - return - - add_coordinate_if_missing(coordinates, coordinate_dict(from_station, "station")) - - -def timed_departures(item: StrDict) -> list[datetime]: - """Return timed train/flight departure datetimes from a travel item.""" - depart_values: list[typing.Any] = [] - - if item.get("type") in ("flight", "train"): - depart_values.append(item.get("depart")) - - if item.get("type") == "train": - legs = item.get("legs", []) - if isinstance(legs, list): - depart_values.extend( - leg.get("depart") for leg in legs if isinstance(leg, dict) - ) - - return [value for value in depart_values if isinstance(value, datetime)] - - -def timed_arrivals(item: StrDict) -> list[datetime]: - """Return timed train/flight arrival datetimes from a travel item.""" - arrive_values: list[typing.Any] = [] - - if item.get("type") in ("flight", "train"): - arrive_values.append(item.get("arrive")) - - if item.get("type") == "train": - legs = item.get("legs", []) - if isinstance(legs, list): - arrive_values.extend( - leg.get("arrive") for leg in legs if isinstance(leg, dict) - ) - - return [value for value in arrive_values if isinstance(value, datetime)] - - -def has_morning_train_or_flight_departure(trip: Trip, target_date: date) -> bool: - """Return true if a train or flight leaves before noon on target_date.""" - return any( - depart.date() == target_date and depart.time() < NOON - for item in trip.travel - for depart in timed_departures(item) - ) - - -def has_afternoon_train_or_flight_arrival(trip: Trip, target_date: date) -> bool: - """Return true if a train or flight arrives after noon on target_date.""" - return any( - arrive.date() == target_date and arrive.time() > NOON - for item in trip.travel - for arrive in timed_arrivals(item) - ) - - -def conference_free_days(trip: Trip) -> dict[str, tuple[int, int]]: - """Return (days_before, days_after) exploration days for each conference. - - Keyed by the conference start date as an ISO string. Days are relative to - the trip boundary or the adjacent conference's end/start for multi-conference - trips. - """ - if not trip.conferences or not trip.end: - return {} - - def conf_attend_start(c: StrDict) -> date: - return as_date(c.get("attend_start") or c["start"]) - - def conf_attend_end(c: StrDict) -> date: - return as_date(c.get("attend_end") or c["end"]) - - sorted_confs = sorted(trip.conferences, key=conf_attend_start) - result: dict[str, tuple[int, int]] = {} - - for i, conf in enumerate(sorted_confs): - before_boundary = conf_attend_end(sorted_confs[i - 1]) if i > 0 else trip.start - after_boundary = ( - conf_attend_start(sorted_confs[i + 1]) - if i < len(sorted_confs) - 1 - else trip.end - ) - days_before = (conf_attend_start(conf) - before_boundary).days - days_after = (after_boundary - conf_attend_end(conf)).days - if ( - i == 0 - and days_before > 0 - and has_afternoon_train_or_flight_arrival(trip, before_boundary) - ): - days_before -= 1 - if ( - i == len(sorted_confs) - 1 - and days_after > 0 - and has_morning_train_or_flight_departure(trip, after_boundary) - ): - days_after -= 1 - result[str(conf["start"])] = (days_before, days_after) - - return result - - -def collect_trip_coordinates(trip: Trip) -> list[StrDict]: - """Extract and de-duplicate travel location coordinates from trip.""" - coords = [] - - src = [ - ("accommodation", trip.accommodation), - ("conference", trip.conferences), - ("event", trip.events), - ] - for coord_type, item_list in src: - coords += [ - coordinate_dict(item, coord_type) - for item in item_list - if "latitude" in item and "longitude" in item - ] - - locations = get_locations(trip) - for coord_type, coord_dict in locations.items(): - coords += [coordinate_dict(s, coord_type) for s in coord_dict.values()] - - return coords - - -def destination_latlon( - stop: StrDict, airports: dict[str, StrDict] -) -> tuple[float, float] | None: - """Resolve destination coordinates from airport lookup or explicit lat/lon.""" - iata = preferred_airport_iata(stop, airports) - if iata: - airport = airports.get(iata) - if airport: - return latlon_tuple(airport) - if "latitude" in stop and "longitude" in stop: - return latlon_tuple(stop) - return None - - -def latlon_tuple(stop: StrDict) -> tuple[float, float]: - """Given a transport stop return the lat/lon as a tuple.""" - return (stop["latitude"], stop["longitude"]) - - -def read_geojson(data_dir: str, filename: str) -> str: - """Read GeoJSON from file.""" - return open(os.path.join(data_dir, filename + ".geojson")).read() - - -def get_trip_routes(trip: Trip, data_dir: str) -> list[StrDict]: - """Get routes for given trip to show on map.""" - routes: list[StrDict] = [] - seen_geojson: set[str] = set() - eurostar_home_feeder_route = get_eurostar_home_feeder_route( - trip, data_dir, seen_geojson - ) - if eurostar_home_feeder_route is not None: - routes.append(eurostar_home_feeder_route) - - for t in trip.travel: - if t["type"] == "ferry": - ferry_from, ferry_to = t["from_terminal"], t["to_terminal"] - - key = "_".join(["ferry"] + sorted([ferry_from["name"], ferry_to["name"]])) - filename = os.path.join("ferry_routes", t["geojson_filename"]) - - routes.append( - { - "type": "train", - "key": key, - "geojson_filename": filename, - } - ) - continue - if t["type"] == "flight": - if "from_airport" not in t or "to_airport" not in t: - continue - fly_from, fly_to = t["from_airport"], t["to_airport"] - key = "_".join(["flight"] + sorted([fly_from["iata"], fly_to["iata"]])) - routes.append( - { - "type": "flight", - "key": key, - "from": latlon_tuple(fly_from), - "to": latlon_tuple(fly_to), - } - ) - continue - if t["type"] in ("coach", "bus"): - route_type = t["type"] - stop_from, stop_to = t["from_station"], t["to_station"] - key = "_".join([route_type] + sorted([stop_from["name"], stop_to["name"]])) - if t.get("geojson_filename"): - filename = os.path.join(f"{route_type}_routes", t["geojson_filename"]) - routes.append( - {"type": route_type, "key": key, "geojson_filename": filename} - ) - else: - routes.append( - { - "type": route_type, - "key": key, - "from": latlon_tuple(stop_from), - "to": latlon_tuple(stop_to), - } - ) - continue - if t["type"] == "car": - route = t.get("geojson_filename") - if not isinstance(route, str): - continue - route_filename = route_filename_without_extension(route) - key = "_".join( - [ - "car", - typing.cast(str, t.get("from", "")), - typing.cast(str, t.get("to", "")), - route_filename, - ] - ) - routes.append( - { - "type": "car", - "key": key, - "geojson_filename": os.path.join("car_routes", route_filename), - } - ) - continue - if t["type"] == "train": - for leg in t["legs"]: - train_from, train_to = leg["from_station"], leg["to_station"] - route = build_train_route(train_from, train_to, seen_geojson) - if route is not None: - routes.append(route) - - if routes: - return routes - - airports = typing.cast(dict[str, StrDict], travel.parse_yaml("airports", data_dir)) - origin_rules = load_flight_destination_rules(data_dir) - - unbooked_routes = [] - for item in trip.conferences: - unbooked_rail_route = get_unbooked_rail_route(item, data_dir) - if unbooked_rail_route is not None: - unbooked_routes.append(unbooked_rail_route) - continue - - if item["country"] in {"gb", "be"}: # not flying to Belgium - continue - destination_iata = preferred_airport_iata(item, airports) - destination = destination_latlon(item, airports) - if not destination: - continue - from_iata = get_unbooked_flight_origin_iata(destination_iata, origin_rules) - from_airport = airports.get(from_iata) - if not from_airport: - from_airport = airports["LHR"] - from_iata = "LHR" - - unbooked_routes.append( - { - "type": "unbooked_flight", - "key": f'{from_iata}_{item["location"]}_{item["country"]}', - "from_iata": from_iata, - "to_iata": destination_iata, - "from": latlon_tuple(from_airport), - "to": destination, - } - ) - - return unbooked_routes - - -def get_coordinates_and_routes( - trip_list: list[Trip], data_dir: str | None = None -) -> tuple[list[StrDict], list[StrDict]]: - """Given a list of trips return the associated coordinates and routes.""" - if data_dir is None: - data_dir = flask.current_app.config["PERSONAL_DATA"] - coordinates = [] - seen_coordinates: set[tuple[str, str]] = set() - routes = [] - seen_routes: set[str] = set() - for trip in trip_list: - for stop in collect_trip_coordinates(trip): - key = coordinate_key(stop) - if key in seen_coordinates: - continue - coordinates.append(stop) - seen_coordinates.add(key) - - add_coordinates_for_eurostar_home_feeder(trip, coordinates, data_dir) - seen_coordinates = {coordinate_key(stop) for stop in coordinates} - - for route in get_trip_routes(trip, data_dir): - if route["key"] in seen_routes: - continue - routes.append(route) - seen_routes.add(route["key"]) - - add_coordinates_for_unbooked_flights(routes, coordinates, data_dir) - - for route in routes: - if "geojson_filename" in route: - route["geojson"] = read_geojson(data_dir, route.pop("geojson_filename")) - - return (coordinates, routes) - - -def get_trip_list( - route_distances: travel.RouteDistances | None = None, -) -> list[Trip]: - """Get list of trips respecting current authentication status.""" - trips = [ - trip - for trip in build_trip_list(route_distances=route_distances) - if flask.g.user.is_authenticated or not trip.private - ] - - # Add Schengen compliance information to each trip - for trip in trips: - trip_schengen.add_schengen_compliance_to_trip(trip) - - return trips - - -DEFAULT_EVENT_DURATION = timedelta(hours=1) - - -def _ensure_utc(dt_value: datetime) -> datetime: - """Ensure datetimes are timezone-aware in UTC.""" - if dt_value.tzinfo is None: - return dt_value.replace(tzinfo=timezone.utc) - return dt_value.astimezone(timezone.utc) - - -def _event_datetimes(element: TripElement) -> tuple[datetime, datetime]: - """Return start and end datetimes for a trip element.""" - start_dt = as_datetime(element.start_time) - end_dt = as_datetime(element.end_time) if element.end_time else None - if end_dt is None or end_dt <= start_dt: - end_dt = start_dt + DEFAULT_EVENT_DURATION - return _ensure_utc(start_dt), _ensure_utc(end_dt) - - -def _event_all_day_dates(element: TripElement) -> tuple[date, date]: - """Return start and exclusive end dates for all-day events.""" - start_date = as_date(element.start_time) - end_source = element.end_time or element.start_time - end_date = as_date(end_source) + timedelta(days=1) - return start_date, end_date - - -def _trip_element_location(element: TripElement) -> str | None: - """Derive a location string for the element.""" - start_loc: str | None = element.start_loc - end_loc: str | None = element.end_loc - if start_loc and end_loc: - return f"{start_loc} → {end_loc}" - if start_loc: - return start_loc - if end_loc: - return end_loc - return None - - -def _trip_element_description(trip: Trip, element: TripElement) -> str: - """Build a textual description for the element.""" - lines = [ - f"Trip: {trip.title}", - f"Type: {element.element_type}", - ] - if element.element_type == "conference": - location_label = element.detail.get("location") - venue_label = element.detail.get("venue") or location_label - if place := _format_place( - location_label, - element.start_country, - element.element_type, - label_prefix="Location", - skip_country_if_in_label=True, - ): - lines.append(place) - if place := _format_place( - venue_label, - element.end_country, - element.element_type, - label_prefix="Venue", - ): - lines.append(place) - else: - if place := _format_place( - element.start_loc, - element.start_country, - element.element_type, - label_prefix="From", - ): - lines.append(place) - if place := _format_place( - element.end_loc, - element.end_country, - element.element_type, - label_prefix="To", - ): - lines.append(place) - lines.append(f"Trip start date: {_format_trip_start_date(trip)}") - return "\n".join(lines) - - -def _format_place( - label: str | None, - country: pycountry.db.Country | None, - element_type: str, - label_prefix: str | None = None, - skip_country_if_in_label: bool = False, -) -> str | None: - """Combine location label and country into a single string.""" - parts: list[str] = [] - formatted_label = label - if element_type == "train" and label: - stripped = label.strip() - if not stripped.lower().endswith("station"): - formatted_label = f"{stripped} station" - else: - formatted_label = stripped - if formatted_label: - parts.append(formatted_label) - if country: - include_country = True - if (element_type == "train" or skip_country_if_in_label) and formatted_label: - include_country = country.name.lower() not in formatted_label.lower() - if include_country: - parts.append(country.name) - if not parts: - return None - if label_prefix: - return f"{label_prefix}: {', '.join(parts)}" - return ", ".join(parts) - - -def _format_trip_start_date(trip: Trip) -> str: - """Return trip start date in UK human-readable form with day name.""" - day_name = trip.start.strftime("%A") - month_year = trip.start.strftime("%B %Y") - return f"{day_name} {trip.start.day} {month_year}" - - -def _flight_iata(detail: StrDict, key: str) -> str | None: - """Extract an IATA code from flight detail.""" - airport = typing.cast(StrDict | None, detail.get(key)) - if not airport: - return None - return typing.cast(str | None, airport.get("iata")) - - -def _flight_label(detail: StrDict) -> str | None: - """Return a compact label for flight elements.""" - from_code = _flight_iata(detail, "from_airport") - to_code = _flight_iata(detail, "to_airport") - if from_code and to_code: - return f"{from_code}→{to_code}" - if from_code: - return from_code - if to_code: - return to_code - return None - - -def _trip_element_label(element: TripElement) -> str: - """Return a succinct label describing the element.""" - if element.element_type == "flight": - if label := _flight_label(element.detail): - return label - if element.element_type == "conference": - conference_name = element.detail.get("name") - if isinstance(conference_name, str): - return conference_name - - start_loc = element.start_loc - end_loc = element.end_loc - if isinstance(start_loc, str) and isinstance(end_loc, str): - return f"{start_loc} → {end_loc}" - if isinstance(start_loc, str): - return start_loc - if isinstance(end_loc, str): - return end_loc - return element.title - - -def _trip_element_summary(trip: Trip, element: TripElement) -> str: - """Build the calendar summary text.""" - label = _trip_element_label(element) - if element.element_type == "conference": - return f"{element.element_type}: {label}" - return f"{element.element_type}: {label} [{trip.title}]" - - -def _trip_element_uid(trip: Trip, element: TripElement, index: int) -> str: - """Generate a deterministic UID for calendar clients.""" - raw = "|".join( - [ - trip.start.isoformat(), - trip.title, - element.element_type, - element.title, - str(index), - ] - ) - digest = hashlib.sha1(raw.encode("utf-8")).hexdigest() - return f"trip-{digest}@agenda-codex" - - -def build_trip_ical(trips: list[Trip]) -> bytes: - """Return an iCal feed containing all trip elements.""" - lines = [ - "BEGIN:VCALENDAR", - "VERSION:2.0", - "PRODID:-//Agenda Codex//Trips//EN", - "CALSCALE:GREGORIAN", - "METHOD:PUBLISH", - "X-WR-CALNAME:Trips", - ] - generated = datetime.now(tz=timezone.utc) - - for trip in trips: - for index, element in enumerate(trip.elements()): - lines.append("BEGIN:VEVENT") - ical.append_property(lines, "UID", _trip_element_uid(trip, element, index)) - ical.append_property(lines, "DTSTAMP", ical.format_datetime_utc(generated)) - if element.all_day: - start_date, end_date = _event_all_day_dates(element) - ical.append_property( - lines, "DTSTART;VALUE=DATE", ical.format_date(start_date) - ) - ical.append_property( - lines, "DTEND;VALUE=DATE", ical.format_date(end_date) - ) - else: - start_dt, end_dt = _event_datetimes(element) - ical.append_property( - lines, "DTSTART", ical.format_datetime_utc(start_dt) - ) - ical.append_property(lines, "DTEND", ical.format_datetime_utc(end_dt)) - summary = ical.escape_text(_trip_element_summary(trip, element)) - ical.append_property(lines, "SUMMARY", summary) - - description = ical.escape_text(_trip_element_description(trip, element)) - ical.append_property(lines, "DESCRIPTION", description) - - if location := _trip_element_location(element): - ical.append_property(lines, "LOCATION", ical.escape_text(location)) - - lines.append("END:VEVENT") - - lines.append("END:VCALENDAR") - ical_text = "\r\n".join(lines) + "\r\n" - return ical_text.encode("utf-8") - - -def get_current_trip(today: date) -> Trip | None: - """Get current trip.""" - trip_list = get_trip_list(route_distances=None) - - current = [ - item - for item in trip_list - if item.start <= today and (item.end or item.start) >= today - ] - assert len(current) < 2 - return current[0] if current else None diff --git a/agenda/trip_schengen.py b/agenda/trip_schengen.py deleted file mode 100644 index 926d022..0000000 --- a/agenda/trip_schengen.py +++ /dev/null @@ -1,279 +0,0 @@ -"""Integration of Schengen calculator with the existing trip system.""" - -import logging -import typing -from datetime import date, timedelta - -import flask - -from . import get_country, trip -from .schengen import ( - SCHENGEN_COUNTRIES, - calculate_schengen_time, - extract_schengen_stays_from_travel, -) -from .types import SchengenCalculation, SchengenStay, StrDict, Trip - - -def trip_includes_schengen(trip: Trip) -> bool: - return bool({c.alpha_2.lower() for c in trip.countries} & SCHENGEN_COUNTRIES) - - -def add_schengen_compliance_to_trip(trip: Trip) -> Trip: - """Add Schengen compliance information to a trip object.""" - if not trip_includes_schengen(trip): - return trip - - try: - # Calculate Schengen compliance for the trip - calculation = calculate_schengen_time(trip.travel) - - # Add the calculation to the trip object - trip.schengen_compliance = calculation - except Exception as e: - # Log the error but don't fail the trip loading - logging.warning( - f"Failed to calculate Schengen compliance for trip {trip.start}: {e}" - ) - trip.schengen_compliance = None - - return trip - - -def get_schengen_compliance_for_all_trips( - trip_list: list[Trip], -) -> dict[date, SchengenCalculation]: - """Calculate Schengen compliance for all trips.""" - compliance_by_date = {} - - # Collect all travel items across all trips - all_travel_items = [] - for trip_obj in trip_list: - all_travel_items.extend(trip_obj.travel) - - # Calculate compliance for each trip's start date - for trip_obj in trip_list: - calculation = calculate_schengen_time(all_travel_items, trip_obj.start) - compliance_by_date[trip_obj.start] = calculation - - return compliance_by_date - - -def generate_schengen_warnings(trip_list: list[Trip]) -> list[dict[str, typing.Any]]: - """Generate warnings for potential Schengen violations.""" - warnings = [] - - # Get compliance for all trips - compliance_by_date = get_schengen_compliance_for_all_trips(trip_list) - - for trip_date, calculation in compliance_by_date.items(): - if not calculation.is_compliant: - warnings.append( - { - "type": "schengen_violation", - "date": trip_date, - "message": f"Schengen violation on {trip_date}: {calculation.days_over_limit} days over limit", - "severity": "error", - "calculation": calculation, - } - ) - elif calculation.days_remaining < 10: - warnings.append( - { - "type": "schengen_warning", - "date": trip_date, - "message": f"Low Schengen allowance on {trip_date}: only {calculation.days_remaining} days remaining", - "severity": "warning", - "calculation": calculation, - } - ) - - return warnings - - -def extract_schengen_stays_with_trip_info( - trip_list: list[Trip], -) -> list[SchengenStay]: - """Extract Schengen stays from travel items with trip information.""" - # Create a mapping of travel items to their trips - travel_to_trip_map = {} - for trip_obj in trip_list: - for travel_item in trip_obj.travel: - travel_to_trip_map[id(travel_item)] = trip_obj - - # Collect all travel items - all_travel_items = [] - for trip_obj in trip_list: - all_travel_items.extend(trip_obj.travel) - - # Get stays with trip information - stays = extract_schengen_stays_from_travel(all_travel_items) - - # Try to associate stays with trips based on entry dates - for stay in stays: - # Find the trip that contains this stay's entry date - for trip_obj in trip_list: - if trip_obj.start <= stay.entry_date <= (trip_obj.end or stay.entry_date): - stay.trip_date = trip_obj.start - stay.trip_name = trip_obj.title - break - - # If no exact match, find the closest trip by start date - if stay.trip_date is None: - closest_trip = min( - trip_list, - key=lambda t: abs((t.start - stay.entry_date).days), - default=None, - ) - if closest_trip: - stay.trip_date = closest_trip.start - stay.trip_name = closest_trip.title - - return stays - - -def schengen_dashboard_data(data_dir: str | None = None) -> dict[str, typing.Any]: - """Generate dashboard data for Schengen compliance.""" - if data_dir is None: - data_dir = flask.current_app.config["PERSONAL_DATA"] - - # Load all trips - trip_list = [ - trip for trip in trip.build_trip_list(data_dir) if trip_includes_schengen(trip) - ] - - # Calculate current compliance with trip information - all_travel_items = [] - for trip_obj in trip_list: - all_travel_items.extend(trip_obj.travel) - - current_calculation = calculate_schengen_time(all_travel_items) - - # Get stays with trip information - stays_with_trip_info = extract_schengen_stays_with_trip_info(trip_list) - - # Update current calculation with trip information - current_calculation.stays_in_period = [ - stay - for stay in stays_with_trip_info - if stay.entry_date >= current_calculation.current_180_day_period[0] - and stay.entry_date <= current_calculation.current_180_day_period[1] - ] - - # Generate warnings - warnings = generate_schengen_warnings(trip_list) - - # Get compliance history for the last year - compliance_history = [] - current_date = date.today() - for i in range(365, 0, -7): # Weekly snapshots for the last year - snapshot_date = current_date - timedelta(days=i) - calculation = calculate_schengen_time(all_travel_items, snapshot_date) - compliance_history.append( - { - "date": snapshot_date, - "days_used": calculation.total_days_used, - "is_compliant": calculation.is_compliant, - } - ) - - return { - "current_compliance": current_calculation, - "warnings": warnings, - "compliance_history": compliance_history, - "trips_with_compliance": get_schengen_compliance_for_all_trips(trip_list), - } - - -def flask_route_schengen_report() -> str: - """Flask route for Schengen compliance report.""" - data_dir = flask.current_app.config["PERSONAL_DATA"] - dashboard_data = schengen_dashboard_data(data_dir) - - # Load trips for the template - trip_list = trip.build_trip_list(data_dir) - - return flask.render_template( - "schengen_report.html", - trip_list=trip_list, - get_country=get_country, - **dashboard_data, - ) - - -def export_schengen_data_for_external_calculator( - travel_items: list[StrDict], -) -> list[dict[str, typing.Any]]: - """Export travel data in format suitable for external Schengen calculators.""" - - stays = extract_schengen_stays_from_travel(travel_items) - - export_data = [] - for stay in stays: - export_data.append( - { - "entry_date": stay.entry_date.strftime("%Y-%m-%d"), - "exit_date": ( - stay.exit_date.strftime("%Y-%m-%d") if stay.exit_date else None - ), - "country": stay.country.upper(), - "days": stay.days, - } - ) - - return export_data - - -# Integration with existing trip.py functions -def enhanced_build_trip_list( - data_dir: str | None = None, - route_distances: typing.Any = None, - include_schengen: bool = True, -) -> list[Trip]: - """Enhanced version of build_trip_list that includes Schengen compliance.""" - # Use the original function - trip_list = trip.build_trip_list(data_dir, route_distances) - - if include_schengen: - # Add Schengen compliance to each trip - for trip_obj in trip_list: - add_schengen_compliance_to_trip(trip_obj) - - return trip_list - - -def check_schengen_compliance_for_new_trip( - existing_trips: list[Trip], - new_trip_dates: tuple[date, date], - destination_country: str, -) -> SchengenCalculation: - """Check if a new trip would violate Schengen rules.""" - # Collect all existing travel - all_travel_items = [] - for trip_obj in existing_trips: - all_travel_items.extend(trip_obj.travel) - - # Add the new trip as mock travel items - entry_date, exit_date = new_trip_dates - - # Mock entry to Schengen - all_travel_items.append( - { - "type": "flight", - "depart": entry_date, - "to_airport": {"country": destination_country}, - } - ) - - # Mock exit from Schengen - all_travel_items.append( - { - "type": "flight", - "depart": exit_date, - "from_airport": {"country": destination_country}, - "to_airport": {"country": "gb"}, # Assuming return to UK - } - ) - - # Calculate compliance at the exit date - return calculate_schengen_time(all_travel_items, exit_date) diff --git a/agenda/types.py b/agenda/types.py index 5450313..ce58d3a 100644 --- a/agenda/types.py +++ b/agenda/types.py @@ -1,498 +1,104 @@ """Types.""" -import collections +import dataclasses import datetime -import functools -import typing -from collections import defaultdict -from dataclasses import dataclass, field -from datetime import date -import emoji -from pycountry.db import Country -import agenda -from agenda import format_list_with_ampersand - -from . import utils - -StrDict = dict[str, typing.Any] -DateOrDateTime = datetime.datetime | datetime.date - - -@dataclass -class TripElement: - """Trip element.""" - - start_time: DateOrDateTime - title: str - element_type: str - detail: StrDict - end_time: DateOrDateTime | None = None - start_loc: str | None = None - end_loc: str | None = None - start_country: Country | None = None - end_country: Country | None = None - all_day: bool = False - - def get_emoji(self) -> str | None: - """Emoji for trip element.""" - emoji_map = { - "check-in": ":hotel:", - "check-out": ":hotel:", - "train": ":train:", - "flight": ":airplane:", - "ferry": ":ferry:", - "coach": ":bus:", - "bus": ":bus:", - "car": ":automobile:", - } - - alias = emoji_map.get(self.element_type) - return emoji.emojize(alias, language="alias") if alias else None - - -def airport_label(airport: StrDict) -> str: - """Airport label: name and iata.""" - name = airport.get("alt_name") or airport["city"] - return f"{name} ({airport['iata']})" - - -def is_generated_drive_stop(label: str) -> bool: - """Return whether a car stop label is synthetic timeline filler.""" - prefix = "Drive stop " - return label.startswith(prefix) and label[len(prefix) :].isdigit() - - -@dataclass -class SchengenStay: - """Represents a stay in the Schengen area.""" - - entry_date: date - exit_date: date | None # None if currently in Schengen - country: str - days: int - trip_date: date | None = None # Trip start date for linking - trip_name: str | None = None # Trip name for display - - def __post_init__(self) -> None: - """Post init.""" - if self.exit_date is None: - # Currently in Schengen, calculate days up to today - self.days = (date.today() - self.entry_date).days + 1 - else: - self.days = (self.exit_date - self.entry_date).days + 1 - - -@dataclass -class SchengenCalculation: - """Result of Schengen time calculation.""" - - total_days_used: int - days_remaining: int - is_compliant: bool - current_180_day_period: tuple[date, date] # (start, end) - stays_in_period: list[SchengenStay] - next_reset_date: typing.Optional[date] # When the 180-day window resets - - @property - def days_over_limit(self) -> int: - """Days over the 90-day limit.""" - return max(0, self.total_days_used - 90) - - -@dataclass -class Trip: - """Trip.""" - - start: datetime.date - travel: list[StrDict] = field(default_factory=list) - accommodation: list[StrDict] = field(default_factory=list) - conferences: list[StrDict] = field(default_factory=list) - events: list[StrDict] = field(default_factory=list) - flight_bookings: list[StrDict] = field(default_factory=list) - name: str | None = None - private: bool = False - schengen_compliance: SchengenCalculation | None = None - - @property - def title(self) -> str: - """Trip title.""" - if self.name: - return self.name - titles: list[str] = [conf["name"] for conf in self.conferences] + [ - event["title"] for event in self.events - ] or self.titles_from_travel() - - if not titles: - titles = [acc["location"] for acc in self.accommodation] - - return format_list_with_ampersand(titles) or "[unnamed trip]" - - def titles_from_travel(self) -> list[str]: - """Titles from travel.""" - titles = [] - for travel in self.travel: - if not (depart := (travel["depart"] and utils.as_date(travel["depart"]))): - continue - for when, from_or_to in ((self.start, "from"), (self.end, "to")): - if depart == when: - continue - label = travel.get(from_or_to) - if not isinstance(label, str) or is_generated_drive_stop(label): - continue - if label not in titles: - titles.append(label) - return titles - - @property - def end(self) -> datetime.date | None: - """End date for trip.""" - max_conference_end = ( - max(utils.as_date(item["end"]) for item in self.conferences) - if self.conferences - else datetime.date.min - ) - assert isinstance(max_conference_end, datetime.date) - - arrive = [ - utils.as_date(item["arrive"]) for item in self.travel if "arrive" in item - ] - travel_end = max(arrive) if arrive else datetime.date.min - assert isinstance(travel_end, datetime.date) - - accommodation_end = ( - max(utils.as_date(item["to"]) for item in self.accommodation) - if self.accommodation - else datetime.date.min - ) - assert isinstance(accommodation_end, datetime.date) - - max_date = max(max_conference_end, travel_end, accommodation_end) - return max_date if max_date != datetime.date.min else None - - def locations(self) -> list[tuple[str, Country]]: - """Locations for trip.""" - seen: set[tuple[str, str]] = set() - items = [] - - for item in self.conferences + self.accommodation + self.events: - if "country" not in item or "location" not in item: - continue - key = (item["location"], item["country"]) - if key in seen: - continue - seen.add(key) - - country = agenda.get_country(item["country"]) - assert country - items.append((item["location"], country)) - - return items - - @property - def countries(self) -> list[Country]: - """Countries visited as part of trip, in order.""" - seen: set[str] = set() - items: list[Country] = [] - for item in self.conferences + self.accommodation + self.events: - if "country" not in item: - continue - if item["country"] in seen: - continue - seen.add(item["country"]) - country = agenda.get_country(item["country"]) - assert country - items.append(country) - - for item in self.travel: - travel_countries = set() - if item["type"] == "flight": - for key in "from_airport", "to_airport": - c = item[key]["country"] - travel_countries.add(c) - if item["type"] == "train": - for leg in item["legs"]: - for key in "from_station", "to_station": - c = leg[key]["country"] - travel_countries.add(c) - - for c in travel_countries - seen: - seen.add(c) - country = agenda.get_country(c) - assert country - items.append(country) - - # Don't include GB in countries visited unless entire trip was GB based - return [c for c in items if c.alpha_2 != "GB"] or items - - @functools.cached_property - def show_flags(self) -> bool: - """Show flags for international trips.""" - return len({c for c in self.countries if c.name != "United Kingdom"}) > 1 - - @property - def countries_str(self) -> str: - """List of countries visited on this trip.""" - return format_list_with_ampersand( - [f"{c.name} {c.flag}" for c in self.countries] - ) - - @property - def locations_str(self) -> str: - """List of countries visited on this trip.""" - return format_list_with_ampersand( - [ - f"{location} ({c.name})" + (f" {c.flag}" if self.show_flags else "") - for location, c in self.locations() - ] - ) - - @property - def country_flags(self) -> str: - """Countries flags for trip.""" - return "".join(c.flag for c in self.countries) - - def total_distance(self) -> float: - """Total distance for trip. - - Sums distances for travel items where a distance value is present. - Ignores legs with missing or falsy distances rather than returning None. - """ - total = 0.0 - for t in self.travel: - distance = t.get("distance") - if distance: - total += distance - return total - - def total_co2_kg(self) -> float | None: - """Total CO₂ for trip.""" - return sum(float(t.get("co2_kg", 0)) for t in self.travel) - - @property - def flights(self) -> list[StrDict]: - """Flights.""" - return [item for item in self.travel if item["type"] == "flight"] - - def distances_by_transport_type(self) -> list[tuple[str, float]]: - """Calculate the total distance travelled for each type of transport. - - Any travel item with a missing or None 'distance' field is ignored. - """ - transport_distances: defaultdict[str, float] = defaultdict(float) - - for item in self.travel: - distance = item.get("distance") - if distance: - transport_type: str = item.get("type", "unknown") - transport_distances[transport_type] += distance - - return list(transport_distances.items()) - - def co2_by_transport_type(self) -> list[tuple[str, float]]: - """Calculate the total CO₂ emissions for each type of transport. - - Any travel item with a missing or None 'co2_kg' field is ignored. - """ - transport_co2: defaultdict[str, float] = defaultdict(float) - - for item in self.travel: - co2_kg = item.get("co2_kg") - if co2_kg: - transport_type: str = item.get("type", "unknown") - transport_co2[transport_type] += float(co2_kg) - - return list(transport_co2.items()) - - def elements(self) -> list[TripElement]: - """Trip elements ordered by time.""" - elements: list[TripElement] = [] - - for item in self.accommodation: - title = "Airbnb" if item.get("operator") == "airbnb" else item["name"] - start = TripElement( - start_time=item["from"], - title=title, - detail=item, - element_type="check-in", - ) - - elements.append(start) - - end = TripElement( - start_time=item["to"], - title=title, - detail=item, - element_type="check-out", - ) - - elements.append(end) - - for item in self.conferences: - location_label = item.get("venue") or item.get("location") or item["name"] - country = agenda.get_country(item.get("country")) - - elements.append( - TripElement( - start_time=item["start"], - end_time=item["end"], - title=item["name"], - detail=item, - element_type="conference", - start_loc=location_label, - start_country=country, - end_country=country, - all_day=True, - ) - ) - - for item in self.travel: - if item["type"] == "flight": - name = ( - f"{airport_label(item['from_airport'])} → " - + f"{airport_label(item['to_airport'])}" - ) - - from_country = agenda.get_country(item["from_airport"]["country"]) - to_country = agenda.get_country(item["to_airport"]["country"]) - - elements.append( - TripElement( - start_time=item["depart"], - end_time=item.get("arrive"), - title=name, - detail=item, - element_type="flight", - start_loc=airport_label(item["from_airport"]), - end_loc=airport_label(item["to_airport"]), - start_country=from_country, - end_country=to_country, - ) - ) - if item["type"] == "train": - for leg in item["legs"]: - from_country = agenda.get_country(leg["from_station"]["country"]) - to_country = agenda.get_country(leg["to_station"]["country"]) - - assert from_country and to_country - name = f"{leg['from']} → {leg['to']}" - elements.append( - TripElement( - start_time=leg["depart"], - end_time=leg["arrive"], - title=name, - detail=leg, - element_type="train", - start_loc=leg["from"], - end_loc=leg["to"], - start_country=from_country, - end_country=to_country, - ) - ) - if item["type"] == "ferry": - from_country = agenda.get_country(item["from_terminal"]["country"]) - to_country = agenda.get_country(item["to_terminal"]["country"]) - - name = f"{item['from']} → {item['to']}" - elements.append( - TripElement( - start_time=item["depart"], - end_time=item["arrive"], - title=name, - detail=item, - element_type="ferry", - start_loc=item["from"], - end_loc=item["to"], - start_country=from_country, - end_country=to_country, - ) - ) - if item["type"] in ("coach", "bus"): - from_country = agenda.get_country(item["from_station"]["country"]) - to_country = agenda.get_country(item["to_station"]["country"]) - name = f"{item['from']} → {item['to']}" - elements.append( - TripElement( - start_time=item["depart"], - end_time=item.get("arrive"), - title=name, - detail=item, - element_type=item["type"], - start_loc=item["from"], - end_loc=item["to"], - start_country=from_country, - end_country=to_country, - ) - ) - if item["type"] == "car": - start_location = item.get("from_location", {}) - end_location = item.get("to_location", {}) - from_country = agenda.get_country(start_location.get("country")) - to_country = agenda.get_country(end_location.get("country")) - name = f"{item.get('from', 'Car journey')} → {item.get('to', 'destination')}" - elements.append( - TripElement( - start_time=item["depart"], - end_time=item.get("arrive"), - title=name, - detail=item, - element_type="car", - start_loc=item.get("from"), - end_loc=item.get("to"), - start_country=from_country, - end_country=to_country, - ) - ) - - return sorted(elements, key=lambda e: utils.as_datetime(e.start_time)) - - def elements_grouped_by_day(self) -> list[tuple[datetime.date, list[TripElement]]]: - """Group trip elements by day.""" - # Create a dictionary to hold lists of TripElements grouped by their date - grouped_elements: collections.defaultdict[datetime.date, list[TripElement]] = ( - collections.defaultdict(list) - ) - - for element in self.elements(): - # Extract the date part of the 'when' attribute - day = utils.as_date(element.start_time) - grouped_elements[day].append(element) - - # Sort elements within each day - for day in grouped_elements: - grouped_elements[day].sort( - key=lambda e: ( - e.element_type == "check-in", # check-out elements last - e.element_type != "check-out", # check-in elements first - utils.as_datetime(e.start_time), # then sort by time - ) - ) - - # Convert the dictionary to a sorted list of tuples - grouped_elements_list = sorted(grouped_elements.items()) - - return grouped_elements_list - - -# Example usage: -# You would call the function with your travel list here to get the results. - - -@dataclass +@dataclasses.dataclass class Holiday: """Holiay.""" name: str country: str date: datetime.date - local_name: str | None = None + + +@dataclasses.dataclass +class Event: + """Event.""" + + name: str + date: datetime.date | datetime.datetime + end_date: datetime.date | datetime.datetime | None = None + title: str | None = None + url: str | None = None + going: bool | None = None @property - def display_name(self) -> str: - """Format name for display.""" + def as_datetime(self) -> datetime.datetime: + """Date/time of event.""" + d = self.date + t0 = datetime.datetime.min.time() return ( - f"{self.name} ({self.local_name})" - if self.local_name and self.local_name != self.name - else self.name + d + if isinstance(d, datetime.datetime) + else datetime.datetime.combine(d, t0).replace(tzinfo=datetime.timezone.utc) ) + + @property + def has_time(self) -> bool: + """Event has a time associated with it.""" + return isinstance(self.date, datetime.datetime) + + @property + def as_date(self) -> datetime.date: + """Date of event.""" + return ( + self.date.date() if isinstance(self.date, datetime.datetime) else self.date + ) + + @property + def end_as_date(self) -> datetime.date: + """Date of event.""" + return ( + ( + self.end_date.date() + if isinstance(self.end_date, datetime.datetime) + else self.end_date + ) + if self.end_date + else self.as_date + ) + + @property + def display_time(self) -> str | None: + """Time for display on web page.""" + return ( + self.date.strftime("%H:%M") + if isinstance(self.date, datetime.datetime) + else None + ) + + @property + def display_timezone(self) -> str | None: + """Timezone for display on web page.""" + return ( + self.date.strftime("%z") + if isinstance(self.date, datetime.datetime) + else None + ) + + def delta_days(self, today: datetime.date) -> str: + """Return number of days from today as a string.""" + delta = (self.as_date - today).days + + match delta: + case 0: + return "today" + case 1: + return "1 day" + case _: + return f"{delta:,d} days" + + @property + def display_date(self) -> str: + """Date for display on web page.""" + if isinstance(self.date, datetime.datetime): + return self.date.strftime("%a, %d, %b %Y %H:%M %z") + else: + return self.date.strftime("%a, %d, %b %Y") + + @property + def display_title(self) -> str: + """Name for display.""" + return self.title or self.name diff --git a/agenda/uk_holiday.py b/agenda/uk_holiday.py index 3bdbfd2..d1ae03e 100644 --- a/agenda/uk_holiday.py +++ b/agenda/uk_holiday.py @@ -3,38 +3,29 @@ import json import os from datetime import date, datetime, timedelta +from time import time import httpx from dateutil.easter import easter -from .types import Holiday, StrDict - -url = "https://www.gov.uk/bank-holidays.json" +from .types import Holiday -def json_filename(data_dir: str) -> str: - """Filename for cached bank holidays.""" - assert os.path.exists(data_dir) - return os.path.join(data_dir, "bank-holidays.json") - - -async def get_holiday_list(data_dir: str) -> list[StrDict]: - """Download holiday list and save cache.""" - filename = json_filename(data_dir) - - async with httpx.AsyncClient() as client: - r = await client.get(url) - events: list[StrDict] = r.json()["england-and-wales"]["events"] # check valid - open(filename, "w").write(r.text) - return events - - -def bank_holiday_list(start_date: date, end_date: date, data_dir: str) -> list[Holiday]: +async def bank_holiday_list( + start_date: date, end_date: date, data_dir: str +) -> list[Holiday]: """Date and name of the next UK bank holiday.""" - filename = json_filename(data_dir) + url = "https://www.gov.uk/bank-holidays.json" + filename = os.path.join(data_dir, "bank-holidays.json") + mtime = os.path.getmtime(filename) + if (time() - mtime) > 60 * 60 * 6: # six hours + async with httpx.AsyncClient() as client: + r = await client.get(url) + open(filename, "w").write(r.text) + events = json.load(open(filename))["england-and-wales"]["events"] hols: list[Holiday] = [] - for event in json.load(open(filename))["england-and-wales"]["events"]: + for event in events: event_date = datetime.strptime(event["date"], "%Y-%m-%d").date() if event_date < start_date: continue diff --git a/agenda/uk_school_holiday.py b/agenda/uk_school_holiday.py deleted file mode 100644 index 1215da9..0000000 --- a/agenda/uk_school_holiday.py +++ /dev/null @@ -1,249 +0,0 @@ -"""UK school holidays (Bristol) via iCalendar.""" - -from __future__ import annotations - -import datetime -import json -import os - -import httpx - -from .event import Event - -school_holiday_page_url = ( - "https://www.bristol.gov.uk/residents/schools-learning-and-early-years/" - "school-term-and-holiday-dates" -) -school_holiday_ics_url = ( - "https://www.bristol.gov.uk/files/documents/" - "4641-bristol-school-term-and-holiday-dates-2021-2022-and-2022-2023-and-2023-" - "2024-calendar" -) - - -def ics_filename(data_dir: str) -> str: - """Filename for cached school-holiday ICS.""" - assert os.path.exists(data_dir) - return os.path.join(data_dir, "bristol-school-holidays.ics") - - -def json_filename(data_dir: str) -> str: - """Filename for cached parsed school-holiday data.""" - assert os.path.exists(data_dir) - return os.path.join(data_dir, "bristol-school-holidays.json") - - -def _unescape_ics_text(value: str) -> str: - """Decode escaped ICS text values.""" - return ( - value.replace("\\n", " ") - .replace("\\N", " ") - .replace("\\,", ",") - .replace("\\;", ";") - .replace("\\\\", "\\") - ).strip() - - -def unfold_ics_lines(ics_text: str) -> list[str]: - """Unfold folded ICS lines (RFC5545).""" - unfolded: list[str] = [] - for raw_line in ics_text.splitlines(): - line = raw_line.rstrip("\r\n") - if not line: - continue - if unfolded and line[:1] in {" ", "\t"}: - unfolded[-1] += line[1:] - else: - unfolded.append(line) - return unfolded - - -def _parse_ics_date(value: str) -> datetime.date: - """Parse date/date-time values in ICS.""" - value = value.strip() - if "T" in value: - date_part = value.split("T", 1)[0] - return datetime.datetime.strptime(date_part, "%Y%m%d").date() - return datetime.datetime.strptime(value, "%Y%m%d").date() - - -def _is_school_holiday_summary(summary: str) -> bool: - """Return True if summary looks like a school holiday event.""" - lower = summary.lower() - if "holiday" not in lower: - return False - if "bank holiday" in lower: - return False - return True - - -def _clean_summary(summary: str) -> str: - """Normalise holiday summary text for display.""" - summary = _unescape_ics_text(summary) - # The feed embeds long policy notes in parentheses after the name. - if " (" in summary: - summary = summary.split(" (", 1)[0] - return summary.strip() - - -def parse_school_holidays_from_ics(ics_text: str) -> list[Event]: - """Parse school holiday ranges from an ICS file as Events.""" - events: list[Event] = [] - current: dict[str, str] = {} - - def flush_current() -> None: - summary = current.get("SUMMARY") - dtstart = current.get("DTSTART") - dtend = current.get("DTEND") - if not summary or not dtstart or not dtend: - return - - clean_summary = _clean_summary(summary) - if not _is_school_holiday_summary(clean_summary): - return - - start_date = _parse_ics_date(dtstart) - end_exclusive = _parse_ics_date(dtend) - end_date = end_exclusive - datetime.timedelta(days=1) - if end_date < start_date: - return - - events.append( - Event( - name="uk_school_holiday", - date=start_date, - end_date=end_date, - title=clean_summary, - url=school_holiday_page_url, - ) - ) - - for line in unfold_ics_lines(ics_text): - if line == "BEGIN:VEVENT": - current = {} - continue - if line == "END:VEVENT": - flush_current() - current = {} - continue - - if ":" not in line: - continue - - key_part, value = line.split(":", 1) - key = key_part.split(";", 1)[0].upper() - - if key in {"SUMMARY", "DTSTART", "DTEND"}: - current[key] = value.strip() - - # De-duplicate by title/date-range. - unique: dict[tuple[str, datetime.date, datetime.date], Event] = {} - for event in events: - end_date = event.end_as_date - unique[(event.title or event.name, event.as_date, end_date)] = event - - return sorted(unique.values(), key=lambda item: (item.as_date, item.end_as_date)) - - -def write_school_holidays_json(events: list[Event], data_dir: str) -> None: - """Write parsed school-holiday events to JSON cache.""" - filename = json_filename(data_dir) - payload: list[dict[str, str]] = [ - { - "name": event.name, - "title": event.title or event.name, - "start": event.as_date.isoformat(), - "end": event.end_as_date.isoformat(), - "url": event.url or "", - } - for event in events - ] - with open(filename, "w", encoding="utf-8") as out: - json.dump(payload, out, indent=2) - - -def read_school_holidays_json(data_dir: str) -> list[Event]: - """Read parsed school-holiday events from JSON cache.""" - filename = json_filename(data_dir) - if not os.path.exists(filename): - return [] - - with open(filename, encoding="utf-8") as in_file: - loaded = json.load(in_file) - if not isinstance(loaded, list): - return [] - - parsed_events: list[Event] = [] - for raw_item in loaded: - if not isinstance(raw_item, dict): - continue - title = raw_item.get("title") - start_value = raw_item.get("start") - end_value = raw_item.get("end") - if not ( - isinstance(title, str) - and isinstance(start_value, str) - and isinstance(end_value, str) - ): - continue - - try: - start_date = datetime.date.fromisoformat(start_value) - end_date = datetime.date.fromisoformat(end_value) - except ValueError: - continue - - event_url = raw_item.get("url") - parsed_events.append( - Event( - name="uk_school_holiday", - date=start_date, - end_date=end_date, - title=title, - url=event_url if isinstance(event_url, str) and event_url else None, - ) - ) - - return sorted(parsed_events, key=lambda item: (item.as_date, item.end_as_date)) - - -def school_holiday_list( - start_date: datetime.date, - end_date: datetime.date, - data_dir: str, -) -> list[Event]: - """Get cached school-holiday events overlapping the supplied range.""" - items = read_school_holidays_json(data_dir) - return [ - item - for item in items - if item.as_date <= end_date and item.end_as_date >= start_date - ] - - -async def get_holiday_list(data_dir: str) -> list[Event]: - """Download, parse and cache school-holiday data.""" - headers = { - "User-Agent": ( - "Mozilla/5.0 (X11; Linux x86_64) " - "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0 Safari/537.36" - ), - "Accept": "text/calendar,*/*;q=0.9", - "Referer": school_holiday_page_url, - } - - async with httpx.AsyncClient(timeout=60.0, follow_redirects=True) as client: - response = await client.get(school_holiday_ics_url, headers=headers) - response.raise_for_status() - - content_type = response.headers.get("content-type", "") - ics_text = response.text - if "text/calendar" not in content_type and "BEGIN:VCALENDAR" not in ics_text: - raise ValueError("School holiday ICS download did not return calendar content") - - with open(ics_filename(data_dir), "w", encoding="utf-8") as out: - out.write(ics_text) - - events = parse_school_holidays_from_ics(ics_text) - write_school_holidays_json(events, data_dir) - return events diff --git a/agenda/utils.py b/agenda/utils.py deleted file mode 100644 index 8c92790..0000000 --- a/agenda/utils.py +++ /dev/null @@ -1,135 +0,0 @@ -"""Utility functions.""" - -import os -import typing -from datetime import date, datetime, time, timedelta, timezone -from time import time as unixtime -from zoneinfo import ZoneInfo - -StrDict = dict[str, typing.Any] - - -def as_date(d: datetime | date) -> date: - """Convert datetime to date.""" - match d: - case datetime(): - return d.date() - case date(): - return d - case _: - raise TypeError(f"Unsupported type: {type(d)}") - - -def as_datetime(d: datetime | date) -> datetime: - """Date/time of event.""" - match d: - case datetime(): - return d - case date(): - return datetime.combine(d, datetime.min.time()).replace(tzinfo=timezone.utc) - case _: - raise TypeError(f"Unsupported type: {type(d)}") - - -def timedelta_display(delta: timedelta) -> str: - """Format timedelta as a human readable string.""" - total_seconds = int(delta.total_seconds()) - days, remainder = divmod(total_seconds, 24 * 60 * 60) - hours, remainder = divmod(remainder, 60 * 60) - mins, secs = divmod(remainder, 60) - - return " ".join( - f"{v} {label}" - for v, label in ((days, "days"), (hours, "hrs"), (mins, "mins")) - if v - ) - - -def plural(value: int, unit: str) -> str: - """Value + unit with unit written as singular or plural as appropriate.""" - return f"{value} {unit}{'s' if value > 1 else ''}" - - -def human_readable_delta(future_date: date) -> str | None: - """ - Calculate the human-readable time delta for a given future date. - - Args: - future_date (date): The future date as a datetime.date object. - - Returns: - str: Human-readable time delta. - """ - # Ensure the input is a future date - if future_date <= date.today(): - return None - - # Calculate the delta - delta = future_date - date.today() - - # Convert delta to a more human-readable format - months, days = divmod(delta.days, 30) - weeks, days = divmod(days, 7) - - # Formatting the output - parts = [ - plural(value, unit) - for value, unit in ((months, "month"), (weeks, "week"), (days, "day")) - if value > 0 - ] - return " ".join(parts) if parts else None - - -def filename_timestamp(filename: str, ext: str) -> tuple[datetime, str] | None: - """Get datetime from filename.""" - try: - ts = datetime.strptime(filename, f"%Y-%m-%d_%H:%M:%S.{ext}") - except ValueError: - return None - return (ts, filename) - - -def get_most_recent_file(directory: str, ext: str) -> str | None: - """Get most recent file from directory.""" - existing = [ - x for x in (filename_timestamp(f, ext) for f in os.listdir(directory)) if x - ] - if not existing: - return None - existing.sort(reverse=True) - return os.path.join(directory, existing[0][1]) - - -def make_waste_dir(data_dir: str) -> None: - """Make waste dir if missing.""" - waste_dir = os.path.join(data_dir, "waste") - if not os.path.exists(waste_dir): - os.mkdir(waste_dir) - - -async def time_function( - name: str, - func: typing.Callable[..., typing.Coroutine[typing.Any, typing.Any, typing.Any]], - *args: typing.Any, - **kwargs: typing.Any, -) -> tuple[str, typing.Any, float, Exception | None]: - """Time the execution of an asynchronous function.""" - start_time, result, exception = unixtime(), None, None - try: - result = await func(*args, **kwargs) - except Exception as e: - exception = e - end_time = unixtime() - return name, result, end_time - start_time, exception - - -def depart_datetime(item: StrDict) -> datetime: - """Return a datetime for this travel item. - - If the travel item already has a datetime return that, otherwise if the - departure time is just a date return midnight UTC for that date. - """ - depart = item["depart"] - if isinstance(depart, datetime): - return depart - return datetime.combine(depart, time.min).replace(tzinfo=ZoneInfo("UTC")) diff --git a/agenda/waste_schedule.py b/agenda/waste_schedule.py new file mode 100644 index 0000000..4f4dade --- /dev/null +++ b/agenda/waste_schedule.py @@ -0,0 +1,209 @@ +"""Waste collection schedules.""" + +import json +import os +import re +import typing +from collections import defaultdict +from datetime import date, datetime, time, timedelta + +import httpx +import lxml.html + +from . import uk_time +from .types import Event + +ttl_hours = 12 + + +def make_waste_dir(data_dir: str) -> None: + """Make waste dir if missing.""" + waste_dir = os.path.join(data_dir, "waste") + if not os.path.exists(waste_dir): + os.mkdir(waste_dir) + + +async def get_html(data_dir: str, postcode: str, uprn: str) -> str: + """Get waste schedule.""" + now = datetime.now() + waste_dir = os.path.join(data_dir, "waste") + + make_waste_dir(data_dir) + + existing_data = os.listdir(waste_dir) + existing = [f for f in existing_data if f.endswith(".html")] + if existing: + recent_filename = max(existing) + recent = datetime.strptime(recent_filename, "%Y-%m-%d_%H:%M.html") + delta = now - recent + + if existing and delta < timedelta(hours=ttl_hours): + return open(os.path.join(waste_dir, recent_filename)).read() + + now_str = now.strftime("%Y-%m-%d_%H:%M") + filename = f"{waste_dir}/{now_str}.html" + + forms_base_url = "https://forms.n-somerset.gov.uk" + # url2 = "https://forms.n-somerset.gov.uk/Waste/CollectionSchedule/ViewSchedule" + url = "https://forms.n-somerset.gov.uk/Waste/CollectionSchedule" + async with httpx.AsyncClient() as client: + r = await client.post( + url, + data={ + "PreviousHouse": "", + "PreviousPostcode": "-", + "Postcode": postcode, + "SelectedUprn": uprn, + }, + ) + form_post_html = r.text + pattern = r'

Object moved to here<\/a>\.<\/h2>' + m = re.search(pattern, form_post_html) + if m: + r = await client.get(forms_base_url + m.group(1)) + html = r.text + open(filename, "w").write(html) + return html + + +def parse_waste_schedule_date(day_and_month: str) -> date: + """Parse waste schedule date.""" + today = date.today() + this_year = today.year + date_format = "%A %d %B %Y" + d = datetime.strptime(f"{day_and_month} {this_year}", date_format).date() + if d < today: + d = datetime.strptime(f"{day_and_month} {this_year + 1}", date_format).date() + return d + + +def parse(root: lxml.html.HtmlElement) -> list[Event]: + """Parse waste schedule.""" + tbody = root.find(".//table/tbody") + assert tbody is not None + by_date = defaultdict(list) + for e_service, e_next_date, e_following in tbody: + assert e_service.text and e_next_date.text and e_following.text + service = e_service.text + next_date = parse_waste_schedule_date(e_next_date.text) + following_date = parse_waste_schedule_date(e_following.text) + + by_date[next_date].append(service) + by_date[following_date].append(service) + + return [ + Event( + name="waste_schedule", + date=uk_time(d, time(6, 30)), + title="🗑️ Backwell: " + ", ".join(services), + ) + for d, services in by_date.items() + ] + + +BristolSchedule = list[dict[str, typing.Any]] + + +async def get_bristol_data(data_dir: str, uprn: str) -> BristolSchedule: + """Get Bristol Waste schedule, with cache.""" + now = datetime.now() + waste_dir = os.path.join(data_dir, "waste") + + make_waste_dir(data_dir) + + existing_data = os.listdir(waste_dir) + existing = [f for f in existing_data if f.endswith(f"_{uprn}.json")] + if existing: + recent_filename = max(existing) + recent = datetime.strptime(recent_filename, f"%Y-%m-%d_%H:%M_{uprn}.json") + delta = now - recent + + def get_from_recent() -> BristolSchedule: + json_data = json.load(open(os.path.join(waste_dir, recent_filename))) + return typing.cast(BristolSchedule, json_data["data"]) + + if existing and delta < timedelta(hours=ttl_hours): + return get_from_recent() + + try: + r = await get_bristol_gov_uk_data(uprn) + except httpx.ReadTimeout: + return get_from_recent() + + with open(f'{waste_dir}/{now.strftime("%Y-%m-%d_%H:%M")}_{uprn}.json', "wb") as out: + out.write(r.content) + + return typing.cast(BristolSchedule, r.json()["data"]) + + +async def get_bristol_gov_uk_data(uprn: str) -> httpx.Response: + """Get JSON from Bristol City Council.""" + UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" + HEADERS = { + "Accept": "*/*", + "Accept-Language": "en-GB,en;q=0.9", + "Connection": "keep-alive", + "Ocp-Apim-Subscription-Key": "47ffd667d69c4a858f92fc38dc24b150", + "Ocp-Apim-Trace": "true", + "Origin": "https://bristolcouncil.powerappsportals.com", + "Referer": "https://bristolcouncil.powerappsportals.com/", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "cross-site", + "Sec-GPC": "1", + "User-Agent": UA, + } + + _uprn = str(uprn).zfill(12) + + async with httpx.AsyncClient(timeout=20) as client: + # Initialise form + payload = {"servicetypeid": "7dce896c-b3ba-ea11-a812-000d3a7f1cdc"} + response = await client.get( + "https://bristolcouncil.powerappsportals.com/completedynamicformunauth/", + headers=HEADERS, + params=payload, + ) + + host = "bcprdapidyna002.azure-api.net" + + # Set the search criteria + payload = {"Uprn": "UPRN" + _uprn} + response = await client.post( + f"https://{host}/bcprdfundyna001-llpg/DetailedLLPG", + headers=HEADERS, + json=payload, + ) + + # Retrieve the schedule + payload = {"uprn": _uprn} + response = await client.post( + f"https://{host}/bcprdfundyna001-alloy/NextCollectionDates", + headers=HEADERS, + json=payload, + ) + + return response + + +async def get_bristol_gov_uk(start_date: date, data_dir: str, uprn: str) -> list[Event]: + """Get waste collection schedule from Bristol City Council.""" + data = await get_bristol_data(data_dir, uprn) + + by_date: defaultdict[date, list[str]] = defaultdict(list) + + for item in data: + service = item["containerName"] + service = "Recycling" if "Recycling" in service else service.partition(" ")[2] + for collection in item["collection"]: + for collection_date_key in ["nextCollectionDate", "lastCollectionDate"]: + d = date.fromisoformat(collection[collection_date_key][:10]) + if d < start_date: + continue + if service not in by_date[d]: + by_date[d].append(service) + + return [ + Event(name="waste_schedule", date=d, title="🗑️ Bristol: " + ", ".join(services)) + for d, services in by_date.items() + ] diff --git a/agenda/weather.py b/agenda/weather.py deleted file mode 100644 index ad0fc0c..0000000 --- a/agenda/weather.py +++ /dev/null @@ -1,105 +0,0 @@ -"""Weather forecast using OpenWeatherMap One Call API.""" - -import json -import os -from datetime import datetime - -import pyowm - - -def _cache_path(data_dir: str, lat: float, lon: float) -> str: - """Path for weather cache file.""" - weather_dir = os.path.join(data_dir, "weather") - os.makedirs(weather_dir, exist_ok=True) - return os.path.join(weather_dir, f"{lat:.2f}_{lon:.2f}.json") - - -def _is_fresh(path: str, max_age_hours: int = 24) -> bool: - """Return True if the cache file exists and is recent enough.""" - if not os.path.exists(path): - return False - age = datetime.now().timestamp() - os.path.getmtime(path) - return age < max_age_hours * 3600 - - -def get_forecast( - data_dir: str, - api_key: str, - lat: float, - lon: float, - cache_only: bool = False, -) -> list[dict]: - """Return 8-day daily forecast for lat/lon, caching results for 24 hours. - - If cache_only=True, return cached data if available (even if stale) and - never call the API. Returns [] if no cache exists. - """ - cache_file = _cache_path(data_dir, lat, lon) - - if _is_fresh(cache_file): - with open(cache_file) as f: - return json.load(f) # type: ignore[no-any-return] - - if cache_only: - if os.path.exists(cache_file): - with open(cache_file) as f: - return json.load(f) # type: ignore[no-any-return] - return [] - - owm = pyowm.OWM(api_key) - mgr = owm.weather_manager() - result = mgr.one_call(lat=lat, lon=lon) - - forecasts = [] - for day in result.forecast_daily: - dt = datetime.fromtimestamp(day.ref_time) - temp = day.temperature("celsius") - forecasts.append( - { - "date": dt.date().isoformat(), - "status": day.status, - "detailed_status": day.detailed_status, - "temp_min": round(temp["min"]), - "temp_max": round(temp["max"]), - "precipitation_probability": day.precipitation_probability, - "icon": day.weather_icon_name, - } - ) - - with open(cache_file, "w") as f: - json.dump(forecasts, f) - - return forecasts - - -def trip_latlon(trip: object) -> tuple[float, float] | None: - """Return (lat, lon) for the primary destination of a trip, or None.""" - from agenda.types import Trip - - assert isinstance(trip, Trip) - for item in list(trip.accommodation) + list(trip.conferences): - if "latitude" in item and "longitude" in item: - return (float(item["latitude"]), float(item["longitude"])) - return None - - -def get_trip_weather( - data_dir: str, - api_key: str, - trip: object, - cache_only: bool = False, -) -> dict[str, dict]: - """Return forecast for a trip keyed by date ISO string. - - Returns an empty dict if no location is known or the API call fails. - If cache_only=True, never call the API (returns stale or empty data). - """ - latlon = trip_latlon(trip) - if not latlon: - return {} - lat, lon = latlon - try: - forecasts = get_forecast(data_dir, api_key, lat, lon, cache_only=cache_only) - except Exception: - return {} - return {f["date"]: f for f in forecasts} diff --git a/docs/personal-data-yaml.md b/docs/personal-data-yaml.md deleted file mode 100644 index 471f632..0000000 --- a/docs/personal-data-yaml.md +++ /dev/null @@ -1,863 +0,0 @@ -# Personal Data YAML Formats - -This document describes the YAML files read from `../personal-data/`. It is intended for humans and LLMs generating new entries. - -## General Rules - -- Use YAML lists for most files. `airports.yaml` is a mapping keyed by IATA code. -- Use ISO-like YAML dates and datetimes: - - Date: `2026-03-14` - - Datetime with timezone: `2026-03-14 09:30:00+01:00` -- Use lowercase ISO 3166-1 alpha-2 country codes, for example `gb`, `be`, `us`. -- Use quoted strings for prices and identifiers that might otherwise be parsed as numbers: `'154.34'`, `'06525269'`, `'0042'`. -- Currencies must be in `config.CURRENCIES` or `GBP`. -- Travel and trip-related entries are grouped by the `trip` date. That date should match an entry in `trips.yaml` when a named trip is needed, but trip groups can also be created from travel/accommodation/conference entries. -- Keep chronological files sorted by their natural start field. `validate_yaml.py` checks ordering for trips, flights, trains, ferries, conferences, and accommodation. -- Preserve the existing whitespace style. Long top-level list files such as `accommodation.yaml`, `buses.yaml`, `car_journeys.yaml`, `coaches.yaml`, `conferences.yaml`, `ferries.yaml`, `flights.yaml`, `stations.yaml`, `trains.yaml`, and `trips.yaml` use one blank line between top-level items. Mapping files such as `airports.yaml` do not use this list-item spacing. -- Coordinates are `latitude` then `longitude`, both numeric. - -## Cross-File References - -- `flights.yaml` flight `airline` values reference `airlines.yaml` `iata`. -- `flights.yaml` flight `from` and `to` values reference `airports.yaml` keys. -- `trains.yaml` journey and leg `from` and `to` values reference `stations.yaml` `name`. -- `ferries.yaml` `from` and `to` values reference `ferry_terminals.yaml` `name`. -- `buses.yaml` `from` and `to` values reference `bus_stops.yaml` `name`. -- `coaches.yaml` `from` and `to` values reference `coach_stations.yaml` `name`. -- `car_journeys.yaml` `route` values name files in `car_routes/`. The `.geojson` extension is optional. -- Station, stop, and terminal `routes` values name GeoJSON files without the `.geojson` extension. - -## `accommodation.yaml` - -Top-level shape: list of accommodation stays. - -Used by: agenda events, trip pages, trip maps, busy/location logic. - -Required fields: - -- `type`: accommodation category such as `hotel`, `apartment`, `airbnb`. -- `name`: property name. -- `country`: lowercase country code. -- `location`: city or place name. -- `trip`: trip start date. -- `from`: check-in datetime. -- `to`: check-out datetime. - -Common optional fields: - -- Booking: `operator`, `booking_reference`, `confirmation_code`, `booking_url`, `url`, `email`, `phone`. -- Money: `price`, `currency`, `room_rate`, `estimated_taxes`, `estimated_additional_fees`. -- Room/stay: `address`, `room_type`, `room_name`, `room_number`, `number_of_adults`, `breakfast_included`, `breakfast`, `cancellation_policy`, `free_cancellation`, `refundable`. -- Coordinates/IDs: `latitude`, `longitude`, `timezone`, `osm_node`, `wikidata`. -- Loyalty: `rewards`, `radisson_rewards_number`. - -Example: - -```yaml -- type: hotel - operator: Example Hotels - name: Example Central Hotel - location: Brussels - country: be - trip: 2026-02-06 - from: 2026-02-06 15:00:00+01:00 - to: 2026-02-09 11:00:00+01:00 - address: 1 Example Street, Brussels - confirmation_code: ABC123 - price: '312.50' - currency: EUR - number_of_adults: 1 - room_type: Standard double - breakfast_included: true - latitude: 50.8466 - longitude: 4.3528 -``` - -## `airlines.yaml` - -Top-level shape: list of airlines. - -Used by: flight loading and display. - -Required fields: - -- `iata`: two-character IATA airline code. -- `icao`: three-character ICAO airline code. -- `name`: airline name. - -Optional fields: - -- `flight_number_prefer_icao`: boolean. When true, display flight numbers with the ICAO code instead of the IATA code. - -Example: - -```yaml -- iata: BA - icao: BAW - name: British Airways -- iata: U2 - icao: EZY - name: easyJet - flight_number_prefer_icao: true -``` - -## `airports.yaml` - -Top-level shape: mapping keyed by IATA airport code. - -Used by: flight loading, distance calculation, maps, unbooked route hints. - -Required fields for each airport: - -- `iata`: IATA code. Should match the mapping key. -- `name`: airport name. -- `city`: city or main served place. -- `country`: lowercase country code. -- `latitude`, `longitude`: numeric coordinates. -- `qid`: Wikidata QID. - -Optional fields: - -- `alt_name`: display name override used in labels. -- `elevation`: metres. -- `website`, `url`. - -Example: - -```yaml -BRU: - iata: BRU - name: Brussels Airport - city: Brussels - country: be - qid: Q220613 - latitude: 50.9014 - longitude: 4.4844 - elevation: 56 - website: https://www.brusselsairport.be/ -``` - -## `bus_stops.yaml` - -Top-level shape: list of bus stops. - -Used by: bus trip loading, maps, route rendering. - -Required fields: - -- `name`: stop name referenced by `buses.yaml`. -- `city`: city or place. -- `country`: lowercase country code. -- `latitude`, `longitude`: numeric coordinates. -- `routes`: mapping from destination stop name to GeoJSON filename without `.geojson`. - -Optional fields: - -- `Atco`: UK ATCO stop code. -- `osm_node`. - -Example: - -```yaml -- name: West Street - city: Bristol - country: gb - Atco: '0100BRA10073' - osm_node: 485403178 - latitude: 51.4393854 - longitude: -2.6017977 - routes: - Bristol Airport: West_Street_to_Bristol_Airport -``` - -## `buses.yaml` - -Top-level shape: list of bus journeys. - -Used by: trip loading, maps, trip timeline. Bus journeys are not counted for Schengen tracking. - -Required fields: - -- `trip`: trip start date. -- `depart`: departure datetime. -- `arrive`: arrival datetime. `validate_yaml.py` requires arrival after departure and duration no more than 12 hours. -- `from`, `to`: names from `bus_stops.yaml`. - -Optional fields: - -- `operator`, `price`, `currency`. - -Example: - -```yaml -- trip: 2026-03-14 - depart: 2026-03-14 08:20:00+00:00 - arrive: 2026-03-14 08:55:00+00:00 - from: West Street - to: Bristol Airport - operator: First Bus - price: '2.00' - currency: GBP -``` - -## `coach_stations.yaml` - -Top-level shape: list of coach stations. - -Used by: coach trip loading, maps, route rendering. - -Fields are the same pattern as `bus_stops.yaml`, except entries describe coach stations. - -Example: - -```yaml -- name: Example Coach Station - city: Example City - country: gb - latitude: 51.4500 - longitude: -2.5800 - routes: - Other Coach Station: example_city_to_other_city -``` - -## `coaches.yaml` - -Top-level shape: list of coach journeys. - -Used by: trip loading, maps, trip timeline. Coach journeys are not counted for Schengen tracking. - -Required fields: - -- `trip`, `depart`, `arrive`, `from`, `to`. -- `from` and `to` must be names from `coach_stations.yaml`. - -Optional fields: - -- `operator`, `class`, `booking_reference`, `price`, `currency`, `price_details`. - -Example: - -```yaml -- booking_reference: ABC123 - trip: 2026-05-25 - price: '55.00' - currency: GBP - depart: 2026-05-26 14:45:00+01:00 - arrive: 2026-05-26 18:30:00+01:00 - from: Example Coach Station - to: Other Coach Station - operator: Example Coaches - class: Standard - price_details: - base_fare: '55.00' -``` - -## `car_journeys.yaml` - -Top-level shape: list of car journeys. - -Used by: trip loading, maps, trip timeline. Car journeys are for driving your own car or a rental car. - -Required fields: - -- `trip`: trip start date. -- `depart`: departure date or datetime. -- `arrive`: arrival date or datetime. -- `route`: GeoJSON filename in `car_routes/`, with or without the `.geojson` extension. - -Optional fields: - -- `from`, `to`: endpoint labels. If omitted and the route filename uses `A_to_B`, labels are inferred from the filename. -- `show_markers`: boolean. When true, render both car endpoint markers on maps. Defaults to false because car endpoints often duplicate airport, accommodation, ferry terminal, or conference pins. Home endpoints still render by default. -- `from_show_marker`, `to_show_marker`: booleans. Per-endpoint marker overrides. -- `from_type`, `to_type`: marker type override used when that endpoint marker is shown. Use `home` to render a house icon. Otherwise car endpoints render as car markers. -- `operator`, `vehicle`, `price`, `currency`, `distance`. - -The route distance is calculated from the GeoJSON when `distance` is not present. Car routes render on maps by default. Non-home endpoint pins are opt-in. If an endpoint label is `home`, `PCH`, or `Picture House Court`, the marker is rendered as a house by default. - -Example: - -```yaml -- trip: 2026-07-16 - depart: 2026-07-16 - arrive: 2026-07-16 - route: PCH_to_EMF.geojson - -- trip: 2026-07-16 - depart: 2026-07-19 - arrive: 2026-07-19 - route: EMF_to_PCH.geojson -``` - -## `conferences.yaml` - -Top-level shape: list of conferences and conference-like events. - -Used by: agenda events, trip pages, trip maps, conference list, CFP reminders. - -Required fields: - -- `name`: event name. -- `topic`: topic/category. -- `location`: city or location label. -- Date information, either as legacy top-level `start` and `end`, or preferred nested `dates`. - -Preferred `dates` fields: - -- `status`: one of `exact`, `tentative`, or `approximate`. -- For `exact` and `tentative`: `start` and `end` dates/datetimes. `end` must be no earlier than `start`, and duration must be under 20 days. -- For `approximate`: `earliest` and `latest` dates for sorting/past-future filtering. -- `label`: optional human-readable date text. Recommended for `tentative` and `approximate`, for example `likely first weekend of February 2027` or `March 2027`. -- `basis`: optional explanation of why a tentative date is expected. - -Date status behavior: - -- `exact`: confirmed dates. These create agenda events, iCalendar entries, and timeline bars. -- `tentative`: guessed or unconfirmed exact dates. These appear on the conference list with a status badge, but do not create agenda/iCalendar events or timeline bars. -- `approximate`: only a broad date range is known. These appear on the conference list with a status badge, but do not create agenda/iCalendar events or timeline bars. - -Legacy fields: - -- Existing top-level `start` and `end` are still supported and are treated as `exact` unless `date_status` says otherwise. - -Common optional fields: - -- Series: `series`, a key from `conference_series.yaml`. -- Trip/location: `trip`, `country`, `venue`, `address`, `latitude`, `longitude`. -- Attendance: `going`, `registered`, `speaking`, `online`, `accommodation_booked`, `transport_booked`. -- Partial attendance: `attend_start`, `attend_end`. These may be dates or timezone-aware datetimes and are used on trip pages instead of official dates. -- Web/CFP: `url`, `cfp_end`, `cfp_url`, `hashtag`, `description`. -- Money/tickets: `free`, `price`, `currency`, `ticket_type`. -- Other flags: `hackathon`, `attendees`. - -Exact example: - -```yaml -- name: FOSDEM - series: fosdem - topic: FOSDEM - location: Brussels - country: be - trip: 2026-02-06 - dates: - status: exact - start: 2026-02-07 - end: 2026-02-08 - attend_start: 2026-02-07 14:00:00+01:00 - attend_end: 2026-02-08 - going: true - registered: true - accommodation_booked: true - transport_booked: true - url: https://fosdem.org/2026/ - venue: Universite Libre de Bruxelles - address: Av. Franklin Roosevelt 50, 1050 Bruxelles, Belgium - latitude: 50.8132 - longitude: 4.3822 -``` - -Tentative example: - -```yaml -- name: FOSDEM - series: fosdem - topic: FOSDEM - location: Brussels - country: be - dates: - status: tentative - start: 2027-01-30 - end: 2027-01-31 - label: likely first weekend of February 2027 - basis: FOSDEM is usually on the weekend where Sunday is the first Sunday in February - url: https://fosdem.org/2027/ -``` - -Approximate examples: - -```yaml -- name: Wikimedia Hackathon 2027 - series: wikimedia-hackathon - topic: Wikimedia - location: Albania - country: al - dates: - status: approximate - label: mid-April 2027 - earliest: 2027-04-11 - latest: 2027-04-20 - hackathon: true - -- name: PyCascades 2027 - series: pycascades - topic: Python - location: TBC - dates: - status: approximate - label: March 2027 - earliest: 2027-03-01 - latest: 2027-03-31 -``` - -## `conference_series.yaml` - -Top-level shape: mapping from stable series ID to series metadata. - -Used by: conference list pages, conference series index/detail pages, and validation of `conferences.yaml` `series` references. - -Required fields for each series: - -- `name`: display name for the series. - -Common optional fields: - -- `topic`: default topic/category. -- `cadence`: for example `annual` or `recurring`. -- `usual_location`: common city/place when the event usually stays in one place. -- `country`: common lowercase country code when stable. -- `url`: series homepage. -- `notes`: free-text generation or scheduling notes. - -Example: - -```yaml -fosdem: - name: FOSDEM - topic: FOSDEM - cadence: annual - usual_location: Brussels - country: be - url: https://fosdem.org/ - notes: Usually the weekend where Sunday is the first Sunday in February. - -geomob-london: - name: Geomob London - topic: Maps - cadence: recurring - usual_location: London - country: gb - url: https://thegeomob.com/ -``` - -## `entities.yaml` - -Top-level shape: list of people/entities. - -Used by: birthday events. - -Required fields for birthday support: - -- `name`: full name. -- `label`: display name. -- `type`: for example `human`. -- `birthday`: mapping with `day`, `month`, and optionally `year`. - -Optional fields: - -- `relation`, `email`. - -If `birthday.year` is omitted, age is shown as unknown. - -Example: - -```yaml -- name: Ada Example - label: Ada - type: human - relation: friend - birthday: - day: 10 - month: 12 - year: 1990 -``` - -## `events.yaml` - -Top-level shape: list of general events. - -Used by: agenda events and trip pages. - -Required fields: - -- `name`: event type. -- One date source: - - `date`: single event date/datetime, or - - `start_date`: used for events with a separate `end_date`, or - - `rrule`: recurrence rule string. - -Optional fields: - -- `title`: display title. -- `end_date`: explicit end date/datetime. -- `duration`: ISO 8601 duration such as `PT2H`, `P1D`. -- `url`. -- Trip/map fields: `trip`, `location`, `country`, `venue`, `address`, `latitude`, `longitude`. - -Special cases: - -- For `name: travel_insurance`, the event date field is `end_date`; no `end_date` is attached to the generated event. -- For recurring events, if the `rrule` has no `BYHOUR`, `BYMINUTE`, or `BYSECOND`, generated events are all-day dates. Otherwise generated datetimes are localized to UK time. -- `skip_trips=True` consumers ignore entries with `trip`. - -Examples: - -```yaml -- name: travel_insurance - start_date: 2026-05-04 - end_date: 2027-05-03 - -- name: meetup - title: Example Geo Meetup - date: 2026-06-18 18:30:00+01:00 - duration: PT2H - url: https://example.org/meetup - location: Bristol - country: gb - latitude: 51.4545 - longitude: -2.5879 - -- name: market - title: Monthly Example Market - rrule: FREQ=MONTHLY;BYDAY=1SA -``` - -## `ferries.yaml` - -Top-level shape: list of ferry journeys. - -Used by: trip loading, maps, trip timeline, Schengen tracking. - -Required fields: - -- `trip`: trip start date. -- `depart`, `arrive`: datetimes. Ferry `arrive` is required. -- `from`, `to`: names from `ferry_terminals.yaml`. - -Common optional fields: - -- `operator`, `ferry`, `direction`, `class`, `booking_reference`, `price`, `currency`. -- `price_details`: free-form mapping of fare components. -- `vehicle`: mapping with fields such as `type`, `registration`, `height`, `length`, `extras`. - -Example: - -```yaml -- booking_reference: ABC123 - trip: 2026-05-04 - price: '302.00' - currency: GBP - depart: 2026-05-04 23:00:00+01:00 - arrive: 2026-05-05 08:00:00+02:00 - from: Portsmouth - to: Cherbourg - operator: Brittany Ferries - class: Commodore cabin - price_details: - base_fare: '153.00' - cabin: '149.00' - vehicle: - type: Example car - registration: AB12CDE - height: 1.63m - length: 4.15m -``` - -## `ferry_terminals.yaml` - -Top-level shape: list of ferry terminals. - -Used by: ferry loading and route rendering. - -Required fields: - -- `name`: terminal name referenced by `ferries.yaml`. -- `city`, `country`. -- `latitude`, `longitude`. -- `routes`: mapping from destination terminal name to GeoJSON filename without `.geojson`. Ferry route rendering expects a GeoJSON route. - -Optional fields: - -- `osm_node`, `osm_way`. - -Example: - -```yaml -- name: Portsmouth - city: Portsmouth - country: gb - osm_way: 123456 - latitude: 50.8120 - longitude: -1.0880 - routes: - Cherbourg: portsmouth_cherbourg -``` - -## `flight_destinations.yaml` - -Top-level shape: list of origin rules for unbooked conference flight route hints. - -Used by: trip maps when a trip has conferences but no booked travel. - -Required fields: - -- `origin`: origin airport IATA code. -- `airline`: airline IATA code. Currently loaded for validation/description but not used in origin selection. -- `destinations`: list of destination airport IATA codes. - -Example: - -```yaml -- origin: BRS - airline: U2 - destinations: - - AMS - - BCN - - CDG -``` - -## `flights.yaml` - -Top-level shape: list of flight bookings. Each booking contains one or more flight legs. - -Used by: agenda transport events, trip loading, maps, distance calculation. - -Required booking fields: - -- `trip`: trip start date. -- `flights`: list of flight leg mappings. - -Common optional booking fields: - -- `booking_reference`, `price`, `currency`. - -Required flight leg fields: - -- `depart`: departure datetime. -- `from`, `to`: airport IATA codes from `airports.yaml`. -- `flight_number`: numeric/string flight number without airline prefix. -- `airline`: airline IATA code from `airlines.yaml`. - -Common optional flight leg fields: - -- Time/location: `arrive`, `from_terminal`, `to_terminal`, `duration`. -- Seat/cabin: `seat`, `seat_type`, `class`, `cabin`. -- Aircraft: `plane`, `registration`. -- Tracking: `distance`, `co2_kg`, `openflights_trip`, `reason`. -- Ticket/passenger: `e_ticket_number`, `ticket_number`, `frequent_flyer_number`, `passenger_name`, `passengers`, `baggage`, `payment_details`. - -`validate_yaml.py` checks that every booking has `trip`, all flight airlines exist in `airlines.yaml`, bookings are sorted by first departure, and currencies are configured. It reports flights missing `co2_kg`. - -Example: - -```yaml -- booking_reference: ABC123 - trip: 2026-04-22 - price: '62.50' - currency: GBP - flights: - - depart: 2026-04-22 17:20:00+01:00 - arrive: 2026-04-22 20:20:00+02:00 - from: LHR - to: BRU - flight_number: '1234' - airline: BA - duration: 01:00 - seat: 5F - seat_type: W - class: C - cabin: business - plane: Airbus A320 - registration: G-ABCD - co2_kg: 154 -``` - -## `follow_launches.yaml` - -Top-level shape: list of SpaceDevs launch slugs. - -Used by: no current in-repo reader was found, but the file appears intended as a watch list for launch update tooling. - -Example: - -```yaml -- starship-integrated-flight-test-5 -- artemis-ii -``` - -## `stations.yaml` - -Top-level shape: list of railway stations. - -Used by: train loading, maps, route rendering. - -Required fields: - -- `name`: station name referenced by `trains.yaml`. -- `country`: lowercase country code. -- `latitude`, `longitude`. -- `routes`: mapping from destination station name to GeoJSON filename without `.geojson`. - -Common optional fields: - -- `uic`, `alpha3`, `wikidata`, `osm_node`. - -Note: the code reads `routes`, not `rotues`; `rotues` appears to be a typo in existing data and should not be used for new entries. - -Example: - -```yaml -- name: London St Pancras - uic: 7015400 - alpha3: STP - wikidata: Q720102 - latitude: 51.531921 - longitude: -0.126361 - country: gb - routes: - Brussels Midi: london_brussels_eurostar -``` - -## `subscriptions.yaml` - -Top-level shape: list of subscriptions. - -Used by: subscription renewal agenda events when `renewal_date` is present. - -Required fields: - -- `name`: subscription name. - -Common optional fields: - -- Dates: `start`, `start_date`, `renewal_date`. -- `price`: mapping with `amount` and `currency`. -- `term`: mapping with `duration` and `unit` or `term_unit`. -- Account: `email`, `account_url`, `account_number`. - -Only items with `renewal_date` create agenda events. - -Example: - -```yaml -- name: Example Magazine - start_date: 2026-01-01 - renewal_date: 2027-01-01 - price: - amount: 99 - currency: GBP - term: - duration: 1 - unit: year - email: me@example.com - account_url: https://example.com/account - account_number: '001234' -``` - -## `trains.yaml` - -Top-level shape: list of train journeys. Each journey contains one or more legs. - -Used by: agenda transport events, trip loading, trip timeline, maps, stats. - -Required journey fields: - -- `operator`: booking/operator label. -- `from`, `to`: station names from `stations.yaml`. -- `trip`: trip start date. -- `depart`, `arrive`: journey datetimes or dates. -- `legs`: list of leg mappings. - -Common optional journey fields: - -- `class`, `number`, `tickets`, `ticket_code`, `total_price`, `co2_kg`. - -Required leg fields: - -- `from`, `to`: station names from `stations.yaml`. -- `depart`, `arrive`. -- `operator`. - -Common optional leg fields: - -- `train`, `number`, `service`, `service_number`, `service_numbers`, `reporting_number`, `mode`. -- Seat/reservation: `coach`, `seat`, `seat_type`, `seat_features`, `reservation_number`, `platform`. -- `class`, `trip`, `url`. - -Ticket fields are free-form but commonly include `booking_reference`, `url`, `price`, `currency`, `booking_date`, `ticket`, `ticket_code`, `ticket_type`, `from`, `to`, `class`, `validity`, `route`, `fare`, `quantity`, `seat_reservation`. - -Example: - -```yaml -- operator: eurostar - from: London St Pancras - to: Brussels Midi - trip: 2026-02-06 - depart: 2026-02-06 15:04:00+00:00 - arrive: 2026-02-06 18:12:00+01:00 - class: Standard Premier - tickets: - - booking_reference: ABCDEF - url: https://example.com/booking/ABCDEF - price: '89.00' - currency: GBP - legs: - - from: London St Pancras - to: Brussels Midi - depart: 2026-02-06 15:04:00+00:00 - arrive: 2026-02-06 18:12:00+01:00 - coach: 1 - seat: 41 - operator: Eurostar -``` - -## `travel_rewards.yaml` - -Top-level shape: list of travel loyalty accounts. - -Used by: no current in-repo reader was found. The file is structured as account metadata. - -Common fields: - -- `name`: programme name. -- `type`: category such as `hotel`, `airline`, `rail`. -- `member_number`: membership identifier. -- `balance`: current points/miles balance. -- `expiry`: expiry date or null. -- `url`: account URL. -- `person`: account holder key/name. -- `email`, `note`. - -Example: - -```yaml -- name: Example Rewards - type: hotel - member_number: '123456789' - balance: 3665 - expiry: 2027-08-15 - url: https://example.com/rewards - person: edward -``` - -## `trips.yaml` - -Top-level shape: list of trip metadata. - -Used by: trip grouping and trip titles. - -Required fields: - -- `trip`: trip start date. This is the grouping key used by travel, accommodation, conferences, and trip events. - -Optional fields: - -- `name`: explicit trip title. -- `private`: boolean. Private trips are hidden from unauthenticated users. - -Example: - -```yaml -- trip: 2026-02-06 - name: Brussels for FOSDEM - private: false -``` diff --git a/frontend/index.js b/frontend/index.js deleted file mode 100644 index e69de29..0000000 diff --git a/get_airport.py b/get_airport.py deleted file mode 100755 index 3e6e29a..0000000 --- a/get_airport.py +++ /dev/null @@ -1,224 +0,0 @@ -#!/usr/bin/python3 - -import sys -from typing import Any, Dict, List - -import requests -import yaml - -# Define the base URL for the Wikidata API -WIKIDATA_API_URL = "https://www.wikidata.org/w/api.php" - - -def get_entity_label(qid: str) -> str | None: - """ - Fetches the English label for a given Wikidata entity QID. - - Args: - qid (str): The Wikidata entity ID (e.g., "Q6106"). - - Returns: - Optional[str]: The English label of the entity, or None if not found. - """ - params: Dict[str, str] = { - "action": "wbgetentities", - "ids": qid, - "format": "json", - "props": "labels", - "languages": "en", - } - try: - response = requests.get(WIKIDATA_API_URL, params=params) - response.raise_for_status() - entity = response.json().get("entities", {}).get(qid, {}) - return entity.get("labels", {}).get("en", {}).get("value") - except requests.exceptions.RequestException as e: - print(f"Error fetching label for QID {qid}: {e}", file=sys.stderr) - return None - - -def get_entity_details(qid: str) -> dict[str, Any] | None: - """ - Fetches and processes detailed information for a given airport QID. - - Args: - qid (str): The QID of the airport Wikidata entity. - - Returns: - Optional[Dict[str, Any]]: A dictionary containing the detailed airport data. - """ - params: Dict[str, str] = { - "action": "wbgetentities", - "ids": qid, - "format": "json", - "props": "claims|labels", - } - try: - response = requests.get(WIKIDATA_API_URL, params=params) - response.raise_for_status() - entity = response.json().get("entities", {}).get(qid, {}) - if not entity: - return None - - claims = entity.get("claims", {}) - - # Helper to safely extract claim values - def get_simple_claim_value(prop_id: str) -> str | None: - claim = claims.get(prop_id) - if not claim: - return None - v = claim[0].get("mainsnak", {}).get("datavalue", {}).get("value") - assert isinstance(v, str) or v is None - return v - - # Get IATA code, name, and website - iata = get_simple_claim_value("P238") - name = entity.get("labels", {}).get("en", {}).get("value") - website = get_simple_claim_value("P856") - - # Get City Name by resolving its QID - city_qid_claim = claims.get("P131") - city_name = None - if city_qid_claim: - city_qid = ( - city_qid_claim[0] - .get("mainsnak", {}) - .get("datavalue", {}) - .get("value", {}) - .get("id") - ) - if city_qid: - city_name = get_entity_label(city_qid) - - # Get coordinates - coords_claim = claims.get("P625") - latitude, longitude = None, None - if coords_claim: - coords = ( - coords_claim[0] - .get("mainsnak", {}) - .get("datavalue", {}) - .get("value", {}) - ) - latitude = coords.get("latitude") - longitude = coords.get("longitude") - - # Get elevation - elevation_claim = claims.get("P2044") - elevation = None - if elevation_claim: - amount_str = ( - elevation_claim[0] - .get("mainsnak", {}) - .get("datavalue", {}) - .get("value", {}) - .get("amount") - ) - if amount_str: - elevation = float(amount_str) if "." in amount_str else int(amount_str) - - # Get Country Code - country_claim = claims.get("P17") - country_code = None - if country_claim: - country_qid = ( - country_claim[0] - .get("mainsnak", {}) - .get("datavalue", {}) - .get("value", {}) - .get("id") - ) - if country_qid: - # Fetch the ISO 3166-1 alpha-2 code (P297) for the country entity - country_code_params = { - "action": "wbgetclaims", - "entity": country_qid, - "property": "P297", - "format": "json", - } - country_res = requests.get(WIKIDATA_API_URL, params=country_code_params) - country_res.raise_for_status() - country_claims = country_res.json().get("claims", {}).get("P297") - if country_claims: - code = ( - country_claims[0] - .get("mainsnak", {}) - .get("datavalue", {}) - .get("value") - ) - if code: - country_code = code.lower() - - data = { - "iata": iata, - "name": name, - "city": city_name, - "qid": qid, - "latitude": latitude, - "longitude": longitude, - "elevation": elevation, - "website": website, - "country": country_code, - } - - # Return the final structure, filtering out null values for cleaner output - return {iata: {k: v for k, v in data.items() if v is not None}} - - except requests.exceptions.RequestException as e: - print(f"Error fetching entity details for QID {qid}: {e}", file=sys.stderr) - return None - - -def find_airport_by_iata(iata_code: str) -> dict[str, Any] | None: - """ - Finds an airport by its IATA code using Wikidata's search API. - - Args: - iata_code (str): The IATA code of the airport (e.g., "PDX"). - - Returns: - Optional[Dict[str, Any]]: A dictionary with the airport data or None. - """ - params: Dict[str, str] = { - "action": "query", - "list": "search", - "srsearch": f"haswbstatement:P238={iata_code.upper()}", - "format": "json", - } - try: - response = requests.get(WIKIDATA_API_URL, params=params) - response.raise_for_status() - search_results: List[Dict[str, Any]] = ( - response.json().get("query", {}).get("search", []) - ) - - if not search_results: - print(f"No airport found with IATA code: {iata_code}", file=sys.stderr) - return None - - qid = search_results[0]["title"] - return get_entity_details(qid) - - except requests.exceptions.RequestException as e: - print(f"Error searching on Wikidata API: {e}", file=sys.stderr) - return None - - -if __name__ == "__main__": - if len(sys.argv) != 2: - print("Usage: python airport_lookup.py ", file=sys.stderr) - sys.exit(1) - - iata_code_arg = sys.argv[1] - airport_data = find_airport_by_iata(iata_code_arg) - - if airport_data: - print( - yaml.safe_dump( - airport_data, - default_flow_style=False, - allow_unicode=True, - sort_keys=False, - ), - end="", - ) diff --git a/package.json b/package.json deleted file mode 100644 index 47a8c57..0000000 --- a/package.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "agenda", - "version": "1.0.0", - "directories": { - "test": "tests" - }, - "repository": { - "type": "git", - "url": "https://git.4angle.com/edward/agenda.git" - }, - "license": "ISC", - "devDependencies": { - "copy-webpack-plugin": "^12.0.2", - "eslint": "^9.2.0", - "webpack": "^5.91.0", - "webpack-cli": "^5.1.4" - }, - "dependencies": { - "@fullcalendar/core": "^6.1.11", - "@fullcalendar/daygrid": "^6.1.11", - "@fullcalendar/list": "^6.1.11", - "@fullcalendar/timegrid": "^6.1.11", - "bootstrap": "^5.3.3", - "es-module-shims": "^1.8.3", - "leaflet": "^1.9.4", - "leaflet.geodesic": "^2.7.1" - } -} diff --git a/parse_airbnb.py b/parse_airbnb.py deleted file mode 100755 index 0f9843f..0000000 --- a/parse_airbnb.py +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/python3 - -import sys - -import yaml - -from agenda.airbnb import parse_multiple_files - - -def main() -> None: - """Main function.""" - - filenames = sys.argv[1:] - bookings = parse_multiple_files(filenames) - print(yaml.dump(bookings, sort_keys=False)) - - -if __name__ == "__main__": - main() diff --git a/requirements.txt b/requirements.txt index 016dc03..ba7a169 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,5 +9,3 @@ dateutil ephem flask requests -emoji -timezonefinder diff --git a/run.fcgi b/run.fcgi index 4235124..851231a 100755 --- a/run.fcgi +++ b/run.fcgi @@ -1,8 +1,8 @@ #!/usr/bin/python3 from flipflop import WSGIServer import sys -sys.path.append('/home/edward/src/agenda') # isort:skip -from web_view import app # isort:skip +sys.path.append('/home/edward/src/2021/agenda') +from web_view import app if __name__ == '__main__': WSGIServer(app).run() diff --git a/scripts/add-car-journeys-for-trip b/scripts/add-car-journeys-for-trip deleted file mode 100755 index 9117068..0000000 --- a/scripts/add-car-journeys-for-trip +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 - -import os -import sys - -SCRIPT_PATH = os.path.realpath(__file__) -SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) -REPO_ROOT = os.path.dirname(SCRIPT_DIR) -if REPO_ROOT not in sys.path: - sys.path.insert(0, REPO_ROOT) - -from agenda.car_journey_yaml import main - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/scripts/add-new-conference b/scripts/add-new-conference deleted file mode 100755 index 0190c07..0000000 --- a/scripts/add-new-conference +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 - -import os -import sys - -SCRIPT_PATH = os.path.realpath(__file__) -SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) -REPO_ROOT = os.path.dirname(SCRIPT_DIR) -if REPO_ROOT not in sys.path: - sys.path.insert(0, REPO_ROOT) - -from agenda.add_new_conference import main - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/scripts/build_airport_yaml b/scripts/build_airport_yaml deleted file mode 100755 index cb7c9d6..0000000 --- a/scripts/build_airport_yaml +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 - -import os -import sys - -SCRIPT_PATH = os.path.realpath(__file__) -SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) -REPO_ROOT = os.path.dirname(SCRIPT_DIR) -if REPO_ROOT not in sys.path: - sys.path.insert(0, REPO_ROOT) - -from agenda.build_place_yaml import airport_main - -if __name__ == "__main__": - raise SystemExit(airport_main()) diff --git a/scripts/build_station_yaml b/scripts/build_station_yaml deleted file mode 100755 index 9285a8f..0000000 --- a/scripts/build_station_yaml +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 - -import os -import sys - -SCRIPT_PATH = os.path.realpath(__file__) -SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) -REPO_ROOT = os.path.dirname(SCRIPT_DIR) -if REPO_ROOT not in sys.path: - sys.path.insert(0, REPO_ROOT) - -from agenda.build_place_yaml import station_main - -if __name__ == "__main__": - raise SystemExit(station_main()) diff --git a/scripts/generate-flight-booking-yaml b/scripts/generate-flight-booking-yaml deleted file mode 100755 index ba9c161..0000000 --- a/scripts/generate-flight-booking-yaml +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 - -import os -import sys - -SCRIPT_PATH = os.path.realpath(__file__) -SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) -REPO_ROOT = os.path.dirname(SCRIPT_DIR) -if REPO_ROOT not in sys.path: - sys.path.insert(0, REPO_ROOT) - -from agenda.generate_booking_yaml import flight_main - -if __name__ == "__main__": - raise SystemExit(flight_main()) diff --git a/scripts/generate-train-booking-yaml b/scripts/generate-train-booking-yaml deleted file mode 100755 index 1c0a7ba..0000000 --- a/scripts/generate-train-booking-yaml +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 - -import os -import sys - -SCRIPT_PATH = os.path.realpath(__file__) -SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) -REPO_ROOT = os.path.dirname(SCRIPT_DIR) -if REPO_ROOT not in sys.path: - sys.path.insert(0, REPO_ROOT) - -from agenda.generate_booking_yaml import train_main - -if __name__ == "__main__": - raise SystemExit(train_main()) diff --git a/scripts/import-car-journeys-from-timeline b/scripts/import-car-journeys-from-timeline deleted file mode 100644 index aeb5daa..0000000 --- a/scripts/import-car-journeys-from-timeline +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 - -import os -import sys - -SCRIPT_PATH = os.path.realpath(__file__) -SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) -REPO_ROOT = os.path.dirname(SCRIPT_DIR) -if REPO_ROOT not in sys.path: - sys.path.insert(0, REPO_ROOT) - -from agenda.car_journey_timeline import main - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/static/css/trips.css b/static/css/trips.css deleted file mode 100644 index 7e1cbbc..0000000 --- a/static/css/trips.css +++ /dev/null @@ -1,381 +0,0 @@ -@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600;700&family=Source+Sans+3:ital,wght@0,300;0,400;0,600;1,300&family=JetBrains+Mono:wght@400;500&display=swap'); - -:root { - --t-navy: #1e2d4a; - --t-slate: #374869; - --t-gold: #b8860b; - --t-amber: #e8a820; - --t-cream: #f9f7f3; - --t-white: #ffffff; - --t-muted: #7a8aa8; - --t-border: #dde3ed; - --t-text: #1e2533; - --t-shadow: rgba(30, 45, 74, 0.08); -} - -/* Text pane background on list page */ -.text-content { - background: var(--t-cream) !important; - padding-left: 12px; - padding-top: 12px; -} - -/* =========================== - TRIP CARDS (list view) - =========================== */ - -.trip-card { - background: var(--t-white); - border-radius: 8px; - border: 1px solid var(--t-border); - border-left: 3px solid #8098c0; - box-shadow: 0 1px 4px var(--t-shadow); - padding: 14px 18px; - margin-bottom: 14px; - transition: box-shadow 0.15s ease, transform 0.15s ease; -} - -.trip-card:hover { - box-shadow: 0 4px 16px rgba(30, 45, 74, 0.14); - transform: translateY(-1px); -} - -.trip-card.trip-current { - border-left-color: var(--t-gold); -} - -/* Trip name heading */ -.trip-name { - margin-bottom: 2px; - font-size: 1.1rem; - line-height: 1.3; -} - -.trip-name a { - font-family: 'Playfair Display', Georgia, 'Times New Roman', serif; - font-weight: 700; - color: var(--t-navy); - text-decoration: none; -} - -.trip-name a:hover { - color: var(--t-gold); -} - -.trip-name small { - font-family: 'JetBrains Mono', 'Courier New', monospace; - font-size: 0.7rem; - color: var(--t-muted); - font-weight: 400; -} - -/* Countries as inline chips */ -.trip-countries { - display: flex !important; - flex-wrap: wrap; - gap: 5px; - margin: 6px 0; - padding: 0; -} - -.trip-countries li { - display: inline-flex; - align-items: center; - gap: 4px; - background: #eef2f8; - border: 1px solid #d5dce8; - border-radius: 20px; - padding: 1px 10px; - font-size: 0.8rem; - color: var(--t-text); -} - -/* Dates */ -.trip-dates { - font-size: 0.83rem; - color: var(--t-muted); - margin: 2px 0; -} - -/* Stats row — pill chips */ -.trip-stats { - display: flex; - flex-wrap: wrap; - gap: 4px; - margin: 7px 0; -} - -.trip-stat { - display: inline-block; - background: #f3f6fa; - border: 1px solid #dde3ed; - border-radius: 20px; - padding: 2px 10px; - font-size: 0.74rem; - color: var(--t-slate); - white-space: nowrap; - font-family: 'JetBrains Mono', monospace; -} - -/* School holiday info */ -.school-holiday-info { - display: flex; - flex-wrap: wrap; - align-items: center; - gap: 6px; - margin: 4px 0 8px; - font-size: 0.82rem; -} - -/* Inline weather chip in day headers */ -.trip-weather-inline { - font-size: 0.78rem; - color: var(--t-muted); - font-weight: 400; - letter-spacing: 0; - text-transform: none; - display: inline-flex; - align-items: center; - gap: 3px; - vertical-align: middle; -} - -/* Day sub-headers within a trip card */ -.trip-day-header { - font-size: 0.75rem; - font-weight: 700; - text-transform: uppercase; - letter-spacing: 0.1em; - color: var(--t-navy); - margin: 18px 0 6px; - padding: 4px 0 4px 10px; - border-left: 3px solid var(--t-amber); - background: linear-gradient(to right, rgba(232, 168, 32, 0.07), transparent); -} - -/* Condensed check-out line */ -.trip-checkout { - font-size: 0.84rem; - padding: 4px 8px; - color: var(--t-muted); - border-left: 2px solid #aacde8; - margin: 3px 0 3px 1px; -} - -/* Transport/accommodation element rows */ -.trip-element { - font-size: 0.84rem; - padding: 2px 0; - color: var(--t-text); - line-height: 1.5; -} - -/* =========================== - INLINE CONFERENCE CARDS - (within trip_item macro) - =========================== */ - -.trip-conference-card { - background: #fffef4; - border: 1px solid #e4d46c; - border-radius: 6px; - padding: 9px 13px; - margin: 5px 0; -} - -.trip-conference-card .card-body { - padding: 0; -} - -.trip-conference-card .card-title { - font-size: 0.9rem; - font-weight: 600; - margin-bottom: 3px; - color: var(--t-navy); -} - -.trip-conference-card .card-text { - font-size: 0.82rem; - margin-bottom: 0; - color: var(--t-text); -} - -/* =========================== - TRIP PAGE ACCOMMODATION - =========================== */ - -.trip-accommodation-card { - background: #f3faff; - border: 1px solid #aacde8; - border-radius: 6px; - padding: 9px 13px; - margin: 5px 0; -} - -.trip-accommodation-card .card-body { padding: 0; } - -.trip-accommodation-card .card-title { - font-size: 0.9rem; - font-weight: 600; - margin-bottom: 3px; - color: var(--t-navy); -} - -.trip-accommodation-card .card-text { - font-size: 0.82rem; - margin-bottom: 0; -} - -/* =========================== - TRIP PAGE TRANSPORT - =========================== */ - -.trip-transport-card { - background: #f7f9fc; - border: 1px solid #d0dbe8; - border-radius: 6px; - padding: 9px 13px; - margin: 5px 0; -} - -.trip-transport-card .card-body { padding: 0; } - -.trip-transport-card .card-title { - font-size: 0.9rem; - font-weight: 600; - margin-bottom: 3px; - color: var(--t-navy); -} - -.trip-transport-card .card-text { - font-size: 0.82rem; - margin-bottom: 0; -} - -/* =========================== - TRIP PAGE EVENTS - =========================== */ - -.trip-event-card { - background: #fdf5ff; - border: 1px solid #d4a8e8; - border-radius: 6px; - padding: 9px 13px; - margin: 5px 0; -} - -.trip-event-card .card-body { padding: 0; } - -.trip-event-card .card-title { - font-size: 0.9rem; - font-weight: 600; - margin-bottom: 3px; - color: var(--t-navy); -} - -.trip-event-card .card-text { - font-size: 0.82rem; - margin-bottom: 0; -} - -/* =========================== - TRIP PAGE HEADER & TYPOGRAPHY - =========================== */ - -.trip-page-title { - font-family: 'Playfair Display', Georgia, 'Times New Roman', serif; - font-weight: 700; - font-size: 1.8rem; - color: var(--t-navy); - margin-bottom: 4px; -} - -/* Section divider headings on the trip detail page */ -h3.trip-section-h, -h4.trip-section-h { - font-size: 0.72rem; - font-weight: 700; - text-transform: uppercase; - letter-spacing: 0.14em; - color: var(--t-muted); - margin-top: 22px; - margin-bottom: 8px; - padding-bottom: 4px; - border-bottom: 1px solid var(--t-border); -} - -/* Prev/next nav */ -.trip-prev-next { - font-size: 0.83rem; - color: var(--t-muted); - margin-bottom: 16px; -} - -.trip-prev-next a { - color: var(--t-slate); - text-decoration: none; - font-weight: 500; -} - -.trip-prev-next a:hover { - color: var(--t-gold); -} - -/* =========================== - TRIP LIST PAGE SUMMARY BOX - =========================== */ - -.trip-list-summary { - background: var(--t-navy); - color: #c8d4e8; - border-radius: 8px; - padding: 14px 18px; - margin-bottom: 18px; - margin-top: 8px; -} - -.trip-list-summary h2 { - color: #f0f4fa; - font-family: 'Playfair Display', Georgia, serif; - font-size: 1.25rem; - margin-bottom: 4px; - font-weight: 700; -} - -.trip-list-summary a { - color: var(--t-amber); - text-decoration: none; - font-size: 0.82rem; -} - -.trip-list-summary a:hover { - text-decoration: underline; -} - -.summary-stats-row { - display: flex; - flex-wrap: wrap; - gap: 18px; - margin-top: 10px; -} - -.summary-stat { - display: flex; - flex-direction: column; -} - -.summary-stat-label { - font-size: 0.62rem; - text-transform: uppercase; - letter-spacing: 0.1em; - opacity: 0.55; - line-height: 1; - margin-bottom: 2px; -} - -.summary-stat-value { - font-family: 'JetBrains Mono', monospace; - font-size: 0.82rem; - color: var(--t-amber); - font-weight: 500; -} diff --git a/static/js/map.js b/static/js/map.js deleted file mode 100644 index 3469fed..0000000 --- a/static/js/map.js +++ /dev/null @@ -1,270 +0,0 @@ -if (![].at) { - Array.prototype.at = function(pos) { return this.slice(pos, pos + 1)[0]; }; -} - -var emojiByType = { - "station": "🚉", - "airport": "✈️", - "ferry_terminal": "🚢", - "coach_station": "🚌", - "bus_stop": "🚏", - "home": "🏠", - "car_stop": "🚗", - "accommodation": "🏨", - "conference": "🖥️", - "event": "🍷" -}; - -function getIconMetrics(zoom) { - var outerSize; - if (zoom <= 3) { - outerSize = 20; - } else if (zoom <= 5) { - outerSize = 26; - } else if (zoom <= 8) { - outerSize = 32; - } else { - outerSize = 38; - } - var innerSize = outerSize - 6; - var fontSize = Math.max(12, Math.round(innerSize * 0.65)); - - return { - outerSize: outerSize, - fontSize: fontSize, - anchor: [outerSize / 2, outerSize / 2] - }; -} - -function emojiIcon(emoji, zoom) { - var symbol = emoji || "📍"; - var metrics = getIconMetrics(zoom); - var iconStyle = [ - "
", - "
", - symbol, - "
" - ].join(""); - - return L.divIcon({ - className: "custom-div-icon", - html: iconStyle, - iconSize: [metrics.outerSize, metrics.outerSize], - iconAnchor: metrics.anchor - }); -} - - -function build_map(map_id, coordinates, routes) { - var bounds = coordinates.map(function(station) { return [station.latitude, station.longitude]; }); - - function addGeoJsonCoordinatesToBounds(geojsonCoordinates) { - if (!geojsonCoordinates) return; - if (typeof geojsonCoordinates[0] === "number" && typeof geojsonCoordinates[1] === "number") { - bounds.push([geojsonCoordinates[1], geojsonCoordinates[0]]); - return; - } - geojsonCoordinates.forEach(addGeoJsonCoordinatesToBounds); - } - - function addGeoJsonGeometryToBounds(geometry) { - if (!geometry) return; - if (geometry.type === "GeometryCollection") { - geometry.geometries.forEach(addGeoJsonGeometryToBounds); - return; - } - addGeoJsonCoordinatesToBounds(geometry.coordinates); - } - - function addGeoJsonToBounds(geojson) { - if (!geojson) return; - if (geojson.type === "FeatureCollection") { - geojson.features.forEach(function(feature) { - addGeoJsonGeometryToBounds(feature.geometry); - }); - return; - } - if (geojson.type === "Feature") { - addGeoJsonGeometryToBounds(geojson.geometry); - return; - } - addGeoJsonGeometryToBounds(geojson); - } - - routes.forEach(function(r) { - if (r.from) bounds.push(r.from); - if (r.to) bounds.push(r.to); - if (r.geojson) addGeoJsonToBounds(JSON.parse(r.geojson)); - }); - - var map = bounds.length > 0 - ? L.map(map_id).fitBounds(bounds) - : L.map(map_id).setView([20, 0], 2); - - L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { - attribution: '©
OpenStreetMap contributors' - }).addTo(map); - - var markers = []; - var offset_lines = []; - - function getIconBounds(latlng, zoom) { - var iconSize = getIconMetrics(zoom).outerSize; - if (!latlng) return null; - var pixel = map.project(latlng, zoom); - var sw = map.unproject([pixel.x - iconSize / 2, pixel.y + iconSize / 2], zoom); - var ne = map.unproject([pixel.x + iconSize / 2, pixel.y - iconSize / 2], zoom); - return L.latLngBounds(sw, ne); - } - - function calculateCentroid(markers) { - var latSum = 0, lngSum = 0, count = 0; - markers.forEach(function(marker) { - latSum += marker.getLatLng().lat; - lngSum += marker.getLatLng().lng; - count += 1; - }); - return count > 0 ? L.latLng(latSum / count, lngSum / count) : null; - } - - // Function to detect and group overlapping markers - function getOverlappingGroups(zoom) { - var groups = []; - var visited = new Set(); - - markers.forEach(function(marker) { - if (visited.has(marker)) { - return; - } - var group = []; - var markerBounds = getIconBounds(marker.getLatLng(), zoom); - - markers.forEach(function(otherMarker) { - var otherBounds = getIconBounds(otherMarker.getLatLng(), zoom); - if (marker !== otherMarker && markerBounds && otherBounds && markerBounds.intersects(otherBounds)) { - group.push(otherMarker); - visited.add(otherMarker); - } - }); - - if (group.length > 0) { - group.push(marker); // Add the original marker to the group - groups.push(group); - visited.add(marker); - } - }); - - return groups; - } - - function displaceMarkers(group, zoom) { - var markerPixelSize = Math.max(18, getIconMetrics(zoom).outerSize); - var mapRef = group[0]._map; // Assuming all markers are on the same map - - var centroid = calculateCentroid(group); - if (!centroid) { - return; - } - var centroidPoint = mapRef.project(centroid, zoom); - - var radius = markerPixelSize * 1.1; - var angleIncrement = (2 * Math.PI) / group.length; - - group.forEach(function(marker, index) { - var angle = index * angleIncrement; - var newX = centroidPoint.x + radius * Math.cos(angle); - var newY = centroidPoint.y + radius * Math.sin(angle); - var newPoint = L.point(newX, newY); - var newLatLng = mapRef.unproject(newPoint, zoom); - - var originalPos = marker.originalLatLng; - marker.setLatLng(newLatLng); - - marker.polyline = L.polyline([originalPos, newLatLng], {color: "#909090", weight: 1, dashArray: "2,4"}).addTo(mapRef); - offset_lines.push(marker.polyline); - }); - } - - function updateMarkerIcons(zoom) { - markers.forEach(function(marker) { - marker.setIcon(emojiIcon(marker.emoji, zoom)); - }); - } - - coordinates.forEach(function(item) { - var latlng = L.latLng(item.latitude, item.longitude); - var marker = L.marker(latlng, { icon: emojiIcon(emojiByType[item.type], map.getZoom()) }).addTo(map); - marker.bindPopup(item.name); - marker.originalLatLng = latlng; - marker.emoji = emojiByType[item.type]; - markers.push(marker); - }); - - function resetMarkerPositions() { - markers.forEach(function(marker) { - marker.setLatLng(marker.originalLatLng); - if (marker.polyline) { - map.removeLayer(marker.polyline); - marker.polyline = null; - } - }); - offset_lines.forEach(function(polyline) { - map.removeLayer(polyline); - }); - offset_lines = []; - } - - map.on('zoomend', function() { - resetMarkerPositions(); - updateMarkerIcons(map.getZoom()); - - var overlappingGroups = getOverlappingGroups(map.getZoom()); - - overlappingGroups.forEach(function(group) { return displaceMarkers(group, map.getZoom()); }); - }); - - updateMarkerIcons(map.getZoom()); - - var initialGroups = getOverlappingGroups(map.getZoom()); - initialGroups.forEach(function(group) { return displaceMarkers(group, map.getZoom()); }); - - - // Draw routes - routes.forEach(function(route) { - var color = {"train": "blue", "flight": "red", "unbooked_flight": "orange", "coach": "green", "bus": "purple", "car": "#555"}[route.type]; - var style = { weight: 3, opacity: 0.5, color: color }; - if (route.geojson) { - L.geoJSON(JSON.parse(route.geojson), { - style: function(feature) { return style; } - }).addTo(map); - } else if (route.type === "flight" || route.type === "unbooked_flight") { - var flightPath = new L.Geodesic([[route.from, route.to]], style).addTo(map); - } else { - L.polyline([route.from, route.to], style).addTo(map); - } - }); - - var mapElement = document.getElementById(map_id); - - document.getElementById('toggleMapSize').addEventListener('click', function() { - var mapElement = document.getElementById(map_id); - var isFullWindow = mapElement.classList.contains('full-window-map'); - - if (isFullWindow) { - mapElement.classList.remove('full-window-map'); - mapElement.classList.add('half-map'); - mapElement.style.position = 'relative'; - } else { - mapElement.classList.add('full-window-map'); - mapElement.classList.remove('half-map'); - mapElement.style.position = ''; - } - - // Ensure the map adjusts to the new container size - map.invalidateSize(); - }); - - return map; -} diff --git a/templates/accommodation.html b/templates/accommodation.html index dda241d..ee98ef4 100644 --- a/templates/accommodation.html +++ b/templates/accommodation.html @@ -1,12 +1,9 @@ {% extends "base.html" %} -{% from "macros.html" import trip_link, accommodation_row with context %} -{% block title %}Accommodation - Edward Betts{% endblock %} {% block style %} -{% set column_count = 9 %} {% endblock %} +{% macro row(item, badge) %} +
{{ item.from.strftime("%a, %d %b %Y") }}
+
{{ item.to.strftime("%a, %d %b") }}
+
{{ (item.to.date() - item.from.date()).days }}
+
{{ item.name }}
+
{{ item.operator }}
+
{{ item.location }}
+{% endmacro %} + {% macro section(heading, item_list, badge) %} {% if item_list %}

{{heading}}

-{% for item in item_list %} - {{ accommodation_row(item, badge) }} -
{% if item.linked_trip %} trip: {{ trip_link(item.linked_trip) }} {% endif %}
-{% endfor %} +{% for item in item_list %}{{ row(item, badge) }}{% endfor %} {% endif %} {% endmacro %} @@ -37,24 +40,13 @@

Accommodation

-

Statistics

- {{ section("Current", current) }} - {{ section("Future", future) }} - {{ section("Past", past) }} + {{ section("Accommodation", items) }}
diff --git a/templates/base.html b/templates/base.html index d51e83c..94fe993 100644 --- a/templates/base.html +++ b/templates/base.html @@ -7,7 +7,7 @@ {% block title %}{% endblock %} - + {% block style %} {% endblock %} @@ -16,9 +16,8 @@ {% block nav %}{{ navbar() }}{% endblock %} -{% include "flash_messages.html" %} {% block content %}{% endblock %} {% block scripts %}{% endblock %} - + diff --git a/templates/birthday_list.html b/templates/birthday_list.html deleted file mode 100644 index 8443685..0000000 --- a/templates/birthday_list.html +++ /dev/null @@ -1,27 +0,0 @@ -{% extends "base.html" %} -{% from "macros.html" import display_date %} -{% block title %}Birthdays - Edward Betts{% endblock %} - -{% block content %} -
-

Birthdays

- - - - - - - - - - {% for event in items %} - - - - - - {% endfor %} - -
DateEventDays
{{event.as_date.strftime("%a, %d, %b %Y")}}{{ event.title }}{{ event.delta_days(today) }}
-
-{% endblock %} diff --git a/templates/calendar.html b/templates/calendar.html deleted file mode 100644 index a4656aa..0000000 --- a/templates/calendar.html +++ /dev/null @@ -1,266 +0,0 @@ - - - - - - Agenda - Edward Betts - - - - - - - - -{% set event_labels = { - "economist": "📰 The Economist", - "mothers_day": "Mothers' day", - "fathers_day": "Fathers' day", - "uk_financial_year_end": "End of financial year", - "bank_holiday": "UK bank holiday", - "us_holiday": "US holiday", - "uk_clock_change": "UK clock change", - "us_clock_change": "US clock change", - "us_presidential_election": "US pres. election", - "xmas_last_second": "Christmas last posting 2nd class", - "xmas_last_first": "Christmas last posting 1st class", - "up_series": "Up documentary", - "waste_schedule": "Waste schedule", - "gwr_advance_tickets": "GWR advance tickets", - "critical_mass": "Critical Mass", -} -%} - -{% from "navbar.html" import navbar with context %} - - {{ navbar() }} - -
-

Agenda

-

- ← personal tools -

- - {% if errors %} - {% for error in errors %} - - {% endfor %} - {% endif %} - -
- Markets: - Hide while away - | Show all - | Hide all -
- - -
-
- - - -
-

-
- - - -
-
- - -
- - {# -
-
Page generation time
- -
- #} - -
- - - - - - - - - diff --git a/templates/conference_list.html b/templates/conference_list.html index dd8bddf..5d490c1 100644 --- a/templates/conference_list.html +++ b/templates/conference_list.html @@ -1,361 +1,63 @@ {% extends "base.html" %} -{% from "macros.html" import trip_link with context %} - -{% block title %}Conferences - Edward Betts{% endblock %} - {% block style %} {% endblock %} -{% set tl_colors = ["#0d6efd","#198754","#dc3545","#fd7e14","#6f42c1","#20c997","#0dcaf0","#d63384"] %} - -{% macro render_timeline(timeline) %} -{% if timeline %} -{% set row_h = 32 %} -{% set header_h = 22 %} -{% set total_h = timeline.lane_count * row_h + header_h %} -
-

Next 90 days

-
- - {% for m in timeline.months %} -
- {{ m.label }} -
- {% endfor %} - -
- - {% for conf in timeline.confs %} - {% set color = tl_colors[conf.lane % tl_colors | length] %} - {% set top_px = conf.lane * row_h + header_h %} -
- {% if conf.url %}{{ conf.name }} - {% else %}{{ conf.name }}{% endif %} -
- {% endfor %} - -
+{% macro row(item, badge) %} +
{{ item.start.strftime("%a, %d %b %Y") }}
+
{{ item.end.strftime("%a, %d %b") }}
+
{{ item.name }} + {% if item.going and not (item.accommodation_booked or item.travel_booked) %} + + {{ badge }} + + {% endif %} + {% if item.accommodation_booked %} + accommodation + {% endif %} + {% if item.transport_booked %} + transport + {% endif %}
-{% endif %} +
{{ item.topic }}
+
{{ item.location }}
+
{{ item.url }}
{% endmacro %} -{% macro conf_rows(heading, item_list, badge) %} +{% macro section(heading, item_list, badge) %} {% if item_list %} -{% set count = item_list | length %} - - {{ heading }} {{ count }} conference{{ "" if count == 1 else "s" }} - -{% set ns = namespace(prev_month="") %} -{% for item in item_list %} - {% set month_label = item.sort_date.strftime("%B %Y") %} - {% if month_label != ns.prev_month %} - {% set ns.prev_month = month_label %} - - {{ month_label }} - - {% endif %} - - - {{ item.display_date }} - {% if item.date_status == "tentative" %} - tentative - {% elif item.date_status == "approximate" %} - approximate - {% endif %} - - - {% if item.url %}{{ item.name }} - {% else %}{{ item.name }}{% endif %} - {% if item.going and not (item.accommodation_booked or item.travel_booked) %} - {{ badge }} - {% endif %} - {% if item.accommodation_booked %} - accommodation - {% endif %} - {% if item.transport_booked %} - transport - {% endif %} - {% if item.linked_trip %} - {% set trip = item.linked_trip %} - - 🧳{% if trip.title != item.name %} {{ trip.title }}{% endif %} - - {% endif %} - - {{ item.topic }} - - {% if item.series and item.series_detail %} - {{ item.series_detail.name }} - {% elif item.series %} - {{ item.series }} - {% endif %} - - - {% set country = get_country(item.country) if item.country else None %} - {% if country %}{{ country.flag }} {{ item.location }} - {% elif item.online %}💻 Online - {% else %}{{ item.location }}{% endif %} - - - {% if item.cfp_end %}{{ item.cfp_end.strftime("%-d %b %Y") }}{% endif %} - - - {% if item.price and item.currency %} - {{ "{:,d}".format(item.price | int) }} {{ item.currency }} - {% if item.currency != "GBP" and item.currency in fx_rate %} - {{ "{:,.0f}".format(item.price / fx_rate[item.currency]) }} GBP - {% endif %} - {% elif item.free %} - free - {% endif %} - - -{% endfor %} +

{{heading}}

+{% for item in item_list %}{{ row(item, badge) }}{% endfor %} {% endif %} {% endmacro %} {% block content %}
+

Conferences

- {% if country_options %} -
-
- - -
-
- - {% if selected_country %} - Clear - {% endif %} -
-
- {% endif %} - - {{ render_timeline(timeline) }} - - - - - - - - - - - - - - - - - - - - - - - - - {{ conf_rows("Current", current, "attending") }} - {{ conf_rows("Future", future, "going") }} - {{ conf_rows("Past", past|reverse|list, "went") }} - -
Conferences grouped by attendance and month
DatesConferenceTopicSeriesLocationCFP endsPrice
+
+ {{ section("Current", current, "attending") }} + {{ section("Future", future, "going") }} + {{ section("Past", past|reverse, "went") }} +
- - {% endblock %} diff --git a/templates/conference_series.html b/templates/conference_series.html deleted file mode 100644 index 10bfdb7..0000000 --- a/templates/conference_series.html +++ /dev/null @@ -1,85 +0,0 @@ -{% extends "base.html" %} - -{% block title %}{{ series.name }} - Edward Betts{% endblock %} - -{% block content %} -
-

{{ series.name }}

- -
- {% if series.topic %} -
Topic
-
{{ series.topic }}
- {% endif %} - {% if series.usual_location or series.country %} -
Usual location
-
- {% set country = get_country(series.country) if series.country else None %} - {% if country %}{{ country.flag }} {% endif %}{{ series.usual_location or "" }} -
- {% endif %} - {% if series.cadence %} -
Cadence
-
{{ series.cadence }}
- {% endif %} - {% if series.url %} -
Website
-
{{ series.url }}
- {% endif %} - {% if series.notes %} -
Notes
-
{{ series.notes }}
- {% endif %} -
- -

Editions

- - - - - - - - - - - {% for item in conferences %} - - - - - - - {% endfor %} - -
DatesConferenceLocationAttendance
- {{ item.display_date }} - {% if item.date_status == "tentative" %} - tentative - {% elif item.date_status == "approximate" %} - approximate - {% endif %} - - {% if item.url %}{{ item.name }} - {% else %}{{ item.name }}{% endif %} - - {% set country = get_country(item.country) if item.country else None %} - {% if country %}{{ country.flag }} {% endif %}{{ item.location }} - - {% if item.going %} - going - {% endif %} - {% if item.registered %} - registered - {% endif %} - {% if item.linked_trip %} - {% set trip = item.linked_trip %} - - trip{% if trip.title != item.name %}: {{ trip.title }}{% endif %} - - {% endif %} -
-
-{% endblock %} diff --git a/templates/conference_series_list.html b/templates/conference_series_list.html deleted file mode 100644 index f9ccc5f..0000000 --- a/templates/conference_series_list.html +++ /dev/null @@ -1,47 +0,0 @@ -{% extends "base.html" %} - -{% block title %}Conference Series - Edward Betts{% endblock %} - -{% block content %} -
-

Conference Series

- - - - - - - - - - - - - {% for item in series_list %} - - - - - - - - {% endfor %} - -
SeriesTopicUsual locationConferencesNext
- {{ item.name }} - {% if item.attended %} - attended - {% endif %} - {% if item.url %} - - {% endif %} - {{ item.topic or "" }} - {% set country = get_country(item.country) if item.country else None %} - {% if country %}{{ country.flag }} {% endif %}{{ item.usual_location or "" }} - {{ item.count }} - {% if item.next_conf %} - {{ item.next_conf.display_date }} · {{ item.next_conf.name }} - {% endif %} -
-
-{% endblock %} diff --git a/templates/fixture/map.html b/templates/fixture/map.html deleted file mode 100644 index 9ae6da7..0000000 --- a/templates/fixture/map.html +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - -Map fixture - - - - - - - - - - - - -
- - - - - - - - - - - diff --git a/templates/flash_messages.html b/templates/flash_messages.html deleted file mode 100644 index 9c45802..0000000 --- a/templates/flash_messages.html +++ /dev/null @@ -1,12 +0,0 @@ -{% with messages = get_flashed_messages() %} -{% if messages %} -
- {% for message in messages %} - - {% endfor %} -
-{% endif %} -{% endwith %} diff --git a/templates/gaps.html b/templates/gaps.html index ceeed19..ae24883 100644 --- a/templates/gaps.html +++ b/templates/gaps.html @@ -1,5 +1,4 @@ {% extends "base.html" %} -{% block title %}Gaps - Edward Betts{% endblock %} {% block content %}
@@ -18,31 +17,11 @@ {% for gap in gaps %} - - {% for event in gap.before %} -
- {% if event.url %} - {{ event.title_with_emoji }} - {% else %} - {{ event.title_with_emoji }} - {% endif %} -
- {% endfor %} - + {% for event in gap.before %}{% if not loop.first %}
{% endif %}{{ event.title or event.name }}{% endfor %} {{ gap.start.strftime("%A, %-d %b %Y") }} {{ (gap.end - gap.start).days }} days {{ gap.end.strftime("%A, %-d %b %Y") }} - - {% for event in gap.after %} -
- {% if event.url %} - {{ event.title_with_emoji }} - {% else %} - {{ event.title_with_emoji }} - {% endif %} -
- {% endfor %} - + {% for event in gap.after %}{% if not loop.first %}
{% endif %}{{ event.title or event.name }}{% endfor %} {% endfor %} diff --git a/templates/holiday_list.html b/templates/holiday_list.html deleted file mode 100644 index a627771..0000000 --- a/templates/holiday_list.html +++ /dev/null @@ -1,39 +0,0 @@ -{% extends "base.html" %} -{% from "macros.html" import display_date %} -{% block title %}Holidays - Edward Betts{% endblock %} - -{% block content %} -
-

Public holidays

- - {% for item in items %} - {% set country = get_country(item.country) %} - - {% if loop.first or item.date != loop.previtem.date %} - - - {% else %} - - {% endif %} - - - - {% endfor %} -
{{ display_date(item.date) }}in {{ (item.date - today).days }} days{{ country.flag }} {{ country.name }}{{ item.display_name }}
- -

- UK school holidays (Bristol) - source -

- - {% for item in school_holidays %} - - - - - - - {% endfor %} -
{{ display_date(item.as_date) }}to {{ display_date(item.end_as_date) }}in {{ (item.as_date - today).days }} days{{ item.title }}
-
-{% endblock %} diff --git a/templates/event_list.html b/templates/index.html similarity index 52% rename from templates/event_list.html rename to templates/index.html index 6adb8da..5dcf8c3 100644 --- a/templates/event_list.html +++ b/templates/index.html @@ -3,11 +3,72 @@ - Agenda - Edward Betts - + Agenda + - + + + + @@ -27,7 +88,6 @@ "waste_schedule": "Waste schedule", "gwr_advance_tickets": "GWR advance tickets", "critical_mass": "Critical Mass", - "uk_school_holiday": "UK school holiday", } %} @@ -35,18 +95,15 @@ "bank_holiday": "bg-success-subtle", "conference": "bg-primary-subtle", "us_holiday": "bg-secondary-subtle", - "uk_school_holiday": "bg-warning-subtle", "birthday": "bg-info-subtle", "waste_schedule": "bg-danger-subtle", } %} -{% from "macros.html" import trip_link, display_date_no_year, trip_item with context %} {% from "navbar.html" import navbar with context %} {{ navbar() }} - {% include "flash_messages.html" %}

Agenda

@@ -70,49 +127,16 @@ Sunset: {{ sunset.strftime("%H:%M:%S") }} - {% if home_weather %} -
- Bristol weather: - {% for day in home_weather %} - - {{ day.date_obj.strftime("%-d %b") }} - {{ day.status }} - {{ day.temp_min }}–{{ day.temp_max }}°C - - {% endfor %} -
- {% endif %} - - {% if errors %} - {% for error in errors %} - - {% endfor %} - {% endif %} -

Stock markets

{% for market in stock_markets %}

{{ market }}

{% endfor %} - {% if current_trip %} -
-

Current trip

- {{ trip_item(current_trip) }} -
- {% endif %} +

Agenda

-
- Markets: - Hide while away - | Show all - | Hide all -
- - {% for event in events if start_event_list <= event.as_date <= end_event_list %} + {% for event in events if event.as_date >= two_weeks_ago %} {% if loop.first or event.date.year != loop.previtem.date.year or event.date.month != loop.previtem.date.month %}
@@ -142,12 +166,11 @@
{% if event.end_date %} - {% set duration = event.display_duration() %} - {% if duration %} + {% if event.end_as_date == event.as_date and event.has_time %} end: {{event.end_date.strftime("%H:%M") }} - (duration: {{duration}}) + (duration: {{event.end_date - event.date}}) {% elif event.end_date != event.date %} - to {{ event.end_as_date.strftime("%a, %d, %b") }} + {{event.end_date}} {% endif %} {% endif %}
@@ -155,7 +178,7 @@
@@ -165,21 +188,7 @@ {% endif %} {% endfor %} -
-
Page generation time
-
    -
  • Data gather took {{ "%.1f" | format(data_gather_seconds) }} seconds
  • -
  • Stock market open/close took - {{ "%.1f" | format(stock_market_times_seconds) }} seconds
  • - {% for name, seconds in timings %} -
  • {{ name }} took {{ "%.1f" | format(seconds) }} seconds
  • - {% endfor %} -
  • Render time: {{ "%.1f" | format(render_time) }} seconds
  • - -
- -
- + diff --git a/templates/launches.html b/templates/launches.html index f2576dc..c048d5e 100644 --- a/templates/launches.html +++ b/templates/launches.html @@ -1,63 +1,11 @@ {% extends "base.html" %} -{% block title %}Space launches - Edward Betts{% endblock %} - {% block content %}

Space launches

-

Filters

- -

Mission type: - - {% if request.args.type %}🗙{% endif %} - - {% for t in mission_types | sort %} - {% if t == request.args.type %} - {{ t }} - {% else %} - - {{ t }} - - {% endif %} - {% if not loop.last %} | {% endif %} - {% endfor %} -

- -

Vehicle: - {% if request.args.rocket %}🗙{% endif %} - - {% for r in rockets | sort %} - {% if r == request.args.rockets %} - {{ r }} - {% else %} - - {{ r }} - - {% endif %} - {% if not loop.last %} | {% endif %} - {% endfor %} -

- -

Orbit: - {% if request.args.orbit %}🗙{% endif %} - - {% for name, abbrev in orbits | sort %} - {% if abbrev == request.args.orbit %} - {{ name }} - {% else %} - - {{ name }} - - {% endif %} - {% if not loop.last %} | {% endif %} - {% endfor %} -

- - {% for launch in launches %} - {% set highlight =" bg-primary-subtle" if launch.slug in config.FOLLOW_LAUNCHES else "" %} - {% set country = get_country(launch.country_code) %} -
+ {% for launch in rockets %} +
{{ launch.t0_date }}
@@ -68,20 +16,8 @@
launch status: {{ launch.status.abbrev }} - {% if launch.is_active_crewed %} - In space - {% endif %} - {% if launch.is_future and launch.probability %}{{ launch.probability }}%{% endif %}
-
-
- {% if launch.image %} -
- -
- {% endif %} - {{ country.flag }} - {{ launch.rocket.full_name }} +
{{ launch.rocket }} – {{launch.mission.name }} – @@ -94,29 +30,14 @@ ({{ launch.launch_provider_type }}) — {{ launch.orbit.name }} ({{ launch.orbit.abbrev }}) - — - {{ launch.mission.type }} -
-
- {% if launch.pad_wikipedia_url %} - {{ launch.pad_name }} - {% else %} - {{ launch.pad_name }} {% if launch.pad_name != "Unknown Pad" %}(no Wikipedia article){% endif %} - {% endif %} - — {{ launch.location }} -
- {% if launch.mission.agencies | count %} -
- {% for agency in launch.mission.agencies %} - {% set agency_country = get_country(agency.country_code) %} - {%- if not loop.first %}, {% endif %} - {{agency.name }} - {{ agency_country.flag }} - ({{ agency.type }}) {# #} - {% endfor %} -
+
+ {% if launch.pad_wikipedia_url %} + {{ launch.pad_name }} + {% else %} + {{ launch.pad_name }} {% if launch.pad_name != "Unknown Pad" %}(no Wikipedia article){% endif %} {% endif %} -
+ — {{ launch.location }}
+ {% if launch.mission %} {% for line in launch.mission.description.splitlines() %}

{{ line }}

@@ -124,13 +45,7 @@ {% else %}

No description.

{% endif %} - {% if launch.weather_concerns and launch.status.name != "Launch Successful" %} -

Weather concerns

- {% for line in launch.weather_concerns.splitlines() %} -

{{ line }}

- {% endfor %} - {% endif %} -
+
{% endfor %} diff --git a/templates/macros.html b/templates/macros.html deleted file mode 100644 index cb96e12..0000000 --- a/templates/macros.html +++ /dev/null @@ -1,534 +0,0 @@ -{% macro display_datetime(dt) %}{{ dt.strftime("%a, %d, %b %Y %H:%M %z") }}{% endmacro %} -{% macro display_time(dt) %} - {% if dt %}{{ dt.strftime("%H:%M %z") }}{% endif %} -{% endmacro %} -{% macro display_date(dt) %}{{ dt.strftime("%a %-d %b %Y") }}{% endmacro %} -{% macro display_date_no_year(dt) %}{{ dt.strftime("%a %-d %b") }}{% endmacro %} -{% macro display_conf_date_no_year(dt) %}{%- if dt.hour is defined %}{{ dt.strftime("%a %-d %b %H:%M") }}{% else %}{{ dt.strftime("%a %-d %b") }}{% endif %}{% endmacro %} - -{% macro format_distance(distance) %} - {{ "{:,.0f} km / {:,.0f} miles".format(distance, distance / 1.60934) }} -{% endmacro %} - -{% macro trip_link(trip) %} - {{ trip.title }} -{% endmacro %} - -{% macro conference_row(item, badge, show_flags=True) %} - {% set country = get_country(item.country) if item.country else None %} -
{{ item.start.strftime("%a, %d %b %Y") }}
-
{{ item.end.strftime("%a, %d %b") }}
-
- {% if item.url %} - {{ item.name }} - {% else %} - {{ item.name }} - {% endif %} - {% if item.going and not (item.accommodation_booked or item.travel_booked) %} - - {{ badge }} - - {% endif %} - {% if item.accommodation_booked %} - accommodation - {% endif %} - {% if item.transport_booked %} - transport - {% endif %} -
-
- {% if item.price and item.currency %} - {{ "{:,d}".format(item.price | int) }} {{ item.currency }} - {% if item.currency != "GBP" and item.currency in fx_rate %} - {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP - {% endif %} - {% elif item.free %} - free to attend - {% endif %} -
-
{{ item.topic }}
-
{{ item.location }}
-
{{ display_date(item.cfp_end) if item.cfp_end else "" }}
-
- {% if country %} - {% if show_flags %}{{ country.flag }}{% endif %} {{ country.name }} - {% elif item.online %} - 💻 Online - {% else %} - - country code {{ item.country }} not found - - {% endif %} -
-{% endmacro %} - -{% macro accommodation_row(item, badge, show_flags=True) %} - {% set country = get_country(item.country) %} - - {% set nights = (item.to.date() - item.from.date()).days %} -
{{ item.from.strftime("%a, %d %b %Y") }}
-
{{ item.to.strftime("%a, %d %b") }}
-
{% if nights == 1 %}1 night{% else %}{{ nights }} nights{% endif %}
-
{{ item.operator }}
-
{{ item.location }}
-
- {% if country %} - {% if show_flags %}{{ country.flag }}{% endif %} {{ country.name }} - {% else %} - - country code {{ item.country }} not found - - {% endif %} -
-
- {% if g.user.is_authenticated and item.url %} - {{ item.name }} - {% else %} - {{ item.name }} - {% endif %} -
-
- {% if g.user.is_authenticated and item.price and item.currency %} - {{ "{:,f}".format(item.price) }} {{ item.currency }} - {% if item.currency != "GBP" %} - {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP - {% endif %} - {% endif %} -
-{% endmacro %} - -{% macro flightradar24_url(flight) -%} -https://www.flightradar24.com/data/flights/{{ flight.airline_detail.iata | lower + flight.flight_number }} -{%- endmacro %} - -{% macro flight_booking_row(booking, show_flags=True) %} -
- {% if g.user.is_authenticated %} - {{ booking.booking_reference or "reference missing" }} - {% else %} - redacted - {% endif %} -
- -
- {% if g.user.is_authenticated and booking.price and booking.currency %} - {{ "{:,f}".format(booking.price) }} {{ booking.currency }} - {% if booking.currency != "GBP" %} - {{ "{:,.2f}".format(booking.price / fx_rate[booking.currency]) }} GBP - {% endif %} - {% endif %} -
- {% for i in range(9) %} -
- {% endfor %} - - {% for item in booking.flights %} - {% set full_flight_number = item.airline_code + item.flight_number %} - {% set radarbox_url = "https://www.radarbox.com/data/flights/" + full_flight_number %} -
-
-
{{ item.depart.strftime("%a, %d %b %Y") }}
-
{{ item.from }} → {{ item.to }}
-
{{ item.depart.strftime("%H:%M") }}
-
- {% if item.arrive %} - {{ item.arrive.strftime("%H:%M") }} - {% if item.arrive.date() != item.depart.date() %}+1 day{% endif %} - {% endif %} -
-
{{ item.duration }}
-
{{ full_flight_number }}
- -
- {% if item.distance %} - {{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }} - {% endif %} -
-
{{ "{:,.1f}".format(item.co2_kg) }} kg
- {% endfor %} -{% endmacro %} - -{% macro flight_row(item) %} - {% set full_flight_number = item.airline_code + item.flight_number %} - {% set radarbox_url = "https://www.radarbox.com/data/flights/" + full_flight_number %} -
{{ item.depart.strftime("%a, %d %b %Y") }}
-
{{ item.from }} → {{ item.to }}
-
{{ item.depart.strftime("%H:%M") }}
-
- {% if item.arrive %} - {{ item.arrive.strftime("%H:%M") }} - {% if item.arrive.date() != item.depart.date() %}+1 day{% endif %} - {% endif %} -
-
{{ item.duration }}
-
{{ full_flight_number }}
-
- {% if g.user.is_authenticated %} - {{ item.booking_reference }} - {% else %} - redacted - {% endif %} -
- -
- {% if item.distance %} - {{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }} - {% endif %} -
-
- {% if g.user.is_authenticated and item.price and item.currency %} - {{ "{:,f}".format(item.price) }} {{ item.currency }} - {% if item.currency != "GBP" %} - {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP - {% endif %} - {% endif %} -
-{% endmacro %} - -{% macro train_row(item) %} - {% set url = item.url %} -
{{ item.depart.strftime("%a, %d %b %Y") }}
- -
{{ item.depart.strftime("%H:%M") }}
-
- {% if item.arrive %} - {{ item.arrive.strftime("%H:%M") }} - {% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %} - {% endif %} -
-
{{ ((item.arrive - item.depart).total_seconds() // 60) | int }} mins
-
{{ item.operator }}
-
- {% if g.user.is_authenticated %} - {{ item.booking_reference }} - {% else %} - redacted - {% endif %} -
-
- {% for leg in item.legs %} - {% if leg.url %} - [{{ loop.index }}] - {% endif %} - {% endfor %} -
-
- {% if item.distance %} - {{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }} - {% endif %} -
-
- {% if g.user.is_authenticated and item.price and item.currency %} - {{ "{:,f}".format(item.price) }} {{ item.currency }} - {% if item.currency != "GBP" and item.currency in fx_rate %} - {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP - {% endif %} - {% endif %} -
-{% endmacro %} - -{% macro coach_row(item) %} - {% set url = item.url %} -
{{ item.depart.strftime("%a, %d %b %Y") }}
- -
{{ item.depart.strftime("%H:%M") }}
-
- {% if item.arrive %} - {{ item.arrive.strftime("%H:%M") }} - {% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %} - {% endif %} -
-
{{ ((item.arrive - item.depart).total_seconds() // 60) | int }} mins
-
{{ item.operator }}
-
- {% if g.user.is_authenticated %} - {{ item.booking_reference }} - {% else %} - redacted - {% endif %} -
-
-
-
- {% if item.distance %} - {{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }} - {% endif %} -
-
- {% if g.user.is_authenticated and item.price and item.currency %} - {{ "{:,f}".format(item.price) }} {{ item.currency }} - {% if item.currency != "GBP" and item.currency in fx_rate %} - {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP - {% endif %} - {% endif %} -
-{% endmacro %} - -{% macro bus_row(item) %} - {% set url = item.url %} -
{{ item.depart.strftime("%a, %d %b %Y") }}
- -
{{ item.depart.strftime("%H:%M") }}
-
- {% if item.arrive %} - {{ item.arrive.strftime("%H:%M") }} - {% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %} - {% endif %} -
-
{{ ((item.arrive - item.depart).total_seconds() // 60) | int }} mins
-
{{ item.operator }}
-
- {% if g.user.is_authenticated %} - {{ item.booking_reference }} - {% else %} - redacted - {% endif %} -
-
-
-
- {% if item.distance %} - {{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }} - {% endif %} -
-
- {% if g.user.is_authenticated and item.price and item.currency %} - {{ "{:,f}".format(item.price) }} {{ item.currency }} - {% if item.currency != "GBP" and item.currency in fx_rate %} - {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP - {% endif %} - {% endif %} -
-{% endmacro %} - -{% macro ferry_row(item) %} -
{{ item.depart.strftime("%a, %d %b %Y") }}
-
- {{ item.from }} → {{ item.to }} -
- -
{{ item.depart.strftime("%H:%M") }}
-
- {% if item.arrive %} - {{ item.arrive.strftime("%H:%M") }} - {% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %} - {% endif %} -
- -
-
{{ item.operator }}
-
-
-
- -
- {% if g.user.is_authenticated and item.price and item.currency %} - {{ "{:,f}".format(item.price) }} {{ item.currency }} - {% if item.currency != "GBP" %} - {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP - {% endif %} - {% endif %} -
- - - {#
{{ item | pprint }}
#} -{% endmacro %} - -{% macro flag(trip, flag) %}{% if trip.show_flags %}{{ flag }}{% endif %}{% endmacro %} - -{% macro conference_list(trip) %} - {% for item in trip.conferences %} - {% set country = get_country(item.country) if item.country else None %} -
-
-
- {{ item.name }} - - {{ display_conf_date_no_year(item.attend_start if item.attend_start else item.start) }} to {{ display_conf_date_no_year(item.attend_end if item.attend_end else item.end) }} - {% if item.attend_start or item.attend_end %} - (full conference: {{ display_date_no_year(item.start) }} to {{ display_date_no_year(item.end) }}) - {% endif %} - -
-

- Topic: {{ item.topic }} - | Venue: {{ item.venue }} - | Location: {{ item.location }} - {% if country %} - {{ flag(trip, country.flag) }} - {% elif item.online %} - 💻 Online - {% else %} - - country code {{ item.country }} not found - - {% endif %} - {% if item.free %} - | free to attend - {% elif item.price and item.currency %} - | price: {{ item.price }} {{ item.currency }} - {% endif %} -

-
-
- {% endfor %} -{% endmacro %} - -{% macro render_trip_element(e, trip) %} - {% set item = e.detail %} - {% if e.element_type == "check-in" %} - {% set nights = (item.to.date() - item.from.date()).days %} -
- {{ e.get_emoji() }} {{ item.name }} - {% if item.operator and item.operator != item.name %}{{ item.operator }}{% endif %} - ({% if nights == 1 %}1 night{% else %}{{ nights }} nights{% endif %}) -
- {% elif e.element_type == "check-out" %} -
- {{ e.get_emoji() }} Check out: {{ item.name }} - {% if item.operator and item.operator != item.name %}{{ item.operator }}{% endif %} -
- {% elif e.element_type != "conference" %} - {# Transport: flight, train, ferry, coach, bus, car #} - {% set has_arrive = item.arrive is defined and item.arrive %} - {# item.depart may be a date (no .date() method) or a datetime (has .date()) #} - {% set has_time = item.depart is defined and item.depart and item.depart.hour is defined %} - {% set depart_date = item.depart.date() if has_time else item.depart %} - {% set arrive_date = item.arrive.date() if (has_arrive and item.arrive.hour is defined) else item.arrive %} - {% set is_overnight = has_arrive and depart_date != arrive_date %} - {% set dur_mins = ((item.arrive - item.depart).total_seconds() // 60) | int if (has_time and has_arrive) else none %} -
- {% if is_overnight %}🌙{% else %}{{ e.get_emoji() }}{% endif %} - {{ e.start_loc }} → {{ e.end_loc }} - {% if has_time %} - · {{ item.depart.strftime("%H:%M") }}{% if has_arrive and item.arrive.hour is defined %} → {{ item.arrive.strftime("%H:%M") }}{% if is_overnight %} +1 day{% endif %}{% endif %} - {% endif %} - {% if dur_mins %} - {%- set h = dur_mins // 60 %}{%- set m = dur_mins % 60 %} - 🕒{% if h %}{{ h }}h {% endif %}{% if m %}{{ m }}m{% endif %} - {% endif %} - {% if e.element_type == "flight" %} - · {{ item.airline_name }} {{ item.airline_code }}{{ item.flight_number }} - {% elif item.operator %} - · {{ item.operator }} - {% endif %} - {% if item.distance %} - · {{ "{:,.0f} km".format(item.distance) }} - {% endif %} - {% if item.co2_kg is defined and item.co2_kg is not none %} - CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg - {% endif %} -
- {% endif %} -{% endmacro %} - -{% macro trip_item(trip) %} - {% set distances_by_transport_type = trip.distances_by_transport_type() %} - {% set total_distance = trip.total_distance() %} - {% set total_co2_kg = trip.total_co2_kg() %} - {% set end = trip.end %} - {% set trip_end = end or trip.start %} - {% set is_current = trip.start <= today and trip_end >= today %} -
-

- {{ trip_link(trip) }} - ({{ display_date(trip.start) }})

- {% set school_holidays = trip_school_holiday_map.get(trip.start.isoformat(), []) if trip_school_holiday_map is defined else [] %} - {% if school_holidays %} -
- UK school holiday - {% for item in school_holidays %} - {{ item.title }} ({{ display_date_no_year(item.as_date) }} to {{ display_date_no_year(item.end_as_date) }}) - {% endfor %} -
- {% endif %} -
    - {% for c in trip.countries %} -
  • {{ c.flag }} {{ c.name }}
  • - {% endfor %} -
- {% if end %} -
Dates: {{ display_date_no_year(trip.start) }} to {{ display_date_no_year(end) }} - {% if g.user.is_authenticated and trip.start <= today %} - photos - {% endif %} -
- {% else %} -
Start: {{ display_date_no_year(trip.start) }} (end date missing)
- {% endif %} -
- {% if total_distance %} - {{ format_distance(total_distance) }} - {% endif %} - {% if distances_by_transport_type %} - {% for transport_type, distance in distances_by_transport_type %} - {{ transport_type | title }}: {{format_distance(distance) }} - {% endfor %} - {% endif %} - {% if total_co2_kg %} - CO₂ {{ "{:,.1f}".format(total_co2_kg) }} kg - {% endif %} - {% set co2_by_transport = trip.co2_by_transport_type() %} - {% if co2_by_transport %} - {% for transport_type, co2_kg in co2_by_transport %} - {{ transport_type | title }} CO₂ {{ "{:,.1f}".format(co2_kg) }} kg - {% endfor %} - {% endif %} -
- - {% if trip.schengen_compliance %} -
- Schengen: - {% if trip.schengen_compliance.is_compliant %} - ✅ Compliant - {% else %} - ❌ Non-compliant - {% endif %} - ({{ trip.schengen_compliance.total_days_used }}/90 days used) -
- {% endif %} - - {{ conference_list(trip) }} - - {% set trip_weather = trip_weather_map.get(trip.start.isoformat(), {}) if trip_weather_map is defined else {} %} - {% for day, elements in trip.elements_grouped_by_day() %} - {% set weather = trip_weather.get(day.isoformat()) %} -

{{ display_date_no_year(day) }} - {% if weather %} - - {{ weather.status }} - {{ weather.temp_min }}–{{ weather.temp_max }}°C - {{ weather.detailed_status }} - - {% endif %} - {% if g.user.is_authenticated and day <= today %} - photos - {% endif %} -

- {% for e in elements %} - {{ render_trip_element(e, trip) }} - {% endfor %} - {% endfor %} - -
-{% endmacro %} diff --git a/templates/meteors.html b/templates/meteors.html deleted file mode 100644 index a9683cd..0000000 --- a/templates/meteors.html +++ /dev/null @@ -1,54 +0,0 @@ -{% extends "base.html" %} - -{% block title %}Meteor Showers 2025 - Edward Betts{% endblock %} - -{% block content %} -
-

Meteor Showers 2025

- -

Major meteor showers visible throughout 2025. All times are approximate and viewing conditions depend on light pollution, weather, and moon phase.

- - {% for meteor in meteors %} -
-
- {{ meteor.peak }} -
- {{ meteor.active }} -
-
- Rate: - {{ meteor.rate }} -
- {{ meteor.radiant }} -
-
-
-

{{ meteor.name }}

-

Moon Phase: {{ meteor.moon_phase }}

-

Best Visibility: {{ meteor.visibility }}

-

{{ meteor.description }}

-
-
-
- {% endfor %} - -
-

Viewing Tips

-
    -
  • Find a dark location away from city lights
  • -
  • Allow your eyes to adjust to darkness for 20-30 minutes
  • -
  • Look northeast for most showers, but meteors can appear anywhere
  • -
  • Best viewing is typically after midnight
  • -
  • No telescope needed - use naked eye viewing
  • -
  • Check weather conditions and moon phase before planning
  • -
-
- -
-

About Meteor Showers

-

Meteor showers occur when Earth passes through debris trails left by comets or asteroids. The debris burns up in our atmosphere, creating the streaks of light we see as "shooting stars."

- -

Data Sources: Information compiled from NASA, American Meteor Society, and astronomical observations for 2025.

-
-
-{% endblock %} \ No newline at end of file diff --git a/templates/navbar.html b/templates/navbar.html index 373c053..444fa14 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -1,34 +1,12 @@ {% macro navbar() %} -{% set pages_before_dropdowns = [ +{% set pages = [ {"endpoint": "index", "label": "Home" }, - {"endpoint": "recent", "label": "Recent" }, - {"endpoint": "calendar_page", "label": "Calendar" }, -] %} - -{% set pages_after_dropdowns = [ + {"endpoint": "conference_list", "label": "Conference" }, {"endpoint": "travel_list", "label": "Travel" }, {"endpoint": "accommodation_list", "label": "Accommodation" }, {"endpoint": "gaps_page", "label": "Gaps" }, - {"endpoint": "weekends", "label": "Weekends" }, {"endpoint": "launch_list", "label": "Space launches" }, - {"endpoint": "meteor_list", "label": "Meteor showers" }, - {"endpoint": "holiday_list", "label": "Holidays" }, - {"endpoint": "schengen_report", "label": "Schengen" }, - ] + ([{"endpoint": "birthday_list", "label": "Birthdays" }] - if g.user.is_authenticated else []) -%} - -{% set trip_pages = [ - {"endpoint": "trip_future_list", "label": "Future trips" }, - {"endpoint": "trip_past_list", "label": "Past trips" }, - {"endpoint": "trip_stats", "label": "Trip statistics" }, -] %} - -{% set conference_pages = [ - {"endpoint": "conference_list", "label": "Conferences" }, - {"endpoint": "past_conference_list", "label": "Past conferences" }, - {"endpoint": "conference_series_list", "label": "Conference series" }, ] %} @@ -40,7 +18,7 @@
diff --git a/templates/schengen_report.html b/templates/schengen_report.html deleted file mode 100644 index b34c02d..0000000 --- a/templates/schengen_report.html +++ /dev/null @@ -1,230 +0,0 @@ -{% extends "base.html" %} - -{% set heading = "Schengen area compliance report" %} - -{% block title %}{{ heading }} - Edward Betts{% endblock %} - -{% block content %} -
-

{{ heading }}

- -
-
-
-
-
Current Status
-
-
-
-
-
Compliance Status
- {% if current_compliance.is_compliant %} - ✅ COMPLIANT - {% else %} - ❌ NON-COMPLIANT - {% endif %} -
-
-
Days Used
-
{{ current_compliance.total_days_used }}/90
-
-
- -
-
- {% if current_compliance.is_compliant %} -
Days Remaining
-
{{ current_compliance.days_remaining }}
- {% else %} -
Days Over Limit
-
{{ current_compliance.days_over_limit }}
- {% endif %} -
-
- {% if current_compliance.next_reset_date %} -
Next Reset Date
-
{{ current_compliance.next_reset_date.strftime('%Y-%m-%d') }}
- {% endif %} -
-
- -
-
Current 180-day Period
-
- {{ current_compliance.current_180_day_period[0].strftime('%Y-%m-%d') }} to - {{ current_compliance.current_180_day_period[1].strftime('%Y-%m-%d') }} -
-
-
-
- - {% if current_compliance.stays_in_period %} -
-
-
Stays in Current 180-day Period
-
-
-
- - - - - - - - - - - - {% for stay in current_compliance.stays_in_period %} - - - - {% set country = get_country(stay.country) %} - - - - - {% endfor %} - -
Entry DateExit DateCountryDaysTrip
{{ stay.entry_date.strftime('%Y-%m-%d') }} - {% if stay.exit_date %} - {{ stay.exit_date.strftime('%Y-%m-%d') }} - {% else %} - ongoing - {% endif %} - {{ country.flag }} {{ country.name }}{{ stay.days }} - {% if stay.trip_date and stay.trip_name %} - - {{ stay.trip_name }} - - {% else %} - - - {% endif %} -
-
-
-
- {% endif %} -
- -
- {% if warnings %} -
-
-
Warnings
-
-
- {% for warning in warnings %} - - {% endfor %} -
-
- {% endif %} - -
-
-
Compliance History
-
-
-
- -
-
-
-
-
- - {% if trips_with_compliance %} -
-
-
Trip Compliance History
-
-
-
- - - - - - - - - - - - {% for trip_date, calculation in trips_with_compliance.items() %} - - - - - - - - {% endfor %} - -
Trip DateTrip NameDays UsedDays RemainingStatus
{{ trip_date.strftime('%Y-%m-%d') }} - - {% for trip in trip_list if trip.start == trip_date %} - {{ trip.title }} {{ trip.country_flags }} - {% endfor %} - - {{ calculation.total_days_used }}/90{{ calculation.days_remaining }} - {% if calculation.is_compliant %} - Compliant - {% else %} - Non-compliant - {% endif %} -
-
-
-
- {% endif %} -
-{% endblock %} - -{% block scripts %} - - -{% endblock %} diff --git a/templates/show_error.html b/templates/show_error.html index a3b4d63..dd79c3c 100644 --- a/templates/show_error.html +++ b/templates/show_error.html @@ -1,7 +1,5 @@ {% extends "base.html" %} -{% block title %}Agenda error - Edward Betts{% endblock %} - {% block style %} {% endblock %} diff --git a/templates/travel.html b/templates/travel.html index ebb937b..86ae8bc 100644 --- a/templates/travel.html +++ b/templates/travel.html @@ -1,23 +1,23 @@ {% extends "base.html" %} -{% from "macros.html" import flight_booking_row, train_row with context %} -{% block title %}Travel - Edward Betts{% endblock %} +{% block travel %} +{% endblock %} -{% set flight_column_count = 11 %} -{% set column_count = 10 %} +{% macro display_datetime(dt) %}{{ dt.strftime("%a, %d, %b %Y %H:%M %z") }}{% endmacro %} +{% macro display_time(dt) %}{{ dt.strftime("%H:%M %z") }}{% endmacro %} {% block style %} -{% endblock %} - -{% macro section(heading, item_list) %} - {% if item_list %} - {% set items = item_list | list %} -
-

{{ heading }}

-

Trip statistics · {{ items | count }} trips

-
-
- Total distance - {{ format_distance(total_distance) }} -
- {% for transport_type, distance in distances_by_transport_type %} -
- {{ transport_type | title }} - {{ format_distance(distance) }} -
- {% endfor %} -
- Total CO₂ - {{ "{:,.1f}".format(total_co2_kg / 1000.0) }} t -
- {% for transport_type, co2_kg in co2_by_transport_type %} -
- {{ transport_type | title }} CO₂ - {{ "{:,.1f}".format(co2_kg) }} kg -
- {% endfor %} -
-
- - {% for trip in items %} - {{ trip_item(trip) }} - {% endfor %} - {% endif %} -{% endmacro %} - - -{% block content %} -
-
- {{ section(heading, trips) }} -
-
-
-
-
-{% endblock %} - -{% block scripts %} - - - - - - - -{% endblock %} diff --git a/templates/trip/stats.html b/templates/trip/stats.html deleted file mode 100644 index 52d3558..0000000 --- a/templates/trip/stats.html +++ /dev/null @@ -1,217 +0,0 @@ -{% extends "base.html" %} - -{% from "macros.html" import format_distance with context %} - -{% set heading = "Trip statistics" %} - -{% block title %}{{ heading }} - Edward Betts{% endblock %} - -{% block style %} - -{% endblock %} - -{% macro stat_list(id, label, counter, show_top=5) %} -
- {{ label }}: {{ counter | count }} - {% if counter | count > 0 %} - -
-
- {% for item, count in counter.most_common() %} - {{ item }} {{ count }} - {% endfor %} -
-
- {% endif %} -
-{% endmacro %} - -{% macro format_co2(co2_kg) %} - {% if co2_kg >= 1000 %} - {{ "{:,.2f}".format(co2_kg / 1000.0) }} tonnes - {% else %} - {{ "{:,.0f}".format(co2_kg) }} kg - {% endif %} -{% endmacro %} - -{% block content %} -
-

Trip statistics

- -
-
Overall Summary
-
-
-
-
Trips: {{ count }}
-
Conferences: {{ conferences }}
-
Total distance: {{ format_distance(total_distance) }}
- {% for transport_type, distance in distances_by_transport_type %} -
{{ transport_type | title }}: {{ format_distance(distance) }}
- {% endfor %} - {% if total_co2_kg %} -
CO₂: {{ format_co2(total_co2_kg) }}
- {% endif %} - {% for transport_type, co2_kg in co2_by_transport_type %} -
{{ transport_type | title }} CO₂: {{ format_co2(co2_kg) }}
- {% endfor %} -
-
-
Flight segments: {{ overall_stats.flight_count }}
-
Train segments: {{ overall_stats.train_count }}
- {% if co2_by_transport_type %} -
- -
- {% endif %} -
-
- -
- - {{ stat_list("overall-airlines", "Airlines", overall_stats.airlines) }} - {{ stat_list("overall-airports", "Airports", overall_stats.airports) }} - {{ stat_list("overall-stations", "Stations", overall_stats.stations) }} -
-
- - {% for year, year_stats in yearly_stats | dictsort(reverse=True) %} - {% set countries = year_stats.countries | default([]) | sort(attribute="name") %} - {% set new_countries = year_stats.new_countries | default([]) %} - -
-
- {{ year }} - {{ year_stats.count }} trips -
-
-
-
-
Trips: {{ year_stats.count }}
-
Conferences: {{ year_stats.conferences }}
-
Distance: {{ format_distance(year_stats.total_distance or 0) }}
- {% if year_stats.distances_by_transport_type %} - {% for transport_type, distance in year_stats.distances_by_transport_type.items() %} -
{{ transport_type | title }}: {{ format_distance(distance) }}
- {% endfor %} - {% endif %} -
-
-
Flight segments: {{ year_stats.flight_count or 0 }}
-
Train segments: {{ year_stats.train_count or 0 }}
- {% if year_stats.co2_kg %} -
CO₂: {{ format_co2(year_stats.co2_kg) }}
- {% for transport_type, co2_kg in year_stats.co2_by_transport_type.items() %} -
{{ transport_type | title }} CO₂: {{ format_co2(co2_kg) }}
- {% endfor %} -
- -
- {% endif %} -
-
- -
- Countries: {{ countries | count }} - {% if new_countries %}({{ new_countries | count }} new){% endif %} -
- {% for c in countries %} - - {{ c.flag }} {{ c.name }} - {% if c in new_countries %} - new - {% endif %} - - {% endfor %} -
-
- - {% if year_stats.airlines %} - {{ stat_list("airlines-" ~ year, "Airlines", year_stats.airlines) }} - {% endif %} - {% if year_stats.airports %} - {{ stat_list("airports-" ~ year, "Airports", year_stats.airports) }} - {% endif %} - {% if year_stats.stations %} - {{ stat_list("stations-" ~ year, "Stations", year_stats.stations) }} - {% endif %} -
-
- {% endfor %} -
-{% endblock %} - -{% block scripts %} - - -{% endblock %} diff --git a/templates/trip_debug.html b/templates/trip_debug.html deleted file mode 100644 index 9d7cb3f..0000000 --- a/templates/trip_debug.html +++ /dev/null @@ -1,133 +0,0 @@ -{% extends "base.html" %} - -{% block title %}Debug: {{ trip.title }} ({{ trip.start }}) - Edward Betts{% endblock %} - -{% block style %} - -{% endblock %} - -{% block content %} -
-
-

🐛 Trip Debug Information

-

Raw trip object data for: {{ trip.title }}

- ← Back to Trip Page - - -
- -
-
-

Trip Object (JSON)

-
{{ trip_json }}
-
-
-

Trip Object (YAML)

-
{{ trip_yaml }}
-
-
-
-{% endblock %} - -{% block scripts %} - -{% endblock %} diff --git a/templates/trip_list_text.html b/templates/trip_list_text.html deleted file mode 100644 index a5aa5b5..0000000 --- a/templates/trip_list_text.html +++ /dev/null @@ -1,67 +0,0 @@ -{% extends "base.html" %} - -{% from "macros.html" import trip_link, display_date_no_year, display_date, conference_row, accommodation_row, flight_row, train_row with context %} - -{% set row = { "flight": flight_row, "train": train_row } %} - -{% block style %} - - - -{% set conference_column_count = 7 %} -{% set accommodation_column_count = 7 %} -{% set travel_column_count = 8 %} - -{% endblock %} - - -{% block content %} -
- -

Trips

-

{{ future | count }} trips

- {% for trip in future %} - {% set end = trip.end %} -
- {{ display_date_no_year(trip.start) }} to {{ display_date_no_year(end) }}: - {{ trip.title }} — {{ trip.locations_str }} -
- {% endfor %} - - -
-{% endblock %} diff --git a/templates/trip_page.html b/templates/trip_page.html deleted file mode 100644 index 10adf94..0000000 --- a/templates/trip_page.html +++ /dev/null @@ -1,549 +0,0 @@ -{% extends "base.html" %} - -{% block title %}{{ trip.title }} ({{ display_date(trip.start) }}) - Edward Betts{% endblock %} - -{% from "macros.html" import trip_link, display_datetime, display_date_no_year, display_date, display_conf_date_no_year, conference_row, accommodation_row, flight_row, train_row, ferry_row, coach_row, bus_row with context %} - -{% set row = {"flight": flight_row, "train": train_row, "ferry": ferry_row, "coach": coach_row, "bus": bus_row} %} - -{% macro trip_duration(depart, arrive) -%} - {%- if depart.hour is defined and arrive.hour is defined -%} - {%- set mins = ((arrive - depart).total_seconds() // 60) | int -%} - {%- set h = mins // 60 -%} - {%- set m = mins % 60 -%} - {%- if h %}{{ h }}h {% endif -%} - {%- if m %}{{ m }}m{% elif h %}0m{% endif -%} - {%- endif -%} -{%- endmacro %} - -{% macro next_and_previous() %} -

- {% if prev_trip %} - previous: {{ trip_link(prev_trip) }} ({{ (trip.start - prev_trip.end).days }} days) - {% endif %} - {% if next_trip %} - next: {{ trip_link(next_trip) }} ({{ (next_trip.start - trip.end).days }} days) - {% endif %} -

-{% endmacro %} - -{% block style %} - -{% if coordinates or routes %} - -{% endif %} - - -{% set conference_column_count = 7 %} -{% set accommodation_column_count = 7 %} -{% set travel_column_count = 9 %} - -{% endblock %} - -{% set end = trip.end %} -{% set total_distance = trip.total_distance() %} -{% set distances_by_transport_type = trip.distances_by_transport_type() %} -{% set total_co2_kg = trip.total_co2_kg() %} -{% set co2_by_transport_type = trip.co2_by_transport_type() %} - -{% block content %} -
-
-
-
{{ next_and_previous() }}
-

{{ trip.title }}

-

- {% if end %} - {{ display_date_no_year(trip.start) }} to {{ display_date_no_year(end) }} - ({{ (end - trip.start).days }} nights) - {% else %} - {{ display_date_no_year(trip.start) }} (end date missing) - {% endif %} -

- -
-
    - {% for location, country in trip.locations() %} -
  • {{ country.flag if trip.show_flags }} {{ location }}
  • - {% endfor %} -
- {% if destination_times %} -
- Destination time zones - - - - - - - - - - {% for item in destination_times %} - - - - - - {% endfor %} - -
DestinationTimezoneDifference from UK
{{ item.destination_label }}{{ item.timezone or "Unknown" }}{{ item.offset_display }}
-
- {% endif %} - -
- {% if total_distance %} - {{ "{:,.0f} km / {:,.0f} mi".format(total_distance, total_distance / 1.60934) }} - {% endif %} - {% if distances_by_transport_type %} - {% for transport_type, distance in distances_by_transport_type %} - {{ transport_type | title }}: {{ "{:,.0f} km".format(distance) }} - {% endfor %} - {% endif %} - {% if total_co2_kg %} - CO₂ {{ "{:,.1f}".format(total_co2_kg) }} kg - {% endif %} - {% if co2_by_transport_type %} - {% for transport_type, co2_kg in co2_by_transport_type %} - {{ transport_type | title }} CO₂ {{ "{:,.1f}".format(co2_kg) }} kg - {% endfor %} - {% endif %} -
- - - {% set delta = human_readable_delta(trip.start) %} - {% if delta %} -
How long until trip: {{ delta }}
- {% endif %} - - {% if trip.schengen_compliance %} -
- Schengen Compliance: - {% if trip.schengen_compliance.is_compliant %} - ✅ Compliant - {% else %} - ❌ Non-compliant - {% endif %} -
- {{ trip.schengen_compliance.total_days_used }}/90 days used - {% if trip.schengen_compliance.is_compliant %} - ({{ trip.schengen_compliance.days_remaining }} remaining) - {% else %} - ({{ trip.schengen_compliance.days_over_limit }} over limit) - {% endif %} -
-
- {% endif %} -
- - {# ---- Chronological itinerary ---- #} - {% for day, day_elements in trip.elements_grouped_by_day() %} - {% set weather = trip_weather.get(day.isoformat()) if trip_weather else None %} -

- {{ display_date_no_year(day) }} - {% if weather %} - - {{ weather.status }} - {{ weather.temp_min }}–{{ weather.temp_max }}°C - {{ weather.detailed_status }} - - {% endif %} - {% if g.user.is_authenticated and day <= today %} - photos - {% endif %} -

- - {% for e in day_elements %} - - {% if e.element_type == "conference" %} - {% set item = e.detail %} - {% set country = get_country(item.country) if item.country else None %} -
-
-
- {{ item.name }} - - {{ display_conf_date_no_year(item.attend_start if item.attend_start else item.start) }} to {{ display_conf_date_no_year(item.attend_end if item.attend_end else item.end) }} - {% if item.attend_start or item.attend_end %} - (full conference: {{ display_date_no_year(item.start) }} to {{ display_date_no_year(item.end) }}) - {% endif %} - -
-

- Topic: {{ item.topic }} - Venue: {{ item.venue }} - Location: {{ item.location }} - {% if country %} - {{ country.flag if trip.show_flags }} - {% elif item.online %} - 💻 Online - {% else %} - country code {{ item.country }} not found - {% endif %} - {% if item.free %} - free to attend - {% elif item.price and item.currency %} - price: {{ item.price }} {{ item.currency }} - {% endif %} - {% set free_days = conference_free_days.get(item.start | string) %} - {% if free_days %} - {% set days_before, days_after = free_days %} - {% if days_before > 0 %} - {{ days_before }} day{{ 's' if days_before != 1 }} to explore before - {% endif %} - {% if days_after > 0 %} - {{ days_after }} day{{ 's' if days_after != 1 }} to explore after - {% endif %} - {% endif %} -

-
-
- - {% elif e.element_type == "check-in" %} - {% set item = e.detail %} - {% set country = get_country(item.country) if item.country else None %} - {% set nights = (item.to.date() - item.from.date()).days %} -
-
-
- {{ e.get_emoji() }} - {{ item.name }} - {% if item.operator and item.operator != item.name %}{{ item.operator }}{% endif %} - ({% if nights == 1 %}1 night{% else %}{{ nights }} nights{% endif %}) -
-

- {{ item.location }} - {% if country %} - {{ country.flag if trip.show_flags }} - {% else %} - country code {{ item.country }} not found - {% endif %} - {% if item.address %} · {{ item.address }}{% endif %} - {% if g.user.is_authenticated and item.price and item.currency %} - {{ item.price }} {{ item.currency }} - {% endif %} -

-
-
- - {% elif e.element_type == "check-out" %} - {% set item = e.detail %} -
- {{ e.get_emoji() }} Check out: {{ item.name }} - {% if item.operator and item.operator != item.name %}{{ item.operator }}{% endif %} -
- - {% elif e.element_type == "flight" %} - {% set item = e.detail %} - {% set full_flight_number = item.airline_code + item.flight_number %} - {% set radarbox_url = "https://www.radarbox.com/data/flights/" + full_flight_number %} - {% set depart_date = item.depart.date() if item.depart.hour is defined else item.depart %} - {% set arrive_date = item.arrive.date() if (item.arrive and item.arrive.hour is defined) else item.arrive %} - {% set is_overnight = item.arrive and depart_date != arrive_date %} -
-
-
- ✈️ - {{ item.from_airport.name }} ({{ item.from_airport.iata }}) - → - {{ item.to_airport.name }} ({{ item.to_airport.iata }}) -
-
-
- {{ item.airline_name }} - {{ full_flight_number }} - · {{ item.depart.strftime("%H:%M") }} - {% if item.arrive %} - → {{ item.arrive.strftime("%H:%M") }}{% if is_overnight %} +1 day{% endif %} - 🕒{{ trip_duration(item.depart, item.arrive) }} - {% endif %} - {% if item.distance %} - 🌍 {{ "{:,.0f} km".format(item.distance) }} - {% endif %} - {% if item.co2_kg is defined and item.co2_kg is not none %} - CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg - {% endif %} -
- -
-
-
- - {% elif e.element_type == "train" %} - {% set item = e.detail %} - {% set has_time = item.depart.hour is defined and item.arrive.hour is defined %} - {% set depart_date = item.depart.date() if has_time else item.depart %} - {% set arrive_date = item.arrive.date() if has_time else item.arrive %} - {% set is_overnight = has_time and depart_date != arrive_date %} -
-
-
- {% if is_overnight %}🌙{% else %}🚆{% endif %} - {{ item.from }} → {{ item.to }} - {% if item.operator %}{{ item.operator }}{% endif %} - {% if is_overnight %}Night train{% endif %} -
-

- {% if has_time %} - {{ item.depart.strftime("%H:%M") }} - → {{ item.arrive.strftime("%H:%M") }}{% if is_overnight %} +1 day{% endif %} - {% endif %} - {% if item.class %} - {{ item.class }} - {% endif %} - {% if has_time %} - 🕒{{ trip_duration(item.depart, item.arrive) }} - {% endif %} - {% if item.distance %} - 🛤️ {{ "{:,.0f} km".format(item.distance) }} - {% endif %} - {% if item.co2_kg is defined and item.co2_kg is not none %} - CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg - {% endif %} - {% if item.coach %} - {% if is_overnight %}🛏️{% else %}💺{% endif %} Coach {{ item.coach }}{% if item.seat %}, Seat {% if item.seat is iterable and item.seat is not string %}{{ item.seat | join(" & ") }}{% else %}{{ item.seat }}{% endif %}{% endif %} - {% endif %} -

-
-
- - {% elif e.element_type in ("coach", "bus", "car") %} - {% set item = e.detail %} - {% set display_depart = item.display_depart if item.display_depart is defined else item.depart %} - {% set display_arrive = item.display_arrive if item.display_arrive is defined else item.arrive %} -
-
-
- {% if e.element_type == "car" %}🚗{% else %}🚌{% endif %} - {{ item.from }} → {{ item.to }} - {% if item.operator %}{{ item.operator }}{% endif %} -
-

- {% if display_depart.hour is defined and display_arrive.hour is defined %} - {{ display_depart.strftime("%H:%M") }} → {{ display_arrive.strftime("%H:%M") }} - {% endif %} - {% if item.class %} - {{ item.class }} - {% endif %} - {% if display_depart.hour is defined and display_arrive.hour is defined %} - 🕒{{ trip_duration(display_depart, display_arrive) }} - {% endif %} - {% if item.distance %} - 🛤️ {{ "{:,.0f} km".format(item.distance) }} - {% endif %} - {% if item.co2_kg is defined and item.co2_kg is not none %} - CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg - {% endif %} -

-
-
- - {% elif e.element_type == "ferry" %} - {% set item = e.detail %} -
-
-
- ⛴️ {{ item.from }} → {{ item.to }} - {{ item.operator }}{% if item.ferry %} · {{ item.ferry }}{% endif %} -
-

-

- {{ item.depart.strftime("%H:%M") }} → {{ item.arrive.strftime("%H:%M") }} - 🕒{{ trip_duration(item.depart, item.arrive) }} - {% if item.class %} - {{ item.class }} - {% endif %} - {% if item.co2_kg is defined and item.co2_kg is not none %} - CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg - {% endif %} -
- {% if item.vehicle %} -
🚗 Vehicle: {{ item.vehicle.type }} {% if g.user.is_authenticated %}({{ item.vehicle.registration }}) {% endif %} - {% if item.vehicle.extras %} - Extras: {{ item.vehicle.extras | join(", ") }}{% endif %} -
- {% endif %} - {% if g.user.is_authenticated %} -
- {% if item.booking_reference %}Booking reference: {{ item.booking_reference }}{% endif %} - {% if item.price and item.currency %}Price: {{ item.price }} {{ item.currency }}{% endif %} -
- {% endif %} -

-
-
- - {% endif %} - {% endfor %} - {% endfor %} - - {% if trip.flight_bookings %} -

Flight bookings

- {% for item in trip.flight_bookings %} -
- {{ item.flights | map(attribute="airline_name") | unique | join(" + ") }} - {% if g.user.is_authenticated and item.booking_reference %} - booking reference: {{ item.booking_reference }} - {% endif %} - {% if g.user.is_authenticated and item.price and item.currency %} - price: {{ item.price }} {{ item.currency }} - {% endif %} -
- {% endfor %} - {% endif %} - - {% if trip.events %} -

Events

- {% for item in trip.events %} - {% set country = get_country(item.country) if item.country else None %} -
-
-
- {{ item.title }} - {{ display_date_no_year(item.date) }} -
-

- Address: {{ item.address }} - | Location: {{ item.location }} - {% if country %} - {{ country.flag if trip.show_flags }} - {% else %} - country code {{ item.country }} not found - {% endif %} - {% if g.user.is_authenticated and item.price and item.currency %} - | price: {{ item.price }} {{ item.currency }} - {% endif %} -

-
-
- {% endfor %} - {% endif %} - -
-

Holidays

- {% if holidays %} - - {% for item in holidays %} - {% set country = get_country(item.country) %} - - {% if loop.first or item.date != loop.previtem.date %} - - {% else %} - - {% endif %} - - - - {% endfor %} -
{{ display_date(item.date) }}{{ country.flag if trip.show_flags }} {{ country.name }}{{ item.display_name }}
- {% else %} -

No public holidays during trip.

- {% endif %} -
- -
-

UK school holidays (Bristol)

- {% if school_holidays %} - - {% for item in school_holidays %} - - - - - - {% endfor %} -
{{ display_date(item.as_date) }}to {{ display_date(item.end_as_date) }}{{ item.title }}
- {% else %} -

No UK school holidays during trip.

- {% endif %} -
- - {{ next_and_previous() }} - -
-
-{% if coordinates or routes %} -
- -
-
-
-{% endif %} -
-{% endblock %} - -{% block scripts %} -{% if coordinates or routes %} - - - - - - -{% endif %} -{% endblock %} diff --git a/templates/weekends.html b/templates/weekends.html deleted file mode 100644 index 665d2c9..0000000 --- a/templates/weekends.html +++ /dev/null @@ -1,71 +0,0 @@ -{% extends "base.html" %} -{% block title %}Weekends - Edward Betts{% endblock %} - -{% block content %} -
- -

Weekends

- - - - - - - - - - - - - - - - {% set current_isocalendar = today.isocalendar() %} - - {% for weekend in items %} - {% set week_number = weekend.date.isocalendar().week %} - {% set iso_week_year = weekend.date.isocalendar().year %} - {% if week_number == current_week_number and iso_week_year == current_isocalendar.year %} - {% set extra_class = " bg-warning-subtle" %} - {% else %} - {% set extra_class = "" %} - {% endif %} - - - - {% for day in "saturday", "sunday" %} - {% if extra_class %} - {% if extra_class %} - {% if extra_class %} - {% endfor %} - - {% endfor %} - -
WeekDateSaturdaySaturday LocationSaturday WeatherSundaySunday LocationSunday Weather
- {{ week_number }} - - {{ weekend.date.strftime("%-d %b %Y") }} - {% else %}{% endif %} - {% if weekend[day] %} - {% for event in weekend[day] %} - {{ event.title }}{% if not loop.last %},{%endif %} - {% endfor %} - {% else %} - free - {% endif %} - {% else %}{% endif %} - {% set city, country = weekend[day + '_location'] %} - {% if city %} - {{ city }}, {{ country.flag }} {{ country.name }} - {% endif %} - {% else %}{% endif %} - {% set w = weekend[day + '_weather'] %} - {% if w %} - {{ w.status }} - {{ w.temp_min }}–{{ w.temp_max }}°C - {% endif %} -
-
- -{% endblock %} - diff --git a/tests/test_accommodation.py b/tests/test_accommodation.py deleted file mode 100644 index 451cbc6..0000000 --- a/tests/test_accommodation.py +++ /dev/null @@ -1,186 +0,0 @@ -"""Tests for accommodation functionality.""" - -import tempfile -from datetime import date, datetime -from typing import Any - -import pytest -import yaml - -from agenda.accommodation import get_events -from agenda.event import Event - - -class TestGetEvents: - """Test the get_events function.""" - - def test_get_events_airbnb(self) -> None: - """Test getting accommodation events for Airbnb.""" - accommodation_data = [ - { - "from": date(2024, 6, 1), - "to": date(2024, 6, 5), - "location": "Paris", - "operator": "airbnb", - "url": "https://airbnb.com/rooms/123" - } - ] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(accommodation_data, f) - filepath = f.name - - try: - events = get_events(filepath) - - assert len(events) == 1 - event = events[0] - assert event.date == date(2024, 6, 1) - assert event.end_date == date(2024, 6, 5) - assert event.name == "accommodation" - assert event.title == "Paris Airbnb" - assert event.url == "https://airbnb.com/rooms/123" - - finally: - import os - os.unlink(filepath) - - def test_get_events_hotel(self) -> None: - """Test getting accommodation events for hotel.""" - accommodation_data = [ - { - "from": date(2024, 6, 1), - "to": date(2024, 6, 5), - "name": "Hilton Hotel", - "url": "https://hilton.com" - } - ] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(accommodation_data, f) - filepath = f.name - - try: - events = get_events(filepath) - - assert len(events) == 1 - event = events[0] - assert event.date == date(2024, 6, 1) - assert event.end_date == date(2024, 6, 5) - assert event.name == "accommodation" - assert event.title == "Hilton Hotel" - assert event.url == "https://hilton.com" - - finally: - import os - os.unlink(filepath) - - def test_get_events_no_url(self) -> None: - """Test getting accommodation events without URL.""" - accommodation_data = [ - { - "from": date(2024, 6, 1), - "to": date(2024, 6, 5), - "name": "Local B&B" - } - ] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(accommodation_data, f) - filepath = f.name - - try: - events = get_events(filepath) - - assert len(events) == 1 - event = events[0] - assert event.url is None - assert event.title == "Local B&B" - - finally: - import os - os.unlink(filepath) - - def test_get_events_multiple_accommodations(self) -> None: - """Test getting multiple accommodation events.""" - accommodation_data = [ - { - "from": date(2024, 6, 1), - "to": date(2024, 6, 5), - "location": "London", - "operator": "airbnb" - }, - { - "from": date(2024, 6, 10), - "to": date(2024, 6, 15), - "name": "Royal Hotel" - } - ] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(accommodation_data, f) - filepath = f.name - - try: - events = get_events(filepath) - - assert len(events) == 2 - - # Check first accommodation (Airbnb) - assert events[0].title == "London Airbnb" - assert events[0].date == date(2024, 6, 1) - - # Check second accommodation (Hotel) - assert events[1].title == "Royal Hotel" - assert events[1].date == date(2024, 6, 10) - - finally: - import os - os.unlink(filepath) - - def test_get_events_empty_file(self) -> None: - """Test getting events from empty file.""" - accommodation_data: list[dict[str, Any]] = [] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(accommodation_data, f) - filepath = f.name - - try: - events = get_events(filepath) - assert events == [] - - finally: - import os - os.unlink(filepath) - - def test_get_events_file_not_found(self) -> None: - """Test error handling when file doesn't exist.""" - with pytest.raises(FileNotFoundError): - get_events("/nonexistent/file.yaml") - - def test_get_events_datetime_objects(self) -> None: - """Test with datetime objects instead of dates.""" - accommodation_data = [ - { - "from": datetime(2024, 6, 1, 15, 0), - "to": datetime(2024, 6, 5, 11, 0), - "name": "Hotel Example" - } - ] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(accommodation_data, f) - filepath = f.name - - try: - events = get_events(filepath) - - assert len(events) == 1 - event = events[0] - assert event.date == datetime(2024, 6, 1, 15, 0) - assert event.end_date == datetime(2024, 6, 5, 11, 0) - - finally: - import os - os.unlink(filepath) \ No newline at end of file diff --git a/tests/test_add_new_conference.py b/tests/test_add_new_conference.py deleted file mode 100644 index 8038724..0000000 --- a/tests/test_add_new_conference.py +++ /dev/null @@ -1,486 +0,0 @@ -"""Tests for agenda.add_new_conference.""" - -from datetime import date, datetime -import typing - -import lxml.html -import pytest -import yaml - -from agenda import add_new_conference -from agenda.conference import ConferenceSeries - - -def test_parse_osm_url_mlat_mlon() -> None: - """OpenStreetMap URLs with mlat/mlon should parse.""" - result = add_new_conference.parse_osm_url( - "https://www.openstreetmap.org/?mlat=51.5&mlon=-0.12" - ) - assert result == (51.5, -0.12) - - -def test_extract_google_maps_latlon_at_pattern() -> None: - """Google Maps @lat,lon URLs should parse.""" - result = add_new_conference.extract_google_maps_latlon( - "https://www.google.com/maps/place/Venue/@51.5242464,-0.0997024,17z/" - ) - assert result == (51.5242464, -0.0997024) - - -def test_url_has_year_component() -> None: - """Only actual year or edition components should count as year-specific.""" - cases = [ - ("https://www.foss4gna.org/", False), - ("https://foss4g.asia/2026/", True), - ("https://2027.fossy.ca/", True), - ("https://www.socallinuxexpo.org/scale/24x/", True), - ("https://2026.stateofthebrowser.com/", True), - ] - for url, expected in cases: - assert add_new_conference.url_has_year_component(url) is expected - - -def test_insert_sorted_allows_same_url_different_year_without_year_component() -> None: - """The same non-year-specific URL can be reused for a different year.""" - conferences: list[dict[str, typing.Any]] = [ - { - "name": "OldConf", - "start": date(2025, 6, 1), - "url": "https://example.com/conf", - } - ] - new_conf: dict[str, typing.Any] = { - "name": "NewConf", - "start": date(2026, 6, 1), - "url": "https://example.com/conf", - } - - updated = add_new_conference.insert_sorted(conferences, new_conf) - - assert len(updated) == 2 - assert updated[1]["name"] == "NewConf" - - -def test_insert_sorted_supports_nested_dates() -> None: - """Nested dates should be used for sorting.""" - conferences: list[dict[str, typing.Any]] = [ - { - "name": "PyCascades", - "dates": { - "status": "approximate", - "label": "March 2027", - "earliest": date(2027, 3, 1), - "latest": date(2027, 3, 31), - }, - } - ] - new_conf: dict[str, typing.Any] = { - "name": "FOSDEM", - "dates": { - "status": "tentative", - "start": date(2027, 1, 30), - "end": date(2027, 1, 31), - }, - } - - updated = add_new_conference.insert_sorted(conferences, new_conf) - - assert [conf["name"] for conf in updated] == ["FOSDEM", "PyCascades"] - - -def test_insert_sorted_updates_inexact_existing_entry() -> None: - """Exact dates should replace an existing inexact series entry.""" - conferences: list[dict[str, typing.Any]] = [ - { - "name": "PyCascades", - "series": "pycascades", - "topic": "Python", - "location": "Seattle, Washington", - "dates": { - "status": "approximate", - "label": "March 2027", - "earliest": date(2027, 3, 1), - "latest": date(2027, 3, 31), - }, - "url": "https://2027.pycascades.com/", - } - ] - new_conf: dict[str, typing.Any] = { - "name": "PyCascades", - "series": "pycascades", - "topic": "Python", - "location": "Seattle, Washington", - "dates": { - "status": "exact", - "start": date(2027, 3, 12), - "end": date(2027, 3, 14), - }, - "url": "https://2027.pycascades.com/", - "venue": "Example Hall", - } - - updated = add_new_conference.insert_sorted(conferences, new_conf) - - assert len(updated) == 1 - assert updated[0]["dates"]["status"] == "exact" - assert updated[0]["dates"]["start"] == date(2027, 3, 12) - assert updated[0]["venue"] == "Example Hall" - - -def test_normalize_dates_field_moves_legacy_dates() -> None: - """Legacy start/end model output should be converted before writing YAML.""" - conf: dict[str, typing.Any] = { - "name": "PyCon", - "start": date(2026, 4, 10), - "end": date(2026, 4, 12), - } - - add_new_conference.normalize_dates_field(conf) - - assert "start" not in conf - assert "end" not in conf - assert conf["dates"] == { - "status": "exact", - "start": date(2026, 4, 10), - "end": date(2026, 4, 12), - } - - -def test_normalize_dates_field_parses_quoted_dates() -> None: - """Quoted ISO dates from generated YAML should become date objects.""" - conf: dict[str, typing.Any] = { - "name": "Git Merge", - "dates": { - "status": "exact", - "start": "2026-09-16", - "end": "2026-09-17", - }, - } - - add_new_conference.normalize_dates_field(conf) - - assert conf["dates"]["start"] == date(2026, 9, 16) - assert conf["dates"]["end"] == date(2026, 9, 17) - - -def test_validate_generated_conference_reports_missing_dates() -> None: - """Missing generated dates should raise a clear importer error.""" - conf: dict[str, typing.Any] = { - "name": "Git Merge", - "topic": "Git", - "location": "TBC", - } - - with pytest.raises(ValueError, match="missing valid date information"): - add_new_conference.validate_generated_conference(conf) - - -def test_webpage_to_text_includes_hidden_event_metadata() -> None: - """Schema.org event metadata should be included even when visually hidden.""" - root = lxml.html.fromstring(""" - -
- - -
- - """) - - text = add_new_conference.webpage_to_text(root) - - assert "- name: State of the Map Asia 2026 OSAKA" in text - assert "- startDate: 2026-09-06T18:30" in text - - -def test_apply_event_metadata_dates_fills_missing_generated_dates() -> None: - """Structured page dates should recover incomplete model output.""" - root = lxml.html.fromstring(""" -
- -
- """) - conf: dict[str, typing.Any] = {"name": "State of the Map Asia 2026"} - - add_new_conference.apply_event_metadata_dates(conf, root) - - assert conf["dates"] == { - "status": "exact", - "start": datetime(2026, 9, 6, 18, 30), - "end": datetime(2026, 9, 6, 18, 30), - } - - -def test_build_prompt_includes_nested_dates_and_series() -> None: - """The prompt should describe nested dates and known series IDs.""" - prompt = add_new_conference.build_prompt( - "https://example.com", - "Conference details", - None, - { - "pycascades": { - "name": "PyCascades", - "topic": "Python", - "usual_location": "Seattle, Washington", - "country": "us", - } - }, - ) - - assert "Do not output legacy top-level `start`, `end`, or `date_status`" in prompt - assert "dates.status" in prompt - assert "- pycascades: PyCascades" in prompt - assert "March 2027" in prompt - assert "For an address written in a non-Latin script" in prompt - assert "If no reliable rendering is available, omit" in prompt - assert "When `free: true`, omit `price` and `currency`" in prompt - - -def test_validate_country_normalises_name() -> None: - """Country names should be normalised to alpha-2 codes.""" - conf: dict[str, typing.Any] = {"country": "United Kingdom"} - - add_new_conference.validate_country(conf) - - assert conf["country"] == "gb" - - -def test_validate_series_rejects_unknown_generated_id() -> None: - """Generated series IDs must exist in conference_series.yaml.""" - conf: dict[str, typing.Any] = {"series": "state-of-the-map-asia"} - - with pytest.raises(ValueError, match="unknown series 'state-of-the-map-asia'"): - add_new_conference.validate_series( - conf, - {"state-of-the-map": {"name": "State of the Map"}}, - ) - - -def test_validate_series_accepts_known_id_or_missing_field() -> None: - """Known series IDs and conferences without a series should pass.""" - series: dict[str, ConferenceSeries] = { - "state-of-the-map": {"name": "State of the Map"} - } - - add_new_conference.validate_series({"series": "state-of-the-map"}, series) - add_new_conference.validate_series({}, series) - - -def test_normalize_free_event_fields_removes_price_and_currency() -> None: - """Free events should not retain redundant pricing fields.""" - conf: dict[str, typing.Any] = { - "free": True, - "price": 0, - "currency": "JPY", - } - - add_new_conference.normalize_free_event_fields(conf) - - assert conf == {"free": True} - - -def test_normalize_free_event_fields_keeps_paid_event_pricing() -> None: - """Pricing fields should remain for events that are not marked free.""" - conf: dict[str, typing.Any] = { - "free": False, - "price": 25, - "currency": "GBP", - } - - add_new_conference.normalize_free_event_fields(conf) - - assert conf == {"free": False, "price": 25, "currency": "GBP"} - - -def test_normalise_end_field_defaults_single_day_date() -> None: - """Non-Geomob conferences should default end to the start date.""" - conf: dict[str, typing.Any] = { - "name": "PyCon", - "start": date(2026, 4, 10), - } - - add_new_conference.normalise_end_field(conf, "plain text") - - assert conf["end"] == date(2026, 4, 10) - - -def test_normalise_end_field_defaults_nested_exact_date() -> None: - """Nested exact dates should get a default end date.""" - conf: dict[str, typing.Any] = { - "name": "PyCon", - "dates": { - "status": "exact", - "start": date(2026, 4, 10), - }, - } - - add_new_conference.normalise_end_field(conf, "plain text") - - assert conf["dates"]["end"] == date(2026, 4, 10) - - -def test_normalise_end_field_sets_geomob_end_time() -> None: - """Geomob conferences should default to a 22:00 end time.""" - conf: dict[str, typing.Any] = { - "name": "Geomob London", - "start": date(2026, 1, 28), - "url": "https://thegeomob.com/post/jan-28th-2026-geomoblon-details", - } - - add_new_conference.normalise_end_field(conf, "see you there") - - assert conf["end"] == datetime(2026, 1, 28, 22, 0) - - -def test_detect_page_coordinates_uses_first_supported_link() -> None: - """Page coordinate detection should inspect anchor hrefs.""" - root = lxml.html.fromstring( - ( - "" - 'Example' - 'Map' - "" - ) - ) - - assert add_new_conference.detect_page_coordinates(root) == (51.5, -0.12) - - -def test_add_new_conference_updates_yaml( - tmp_path: typing.Any, monkeypatch: pytest.MonkeyPatch -) -> None: - """The end-to-end import flow should append a generated conference.""" - yaml_path = tmp_path / "conferences.yaml" - yaml_path.write_text( - yaml.dump( - [ - { - "name": "ExistingConf", - "start": date(2026, 4, 1), - "end": date(2026, 4, 2), - "url": "https://example.com/existing", - } - ], - sort_keys=False, - ) - ) - - root = lxml.html.fromstring( - ( - "" - 'Map' - "" - ) - ) - - monkeypatch.setattr(add_new_conference, "fetch_webpage", lambda url: root) - monkeypatch.setattr( - add_new_conference, - "webpage_to_text", - lambda parsed: "Conference details", - ) - monkeypatch.setattr( - add_new_conference, - "get_from_open_ai", - lambda prompt: { - "yaml": yaml.dump( - { - "name": "NewConf", - "topic": "Tech", - "location": "New York", - "country": "United States", - "start": date(2026, 5, 3), - "url": "https://example.com/newconf", - }, - sort_keys=False, - ) - }, - ) - - added = add_new_conference.add_new_conference( - "https://example.com/newconf", str(yaml_path) - ) - - assert added is True - written = yaml.safe_load(yaml_path.read_text()) - assert len(written) == 2 - assert written[1]["name"] == "NewConf" - assert written[1]["country"] == "us" - assert written[1]["dates"] == { - "status": "exact", - "start": date(2026, 5, 3), - "end": date(2026, 5, 3), - } - assert written[1]["latitude"] == 40.0 - assert written[1]["longitude"] == -74.0 - - -def test_add_new_conference_reuses_generic_url_for_new_year( - tmp_path: typing.Any, monkeypatch: pytest.MonkeyPatch -) -> None: - """Generic URLs with digits in the domain should not be skipped early.""" - yaml_path = tmp_path / "conferences.yaml" - (tmp_path / "conference_series.yaml").write_text( - yaml.dump( - {"foss4g-north-america": {"name": "FOSS4G North America"}}, - sort_keys=False, - ) - ) - yaml_path.write_text( - yaml.dump( - [ - { - "name": "FOSS4G North America", - "series": "foss4g-north-america", - "dates": { - "status": "exact", - "start": date(2025, 11, 3), - "end": date(2025, 11, 5), - }, - "url": "https://www.foss4gna.org/", - } - ], - sort_keys=False, - ) - ) - - root = lxml.html.fromstring("Conference details") - monkeypatch.setattr(add_new_conference, "fetch_webpage", lambda url: root) - monkeypatch.setattr( - add_new_conference, - "webpage_to_text", - lambda parsed: "FOSS4G North America 2026", - ) - monkeypatch.setattr( - add_new_conference, "detect_page_coordinates", lambda parsed: None - ) - monkeypatch.setattr( - add_new_conference, - "get_from_open_ai", - lambda prompt: { - "yaml": yaml.dump( - { - "name": "FOSS4G North America", - "series": "foss4g-north-america", - "topic": "Geospatial", - "location": "St. Louis, Missouri", - "country": "us", - "dates": { - "status": "exact", - "start": date(2026, 10, 26), - "end": date(2026, 10, 29), - }, - "url": "https://www.foss4gna.org/", - }, - sort_keys=False, - ) - }, - ) - - added = add_new_conference.add_new_conference( - "https://www.foss4gna.org/", str(yaml_path) - ) - - assert added is True - written = yaml.safe_load(yaml_path.read_text()) - assert len(written) == 2 - assert [conf["dates"]["start"].year for conf in written] == [2025, 2026] diff --git a/tests/test_agenda.py b/tests/test_agenda.py index 78484cb..fd5ce5d 100644 --- a/tests/test_agenda.py +++ b/tests/test_agenda.py @@ -1,149 +1,80 @@ -"""Tests for agenda.""" - import datetime -import json from decimal import Decimal -from unittest.mock import patch import pytest -from agenda import format_list_with_ampersand, get_country, uk_time -from agenda.data import event_sort_datetime, timezone_transition -from agenda.economist import publication_dates -from agenda.event import Event -from agenda.fx import get_gbpusd -from agenda.holidays import get_all -from agenda.uk_holiday import bank_holiday_list, get_mothers_day -from agenda.utils import timedelta_display +from agenda import ( + get_gbpusd, + get_next_bank_holiday, + get_next_timezone_transition, + next_economist, + next_uk_fathers_day, + next_uk_mothers_day, + timedelta_display, + uk_financial_year_end, +) -@pytest.fixture # type: ignore[untyped-decorator] -def mock_today() -> datetime.date: - """Mock the current date for testing purposes.""" +@pytest.fixture +def mock_today(): + # Mock the current date for testing purposes return datetime.date(2023, 10, 5) -@pytest.fixture # type: ignore[untyped-decorator] -def mock_now() -> datetime.datetime: - """Mock the current date and time for testing purposes.""" +@pytest.fixture +def mock_now(): + # Mock the current date and time for testing purposes return datetime.datetime(2023, 10, 5, 12, 0, 0) -def test_get_mothers_day(mock_today: datetime.date) -> None: - """Test get_mothers_day function.""" - mothers_day = get_mothers_day(mock_today) - # UK Mother's Day 2024 is April 21st (3 weeks after Easter) - assert mothers_day == datetime.date(2024, 4, 21) +def test_next_uk_mothers_day(mock_today): + # Test next_uk_mothers_day function + next_mothers_day = next_uk_mothers_day(mock_today) + assert next_mothers_day == datetime.date(2024, 4, 21) -def test_timezone_transition(mock_now: datetime.datetime) -> None: - """Test timezone_transition function.""" - start = datetime.datetime(2023, 10, 1) - end = datetime.datetime(2023, 11, 1) - transitions = timezone_transition(start, end, "uk_clock_change", "Europe/London") - assert len(transitions) == 1 - assert transitions[0].name == "uk_clock_change" - assert transitions[0].date.date() == datetime.date(2023, 10, 29) +def test_next_uk_fathers_day(mock_today): + # Test next_uk_fathers_day function + next_fathers_day = next_uk_fathers_day(mock_today) + assert next_fathers_day == datetime.date(2024, 6, 21) -def test_event_sort_datetime_handles_mixed_timezone_awareness() -> None: - """Event sort keys should be comparable for date, naive, and aware datetimes.""" - events = [ - Event( - name="aware", - date=datetime.datetime( - 2026, 1, 1, 9, 0, tzinfo=datetime.timezone(datetime.timedelta(hours=1)) - ), - ), - Event(name="date", date=datetime.date(2026, 1, 1)), - Event(name="naive", date=datetime.datetime(2026, 1, 1, 8, 30)), - ] - - sorted_events = sorted(events, key=lambda e: (event_sort_datetime(e), e.name)) - - assert [event.name for event in sorted_events] == ["date", "aware", "naive"] +def test_get_next_timezone_transition(mock_now) -> None: + # Test get_next_timezone_transition function + next_transition = get_next_timezone_transition(mock_now, "Europe/London") + assert next_transition == datetime.date(2023, 10, 29) -def test_get_gbpusd_function_exists() -> None: - """Test that get_gbpusd function exists and is callable.""" - # Simple test to verify the function exists and has correct signature - from inspect import signature - - sig = signature(get_gbpusd) - assert len(sig.parameters) == 1 - assert "config" in sig.parameters - assert sig.return_annotation == Decimal +def test_get_next_bank_holiday(mock_today) -> None: + # Test get_next_bank_holiday function + next_holiday = get_next_bank_holiday(mock_today)[0] + assert next_holiday.date == datetime.date(2023, 12, 25) + assert next_holiday.title == "Christmas Day" -def test_publication_dates(mock_today: datetime.date) -> None: - """Test publication_dates function.""" - start_date = mock_today - end_date = mock_today + datetime.timedelta(days=30) - publications = publication_dates(start_date, end_date) - assert len(publications) >= 0 # Should return some publications - if publications: - assert all(pub.name == "economist" for pub in publications) +def test_get_gbpusd(mock_now): + # Test get_gbpusd function + gbpusd = get_gbpusd() + assert isinstance(gbpusd, Decimal) + # You can add more assertions based on your specific use case. -def test_timedelta_display() -> None: - """Test timedelta_display function.""" +def test_next_economist(mock_today): + # Test next_economist function + next_publication = next_economist(mock_today) + assert next_publication == datetime.date(2023, 10, 5) + + +def test_uk_financial_year_end(): + # Test uk_financial_year_end function + financial_year_end = uk_financial_year_end(datetime.date(2023, 4, 1)) + assert financial_year_end == datetime.date(2023, 4, 5) + + +def test_timedelta_display(): + # Test timedelta_display function delta = datetime.timedelta(days=2, hours=5, minutes=30) display = timedelta_display(delta) - assert display == "2 days 5 hrs 30 mins" + assert display == " 2 days 5 hrs 30 mins" -def test_format_list_with_ampersand() -> None: - """Test format_list_with_ampersand function.""" - # Test with multiple items - items = ["apple", "banana", "cherry"] - result = format_list_with_ampersand(items) - assert result == "apple, banana & cherry" - - # Test with two items - items = ["apple", "banana"] - result = format_list_with_ampersand(items) - assert result == "apple & banana" - - # Test with single item - items = ["apple"] - result = format_list_with_ampersand(items) - assert result == "apple" - - # Test with empty list - items = [] - result = format_list_with_ampersand(items) - assert result == "" - - -def test_get_country() -> None: - """Test get_country function.""" - # Test with valid alpha-2 code - country = get_country("US") - assert country is not None - assert country.name == "United States" - - # Test with valid alpha-3 code - country = get_country("GBR") - assert country is not None - assert country.name == "United Kingdom" - - # Test with None - country = get_country(None) - assert country is None - - # Test with Kosovo special case - country = get_country("xk") - assert country is not None - assert country.name == "Kosovo" - - -def test_uk_time() -> None: - """Test uk_time function.""" - test_date = datetime.date(2023, 7, 15) # Summer time - test_time = datetime.time(14, 30, 0) - - result = uk_time(test_date, test_time) - - assert isinstance(result, datetime.datetime) - assert result.date() == test_date - assert result.time() == test_time - assert result.tzinfo is not None +# You can add more test cases for other functions as needed. diff --git a/tests/test_airbnb.py b/tests/test_airbnb.py deleted file mode 100644 index 7036cbf..0000000 --- a/tests/test_airbnb.py +++ /dev/null @@ -1,186 +0,0 @@ -"""Tests for agenda.airbnb module.""" - -import pytest -from datetime import datetime -from zoneinfo import ZoneInfo -from unittest.mock import Mock, patch, mock_open - -from agenda.airbnb import ( - build_datetime, - list_to_dict, - extract_country_code, - walk_tree, - get_ui_state, - get_reservation_data, - get_price_from_reservation, - parse_multiple_files, -) - - -class TestBuildDatetime: - def test_build_datetime_utc(self): - result = build_datetime("2025-07-28", "15:30", "UTC") - expected = datetime(2025, 7, 28, 15, 30, tzinfo=ZoneInfo("UTC")) - assert result == expected - - def test_build_datetime_local_timezone(self): - result = build_datetime("2025-12-25", "09:00", "Europe/London") - expected = datetime(2025, 12, 25, 9, 0, tzinfo=ZoneInfo("Europe/London")) - assert result == expected - - -class TestListToDict: - def test_list_to_dict_even_items(self): - items = ["key1", "value1", "key2", "value2"] - result = list_to_dict(items) - expected = {"key1": "value1", "key2": "value2"} - assert result == expected - - def test_list_to_dict_empty_list(self): - result = list_to_dict([]) - assert result == {} - - def test_list_to_dict_single_pair(self): - items = ["name", "John"] - result = list_to_dict(items) - assert result == {"name": "John"} - - -class TestExtractCountryCode: - def test_extract_country_code_uk(self): - address = "123 Main Street, London, United Kingdom" - result = extract_country_code(address) - assert result == "gb" - - def test_extract_country_code_france(self): - address = "456 Rue de la Paix, Paris, France" - result = extract_country_code(address) - assert result == "fr" - - def test_extract_country_code_usa(self): - address = "789 Broadway, New York, United States" - result = extract_country_code(address) - assert result == "us" - - def test_extract_country_code_not_found(self): - address = "123 Unknown Street, Mystery City" - result = extract_country_code(address) - assert result is None - - def test_extract_country_code_case_insensitive(self): - address = "123 Main Street, UNITED KINGDOM" - result = extract_country_code(address) - assert result == "gb" - - -class TestWalkTree: - def test_walk_tree_dict_found(self): - data = {"level1": {"level2": {"target": "found"}}} - result = walk_tree(data, "target") - assert result == "found" - - def test_walk_tree_dict_not_found(self): - data = {"level1": {"level2": {"other": "value"}}} - result = walk_tree(data, "target") - assert result is None - - def test_walk_tree_list_found(self): - data = [{"other": "value"}, {"target": "found"}] - result = walk_tree(data, "target") - assert result == "found" - - def test_walk_tree_nested_list_dict(self): - data = [{"level1": [{"target": "found"}]}] - result = walk_tree(data, "target") - assert result == "found" - - def test_walk_tree_empty_data(self): - result = walk_tree({}, "target") - assert result is None - - -class TestGetPriceFromReservation: - def test_get_price_from_reservation_valid(self): - reservation = { - "payment_summary": {"subtitle": "Total cost: £150.00"} - } - result = get_price_from_reservation(reservation) - assert result == "150.00" - - def test_get_price_from_reservation_different_amount(self): - reservation = { - "payment_summary": {"subtitle": "Total cost: £89.99"} - } - result = get_price_from_reservation(reservation) - assert result == "89.99" - - -class TestParseMultipleFiles: - @patch('agenda.airbnb.extract_booking_from_html') - def test_parse_multiple_files_single_file(self, mock_extract): - mock_booking = { - "type": "apartment", - "operator": "airbnb", - "name": "Test Apartment", - "booking_reference": "ABC123" - } - mock_extract.return_value = mock_booking - - result = parse_multiple_files(["test1.html"]) - - assert len(result) == 1 - assert result[0] == mock_booking - mock_extract.assert_called_once_with("test1.html") - - @patch('agenda.airbnb.extract_booking_from_html') - def test_parse_multiple_files_multiple_files(self, mock_extract): - mock_booking1 = {"booking_reference": "ABC123"} - mock_booking2 = {"booking_reference": "DEF456"} - mock_extract.side_effect = [mock_booking1, mock_booking2] - - result = parse_multiple_files(["test2.html", "test1.html"]) - - assert len(result) == 2 - assert result[0] == mock_booking1 - assert result[1] == mock_booking2 - - @patch('agenda.airbnb.extract_booking_from_html') - def test_parse_multiple_files_empty_list(self, mock_extract): - result = parse_multiple_files([]) - assert result == [] - mock_extract.assert_not_called() - - -class TestGetUiState: - @patch('lxml.html.etree') - def test_get_ui_state_with_mock_tree(self, mock_etree): - mock_tree = Mock() - mock_tree.xpath.return_value = ['{"test": [["uiState", {"key": "value"}]]}'] - - with patch('agenda.airbnb.walk_tree') as mock_walk: - mock_walk.return_value = [["key", "value"]] - result = get_ui_state(mock_tree) - - assert result == {"key": "value"} - mock_tree.xpath.assert_called_once_with('//*[@id="data-injector-instances"]/text()') - - -class TestGetReservationData: - def test_get_reservation_data(self): - ui_state = { - "reservation": { - "scheduled_event": { - "rows": [ - {"id": "row1", "data": "value1"}, - {"id": "row2", "data": "value2"} - ] - } - } - } - - result = get_reservation_data(ui_state) - expected = { - "row1": {"id": "row1", "data": "value1"}, - "row2": {"id": "row2", "data": "value2"} - } - assert result == expected \ No newline at end of file diff --git a/tests/test_birthday.py b/tests/test_birthday.py deleted file mode 100644 index c36a4e8..0000000 --- a/tests/test_birthday.py +++ /dev/null @@ -1,227 +0,0 @@ -"""Tests for birthday functionality.""" - -import tempfile -from datetime import date -from typing import Any - -import pytest -import yaml - -from agenda.birthday import YEAR_NOT_KNOWN, get_birthdays, next_birthday -from agenda.event import Event - - -class TestNextBirthday: - """Test the next_birthday function.""" - - def test_birthday_this_year_future(self) -> None: - """Test birthday that hasn't occurred this year.""" - from_date = date(2024, 3, 15) - birth_date = date(1990, 6, 20) - - next_bday, age = next_birthday(from_date, birth_date) - - assert next_bday == date(2024, 6, 20) - assert age == 34 - - def test_birthday_this_year_past(self) -> None: - """Test birthday that already occurred this year.""" - from_date = date(2024, 8, 15) - birth_date = date(1990, 6, 20) - - next_bday, age = next_birthday(from_date, birth_date) - - assert next_bday == date(2025, 6, 20) - assert age == 35 - - def test_birthday_today(self) -> None: - """Test when today is the birthday.""" - from_date = date(2024, 6, 20) - birth_date = date(1990, 6, 20) - - next_bday, age = next_birthday(from_date, birth_date) - - assert next_bday == date(2024, 6, 20) - assert age == 34 - - def test_birthday_unknown_year(self) -> None: - """Test birthday with unknown year.""" - from_date = date(2024, 3, 15) - birth_date = date(YEAR_NOT_KNOWN, 6, 20) - - next_bday, age = next_birthday(from_date, birth_date) - - assert next_bday == date(2024, 6, 20) - assert age is None - - def test_birthday_unknown_year_past(self) -> None: - """Test birthday with unknown year that already passed.""" - from_date = date(2024, 8, 15) - birth_date = date(YEAR_NOT_KNOWN, 6, 20) - - next_bday, age = next_birthday(from_date, birth_date) - - assert next_bday == date(2025, 6, 20) - assert age is None - - def test_leap_year_birthday(self) -> None: - """Test birthday on leap day.""" - from_date = date(2024, 1, 15) # 2024 is a leap year - birth_date = date(2000, 2, 29) # Born on leap day - - next_bday, age = next_birthday(from_date, birth_date) - - assert next_bday == date(2024, 2, 29) - assert age == 24 - - -class TestGetBirthdays: - """Test the get_birthdays function.""" - - def test_get_birthdays_with_year(self) -> None: - """Test getting birthdays with known birth years.""" - birthday_data = [ - { - "label": "John Doe", - "birthday": {"year": 1990, "month": 6, "day": 20} - }, - { - "label": "Jane Smith", - "birthday": {"year": 1985, "month": 12, "day": 15} - } - ] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(birthday_data, f) - filepath = f.name - - try: - from_date = date(2024, 3, 15) - events = get_birthdays(from_date, filepath) - - # Should have 4 events (2 people × 2 years each) - assert len(events) == 4 - - # Check John's birthdays - john_events = [e for e in events if "John Doe" in e.title] - assert len(john_events) == 2 - assert john_events[0].date == date(2024, 6, 20) - assert "aged 34" in john_events[0].title - assert john_events[1].date == date(2025, 6, 20) - assert "aged 35" in john_events[1].title - - # Check Jane's birthdays - jane_events = [e for e in events if "Jane Smith" in e.title] - assert len(jane_events) == 2 - assert jane_events[0].date == date(2024, 12, 15) - assert "aged 39" in jane_events[0].title - - # All events should be birthday events - for event in events: - assert event.name == "birthday" - assert isinstance(event, Event) - - finally: - import os - os.unlink(filepath) - - def test_get_birthdays_without_year(self) -> None: - """Test getting birthdays with unknown birth years.""" - birthday_data = [ - { - "label": "Anonymous Person", - "birthday": {"month": 6, "day": 20} - } - ] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(birthday_data, f) - filepath = f.name - - try: - from_date = date(2024, 3, 15) - events = get_birthdays(from_date, filepath) - - # Should have 2 events (1 person × 2 years) - assert len(events) == 2 - - # Check that age is unknown - for event in events: - assert "age unknown" in event.title - assert "Anonymous Person" in event.title - assert event.name == "birthday" - - finally: - import os - os.unlink(filepath) - - def test_get_birthdays_mixed_data(self) -> None: - """Test getting birthdays with mixed known/unknown years.""" - birthday_data = [ - { - "label": "Known Person", - "birthday": {"year": 1990, "month": 6, "day": 20} - }, - { - "label": "Unknown Person", - "birthday": {"month": 8, "day": 15} - } - ] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(birthday_data, f) - filepath = f.name - - try: - from_date = date(2024, 3, 15) - events = get_birthdays(from_date, filepath) - - # Should have 4 events total - assert len(events) == 4 - - # Check known person events - known_events = [e for e in events if "Known Person" in e.title] - assert len(known_events) == 2 - assert all("aged" in e.title and "age unknown" not in e.title for e in known_events) - - # Check unknown person events - unknown_events = [e for e in events if "Unknown Person" in e.title] - assert len(unknown_events) == 2 - assert all("age unknown" in e.title for e in unknown_events) - - finally: - import os - os.unlink(filepath) - - def test_get_birthdays_empty_file(self) -> None: - """Test getting birthdays from empty file.""" - birthday_data: list[dict[str, Any]] = [] - - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - yaml.dump(birthday_data, f) - filepath = f.name - - try: - from_date = date(2024, 3, 15) - events = get_birthdays(from_date, filepath) - - assert events == [] - - finally: - import os - os.unlink(filepath) - - def test_get_birthdays_file_not_found(self) -> None: - """Test error handling when file doesn't exist.""" - from_date = date(2024, 3, 15) - - with pytest.raises(FileNotFoundError): - get_birthdays(from_date, "/nonexistent/file.yaml") - - -class TestConstants: - """Test module constants.""" - - def test_year_not_known_constant(self) -> None: - """Test that YEAR_NOT_KNOWN has expected value.""" - assert YEAR_NOT_KNOWN == 1900 \ No newline at end of file diff --git a/tests/test_bristol_waste.py b/tests/test_bristol_waste.py deleted file mode 100644 index c0a35d1..0000000 --- a/tests/test_bristol_waste.py +++ /dev/null @@ -1,370 +0,0 @@ -"""Test Bristol waste collection module.""" - -import json -import os -import tempfile -from datetime import date, datetime, timedelta -from unittest.mock import AsyncMock, Mock, patch - -import httpx -import pytest -from agenda import bristol_waste -from agenda.event import Event - - -@pytest.fixture -def temp_data_dir(): - """Create a temporary directory for test data.""" - with tempfile.TemporaryDirectory() as tmpdir: - yield tmpdir - - -@pytest.fixture -def sample_bristol_data(): - """Sample Bristol waste collection data.""" - return [ - { - "containerName": "Recycling Container", - "collection": [ - { - "nextCollectionDate": "2024-07-15T00:00:00Z", - "lastCollectionDate": "2024-07-01T00:00:00Z", - } - ], - }, - { - "containerName": "General Waste Container", - "collection": [ - { - "nextCollectionDate": "2024-07-22T00:00:00Z", - "lastCollectionDate": "2024-07-08T00:00:00Z", - } - ], - }, - { - "containerName": "Food Waste Container", - "collection": [ - { - "nextCollectionDate": "2024-07-16T00:00:00Z", - "lastCollectionDate": "2024-07-02T00:00:00Z", - } - ], - }, - ] - - -@pytest.fixture -def mock_response(sample_bristol_data): - """Mock HTTP response for Bristol waste API.""" - response = Mock(spec=httpx.Response) - response.content = json.dumps({"data": sample_bristol_data}).encode() - response.json.return_value = {"data": sample_bristol_data} - return response - - -class TestGetService: - """Test get_service function.""" - - def test_recycling_container(self): - """Test extracting recycling service name.""" - item = {"containerName": "Recycling Container"} - result = bristol_waste.get_service(item) - assert result == "Recycling" - - def test_general_waste_container(self): - """Test extracting general waste service name.""" - item = {"containerName": "General Waste Container"} - result = bristol_waste.get_service(item) - assert result == "Waste Container" - - def test_food_waste_container(self): - """Test extracting food waste service name.""" - item = {"containerName": "Food Waste Container"} - result = bristol_waste.get_service(item) - assert result == "Waste Container" - - def test_garden_waste_container(self): - """Test extracting garden waste service name.""" - item = {"containerName": "Garden Waste Container"} - result = bristol_waste.get_service(item) - assert result == "Waste Container" - - -class TestCollections: - """Test collections function.""" - - def test_single_collection_dates(self): - """Test extracting dates from a single collection.""" - item = { - "collection": [ - { - "nextCollectionDate": "2024-07-15T00:00:00Z", - "lastCollectionDate": "2024-07-01T00:00:00Z", - } - ] - } - dates = list(bristol_waste.collections(item)) - expected = [date(2024, 7, 15), date(2024, 7, 1)] - assert dates == expected - - def test_multiple_collection_dates(self): - """Test extracting dates from multiple collections.""" - item = { - "collection": [ - { - "nextCollectionDate": "2024-07-15T00:00:00Z", - "lastCollectionDate": "2024-07-01T00:00:00Z", - }, - { - "nextCollectionDate": "2024-07-22T00:00:00Z", - "lastCollectionDate": "2024-07-08T00:00:00Z", - }, - ] - } - dates = list(bristol_waste.collections(item)) - expected = [ - date(2024, 7, 15), - date(2024, 7, 1), - date(2024, 7, 22), - date(2024, 7, 8), - ] - assert dates == expected - - def test_empty_collection(self): - """Test extracting dates from empty collection.""" - item = {"collection": []} - dates = list(bristol_waste.collections(item)) - assert dates == [] - - -class TestGetWebData: - """Test get_web_data function.""" - - @pytest.mark.asyncio - async def test_get_web_data_success(self, mock_response): - """Test successful web data retrieval.""" - uprn = "123456789012" - - with patch("httpx.AsyncClient") as mock_client: - mock_async_client = AsyncMock() - mock_client.return_value.__aenter__.return_value = mock_async_client - mock_async_client.get.return_value = mock_response - mock_async_client.post.return_value = mock_response - - result = await bristol_waste.get_web_data(uprn) - - assert result == mock_response - assert mock_async_client.get.call_count == 1 - assert mock_async_client.post.call_count == 2 - - @pytest.mark.asyncio - async def test_get_web_data_zero_padded_uprn(self, mock_response): - """Test UPRN is zero-padded to 12 digits.""" - uprn = "123456" - - with patch("httpx.AsyncClient") as mock_client: - mock_async_client = AsyncMock() - mock_client.return_value.__aenter__.return_value = mock_async_client - mock_async_client.get.return_value = mock_response - mock_async_client.post.return_value = mock_response - - await bristol_waste.get_web_data(uprn) - - # Check that the UPRN was zero-padded in the requests - calls = mock_async_client.post.call_args_list - assert calls[0][1]["json"] == {"Uprn": "UPRN000000123456"} - assert calls[1][1]["json"] == {"uprn": "000000123456"} - - -class TestGetData: - """Test get_data function.""" - - @pytest.mark.asyncio - async def test_get_data_force_cache(self, temp_data_dir, sample_bristol_data): - """Test using forced cache.""" - uprn = "123456789012" - os.makedirs(os.path.join(temp_data_dir, "waste")) - - # Create a cached file - cache_file = os.path.join( - temp_data_dir, "waste", f"2024-07-01_12:00_{uprn}.json" - ) - with open(cache_file, "w") as f: - json.dump({"data": sample_bristol_data}, f) - - result = await bristol_waste.get_data(temp_data_dir, uprn, "force") - assert result == sample_bristol_data - - @pytest.mark.asyncio - async def test_get_data_refresh_cache( - self, temp_data_dir, sample_bristol_data, mock_response - ): - """Test refreshing cache.""" - uprn = "123456789012" - - with patch("agenda.bristol_waste.get_web_data", return_value=mock_response): - with patch("agenda.bristol_waste.datetime") as mock_datetime: - mock_now = datetime(2024, 7, 15, 14, 30) - mock_datetime.now.return_value = mock_now - mock_datetime.strptime = datetime.strptime - - result = await bristol_waste.get_data(temp_data_dir, uprn, "refresh") - - assert result == sample_bristol_data - # Check that cache file was created - waste_dir = os.path.join(temp_data_dir, "waste") - cache_files = [ - f for f in os.listdir(waste_dir) if f.endswith(f"_{uprn}.json") - ] - assert len(cache_files) == 1 - assert cache_files[0] == f"2024-07-15_14:30_{uprn}.json" - - @pytest.mark.asyncio - async def test_get_data_recent_cache(self, temp_data_dir, sample_bristol_data): - """Test using recent cache within TTL.""" - uprn = "123456789012" - os.makedirs(os.path.join(temp_data_dir, "waste")) - - # Create a recent cached file (within TTL) - recent_time = datetime.now() - timedelta(hours=6) - cache_file = os.path.join( - temp_data_dir, - "waste", - f"{recent_time.strftime('%Y-%m-%d_%H:%M')}_{uprn}.json", - ) - with open(cache_file, "w") as f: - json.dump({"data": sample_bristol_data}, f) - - result = await bristol_waste.get_data(temp_data_dir, uprn, "auto") - assert result == sample_bristol_data - - @pytest.mark.asyncio - async def test_get_data_expired_cache( - self, temp_data_dir, sample_bristol_data, mock_response - ): - """Test with expired cache, should fetch new data.""" - uprn = "123456789012" - os.makedirs(os.path.join(temp_data_dir, "waste")) - - # Create an old cached file (beyond TTL) - old_time = datetime.now() - timedelta(hours=25) - cache_file = os.path.join( - temp_data_dir, "waste", f"{old_time.strftime('%Y-%m-%d_%H:%M')}_{uprn}.json" - ) - with open(cache_file, "w") as f: - json.dump({"data": [{"old": "data"}]}, f) - - with patch("agenda.bristol_waste.get_web_data", return_value=mock_response): - result = await bristol_waste.get_data(temp_data_dir, uprn, "auto") - - assert result == sample_bristol_data - - @pytest.mark.asyncio - async def test_get_data_timeout_fallback(self, temp_data_dir, sample_bristol_data): - """Test fallback to cache when web request times out.""" - uprn = "123456789012" - os.makedirs(os.path.join(temp_data_dir, "waste")) - - # Create a cached file - cache_file = os.path.join( - temp_data_dir, "waste", f"2024-07-01_12:00_{uprn}.json" - ) - with open(cache_file, "w") as f: - json.dump({"data": sample_bristol_data}, f) - - with patch( - "agenda.bristol_waste.get_web_data", - side_effect=httpx.ReadTimeout("Timeout"), - ): - result = await bristol_waste.get_data(temp_data_dir, uprn, "refresh") - - assert result == sample_bristol_data - - -class TestGet: - """Test main get function.""" - - @pytest.mark.asyncio - async def test_get_events(self, temp_data_dir, sample_bristol_data): - """Test generating events from Bristol waste data.""" - start_date = date(2024, 7, 10) - uprn = "123456789012" - - with patch("agenda.bristol_waste.get_data", return_value=sample_bristol_data): - events = await bristol_waste.get(start_date, temp_data_dir, uprn, "auto") - - assert len(events) == 3 # Only dates before start_date - - # Check event properties - for event in events: - assert isinstance(event, Event) - assert event.name == "waste_schedule" - assert event.title.startswith("Bristol: ") - assert event.date < start_date - - @pytest.mark.asyncio - async def test_get_events_filtered_by_start_date( - self, temp_data_dir, sample_bristol_data - ): - """Test events are filtered by start date.""" - start_date = date(2024, 7, 20) # Later start date - uprn = "123456789012" - - with patch("agenda.bristol_waste.get_data", return_value=sample_bristol_data): - events = await bristol_waste.get(start_date, temp_data_dir, uprn, "auto") - - # Should get all events before start_date (all dates in sample data) - assert len(events) == 5 # All collection dates are before 2024-07-20 - for event in events: - assert event.date < start_date - - @pytest.mark.asyncio - async def test_get_events_combined_services(self, temp_data_dir): - """Test services are combined for same date.""" - # Create data with multiple services on same date - data = [ - { - "containerName": "Recycling Container", - "collection": [ - { - "nextCollectionDate": "2024-07-15T00:00:00Z", - "lastCollectionDate": "2024-07-01T00:00:00Z", - } - ], - }, - { - "containerName": "Food Waste Container", - "collection": [ - { - "nextCollectionDate": "2024-07-15T00:00:00Z", - "lastCollectionDate": "2024-07-01T00:00:00Z", - } - ], - }, - ] - - start_date = date(2024, 7, 10) - uprn = "123456789012" - - with patch("agenda.bristol_waste.get_data", return_value=data): - events = await bristol_waste.get(start_date, temp_data_dir, uprn, "auto") - - # Should have 1 event (only the 2024-07-01 date is before start_date) - assert len(events) == 1 - - # Check that the event for 2024-07-01 combines both services - july_1_event = events[0] - assert july_1_event.date == date(2024, 7, 1) - assert "Recycling" in july_1_event.title - assert "Waste Container" in july_1_event.title - - @pytest.mark.asyncio - async def test_get_empty_data(self, temp_data_dir): - """Test with empty data.""" - start_date = date(2024, 7, 10) - uprn = "123456789012" - - with patch("agenda.bristol_waste.get_data", return_value=[]): - events = await bristol_waste.get(start_date, temp_data_dir, uprn, "auto") - - assert events == [] diff --git a/tests/test_build_place_yaml.py b/tests/test_build_place_yaml.py deleted file mode 100644 index 9e181dd..0000000 --- a/tests/test_build_place_yaml.py +++ /dev/null @@ -1,101 +0,0 @@ -"""Tests for agenda.build_place_yaml.""" - -from pathlib import Path - -import yaml - -from agenda import build_place_yaml - - -def test_upsert_station_adds_new_station(tmp_path: Path) -> None: - """Station upsert should add a new station to stations.yaml.""" - path = tmp_path / "stations.yaml" - path.write_text("""- name: London St Pancras - latitude: 51.531921 - longitude: -0.126361 - country: gb - wikidata: Q720102 - routes: {} -""") - - replaced = build_place_yaml.upsert_station( - tmp_path, - { - "name": "Paris Gare du Nord", - "latitude": 48.8809, - "longitude": 2.3553, - "country": "fr", - "wikidata": "Q624511", - "routes": {}, - }, - ) - - stations = yaml.safe_load(path.read_text()) - assert replaced is False - assert [station["name"] for station in stations] == [ - "London St Pancras", - "Paris Gare du Nord", - ] - assert "\n\n- name: Paris Gare du Nord\n" in path.read_text() - - -def test_upsert_station_replaces_existing_station(tmp_path: Path) -> None: - """Station upsert should replace an existing station with the same name.""" - path = tmp_path / "stations.yaml" - path.write_text("""- name: Paris Gare du Nord - latitude: 0 - longitude: 0 - country: fr - wikidata: Q624511 - routes: {} -""") - - replaced = build_place_yaml.upsert_station( - tmp_path, - { - "name": "Paris Gare du Nord", - "latitude": 48.8809, - "longitude": 2.3553, - "country": "fr", - "wikidata": "Q624511", - "routes": {}, - }, - ) - - stations = yaml.safe_load(path.read_text()) - assert replaced is True - assert len(stations) == 1 - assert stations[0]["latitude"] == 48.8809 - assert "\n\n- name:" not in path.read_text() - - -def test_upsert_airport_adds_mapping_entry(tmp_path: Path) -> None: - """Airport upsert should add a new IATA-keyed airport entry.""" - path = tmp_path / "airports.yaml" - path.write_text("""LHR: - iata: LHR - name: Heathrow Airport - city: London - country: gb - latitude: 51.47 - longitude: -0.4543 - qid: Q8691 -""") - - replaced = build_place_yaml.upsert_airport( - tmp_path, - { - "iata": "ORY", - "name": "Paris Orly Airport", - "city": "Paris Orly Airport", - "country": "fr", - "latitude": 48.723333, - "longitude": 2.379444, - "qid": "Q193353", - }, - ) - - airports = yaml.safe_load(path.read_text()) - assert replaced is False - assert list(airports) == ["LHR", "ORY"] - assert airports["ORY"]["country"] == "fr" diff --git a/tests/test_busy.py b/tests/test_busy.py deleted file mode 100644 index d3b80fc..0000000 --- a/tests/test_busy.py +++ /dev/null @@ -1,215 +0,0 @@ -from datetime import date, datetime, timezone - -import agenda.busy -import agenda.travel as travel -import agenda.trip -import pytest -from agenda.busy import _parse_datetime_field -from agenda.event import Event -from web_view import app - - -@pytest.fixture(scope="session") -def app_context(): - """Set up Flask app context for tests.""" - app.config["SERVER_NAME"] = "test" - with app.app_context(): - yield - - -@pytest.fixture(scope="session") -def trips(app_context): - """Load trip list once for all tests.""" - return agenda.trip.build_trip_list() - - -@pytest.fixture(scope="session") -def travel_data(app_context): - """Load travel data (bookings, accommodations, airports) once for all tests.""" - data_dir = app.config["PERSONAL_DATA"] - return { - "bookings": travel.parse_yaml("flights", data_dir), - "accommodations": travel.parse_yaml("accommodation", data_dir), - "airports": travel.parse_yaml("airports", data_dir), - "data_dir": data_dir, - } - - -def test_weekend_location_consistency(app_context, trips, travel_data): - """Test that weekend locations are consistent with events (free=home, events=away).""" - for year in range(2023, 2025): - start = date(2023, 1, 1) - busy_events = agenda.busy.get_busy_events(start, app.config, trips) - weekends = agenda.busy.weekends( - start, busy_events, trips, travel_data["data_dir"] - ) - - for weekend in weekends: - for day in "saturday", "sunday": - # When free (no events), should be home (None) - # When traveling (events), should be away (City name) - location_exists = bool(weekend[day + "_location"][0]) - has_events = bool(weekend[day]) - assert location_exists == has_events, ( - f"Weekend {weekend['date']} {day}: " - f"location_exists={location_exists}, has_events={has_events}" - ) - - -def test_specific_home_dates(travel_data): - """Test specific dates that should return home (None).""" - trips = agenda.trip.build_trip_list() - - home_dates = [ - date(2023, 4, 29), - date(2025, 7, 1), - date(2023, 12, 2), - date(2023, 10, 7), - date(2023, 2, 18), - date(2025, 8, 2), - ] - - for test_date in home_dates: - location = agenda.busy.get_location_for_date( - test_date, - trips, - ) - assert not location[ - 0 - ], f"Expected home (None) for {test_date}, got {location[0]}" - - -def test_specific_away_dates(travel_data): - """Test specific dates that should return away locations.""" - trips = agenda.trip.build_trip_list() - - away_cases = [ - (date(2025, 2, 15), "Hackettstown"), - ] - - for test_date, expected_city in away_cases: - location = agenda.busy.get_location_for_date( - test_date, - trips, - ) - assert ( - location[0] == expected_city - ), f"Expected {expected_city} for {test_date}, got {location[0]}" - - -def test_get_location_for_date_basic(travel_data): - """Test basic functionality of get_location_for_date function.""" - trips = agenda.trip.build_trip_list() - test_date = date(2023, 1, 1) - - location = agenda.busy.get_location_for_date( - test_date, - trips, - ) - - # Should return a tuple with (city|None, country) - assert isinstance(location, tuple) - assert len(location) == 2 - assert location[1] is not None # Should always have a country - - -def test_busy_event_classification(): - """Test the busy_event function for different event types.""" - - # Busy event types - busy_events = [ - Event(name="event", title="Test Event", date=date(2023, 1, 1)), - Event( - name="conference", - title="Test Conference", - date=date(2023, 1, 1), - going=True, - ), - Event(name="accommodation", title="Hotel", date=date(2023, 1, 1)), - Event(name="transport", title="Flight", date=date(2023, 1, 1)), - ] - - for event in busy_events: - assert agenda.busy.busy_event(event), f"Event {event.name} should be busy" - - # Non-busy events - non_busy_events = [ - Event( - name="conference", - title="Test Conference", - date=date(2023, 1, 1), - going=False, - ), - Event(name="other", title="Other Event", date=date(2023, 1, 1)), - Event(name="event", title="LHG Run Club", date=date(2023, 1, 1)), - Event(name="event", title="IA UK board meeting", date=date(2023, 1, 1)), - ] - - for event in non_busy_events: - assert not agenda.busy.busy_event( - event - ), f"Event {event.name}/{event.title} should not be busy" - - -def test_parse_datetime_field(): - """Test the _parse_datetime_field helper function.""" - - # Test with datetime object - dt = datetime(2023, 1, 1, 12, 0, 0) - parsed_dt, parsed_date = _parse_datetime_field(dt) - assert parsed_dt == dt.replace(tzinfo=timezone.utc) - assert parsed_dt.tzinfo == timezone.utc - assert parsed_date == date(2023, 1, 1) - - # Test with ISO string - iso_string = "2023-01-01T12:00:00Z" - parsed_dt, parsed_date = _parse_datetime_field(iso_string) - assert parsed_date == date(2023, 1, 1) - assert parsed_dt.year == 2023 - assert parsed_dt.month == 1 - assert parsed_dt.day == 1 - - -def test_get_busy_events(app_context, trips): - """Test get_busy_events function.""" - start_date = date(2023, 1, 1) - busy_events = agenda.busy.get_busy_events(start_date, app.config, trips) - - # Should return a list - assert isinstance(busy_events, list) - - # All events should be Event objects - for event in busy_events: - assert hasattr(event, "name") - assert hasattr(event, "as_date") - - # Events should be sorted by date - dates = [event.as_date for event in busy_events] - assert dates == sorted(dates), "Events should be sorted by date" - - -def test_weekends_function(app_context, trips, travel_data): - """Test the weekends function.""" - start_date = date(2023, 1, 1) - busy_events = agenda.busy.get_busy_events(start_date, app.config, trips) - weekends = agenda.busy.weekends( - start_date, busy_events, trips, travel_data["data_dir"] - ) - - # Should return a list of weekend info - assert isinstance(weekends, list) - assert len(weekends) == 52 # Should return 52 weekends - - # Each weekend should have the required keys - for weekend in weekends[:5]: # Check first 5 weekends - assert "date" in weekend - assert "saturday" in weekend - assert "sunday" in weekend - assert "saturday_location" in weekend - assert "sunday_location" in weekend - - # Locations should be tuples - assert isinstance(weekend["saturday_location"], tuple) - assert isinstance(weekend["sunday_location"], tuple) - assert len(weekend["saturday_location"]) == 2 - assert len(weekend["sunday_location"]) == 2 diff --git a/tests/test_busy_timezone.py b/tests/test_busy_timezone.py deleted file mode 100644 index 73d35dd..0000000 --- a/tests/test_busy_timezone.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Regression tests for timezone handling in busy location logic.""" - -from datetime import date, datetime, timezone - -import agenda.busy -from agenda.types import Trip - - -def test_mixed_naive_and_aware_arrivals_do_not_crash() -> None: - """Most recent travel should compare mixed timezone styles safely.""" - trips = [ - Trip( - start=date(2099, 12, 30), - travel=[ - { - "type": "flight", - "arrive": datetime(2100, 1, 1, 10, 0, 0), - "to": "CDG", - "to_airport": {"country": "fr", "city": "Paris"}, - }, - { - "type": "flight", - "arrive": datetime(2100, 1, 1, 12, 0, 0, tzinfo=timezone.utc), - "to": "AMS", - "to_airport": {"country": "nl", "city": "Amsterdam"}, - }, - ], - ) - ] - - location = agenda.busy._find_most_recent_travel_before_date(date(2100, 1, 1), trips) - assert location is not None - assert location[0] == "Amsterdam" - assert location[1] is not None - assert location[1].alpha_2 == "NL" diff --git a/tests/test_car_journey_timeline.py b/tests/test_car_journey_timeline.py deleted file mode 100644 index ad8b86d..0000000 --- a/tests/test_car_journey_timeline.py +++ /dev/null @@ -1,152 +0,0 @@ -"""Tests for importing car journeys from a Google Timeline export.""" - -from __future__ import annotations - -import json -from datetime import date, datetime, timezone -from pathlib import Path -import typing - -from agenda import car_journey_timeline, car_journey_yaml - - -def test_journey_key_orders_split_same_day_rows() -> None: - """Datetime rows should sort chronologically within a single day.""" - earlier = { - "trip": date(2025, 7, 4), - "depart": datetime(2025, 7, 4, 8, 0, tzinfo=timezone.utc), - "arrive": datetime(2025, 7, 4, 8, 30, tzinfo=timezone.utc), - "route": "a", - } - later = { - "trip": date(2025, 7, 4), - "depart": datetime(2025, 7, 4, 9, 0, tzinfo=timezone.utc), - "arrive": datetime(2025, 7, 4, 9, 30, tzinfo=timezone.utc), - "route": "b", - } - - assert car_journey_yaml.journey_key(earlier) < car_journey_yaml.journey_key(later) - - -def write_json(path: Path, data: dict[str, typing.Any]) -> None: - """Write JSON test data.""" - path.write_text(json.dumps(data)) - - -def test_import_from_timeline_splits_stopovers(tmp_path: Path) -> None: - """Past car journeys should be split into multiple timeline segments.""" - data_dir = tmp_path - route_dir = data_dir / "car_routes" - route_dir.mkdir() - (data_dir / "car_journeys.yaml").write_text("""--- - -- trip: 2025-07-04 - depart: 2025-07-04 - arrive: 2025-07-04 - route: PCH_to_Far - -- trip: 2026-07-16 - depart: 2026-07-16 - arrive: 2026-07-16 - route: Future_to_Elsewhere -""") - write_json( - route_dir / "PCH_to_Far.geojson", - { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [[-2.6, 51.4], [-4.1, 50.3]], - }, - }, - ) - write_json( - route_dir / "Future_to_Elsewhere.geojson", - { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [[0.0, 0.0], [1.0, 1.0]], - }, - }, - ) - - timeline_path = tmp_path / "Timeline.json" - write_json( - timeline_path, - { - "semanticSegments": [ - { - "startTime": "2025-07-04T08:00:00+00:00", - "endTime": "2025-07-04T10:00:00+00:00", - "timelinePath": [ - { - "point": "51.4000°, -2.6000°", - "time": "2025-07-04T08:00:00+00:00", - }, - { - "point": "51.0000°, -3.1000°", - "time": "2025-07-04T08:30:00+00:00", - }, - ], - }, - { - "startTime": "2025-07-04T08:00:00+00:00", - "endTime": "2025-07-04T08:30:00+00:00", - "activity": { - "start": {"latLng": "51.4000°, -2.6000°"}, - "end": {"latLng": "51.0000°, -3.1000°"}, - "topCandidate": {"type": "IN_PASSENGER_VEHICLE"}, - }, - }, - { - "startTime": "2025-07-04T10:00:00+00:00", - "endTime": "2025-07-04T12:00:00+00:00", - "timelinePath": [ - { - "point": "51.0000°, -3.1000°", - "time": "2025-07-04T10:00:00+00:00", - }, - { - "point": "50.3000°, -4.1000°", - "time": "2025-07-04T11:00:00+00:00", - }, - ], - }, - { - "startTime": "2025-07-04T10:00:00+00:00", - "endTime": "2025-07-04T11:00:00+00:00", - "activity": { - "start": {"latLng": "51.0000°, -3.1000°"}, - "end": {"latLng": "50.3000°, -4.1000°"}, - "topCandidate": {"type": "IN_PASSENGER_VEHICLE"}, - }, - }, - ] - }, - ) - - routes_written, rows_replaced, rows_written = ( - car_journey_timeline.import_from_timeline( - timeline_path, data_dir=data_dir, today=date(2026, 7, 9) - ) - ) - - assert routes_written == 2 - assert rows_replaced == 1 - assert rows_written == 2 - - updated = car_journey_yaml.load_yaml_list(data_dir / "car_journeys.yaml") - assert len(updated) == 3 - assert updated[0]["from"] == "PCH" - assert updated[0]["to"] == "Drive stop 1" - assert updated[1]["from"] == "Drive stop 1" - assert updated[1]["to"] == "Far" - assert updated[2]["route"] == "Future_to_Elsewhere" - - generated_paths = sorted(route_dir.glob("timeline_*.geojson")) - assert len(generated_paths) == 2 - generated_geojson = json.loads(generated_paths[0].read_text()) - assert generated_geojson["geometry"]["type"] == "LineString" diff --git a/tests/test_car_journey_yaml.py b/tests/test_car_journey_yaml.py deleted file mode 100644 index e746cab..0000000 --- a/tests/test_car_journey_yaml.py +++ /dev/null @@ -1,344 +0,0 @@ -"""Tests for car journey YAML generation.""" - -import json -from datetime import date, datetime -from pathlib import Path - -import agenda.car_journey_yaml -from agenda.types import Trip - - -def write_home_route(data_dir: Path) -> None: - """Write a route that identifies PCH as the home coordinate.""" - route_dir = data_dir / "car_routes" - route_dir.mkdir() - (route_dir / "PCH_to_EMF.geojson").write_text( - json.dumps( - { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [[-2.60283, 51.44083], [-2.37789, 52.038]], - }, - } - ) - ) - - -def test_car_journeys_for_trip_uses_accommodation_dates_and_locations( - tmp_path: Path, -) -> None: - """Build outbound and return journeys for the trip accommodation.""" - write_home_route(tmp_path) - trip = Trip( - start=date(2025, 4, 22), - accommodation=[ - { - "location": "Callestick", - "from": datetime(2025, 4, 22, 15, 0), - "to": datetime(2025, 4, 25, 10, 0), - "latitude": 50.307093, - "longitude": -5.13466, - } - ], - ) - - journeys = agenda.car_journey_yaml.car_journeys_for_trip( - trip, tmp_path, destination="accommodation" - ) - - assert [journey.route for journey in journeys] == [ - "PCH_to_Callestick", - "Callestick_to_PCH", - ] - assert [journey.depart for journey in journeys] == [ - date(2025, 4, 22), - date(2025, 4, 25), - ] - assert journeys[0].start == (-2.60283, 51.44083) - assert journeys[0].end == (-5.13466, 50.307093) - assert journeys[1].start == (-5.13466, 50.307093) - assert journeys[1].end == (-2.60283, 51.44083) - - -def test_car_journeys_for_trip_can_route_to_airport(tmp_path: Path) -> None: - """Build outbound and return journeys for airport access.""" - write_home_route(tmp_path) - trip = Trip( - start=date(2025, 11, 17), - travel=[ - { - "type": "flight", - "depart": datetime(2025, 11, 17, 17, 55), - "arrive": datetime(2025, 11, 18, 14, 50), - "from_airport": { - "iata": "LHR", - "latitude": 51.4775, - "longitude": -0.461389, - }, - "to_airport": { - "iata": "HKG", - "latitude": 22.308889, - "longitude": 113.914444, - }, - }, - { - "type": "flight", - "depart": datetime(2025, 11, 30, 22, 45), - "arrive": datetime(2025, 12, 1, 5, 35), - "from_airport": { - "iata": "HKG", - "latitude": 22.308889, - "longitude": 113.914444, - }, - "to_airport": { - "iata": "LHR", - "latitude": 51.4775, - "longitude": -0.461389, - }, - }, - ], - ) - - journeys = agenda.car_journey_yaml.car_journeys_for_trip( - trip, tmp_path, destination="airport" - ) - - assert [journey.route for journey in journeys] == ["PCH_to_LHR", "LHR_to_PCH"] - assert [journey.depart for journey in journeys] == [ - date(2025, 11, 17), - date(2025, 12, 1), - ] - assert journeys[0].start == (-2.60283, 51.44083) - assert journeys[0].end == (-0.461389, 51.4775) - assert journeys[1].start == (-0.461389, 51.4775) - assert journeys[1].end == (-2.60283, 51.44083) - - -def test_car_journeys_for_trip_can_route_around_ferry(tmp_path: Path) -> None: - """Build home, ferry terminal and accommodation driving legs.""" - write_home_route(tmp_path) - trip = Trip( - start=date(2025, 7, 4), - travel=[ - { - "type": "ferry", - "depart": datetime(2025, 7, 4, 22, 0), - "arrive": datetime(2025, 7, 5, 8, 0), - "from": "Plymouth", - "to": "Roscoff", - "from_terminal": { - "name": "Plymouth", - "latitude": 50.3651254, - "longitude": -4.1578164, - }, - "to_terminal": { - "name": "Roscoff", - "latitude": 48.721672, - "longitude": -3.966925, - }, - }, - { - "type": "ferry", - "depart": datetime(2025, 7, 21, 15, 0), - "arrive": datetime(2025, 7, 21, 20, 10), - "from": "Roscoff", - "to": "Plymouth", - "from_terminal": { - "name": "Roscoff", - "latitude": 48.721672, - "longitude": -3.966925, - }, - "to_terminal": { - "name": "Plymouth", - "latitude": 50.3651254, - "longitude": -4.1578164, - }, - }, - ], - accommodation=[ - { - "location": "Brest", - "from": datetime(2025, 7, 5, 16, 0), - "to": datetime(2025, 7, 21, 10, 0), - "latitude": 48.3645307, - "longitude": -4.5534685, - } - ], - ) - - journeys = agenda.car_journey_yaml.car_journeys_for_trip( - trip, tmp_path, destination="ferry" - ) - - assert [journey.route for journey in journeys] == [ - "PCH_to_Plymouth", - "Roscoff_to_Brest", - "Brest_to_Roscoff", - "Plymouth_to_PCH", - ] - assert [journey.depart for journey in journeys] == [ - date(2025, 7, 4), - date(2025, 7, 5), - date(2025, 7, 21), - date(2025, 7, 21), - ] - assert journeys[0].start == (-2.60283, 51.44083) - assert journeys[0].end == (-4.1578164, 50.3651254) - assert journeys[1].start == (-3.966925, 48.721672) - assert journeys[1].end == (-4.5534685, 48.3645307) - assert journeys[2].start == (-4.5534685, 48.3645307) - assert journeys[2].end == (-3.966925, 48.721672) - assert journeys[3].start == (-4.1578164, 50.3651254) - assert journeys[3].end == (-2.60283, 51.44083) - - -def test_car_journeys_for_trip_can_route_fly_drive_chain(tmp_path: Path) -> None: - """Build home, destination rental and return airport driving legs.""" - write_home_route(tmp_path) - trip = Trip( - start=date(2025, 9, 13), - travel=[ - { - "type": "flight", - "depart": datetime(2025, 9, 14, 6, 50), - "arrive": datetime(2025, 9, 14, 8, 55), - "from_airport": { - "iata": "LTN", - "latitude": 51.8747, - "longitude": -0.3683, - "country": "gb", - }, - "to_airport": { - "iata": "KEF", - "latitude": 63.985, - "longitude": -22.605556, - "country": "is", - }, - }, - { - "type": "flight", - "depart": datetime(2025, 10, 1, 9, 0), - "arrive": datetime(2025, 10, 1, 13, 0), - "from_airport": { - "iata": "KEF", - "latitude": 63.985, - "longitude": -22.605556, - "country": "is", - }, - "to_airport": { - "iata": "LTN", - "latitude": 51.8747, - "longitude": -0.3683, - "country": "gb", - }, - }, - ], - accommodation=[ - { - "location": "Luton Airport", - "country": "gb", - "from": datetime(2025, 9, 13, 15, 0), - "to": datetime(2025, 9, 14, 12, 0), - "latitude": 51.8745678, - "longitude": -0.3844256, - }, - { - "location": "Akranes", - "country": "is", - "from": datetime(2025, 9, 14, 16, 0), - "to": datetime(2025, 9, 16, 11, 0), - "latitude": 64.4344723, - "longitude": -21.5517961, - }, - { - "location": "Ísafjörður", - "country": "is", - "from": datetime(2025, 9, 16, 15, 0), - "to": datetime(2025, 10, 1, 11, 0), - "latitude": 66.072445, - "longitude": -23.120111, - }, - ], - ) - - journeys = agenda.car_journey_yaml.car_journeys_for_trip( - trip, tmp_path, destination="fly-drive" - ) - - assert [journey.route for journey in journeys] == [ - "PCH_to_Luton_Airport", - "KEF_to_Akranes", - "Akranes_to_Isafjordur", - "Isafjordur_to_KEF", - "LTN_to_PCH", - ] - assert [journey.depart for journey in journeys] == [ - date(2025, 9, 13), - date(2025, 9, 14), - date(2025, 9, 16), - date(2025, 10, 1), - date(2025, 10, 1), - ] - - -def test_write_route_files_skips_existing_routes(tmp_path: Path) -> None: - """Existing route files should not call the route API.""" - write_home_route(tmp_path) - journey = agenda.car_journey_yaml.CarJourney( - trip=date(2025, 4, 22), - depart=date(2025, 4, 22), - arrive=date(2025, 4, 22), - route="PCH_to_EMF", - start=(-2.60283, 51.44083), - end=(-2.37789, 52.038), - ) - - def fail_fetch( - _start: agenda.car_journey_yaml.LonLat, - _end: agenda.car_journey_yaml.LonLat, - ) -> agenda.car_journey_yaml.GeoJSON: - raise AssertionError("route API should not be called") - - written = agenda.car_journey_yaml.write_route_files(tmp_path, [journey], fail_fetch) - - assert written == 0 - - -def test_import_car_journeys_inserts_chronologically_and_is_idempotent( - tmp_path: Path, -) -> None: - """Generated journeys should be inserted without duplicates.""" - (tmp_path / "car_journeys.yaml").write_text("""--- -- trip: 2025-05-01 - depart: 2025-05-01 - arrive: 2025-05-01 - route: PCH_to_Later -""") - journeys = [ - agenda.car_journey_yaml.CarJourney( - trip=date(2025, 4, 22), - depart=date(2025, 4, 22), - arrive=date(2025, 4, 22), - route="PCH_to_Callestick", - start=(-2.60283, 51.44083), - end=(-5.13466, 50.307093), - ), - agenda.car_journey_yaml.CarJourney( - trip=date(2025, 4, 22), - depart=date(2025, 4, 25), - arrive=date(2025, 4, 25), - route="Callestick_to_PCH", - start=(-5.13466, 50.307093), - end=(-2.60283, 51.44083), - ), - ] - - added = agenda.car_journey_yaml.import_car_journeys(tmp_path, journeys) - added_again = agenda.car_journey_yaml.import_car_journeys(tmp_path, journeys) - - assert added == 2 - assert added_again == 0 - text = (tmp_path / "car_journeys.yaml").read_text() - assert text.index("PCH_to_Callestick") < text.index("PCH_to_Later") diff --git a/tests/test_carnival.py b/tests/test_carnival.py deleted file mode 100644 index 5985262..0000000 --- a/tests/test_carnival.py +++ /dev/null @@ -1,125 +0,0 @@ -"""Tests for carnival functionality.""" - -from datetime import date - -from agenda.carnival import rio_carnival_events -from agenda.event import Event - - -class TestRioCarnivalEvents: - """Test the rio_carnival_events function.""" - - def test_carnival_events_single_year(self) -> None: - """Test getting carnival events for a single year.""" - # 2024 Easter is March 31, so carnival should be around Feb 9-14 - start_date = date(2024, 1, 1) - end_date = date(2024, 12, 31) - - events = rio_carnival_events(start_date, end_date) - - assert len(events) == 1 - event = events[0] - assert event.name == "carnival" - assert event.title == "Rio Carnival" - assert event.url == "https://en.wikipedia.org/wiki/Rio_Carnival" - assert event.date.year == 2024 - assert event.end_date is not None - assert event.end_date.year == 2024 - # Should be about 51 days before Easter (around early-mid February) - assert event.date.month == 2 - assert event.end_date.month == 2 - - def test_carnival_events_multiple_years(self) -> None: - """Test getting carnival events for multiple years.""" - start_date = date(2023, 1, 1) - end_date = date(2025, 12, 31) - - events = rio_carnival_events(start_date, end_date) - - # Should have carnival for 2023, 2024, and 2025 - assert len(events) == 3 - - years = [event.date.year for event in events] - assert sorted(years) == [2023, 2024, 2025] - - # All events should be carnival events - for event in events: - assert event.name == "carnival" - assert event.title == "Rio Carnival" - assert event.url == "https://en.wikipedia.org/wiki/Rio_Carnival" - - def test_carnival_events_no_overlap(self) -> None: - """Test when date range doesn't overlap with carnival.""" - # Choose a range that's unlikely to include carnival (summer) - start_date = date(2024, 6, 1) - end_date = date(2024, 8, 31) - - events = rio_carnival_events(start_date, end_date) - - assert events == [] - - def test_carnival_events_partial_overlap_start(self) -> None: - """Test when carnival start overlaps with date range.""" - # 2024 carnival should be around Feb 9-14 - start_date = date(2024, 2, 10) # Might overlap with carnival start - end_date = date(2024, 2, 15) - - events = rio_carnival_events(start_date, end_date) - - # Should include carnival if there's any overlap - if events: - assert len(events) == 1 - assert events[0].name == "carnival" - - def test_carnival_events_partial_overlap_end(self) -> None: - """Test when carnival end overlaps with date range.""" - # 2024 carnival should be around Feb 9-14 - start_date = date(2024, 2, 12) - end_date = date(2024, 2, 20) # Might overlap with carnival end - - events = rio_carnival_events(start_date, end_date) - - # Should include carnival if there's any overlap - if events: - assert len(events) == 1 - assert events[0].name == "carnival" - - def test_carnival_dates_relative_to_easter(self) -> None: - """Test that carnival dates are correctly calculated relative to Easter.""" - start_date = date(2024, 1, 1) - end_date = date(2024, 12, 31) - - events = rio_carnival_events(start_date, end_date) - - assert len(events) == 1 - event = events[0] - - # Carnival should be 5 days long - duration = (event.end_date - event.date).days + 1 - assert duration == 6 # 51 days before to 46 days before Easter (6 days total) - - # Both dates should be in February for 2024 - assert event.date.month == 2 - assert event.end_date.month == 2 - - # End date should be after start date - assert event.end_date > event.date - - def test_carnival_events_empty_date_range(self) -> None: - """Test with empty date range.""" - start_date = date(2024, 6, 15) - end_date = date(2024, 6, 10) # End before start - - events = rio_carnival_events(start_date, end_date) - - # Should return empty list for invalid range - assert events == [] - - def test_carnival_events_same_start_end_date(self) -> None: - """Test with same start and end date.""" - # Pick a date that's definitely not carnival - test_date = date(2024, 7, 15) - - events = rio_carnival_events(test_date, test_date) - - assert events == [] \ No newline at end of file diff --git a/tests/test_conference.py b/tests/test_conference.py deleted file mode 100644 index d056527..0000000 --- a/tests/test_conference.py +++ /dev/null @@ -1,468 +0,0 @@ -"""Tests for agenda.conference module.""" - -import decimal -import tempfile -from datetime import date, datetime -from typing import Any - -import pytest -import yaml - -from agenda.conference import Conference, conference_date_fields, get_list -from agenda.event import Event - - -class TestConference: - """Tests for Conference dataclass.""" - - def test_conference_creation_minimal(self) -> None: - """Test creating conference with minimal required fields.""" - conf = Conference( - name="PyCon", - topic="Python", - location="Portland", - start=date(2024, 5, 15), - end=date(2024, 5, 17), - ) - assert conf.name == "PyCon" - assert conf.topic == "Python" - assert conf.location == "Portland" - assert conf.start == date(2024, 5, 15) - assert conf.end == date(2024, 5, 17) - assert conf.trip is None - assert conf.going is False - assert conf.online is False - - def test_conference_creation_full(self) -> None: - """Test creating conference with all fields.""" - conf = Conference( - name="PyCon US", - topic="Python", - location="Portland", - start=date(2024, 5, 15), - end=date(2024, 5, 17), - trip=date(2024, 5, 14), - country="USA", - venue="Convention Center", - address="123 Main St", - url="https://pycon.org", - accommodation_booked=True, - transport_booked=True, - going=True, - registered=True, - speaking=True, - online=False, - price=decimal.Decimal("500.00"), - currency="USD", - latitude=45.5152, - longitude=-122.6784, - cfp_end=date(2024, 2, 1), - cfp_url="https://pycon.org/cfp", - free=False, - hackathon=True, - ticket_type="early_bird", - attendees=3000, - hashtag="#pycon2024", - ) - assert conf.name == "PyCon US" - assert conf.going is True - assert conf.price == decimal.Decimal("500.00") - assert conf.currency == "USD" - assert conf.latitude == 45.5152 - assert conf.longitude == -122.6784 - assert conf.cfp_end == date(2024, 2, 1) - assert conf.hashtag == "#pycon2024" - - def test_display_name_location_in_name(self) -> None: - """Test display_name when location is already in conference name.""" - conf = Conference( - name="PyCon Portland", - topic="Python", - location="Portland", - start=date(2024, 5, 15), - end=date(2024, 5, 17), - ) - assert conf.display_name == "PyCon Portland" - - def test_display_name_location_not_in_name(self) -> None: - """Test display_name when location is not in conference name.""" - conf = Conference( - name="PyCon", - topic="Python", - location="Portland", - start=date(2024, 5, 15), - end=date(2024, 5, 17), - ) - assert conf.display_name == "PyCon (Portland)" - - def test_display_name_partial_location_match(self) -> None: - """Test display_name when location is partially in name.""" - conf = Conference( - name="PyConf", - topic="Python", - location="Conference Center", - start=date(2024, 5, 15), - end=date(2024, 5, 17), - ) - assert conf.display_name == "PyConf (Conference Center)" - - def test_conference_with_datetime(self) -> None: - """Test conference with datetime objects.""" - start_dt = datetime(2024, 5, 15, 9, 0) - end_dt = datetime(2024, 5, 17, 17, 0) - conf = Conference( - name="PyCon", - topic="Python", - location="Portland", - start=start_dt, - end=end_dt, - ) - assert conf.start == start_dt - assert conf.end == end_dt - - -class TestGetList: - """Tests for get_list function.""" - - def test_get_list_single_conference(self) -> None: - """Test reading single conference from YAML.""" - yaml_data = [ - { - "name": "PyCon", - "topic": "Python", - "location": "Portland", - "start": date(2024, 5, 15), - "end": date(2024, 5, 17), - "url": "https://pycon.org", - "going": True, - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 1 - event = events[0] - assert isinstance(event, Event) - assert event.name == "conference" - assert event.date == date(2024, 5, 15) - assert event.end_date == date(2024, 5, 17) - assert event.title == "PyCon (Portland)" - assert event.url == "https://pycon.org" - assert event.going is True - - def test_get_list_conference_with_cfp(self) -> None: - """Test reading conference with CFP end date.""" - yaml_data = [ - { - "name": "PyCon", - "topic": "Python", - "location": "Portland", - "start": date(2024, 5, 15), - "end": date(2024, 5, 17), - "url": "https://pycon.org", - "cfp_end": date(2024, 2, 1), - "cfp_url": "https://pycon.org/cfp", - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 2 - - # Conference event - conf_event = events[0] - assert conf_event.name == "conference" - assert conf_event.title == "PyCon (Portland)" - assert conf_event.url == "https://pycon.org" - - # CFP end event - cfp_event = events[1] - assert cfp_event.name == "cfp_end" - assert cfp_event.date == date(2024, 2, 1) - assert cfp_event.title == "CFP end: PyCon (Portland)" - assert cfp_event.url == "https://pycon.org/cfp" - - def test_get_list_conference_cfp_no_url(self) -> None: - """Test reading conference with CFP end date but no CFP URL.""" - yaml_data = [ - { - "name": "PyCon", - "topic": "Python", - "location": "Portland", - "start": date(2024, 5, 15), - "end": date(2024, 5, 17), - "url": "https://pycon.org", - "cfp_end": date(2024, 2, 1), - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 2 - cfp_event = events[1] - assert cfp_event.url == "https://pycon.org" # Falls back to conference URL - - def test_get_list_multiple_conferences(self) -> None: - """Test reading multiple conferences from YAML.""" - yaml_data = [ - { - "name": "PyCon", - "topic": "Python", - "location": "Portland", - "start": date(2024, 5, 15), - "end": date(2024, 5, 17), - }, - { - "name": "EuroPython", - "topic": "Python", - "location": "Prague", - "start": date(2024, 7, 8), - "end": date(2024, 7, 14), - "cfp_end": date(2024, 3, 15), - }, - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 3 # 2 conferences + 1 CFP end - - # First conference - assert events[0].title == "PyCon (Portland)" - assert events[0].date == date(2024, 5, 15) - - # Second conference - assert events[1].title == "EuroPython (Prague)" - assert events[1].date == date(2024, 7, 8) - - # CFP end event for second conference - assert events[2].name == "cfp_end" - assert events[2].title == "CFP end: EuroPython (Prague)" - - def test_get_list_location_in_name(self) -> None: - """Test conference where location is already in name.""" - yaml_data = [ - { - "name": "PyCon Portland", - "topic": "Python", - "location": "Portland", - "start": date(2024, 5, 15), - "end": date(2024, 5, 17), - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 1 - assert events[0].title == "PyCon Portland" # No location appended - - def test_get_list_datetime_objects(self) -> None: - """Test reading conferences with datetime objects.""" - yaml_data = [ - { - "name": "PyCon", - "topic": "Python", - "location": "Portland", - "start": datetime(2024, 5, 15, 9, 0), - "end": datetime(2024, 5, 17, 17, 0), - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 1 - event = events[0] - assert event.date == datetime(2024, 5, 15, 9, 0) - assert event.end_date == datetime(2024, 5, 17, 17, 0) - - def test_get_list_nested_exact_dates(self) -> None: - """Test reading conference with nested exact dates.""" - yaml_data = [ - { - "name": "PyCon", - "topic": "Python", - "location": "Portland", - "dates": { - "status": "exact", - "start": date(2024, 5, 15), - "end": date(2024, 5, 17), - }, - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 1 - assert events[0].date == date(2024, 5, 15) - assert events[0].end_date == date(2024, 5, 17) - - def test_get_list_tentative_dates_do_not_create_conference_event(self) -> None: - """Test tentative conference dates are not emitted as calendar events.""" - yaml_data = [ - { - "name": "FOSDEM", - "topic": "FOSDEM", - "location": "Brussels", - "dates": { - "status": "tentative", - "start": date(2027, 1, 30), - "end": date(2027, 1, 31), - "label": "likely first weekend of February 2027", - }, - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert events == [] - - def test_get_list_approximate_dates_keep_cfp_event(self) -> None: - """Test approximate dates do not block CFP reminders.""" - yaml_data = [ - { - "name": "PyCascades", - "topic": "Python", - "location": "TBC", - "dates": { - "status": "approximate", - "label": "March 2027", - "earliest": date(2027, 3, 1), - "latest": date(2027, 3, 31), - }, - "cfp_end": date(2026, 11, 1), - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 1 - assert events[0].name == "cfp_end" - assert events[0].date == date(2026, 11, 1) - - def test_conference_date_fields_approximate(self) -> None: - """Test derived fields for approximate conference dates.""" - fields = conference_date_fields( - { - "name": "PyCascades", - "topic": "Python", - "location": "TBC", - "dates": { - "status": "approximate", - "label": "March 2027", - "earliest": date(2027, 3, 1), - "latest": date(2027, 3, 31), - }, - } - ) - - assert fields["date_status"] == "approximate" - assert fields["sort_date"] == date(2027, 3, 1) - assert fields["latest_date"] == date(2027, 3, 31) - assert fields["display_date"] == "March 2027" - assert fields["has_exact_dates"] is False - - def test_get_list_invalid_date_order(self) -> None: - """Test that conferences with end before start raise assertion error.""" - yaml_data = [ - { - "name": "Invalid Conference", - "topic": "Testing", - "location": "Nowhere", - "start": date(2024, 5, 17), - "end": date(2024, 5, 15), # End before start - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - with pytest.raises(AssertionError): - get_list(f.name) - - def test_get_list_too_long_conference(self) -> None: - """Test that conferences longer than MAX_CONF_DAYS raise assertion error.""" - yaml_data = [ - { - "name": "Too Long Conference", - "topic": "Testing", - "location": "Nowhere", - "start": date(2024, 5, 1), - "end": date(2024, 6, 1), # More than 20 days - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - with pytest.raises(AssertionError): - get_list(f.name) - - def test_get_list_empty_file(self) -> None: - """Test reading empty YAML file.""" - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump([], f) - f.flush() - - events = get_list(f.name) - - assert events == [] - - def test_get_list_same_day_conference(self) -> None: - """Test conference that starts and ends on same day.""" - yaml_data = [ - { - "name": "One Day Conference", - "topic": "Testing", - "location": "Test City", - "start": date(2024, 5, 15), - "end": date(2024, 5, 15), - } - ] - - with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: - yaml.dump(yaml_data, f) - f.flush() - - events = get_list(f.name) - - assert len(events) == 1 - event = events[0] - assert event.date == date(2024, 5, 15) - assert event.end_date == date(2024, 5, 15) diff --git a/tests/test_conference_list.py b/tests/test_conference_list.py deleted file mode 100644 index d1ccc87..0000000 --- a/tests/test_conference_list.py +++ /dev/null @@ -1,205 +0,0 @@ -"""Tests for conference list date handling.""" - -from datetime import date -import typing -from types import SimpleNamespace - -import yaml - -import agenda.fx -import agenda.trip -import web_view - - -def test_build_conference_list_supports_inexact_dates( - tmp_path: typing.Any, monkeypatch: typing.Any -) -> None: - """Conference list should include tentative and approximate dates.""" - conferences = [ - { - "name": "PyCascades 2027", - "series": "pycascades", - "topic": "Python", - "location": "TBC", - "dates": { - "status": "approximate", - "label": "March 2027", - "earliest": date(2027, 3, 1), - "latest": date(2027, 3, 31), - }, - }, - { - "name": "FOSDEM 2027", - "topic": "FOSDEM", - "location": "Brussels", - "dates": { - "status": "tentative", - "start": date(2027, 1, 30), - "end": date(2027, 1, 31), - "label": "likely first weekend of February 2027", - }, - }, - ] - (tmp_path / "conferences.yaml").write_text( - yaml.safe_dump(conferences), encoding="utf-8" - ) - (tmp_path / "conference_series.yaml").write_text( - yaml.safe_dump( - { - "pycascades": { - "name": "PyCascades", - "topic": "Python", - "cadence": "annual", - "url": "https://pycascades.com/", - } - } - ), - encoding="utf-8", - ) - - monkeypatch.setitem(web_view.app.config, "PERSONAL_DATA", str(tmp_path)) - monkeypatch.setattr(agenda.trip, "build_trip_list", lambda: []) - - items = web_view.build_conference_list() - - assert [item["name"] for item in items] == ["FOSDEM 2027", "PyCascades 2027"] - assert items[0]["date_status"] == "tentative" - assert items[0]["display_date"] == "likely first weekend of February 2027" - assert items[0]["sort_date"] == date(2027, 1, 30) - assert items[1]["date_status"] == "approximate" - assert items[1]["display_date"] == "March 2027" - assert items[1]["latest_date"] == date(2027, 3, 31) - assert items[1]["series_detail"]["name"] == "PyCascades" - - -def test_conference_series_pages(tmp_path: typing.Any, monkeypatch: typing.Any) -> None: - """Series index and detail pages should render linked conferences.""" - conferences = [ - { - "name": "PyCascades 2027", - "series": "pycascades", - "topic": "Python", - "location": "TBC", - "trip": date(2027, 3, 1), - "dates": { - "status": "exact", - "start": date(2027, 3, 5), - "end": date(2027, 3, 6), - }, - "going": True, - } - ] - series = { - "pycascades": { - "name": "PyCascades", - "topic": "Python", - "cadence": "annual", - "url": "https://pycascades.com/", - } - } - (tmp_path / "conferences.yaml").write_text( - yaml.safe_dump(conferences), encoding="utf-8" - ) - (tmp_path / "conference_series.yaml").write_text( - yaml.safe_dump(series), encoding="utf-8" - ) - - monkeypatch.setitem(web_view.app.config, "PERSONAL_DATA", str(tmp_path)) - fake_trip = SimpleNamespace( - start=date(2027, 3, 1), - title="Seattle Python trip", - conferences=[{"start": date(2027, 3, 5), "name": "PyCascades 2027"}], - ) - monkeypatch.setattr(agenda.trip, "build_trip_list", lambda: [fake_trip]) - - web_view.app.config["TESTING"] = True - with web_view.app.test_client() as client: - index_response = client.get("/conference/series") - detail_response = client.get("/conference/series/pycascades") - - assert index_response.status_code == 200 - assert b"PyCascades" in index_response.data - assert b"attended" in index_response.data - assert detail_response.status_code == 200 - assert b"trip: Seattle Python trip" in detail_response.data - - -def test_conference_page_filters_by_country( - tmp_path: typing.Any, monkeypatch: typing.Any -) -> None: - """Conference page should filter upcoming conferences by country code.""" - conferences = [ - { - "name": "UK Mapping Conf 2099", - "topic": "Maps", - "location": "London", - "country": "GB", - "start": date(2099, 5, 1), - "end": date(2099, 5, 2), - }, - { - "name": "US Python Conf 2099", - "topic": "Python", - "location": "Pittsburgh", - "country": "US", - "start": date(2099, 6, 1), - "end": date(2099, 6, 2), - }, - ] - (tmp_path / "conferences.yaml").write_text( - yaml.safe_dump(conferences), encoding="utf-8" - ) - - monkeypatch.setitem(web_view.app.config, "PERSONAL_DATA", str(tmp_path)) - monkeypatch.setattr(agenda.trip, "build_trip_list", lambda: []) - monkeypatch.setattr(agenda.fx, "get_rates", lambda config: {}) - - web_view.app.config["TESTING"] = True - with web_view.app.test_client() as client: - response = client.get("/conference?country=gb") - - assert response.status_code == 200 - assert b"UK Mapping Conf 2099" in response.data - assert b"US Python Conf 2099" not in response.data - assert b'