Compare commits

..

No commits in common. "main" and "launch-page" have entirely different histories.

118 changed files with 1149 additions and 20741 deletions

View file

@ -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": {
}
};

6
.gitignore vendored
View file

@ -3,8 +3,4 @@ __pycache__/
__pycache__
.mypy_cache
config
.hypothesis
personal-data
static/bootstrap5
static/leaflet*
static/es-module-shims
.hypothesis/

View file

@ -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.

View file

@ -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

View file

@ -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"]

View file

@ -1,734 +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 # type: ignore[import-untyped]
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`.
- `free`, `price`, `currency`, `hackathon`, `online`, `attendees`.
- 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."""
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
return typing.cast(
str, text_maker.handle(lxml.html.tostring(root_copy, encoding="unicode"))
)
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 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 typing.cast(
dict[str, ConferenceSeries],
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)
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

View file

@ -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

View file

@ -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})',
)
)

View file

@ -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])

View file

@ -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

View file

@ -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
]

View file

@ -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,
item = {
"allDay": True,
"title": e.display_title,
"start": e.as_date.isoformat(),
"end": (e.end_as_date + one_day).isoformat(),
"isAllday": True,
"raw": {"url": e.url},
"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},
}
)
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()
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,
}
if e.url:
item["raw"] = {"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
if e.has_time:
end = e.end_date or e.date + timedelta(hours=1)
else:
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["url"] = e.url
items.append(item)
return items

View file

@ -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

View file

@ -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 typing.cast(date, 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(
return [
Event(
name="conference",
date=conf.start,
end_date=conf.end,
title=conf.display_name,
title=f"🎤 {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,
for conf in (
Conference(**conf)
for conf in yaml.safe_load(open(filepath, "r"))["conferences"]
)
events.append(cfp_end_event)
return events
]

View file

@ -1,50 +1,51 @@
"""Agenda data."""
import asyncio
import collections
import itertools
import os
import typing
from datetime import date, datetime, timedelta
from time import time
import dateutil.rrule
import dateutil.tz
import flask
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,114 +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)
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 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
async def get_data(now: datetime, config: flask.config.Config) -> AgendaData:
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_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"]
@ -182,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"]
@ -243,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
@ -298,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

View file

@ -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/"

View file

@ -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]:

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
]

View file

@ -1,457 +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
json_key: str = "booking"
BOOKING_CONFIGS: dict[str, BookingConfig] = {
"flight": BookingConfig(
booking_type="flight",
yaml_filename="flights.yaml",
spec_heading="flights.yaml",
),
"train": BookingConfig(
booking_type="train",
yaml_filename="trains.yaml",
spec_heading="trains.yaml",
),
}
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)
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 the top-level "trip" key from the YAML.
- 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["depart"])
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 "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)

View file

@ -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)

View file

@ -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"<tr>\s*<td>(Weekdays|Saturdays|Sundays)</td>*"
+ r"\s*<td>(.*?)(?:\*\*)?</td>\s*</tr>",
)
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)

View file

@ -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")

View file

@ -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

View file

@ -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")

View file

@ -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()

View file

@ -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)

View file

@ -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)

View file

@ -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'<h2>Object moved to <a href="([^"]*)">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()
]

View file

@ -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

View file

@ -1,138 +0,0 @@
"""Trip statistic functions."""
from collections import defaultdict
from typing import Counter, Mapping
import agenda
from agenda.types import StrDict, Trip, airport_label
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":
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,
}
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)
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)

View file

@ -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 ""
)

View file

@ -2,7 +2,7 @@
import yaml
from .event import Event
from .types import Event
def get_events(filepath: str) -> list[Event]:

View file

@ -3,7 +3,7 @@
import typing
from datetime import datetime
import ephem # type: ignore
import ephem
def bristol() -> ephem.Observer:

View file

@ -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):
async with httpx.AsyncClient() as client:
r = await client.get(url, params=params)
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
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
f = existing[0][1]
# 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")))
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"
)
data = json.load(open(filename))
return [summarize_launch(launch) for launch in data["results"]]

View file

@ -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(
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") if flask.g.user.is_authenticated else None),
url=item.get("url"),
)
events.append(e)
return events
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

File diff suppressed because it is too large Load diff

View file

@ -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)

View file

@ -1,468 +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:",
}
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']})"
@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 and travel[from_or_to] not in titles:
titles.append(travel[from_or_to])
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,
)
)
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

View file

@ -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
from .types import 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."""
url = "https://www.gov.uk/bank-holidays.json"
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)
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)
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]:
"""Date and name of the next UK bank holiday."""
filename = json_filename(data_dir)
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

View file

@ -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

View file

@ -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
from .types import StrDict
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"))

209
agenda/waste_schedule.py Normal file
View file

@ -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'<h2>Object moved to <a href="([^"]*)">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()
]

View file

@ -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}

View file

@ -1,824 +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.
- 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`.
- 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'
```
## `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
```

View file

View file

@ -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 <IATA_CODE>", 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="",
)

View file

@ -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"
}
}

View file

@ -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()

View file

@ -9,5 +9,3 @@ dateutil
ephem
flask
requests
emoji
timezonefinder

View file

@ -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()

View file

@ -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())

View file

@ -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())

View file

@ -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())

View file

@ -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())

View file

@ -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())

View file

@ -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;
}

View file

@ -1,234 +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": "🚏",
"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 = [
"<div style=\"width:", metrics.outerSize, "px;height:", metrics.outerSize, "px;",
"border-radius:50%;border:1px solid rgba(0,0,0,0.2);background-color:rgba(255,255,255,0.9);",
"box-shadow:0 1px 3px rgba(0,0,0,0.2);display:flex;justify-content:center;align-items:center;\">",
"<div style=\"font-size:", metrics.fontSize, "px;line-height:1;\">",
symbol,
"</div></div>"
].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]; });
if (bounds.length === 0) {
routes.forEach(function(r) {
if (r.from) bounds.push(r.from);
if (r.to) bounds.push(r.to);
});
}
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: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> 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"}[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;
}

View file

@ -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 %}
<style>
.grid-container {
display: grid;
grid-template-columns: repeat({{ column_count }}, auto);
grid-template-columns: repeat(6, auto);
gap: 10px;
justify-content: start;
}
@ -16,18 +13,24 @@
}
.heading {
grid-column: 1 / {{ column_count + 1 }}; /* Spans from the 1st line to the 7th line */
grid-column: 1 / 7; /* Spans from the 1st line to the 7th line */
}
</style>
{% endblock %}
{% macro row(item, badge) %}
<div class="grid-item text-end">{{ item.from.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item text-end">{{ item.to.strftime("%a, %d %b") }}</div>
<div class="grid-item text-end">{{ (item.to.date() - item.from.date()).days }}</div>
<div class="grid-item">{{ item.name }}</div>
<div class="grid-item">{{ item.operator }}</div>
<div class="grid-item">{{ item.location }}</div>
{% endmacro %}
{% macro section(heading, item_list, badge) %}
{% if item_list %}
<div class="heading"><h2>{{heading}}</h2></div>
{% for item in item_list %}
{{ accommodation_row(item, badge) }}
<div class="grid-item">{% if item.linked_trip %} trip: {{ trip_link(item.linked_trip) }} {% endif %}</div>
{% endfor %}
{% for item in item_list %}{{ row(item, badge) }}{% endfor %}
{% endif %}
{% endmacro %}
@ -37,24 +40,13 @@
<h1>Accommodation</h1>
<h3>Statistics</h3>
<ul>
{% for year, stats in year_stats %}
{% set days_in_year = 366 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else 365 %}
{% set total_percentage = (stats.total_nights / days_in_year) * 100 %}
{% set abroad_percentage = (stats.nights_abroad / days_in_year) * 100 %}
<li>
<strong>{{ year }}:</strong>
Total nights away: {{ stats.total_nights }} ({{ "%.1f"|format(total_percentage) }}%),
Total nights abroad: {{ stats.nights_abroad }} ({{ "%.1f"|format(abroad_percentage) }}%)
</li>
{% endfor %}
<li>Total nights away in 2024: {{ total_nights_2024 }}</li>
<li>Total nights abroad in 2024: {{ nights_abroad_2024 }}</li>
</ul>
<div class="grid-container">
{{ section("Current", current) }}
{{ section("Future", future) }}
{{ section("Past", past) }}
{{ section("Accommodation", items) }}
</div>
</div>

View file

@ -7,7 +7,7 @@
<title>{% block title %}{% endblock %}</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📅</text></svg>">
<link href="{{ url_for("static", filename="bootstrap5/css/bootstrap.min.css") }}" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
{% block style %}
{% endblock %}
@ -16,9 +16,8 @@
<body>
{% block nav %}{{ navbar() }}{% endblock %}
{% include "flash_messages.html" %}
{% block content %}{% endblock %}
{% block scripts %}{% endblock %}
<script src="{{ url_for("static", filename="bootstrap5/js/bootstrap.bundle.min.js") }}"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>

View file

@ -1,27 +0,0 @@
{% extends "base.html" %}
{% from "macros.html" import display_date %}
{% block title %}Birthdays - Edward Betts{% endblock %}
{% block content %}
<div class="container-fluid mt-2">
<h1>Birthdays</h1>
<table class="w-auto table">
<thead>
<tr>
<th class="text-end">Date</th>
<th>Event</th>
<th class="text-end">Days</th>
</tr>
</thead>
<tbody>
{% for event in items %}
<tr>
<td class="text-end">{{event.as_date.strftime("%a, %d, %b %Y")}}</td>
<td>{{ event.title }}</td>
<td class="text-end">{{ event.delta_days(today) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}

View file

@ -1,266 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Agenda - Edward Betts</title>
<link href="{{ url_for("static", filename="bootstrap5/css/bootstrap.min.css") }}" rel="stylesheet">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📅</text></svg>">
<!-- TOAST UI Calendar CSS -->
<link rel="stylesheet" href="https://uicdn.toast.com/calendar/latest/toastui-calendar.min.css" />
<style>
/* Custom styles to better integrate with Bootstrap */
.toastui-calendar-layout {
border: 1px solid #dee2e6;
border-radius: 0.375rem;
}
#calendar-header {
margin-bottom: 1rem;
}
</style>
</head>
{% 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 %}
<body>
{{ navbar() }}
<div class="container-fluid mt-2">
<h1>Agenda</h1>
<p>
<a href="/tools">← personal tools</a>
</p>
{% if errors %}
{% for error in errors %}
<div class="alert alert-danger" role="alert">
Error: {{ error }}
</div>
{% endfor %}
{% endif %}
<div>
Markets:
<a href="{{ url_for(request.endpoint) }}">Hide while away</a>
| <a href="{{ url_for(request.endpoint, markets="show") }}">Show all</a>
| <a href="{{ url_for(request.endpoint, markets="hide") }}">Hide all</a>
</div>
<!-- Header for calendar controls -->
<div id="calendar-header" class="d-flex justify-content-between align-items-center mt-3">
<div class="btn-group" role="group">
<button type="button" class="btn btn-primary" id="prevBtn">Prev</button>
<button type="button" class="btn btn-primary" id="nextBtn">Next</button>
<button type="button" class="btn btn-outline-primary" id="todayBtn">Today</button>
</div>
<h3 id="calendar-title" class="mb-0"></h3>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-primary" id="monthViewBtn">Month</button>
<button type="button" class="btn btn-outline-primary" id="weekViewBtn">Week</button>
<button type="button" class="btn btn-outline-primary" id="dayViewBtn">Day</button>
</div>
</div>
<div class="mb-3" id="calendar" style="height: 80vh;"></div>
{#
<div class="mt-2">
<h5>Page generation time</h5>
<ul>
<li>Data gather took {{ "%.1f" | format(data_gather_seconds) }} seconds</li>
<li>Stock market open/close took
{{ "%.1f" | format(stock_market_times_seconds) }} seconds</li>
{% for name, seconds in timings %}
<li>{{ name }} took {{ "%.1f" | format(seconds) }} seconds</li>
{% endfor %}
</ul>
</div>
#}
</div>
<!-- TOAST UI Calendar JS -->
<script src="https://uicdn.toast.com/calendar/latest/toastui-calendar.min.js"></script>
<script src="{{ url_for("static", filename="bootstrap5/js/bootstrap.bundle.min.js") }}"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const Calendar = tui.Calendar;
const container = document.getElementById('calendar');
// --- Configuration ---
const calendars = {{ toastui_calendars | tojson }};
const events = {{ toastui_events | tojson }};
function getSavedView() {
return localStorage.getItem('toastUiCalendarDefaultView') || 'month';
}
function saveView(view) {
localStorage.setItem('toastUiCalendarDefaultView', view);
}
const options = {
defaultView: getSavedView(),
useDetailPopup: true,
useCreationPopup: false,
calendars: calendars,
week: {
startDayOfWeek: 1, // Monday
taskView: false,
eventView: ['time'],
showNowIndicator: true,
},
month: {
startDayOfWeek: 1, // Monday
},
timezone: {
zones: [{
timezoneName: Intl.DateTimeFormat().resolvedOptions().timeZone
}]
},
template: {
allday(event) {
return `<span title="${event.title}">${event.title}</span>`;
},
time(event) {
return `<span title="${event.title}">${event.title}</span>`;
},
},
};
const calendar = new Calendar(container, options);
calendar.createEvents(events);
// --- Event Handlers ---
calendar.on('clickEvent', ({ event }) => {
if (event.raw && event.raw.url) {
window.open(event.raw.url, '_blank');
}
});
calendar.on('afterRenderEvent', () => {
const currentView = calendar.getViewName();
saveView(currentView);
updateCalendarTitle();
});
// --- UI Control Logic ---
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const todayBtn = document.getElementById('todayBtn');
const monthViewBtn = document.getElementById('monthViewBtn');
const weekViewBtn = document.getElementById('weekViewBtn');
const dayViewBtn = document.getElementById('dayViewBtn');
const calendarTitle = document.getElementById('calendar-title');
function updateCalendarTitle() {
const currentView = calendar.getViewName();
const currentDate = calendar.getDate().toDate();
let titleText = '';
if (currentView === 'month') {
const year = currentDate.getFullYear();
const monthName = currentDate.toLocaleDateString('en-GB', { month: 'long' });
titleText = `${monthName} ${year}`;
} else if (currentView === 'week') {
// Manually calculate the start and end of the week
const startDayOfWeek = options.week.startDayOfWeek; // 1 for Monday
const day = currentDate.getDay(); // 0 for Sunday, 1 for Monday...
// Calculate days to subtract to get to the start of the week
const offset = (day - startDayOfWeek + 7) % 7;
const startDate = new Date(currentDate);
startDate.setDate(currentDate.getDate() - offset);
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + 6);
const startMonth = startDate.toLocaleDateString('en-GB', { month: 'short' });
const endMonth = endDate.toLocaleDateString('en-GB', { month: 'short' });
if (startMonth === endMonth) {
titleText = `${startDate.getDate()} - ${endDate.getDate()} ${startMonth} ${startDate.getFullYear()}`;
} else {
titleText = `${startDate.getDate()} ${startMonth} - ${endDate.getDate()} ${endMonth} ${startDate.getFullYear()}`;
}
} else { // Day view
titleText = currentDate.toLocaleDateString('en-GB', { dateStyle: 'full' });
}
calendarTitle.textContent = titleText;
}
// --- View-aware navigation logic (no changes here) ---
prevBtn.addEventListener('click', () => {
const currentView = calendar.getViewName();
if (currentView === 'month') {
const currentDate = calendar.getDate().toDate();
currentDate.setMonth(currentDate.getMonth() - 1);
currentDate.setDate(1);
calendar.setDate(currentDate);
} else {
calendar.prev();
}
updateCalendarTitle();
});
nextBtn.addEventListener('click', () => {
const currentView = calendar.getViewName();
if (currentView === 'month') {
const currentDate = calendar.getDate().toDate();
currentDate.setMonth(currentDate.getMonth() + 1);
currentDate.setDate(1);
calendar.setDate(currentDate);
} else {
calendar.next();
}
updateCalendarTitle();
});
todayBtn.addEventListener('click', () => {
calendar.today();
updateCalendarTitle();
});
monthViewBtn.addEventListener('click', () => {
calendar.changeView('month');
updateCalendarTitle();
});
weekViewBtn.addEventListener('click', () => {
calendar.changeView('week');
updateCalendarTitle();
});
dayViewBtn.addEventListener('click', () => {
calendar.changeView('day');
updateCalendarTitle();
});
// Initial setup
updateCalendarTitle();
});
</script>
</body>
</html>

View file

@ -1,269 +1,63 @@
{% extends "base.html" %}
{% from "macros.html" import trip_link with context %}
{% block title %}Conferences - Edward Betts{% endblock %}
{% block style %}
<style>
/* Timeline */
.conf-timeline {
position: relative;
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 4px;
overflow: hidden;
user-select: none;
}
.conf-tl-month {
position: absolute;
top: 0;
height: 100%;
border-left: 1px solid #dee2e6;
pointer-events: none;
}
.conf-tl-month-label {
position: absolute;
top: 3px;
left: 3px;
font-size: 0.68em;
color: #6c757d;
white-space: nowrap;
}
.conf-tl-today {
position: absolute;
top: 22px;
height: calc(100% - 22px);
border-left: 2px solid #dc3545;
pointer-events: none;
z-index: 10;
}
.conf-tl-bar {
position: absolute;
height: 26px;
border-radius: 3px;
overflow: hidden;
z-index: 5;
}
.conf-tl-bar a, .conf-tl-bar span {
display: block;
padding: 4px 6px;
font-size: 0.72em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 18px;
color: white;
text-decoration: none;
.grid-container {
display: grid;
grid-template-columns: repeat(6, auto); /* 7 columns for each piece of information */
gap: 10px;
justify-content: start;
}
/* Bidirectional hover highlight */
.conf-tl-bar.conf-hl {
filter: brightness(1.25);
box-shadow: inset 0 0 0 2px rgba(255,255,255,0.8);
z-index: 15;
}
tr.conf-hl > td {
background-color: rgba(255, 193, 7, 0.25) !important;
.grid-item {
/* Additional styling for grid items can go here */
}
/* Conference table */
.conf-section-row td {
background: #343a40 !important;
color: #fff;
font-weight: 700;
font-size: 0.85em;
padding-top: 0.5rem;
padding-bottom: 0.4rem;
border-bottom: none;
}
.conf-month-row td {
background: #e9ecef !important;
font-weight: 600;
font-size: 0.8em;
text-transform: uppercase;
letter-spacing: 0.06em;
color: #495057;
padding-top: 0.5rem;
padding-bottom: 0.3rem;
border-bottom: none;
}
.conf-going {
--bs-table-bg: rgba(25, 135, 84, 0.07);
.heading {
grid-column: 1 / 7; /* Spans from the 1st line to the 7th line */
}
</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 %}
<div class="mb-4">
<h3>Next 90 days</h3>
<div class="conf-timeline" style="height: {{ total_h }}px;">
{% for m in timeline.months %}
<div class="conf-tl-month" style="left: {{ m.left_pct }}%;">
<span class="conf-tl-month-label">{{ m.label }}</span>
</div>
{% endfor %}
<div class="conf-tl-today" style="left: 0;"></div>
{% for conf in timeline.confs %}
{% set color = tl_colors[conf.lane % tl_colors | length] %}
{% set top_px = conf.lane * row_h + header_h %}
<div class="conf-tl-bar"
style="left: {{ conf.left_pct }}%; width: {{ conf.width_pct }}%; top: {{ top_px }}px; background: {{ color }};"
title="{{ conf.label }}"
data-conf-key="{{ conf.key }}">
{% if conf.url %}<a href="{{ conf.url }}">{{ conf.name }}</a>
{% else %}<span>{{ conf.name }}</span>{% endif %}
</div>
{% endfor %}
</div>
</div>
{% endif %}
{% endmacro %}
{% macro conf_rows(heading, item_list, badge) %}
{% if item_list %}
{% set count = item_list | length %}
<tr class="conf-section-row">
<td colspan="7">{{ heading }} <span class="fw-normal opacity-75">{{ count }} conference{{ "" if count == 1 else "s" }}</span></td>
</tr>
{% 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 %}
<tr class="conf-month-row">
<td colspan="7">{{ month_label }}</td>
</tr>
{% endif %}
<tr{% if item.going %} class="conf-going"{% endif %} data-conf-key="{{ item.sort_date.isoformat() }}|{{ item.name }}">
<td class="text-nowrap text-muted small">
{{ item.display_date }}
{% if item.date_status == "tentative" %}
<span class="badge text-bg-warning ms-1">tentative</span>
{% elif item.date_status == "approximate" %}
<span class="badge text-bg-secondary ms-1">approximate</span>
{% endif %}
</td>
<td>
{% if item.url %}<a href="{{ item.url }}">{{ item.name }}</a>
{% else %}{{ item.name }}{% endif %}
{% macro row(item, badge) %}
<div class="grid-item text-end">{{ item.start.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item text-end">{{ item.end.strftime("%a, %d %b") }}</div>
<div class="grid-item">{{ item.name }}
{% if item.going and not (item.accommodation_booked or item.travel_booked) %}
<span class="badge text-bg-primary ms-1">{{ badge }}</span>
<span class="badge text-bg-primary">
{{ badge }}
</span>
{% endif %}
{% if item.accommodation_booked %}
<span class="badge text-bg-success ms-1">accommodation</span>
<span class="badge text-bg-success">accommodation</span>
{% endif %}
{% if item.transport_booked %}
<span class="badge text-bg-success ms-1">transport</span>
<span class="badge text-bg-success">transport</span>
{% endif %}
{% if item.linked_trip %}
{% set trip = item.linked_trip %}
<a href="{{ url_for('trip_page', start=trip.start.isoformat()) }}"
class="text-muted ms-1 text-decoration-none"
title="Trip: {{ trip.title }}">
🧳{% if trip.title != item.name %} {{ trip.title }}{% endif %}
</a>
{% endif %}
</td>
<td class="text-muted small">{{ item.topic }}</td>
<td class="text-nowrap text-muted small">
{% if item.series and item.series_detail %}
<a href="{{ url_for('conference_series_page', series_id=item.series) }}">{{ item.series_detail.name }}</a>
{% elif item.series %}
{{ item.series }}
{% endif %}
</td>
<td class="text-nowrap">
{% 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 %}
</td>
<td class="text-nowrap text-muted small">
{% if item.cfp_end %}{{ item.cfp_end.strftime("%-d %b %Y") }}{% endif %}
</td>
<td>
{% if item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,d}".format(item.price | int) }} {{ item.currency }}</span>
{% if item.currency != "GBP" and item.currency in fx_rate %}
<span class="badge bg-info text-nowrap">{{ "{:,.0f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% elif item.free %}
<span class="badge bg-success text-nowrap">free</span>
{% endif %}
</td>
</tr>
{% endfor %}
</div>
<div class="grid-item">{{ item.topic }}</div>
<div class="grid-item">{{ item.location }}</div>
<div class="grid-item"><a href="{{ item.url }}">{{ item.url }}</a></div>
{% endmacro %}
{% macro section(heading, item_list, badge) %}
{% if item_list %}
<div class="heading"><h2>{{heading}}</h2></div>
{% for item in item_list %}{{ row(item, badge) }}{% endfor %}
{% endif %}
{% endmacro %}
{% block content %}
<div class="container-fluid mt-2">
<h1>Conferences</h1>
{{ render_timeline(timeline) }}
<table class="table table-sm table-hover align-middle">
<colgroup>
<col style="width: 9rem">
<col>
<col style="width: 18rem">
<col style="width: 14rem">
<col style="width: 14rem">
<col style="width: 7rem">
<col style="width: 10rem">
</colgroup>
<thead class="table-light">
<tr>
<th>Dates</th>
<th>Conference</th>
<th>Topic</th>
<th>Series</th>
<th>Location</th>
<th>CFP ends</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{{ conf_rows("Current", current, "attending") }}
{{ conf_rows("Future", future, "going") }}
{{ conf_rows("Past", past|reverse|list, "went") }}
</tbody>
</table>
<div class="grid-container">
{{ section("Current", current, "attending") }}
{{ section("Future", future, "going") }}
{{ section("Past", past|reverse, "went") }}
</div>
</div>
<script>
(function () {
// Build a map from conf-key → all matching elements (bars + rows)
const map = new Map();
document.querySelectorAll('[data-conf-key]').forEach(el => {
const k = el.dataset.confKey;
if (!map.has(k)) map.set(k, []);
map.get(k).push(el);
});
function setHighlight(key, on) {
(map.get(key) || []).forEach(el => el.classList.toggle('conf-hl', on));
}
map.forEach((els, key) => {
els.forEach(el => {
el.addEventListener('mouseenter', () => setHighlight(key, true));
el.addEventListener('mouseleave', () => setHighlight(key, false));
});
});
})();
</script>
{% endblock %}

View file

@ -1,85 +0,0 @@
{% extends "base.html" %}
{% block title %}{{ series.name }} - Edward Betts{% endblock %}
{% block content %}
<div class="container-fluid mt-2">
<h1>{{ series.name }}</h1>
<dl class="row">
{% if series.topic %}
<dt class="col-sm-2">Topic</dt>
<dd class="col-sm-10">{{ series.topic }}</dd>
{% endif %}
{% if series.usual_location or series.country %}
<dt class="col-sm-2">Usual location</dt>
<dd class="col-sm-10">
{% set country = get_country(series.country) if series.country else None %}
{% if country %}{{ country.flag }} {% endif %}{{ series.usual_location or "" }}
</dd>
{% endif %}
{% if series.cadence %}
<dt class="col-sm-2">Cadence</dt>
<dd class="col-sm-10">{{ series.cadence }}</dd>
{% endif %}
{% if series.url %}
<dt class="col-sm-2">Website</dt>
<dd class="col-sm-10"><a href="{{ series.url }}">{{ series.url }}</a></dd>
{% endif %}
{% if series.notes %}
<dt class="col-sm-2">Notes</dt>
<dd class="col-sm-10">{{ series.notes }}</dd>
{% endif %}
</dl>
<h2>Editions</h2>
<table class="table table-sm table-hover align-middle">
<thead class="table-light">
<tr>
<th>Dates</th>
<th>Conference</th>
<th>Location</th>
<th>Attendance</th>
</tr>
</thead>
<tbody>
{% for item in conferences %}
<tr>
<td class="text-nowrap text-muted small">
{{ item.display_date }}
{% if item.date_status == "tentative" %}
<span class="badge text-bg-warning ms-1">tentative</span>
{% elif item.date_status == "approximate" %}
<span class="badge text-bg-secondary ms-1">approximate</span>
{% endif %}
</td>
<td>
{% if item.url %}<a href="{{ item.url }}">{{ item.name }}</a>
{% else %}{{ item.name }}{% endif %}
</td>
<td>
{% set country = get_country(item.country) if item.country else None %}
{% if country %}{{ country.flag }} {% endif %}{{ item.location }}
</td>
<td>
{% if item.going %}
<span class="badge text-bg-success">going</span>
{% endif %}
{% if item.registered %}
<span class="badge text-bg-primary">registered</span>
{% endif %}
{% if item.linked_trip %}
{% set trip = item.linked_trip %}
<a href="{{ url_for('trip_page', start=trip.start.isoformat()) }}"
class="ms-1"
title="Trip: {{ trip.title }}">
trip{% if trip.title != item.name %}: {{ trip.title }}{% endif %}
</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}

View file

@ -1,47 +0,0 @@
{% extends "base.html" %}
{% block title %}Conference Series - Edward Betts{% endblock %}
{% block content %}
<div class="container-fluid mt-2">
<h1>Conference Series</h1>
<table class="table table-sm table-hover align-middle">
<thead class="table-light">
<tr>
<th>Series</th>
<th>Topic</th>
<th>Usual location</th>
<th>Conferences</th>
<th>Next</th>
</tr>
</thead>
<tbody>
{% for item in series_list %}
<tr>
<td>
<a href="{{ url_for('conference_series_page', series_id=item.id) }}">{{ item.name }}</a>
{% if item.attended %}
<span class="badge text-bg-success ms-1">attended</span>
{% endif %}
{% if item.url %}
<a class="text-muted ms-1 text-decoration-none" href="{{ item.url }}"></a>
{% endif %}
</td>
<td class="text-muted small">{{ item.topic or "" }}</td>
<td>
{% set country = get_country(item.country) if item.country else None %}
{% if country %}{{ country.flag }} {% endif %}{{ item.usual_location or "" }}
</td>
<td>{{ item.count }}</td>
<td class="text-muted small">
{% if item.next_conf %}
{{ item.next_conf.display_date }} · {{ item.next_conf.name }}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}

View file

@ -1,46 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Map fixture</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📅</text></svg>">
<link href="{{ url_for("static", filename="bootstrap5/css/bootstrap.min.css") }}" rel="stylesheet">
<link rel="stylesheet" href="{{ url_for("static", filename="leaflet/leaflet.css") }}">
<style>
body, html {
height: 100%;
margin: 0;
}
#map {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<script src="{{ url_for("static", filename="leaflet/leaflet.js") }}"></script>
<script src="{{ url_for("static", filename="leaflet-geodesic/leaflet.geodesic.umd.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/map.js") }}"></script>
<script>
var coordinates = {{ coordinates | tojson }};
var routes = {{ routes | tojson }};
build_map("map", coordinates, routes);
</script>
<script src="{{ url_for("static", filename="bootstrap5/js/bootstrap.bundle.min.js") }}"></script>
</body>
</html>

View file

@ -1,12 +0,0 @@
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="container-fluid mt-2">
{% for message in messages %}
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
</div>
{% endif %}
{% endwith %}

View file

@ -1,5 +1,4 @@
{% extends "base.html" %}
{% block title %}Gaps - Edward Betts{% endblock %}
{% block content %}
<div class="p-2">
@ -18,31 +17,11 @@
<tbody>
{% for gap in gaps %}
<tr>
<td class="text-start">
{% for event in gap.before %}
<div>
{% if event.url %}
<a href="{{event.url}}">{{ event.title_with_emoji }}</a>
{% else %}
{{ event.title_with_emoji }}
{% endif %}
</div>
{% endfor %}
</td>
<td>{% for event in gap.before %}{% if not loop.first %}<br/>{% endif %}<span class="text-nowrap">{{ event.title or event.name }}</span>{% endfor %}</td>
<td class="text-end text-nowrap">{{ gap.start.strftime("%A, %-d %b %Y") }}</td>
<td class="text-end text-nowrap">{{ (gap.end - gap.start).days }} days</td>
<td class="text-end text-nowrap">{{ gap.end.strftime("%A, %-d %b %Y") }}</td>
<td class="text-start">
{% for event in gap.after %}
<div>
{% if event.url %}
<a href="{{event.url}}">{{ event.title_with_emoji }}</a>
{% else %}
{{ event.title_with_emoji }}
{% endif %}
</div>
{% endfor %}
</td>
<td>{% for event in gap.after %}{% if not loop.first %}<br/>{% endif %}<span class="text-nowrap">{{ event.title or event.name }}</span>{% endfor %}</td>
</tr>
{% endfor %}
</tbody>

View file

@ -1,39 +0,0 @@
{% extends "base.html" %}
{% from "macros.html" import display_date %}
{% block title %}Holidays - Edward Betts{% endblock %}
{% block content %}
<div class="container-fluid mt-2">
<h1>Public holidays</h1>
<table class="table table-hover w-auto">
{% for item in items %}
{% set country = get_country(item.country) %}
<tr>
{% if loop.first or item.date != loop.previtem.date %}
<td class="text-end">{{ display_date(item.date) }}</td>
<td>in {{ (item.date - today).days }} days</td>
{% else %}
<td colspan="2"></td>
{% endif %}
<td>{{ country.flag }} {{ country.name }}</td>
<td>{{ item.display_name }}</td>
</tr>
{% endfor %}
</table>
<h2>
UK school holidays (Bristol)
<small class="fs-6 ms-2"><a href="{{ school_holiday_page_url }}">source</a></small>
</h2>
<table class="table table-hover w-auto">
{% for item in school_holidays %}
<tr>
<td class="text-end">{{ display_date(item.as_date) }}</td>
<td>to {{ display_date(item.end_as_date) }}</td>
<td>in {{ (item.as_date - today).days }} days</td>
<td>{{ item.title }}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}

View file

@ -3,11 +3,72 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Agenda - Edward Betts</title>
<link href="{{ url_for("static", filename="bootstrap5/css/bootstrap.min.css") }}" rel="stylesheet">
<title>Agenda</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📅</text></svg>">
<script async src="{{ url_for("static", filename="es-module-shims/es-module-shims.js") }}"></script>
<script async src="https://unpkg.com/es-module-shims@1.8.2/dist/es-module-shims.js"></script>
<script type='importmap'>
{
"imports": {
"@fullcalendar/core": "https://cdn.skypack.dev/@fullcalendar/core@6.1.9",
"@fullcalendar/daygrid": "https://cdn.skypack.dev/@fullcalendar/daygrid@6.1.9",
"@fullcalendar/timegrid": "https://cdn.skypack.dev/@fullcalendar/timegrid@6.1.9",
"@fullcalendar/list": "https://cdn.skypack.dev/@fullcalendar/list@6.1.9",
"@fullcalendar/core/locales/en-gb": "https://cdn.skypack.dev/@fullcalendar/core@6.1.9/locales/en-gb"
}
}
</script>
<script type='module'>
import { Calendar } from '@fullcalendar/core'
import dayGridPlugin from '@fullcalendar/daygrid'
import timeGridPlugin from '@fullcalendar/timegrid'
import listPlugin from '@fullcalendar/list'
import gbLocale from '@fullcalendar/core/locales/en-gb';
// Function to save the current view to local storage
function saveView(view) {
localStorage.setItem('fullCalendarDefaultView', view);
}
// Function to get the saved view from local storage
function getSavedView() {
return localStorage.getItem('fullCalendarDefaultView') || 'dayGridMonth';
}
document.addEventListener('DOMContentLoaded', function() {
const calendarEl = document.getElementById('calendar')
const calendar = new Calendar(calendarEl, {
locale: gbLocale,
plugins: [dayGridPlugin, timeGridPlugin, listPlugin ],
themeSystem: 'bootstrap5',
firstDay: 1,
initialView: getSavedView(),
viewDidMount: function(info) {
saveView(info.view.type);
},
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
},
nowIndicator: true,
weekNumbers: true,
eventTimeFormat: {
hour: '2-digit',
minute: '2-digit',
hour12: false,
},
events: {{ fullcalendar_events | tojson(indent=2) }},
eventDidMount: function(info) {
info.el.title = info.event.title;
},
})
calendar.render()
})
</script>
</head>
@ -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 %}
<body>
{{ navbar() }}
{% include "flash_messages.html" %}
<div class="container-fluid mt-2">
<h1>Agenda</h1>
@ -70,49 +127,16 @@
Sunset: {{ sunset.strftime("%H:%M:%S") }}</li>
</ul>
{% if home_weather %}
<div class="mb-2">
<strong>Bristol weather:</strong>
{% for day in home_weather %}
<span class="me-3 text-nowrap">
<small class="text-muted">{{ day.date_obj.strftime("%-d %b") }}</small>
<img src="https://openweathermap.org/img/wn/{{ day.icon }}.png" alt="{{ day.status }}" title="{{ day.detailed_status }}" width="25" height="25">
{{ day.temp_min }}{{ day.temp_max }}°C
</span>
{% endfor %}
</div>
{% endif %}
{% if errors %}
{% for error in errors %}
<div class="alert alert-danger" role="alert">
Error: {{ error }}
</div>
{% endfor %}
{% endif %}
<h3>Stock markets</h3>
{% for market in stock_markets %}
<p>{{ market }}</p>
{% endfor %}
{% if current_trip %}
<div>
<h3>Current trip</h3>
{{ trip_item(current_trip) }}
</div>
{% endif %}
<div class="mb-3" id="calendar"></div>
<h3>Agenda</h3>
<div>
Markets:
<a href="{{ url_for(request.endpoint) }}">Hide while away</a>
| <a href="{{ url_for(request.endpoint, markets="show") }}">Show all</a>
| <a href="{{ url_for(request.endpoint, markets="hide") }}">Hide all</a>
</div>
{% 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 %}
<div class="row mt-2">
<div class="col">
@ -142,12 +166,11 @@
<div class="col-md-2{{ cell_bg }}">
{% 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 %}
</div>
@ -155,7 +178,7 @@
<div class="col-md-7 text-start">
{% if event.url %}<a href="{{ event.url }}">{% endif %}
{{ event_labels.get(event.name) or event.name }}
{%- if event.title -%}: {{ event.title_with_emoji }}{% endif %}
{%- if event.title -%}: {{ event.title }}{% endif %}
{% if event.url %}</a>{% endif %}
</div>
<div class="col-md-1{{ cell_bg }}">
@ -165,21 +188,7 @@
{% endif %}
{% endfor %}
<div class="mt-2">
<h5>Page generation time</h5>
<ul>
<li>Data gather took {{ "%.1f" | format(data_gather_seconds) }} seconds</li>
<li>Stock market open/close took
{{ "%.1f" | format(stock_market_times_seconds) }} seconds</li>
{% for name, seconds in timings %}
<li>{{ name }} took {{ "%.1f" | format(seconds) }} seconds</li>
{% endfor %}
<li>Render time: {{ "%.1f" | format(render_time) }} seconds</li>
</ul>
</div>
</div>
<script src="{{ url_for("static", filename="bootstrap5/js/bootstrap.bundle.min.js") }}"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>

View file

@ -1,63 +1,11 @@
{% extends "base.html" %}
{% block title %}Space launches - Edward Betts{% endblock %}
{% block content %}
<div class="container-fluid mt-2">
<h1>Space launches</h1>
<h4>Filters</h4>
<p>Mission type:
{% if request.args.type %}<a href="{{ request.path }}">🗙</a>{% endif %}
{% for t in mission_types | sort %}
{% if t == request.args.type %}
<strong>{{ t }}</strong>
{% else %}
<a href="?type={{ t }}" class="text-nowrap">
{{ t }}
</a>
{% endif %}
{% if not loop.last %} | {% endif %}
{% endfor %}
</p>
<p>Vehicle:
{% if request.args.rocket %}<a href="{{ request.path }}">🗙</a>{% endif %}
{% for r in rockets | sort %}
{% if r == request.args.rockets %}
<strong>{{ r }}</strong>
{% else %}
<a href="?rocket={{ r }}" class="text-nowrap">
{{ r }}
</a>
{% endif %}
{% if not loop.last %} | {% endif %}
{% endfor %}
</p>
<p>Orbit:
{% if request.args.orbit %}<a href="{{ request.path }}">🗙</a>{% endif %}
{% for name, abbrev in orbits | sort %}
{% if abbrev == request.args.orbit %}
<strong>{{ name }}</strong>
{% else %}
<a href="?orbit={{ abbrev }}" class="text-nowrap">
{{ name }}
</a>
{% endif %}
{% if not loop.last %} | {% endif %}
{% endfor %}
</p>
{% for launch in launches %}
{% set highlight =" bg-primary-subtle" if launch.slug in config.FOLLOW_LAUNCHES else "" %}
{% set country = get_country(launch.country_code) %}
<div class="row{{highlight}}">
{% for launch in rockets %}
<div class="row">
<div class="col-md-1 text-nowrap text-md-end">{{ launch.t0_date }}
<br class="d-none d-md-block"/>
@ -68,20 +16,8 @@
<div class="col-md-1 text-md-nowrap">
<span class="d-md-none">launch status:</span>
<abbr title="{{ launch.status.name }}">{{ launch.status.abbrev }}</abbr>
{% if launch.is_active_crewed %}
<span class="badge text-bg-info">In space</span>
{% endif %}
{% if launch.is_future and launch.probability %}{{ launch.probability }}%{% endif %}
</div>
<div class="col">
<div>
{% if launch.image %}
<div>
<img src="{{ launch.image }}" class="img-fluid img-thumbnail" style="max-width: 350px; height: auto;" />
</div>
{% endif %}
<abbr title="{{ country.name }}">{{ country.flag }}</abbr>
{{ launch.rocket.full_name }}
<div class="col">{{ launch.rocket }}
&ndash;
<strong>{{launch.mission.name }}</strong>
&ndash;
@ -94,29 +30,14 @@
({{ launch.launch_provider_type }})
&mdash;
{{ launch.orbit.name }} ({{ launch.orbit.abbrev }})
&mdash;
{{ launch.mission.type }}
</div>
<div>
<br/>
{% if launch.pad_wikipedia_url %}
<a href="{{ launch.pad_wikipedia_url }}">{{ launch.pad_name }}</a>
{% else %}
{{ launch.pad_name }} {% if launch.pad_name != "Unknown Pad" %}(no Wikipedia article){% endif %}
{% endif %}
&mdash; {{ launch.location }}
</div>
{% if launch.mission.agencies | count %}
<div>
{% for agency in launch.mission.agencies %}
{% set agency_country = get_country(agency.country_code) %}
{%- if not loop.first %}, {% endif %}
<a href="{{ agency.wiki_url }}">{{agency.name }}</a>
<abbr title="{{ agency_country.name }}">{{ agency_country.flag }}</abbr>
({{ agency.type }}) {# <img src="{{ agency.logo_url }}"/> #}
{% endfor %}
</div>
{% endif %}
<div>
&mdash; {{ launch.location }}<br/>
{% if launch.mission %}
{% for line in launch.mission.description.splitlines() %}
<p>{{ line }}</p>
@ -124,13 +45,7 @@
{% else %}
<p>No description.</p>
{% endif %}
{% if launch.weather_concerns and launch.status.name != "Launch Successful" %}
<h4>Weather concerns</h4>
{% for line in launch.weather_concerns.splitlines() %}
<p>{{ line }}</p>
{% endfor %}
{% endif %}
</div>
</div>
</div>
{% endfor %}

View file

@ -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) %}
<a href="{{ url_for("trip_page", start=trip.start.isoformat()) }}">{{ trip.title }}</a>
{% endmacro %}
{% macro conference_row(item, badge, show_flags=True) %}
{% set country = get_country(item.country) if item.country else None %}
<div class="grid-item text-end text-nowrap">{{ item.start.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item text-end text-nowrap">{{ item.end.strftime("%a, %d %b") }}</div>
<div class="grid-item">
{% if item.url %}
<a href="{{ item.url }}">{{ item.name }}</a>
{% else %}
{{ item.name }}
{% endif %}
{% if item.going and not (item.accommodation_booked or item.travel_booked) %}
<span class="badge text-bg-primary">
{{ badge }}
</span>
{% endif %}
{% if item.accommodation_booked %}
<span class="badge text-bg-success">accommodation</span>
{% endif %}
{% if item.transport_booked %}
<span class="badge text-bg-success">transport</span>
{% endif %}
</div>
<div class="grid-item text-end">
{% if item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,d}".format(item.price | int) }} {{ item.currency }}</span>
{% if item.currency != "GBP" and item.currency in fx_rate %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% elif item.free %}
<span class="badge bg-success text-nowrap">free to attend</span>
{% endif %}
</div>
<div class="grid-item">{{ item.topic }}</div>
<div class="grid-item">{{ item.location }}</div>
<div class="grid-item text-end text-nowrap">{{ display_date(item.cfp_end) if item.cfp_end else "" }}</div>
<div class="grid-item">
{% if country %}
{% if show_flags %}{{ country.flag }}{% endif %} {{ country.name }}
{% elif item.online %}
💻 Online
{% else %}
<span class="text-bg-danger p-2">
country code <strong>{{ item.country }}</strong> not found
</span>
{% endif %}
</div>
{% endmacro %}
{% macro accommodation_row(item, badge, show_flags=True) %}
{% set country = get_country(item.country) %}
{% set nights = (item.to.date() - item.from.date()).days %}
<div class="grid-item text-end">{{ item.from.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item text-end">{{ item.to.strftime("%a, %d %b") }}</div>
<div class="grid-item text-end">{% if nights == 1 %}1 night{% else %}{{ nights }} nights{% endif %}</div>
<div class="grid-item">{{ item.operator }}</div>
<div class="grid-item">{{ item.location }}</div>
<div class="grid-item">
{% if country %}
{% if show_flags %}{{ country.flag }}{% endif %} {{ country.name }}
{% else %}
<span class="text-bg-danger p-2">
country code <strong>{{ item.country }}</strong> not found
</span>
{% endif %}
</div>
<div class="grid-item">
{% if g.user.is_authenticated and item.url %}
<a href="{{ item.url }}">{{ item.name }}</a>
{% else %}
{{ item.name }}
{% endif %}
</div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% 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) %}
<div class="grid-item">
{% if g.user.is_authenticated %}
{{ booking.booking_reference or "reference missing" }}
{% else %}
<em>redacted</em>
{% endif %}
</div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and booking.price and booking.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(booking.price) }} {{ booking.currency }}</span>
{% if booking.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(booking.price / fx_rate[booking.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% for i in range(9) %}
<div class="grid-item"></div>
{% 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 %}
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">{{ item.from }} → {{ item.to }}</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item">{{ item.duration }}</div>
<div class="grid-item">{{ full_flight_number }}</div>
<div class="grid-item">
<a href="{{ flightradar24_url(item) }}">flightradar24</a>
| <a href="https://uk.flightaware.com/live/flight/{{ full_flight_number }}">FlightAware</a>
| <a href="{{ radarbox_url }}">radarbox</a>
</div>
<div class="grid-item text-end">
{% if item.distance %}
{{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }}
{% endif %}
</div>
<div class="grid-item text-end">{{ "{:,.1f}".format(item.co2_kg) }} kg</div>
{% 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 %}
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">{{ item.from }} → {{ item.to }}</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item">{{ item.duration }}</div>
<div class="grid-item">{{ full_flight_number }}</div>
<div class="grid-item">
{% if g.user.is_authenticated %}
{{ item.booking_reference }}
{% else %}
<em>redacted</em>
{% endif %}
</div>
<div class="grid-item">
<a href="{{ flightradar24_url(item) }}">flightradar24</a>
| <a href="https://uk.flightaware.com/live/flight/{{ full_flight_number }}">FlightAware</a>
| <a href="{{ radarbox_url }}">radarbox</a>
</div>
<div class="grid-item text-end">
{% if item.distance %}
{{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }}
{% endif %}
</div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% endmacro %}
{% macro train_row(item) %}
{% set url = item.url %}
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">
{% if g.user.is_authenticated and item.url %}<a href="{{ url }}">{% endif %}
{{ item.from }} → {{ item.to }}
{% if g.user.is_authenticated and item.url %}</a>{% endif %}
</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item">{{ ((item.arrive - item.depart).total_seconds() // 60) | int }} mins</div>
<div class="grid-item">{{ item.operator }}</div>
<div class="grid-item">
{% if g.user.is_authenticated %}
{{ item.booking_reference }}
{% else %}
<em>redacted</em>
{% endif %}
</div>
<div class="grid-item">
{% for leg in item.legs %}
{% if leg.url %}
<a href="{{ leg.url }}">[{{ loop.index }}]</a>
{% endif %}
{% endfor %}
</div>
<div class="grid-item text-end">
{% if item.distance %}
{{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }}
{% endif %}
</div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" and item.currency in fx_rate %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% endmacro %}
{% macro coach_row(item) %}
{% set url = item.url %}
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">
{% if g.user.is_authenticated and item.url %}<a href="{{ url }}">{% endif %}
{{ item.from }} → {{ item.to }}
{% if g.user.is_authenticated and item.url %}</a>{% endif %}
</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item">{{ ((item.arrive - item.depart).total_seconds() // 60) | int }} mins</div>
<div class="grid-item">{{ item.operator }}</div>
<div class="grid-item">
{% if g.user.is_authenticated %}
{{ item.booking_reference }}
{% else %}
<em>redacted</em>
{% endif %}
</div>
<div class="grid-item">
</div>
<div class="grid-item text-end">
{% if item.distance %}
{{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }}
{% endif %}
</div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" and item.currency in fx_rate %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% endmacro %}
{% macro bus_row(item) %}
{% set url = item.url %}
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">
{% if g.user.is_authenticated and item.url %}<a href="{{ url }}">{% endif %}
{{ item.from }} → {{ item.to }}
{% if g.user.is_authenticated and item.url %}</a>{% endif %}
</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item">{{ ((item.arrive - item.depart).total_seconds() // 60) | int }} mins</div>
<div class="grid-item">{{ item.operator }}</div>
<div class="grid-item">
{% if g.user.is_authenticated %}
{{ item.booking_reference }}
{% else %}
<em>redacted</em>
{% endif %}
</div>
<div class="grid-item">
</div>
<div class="grid-item text-end">
{% if item.distance %}
{{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }}
{% endif %}
</div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" and item.currency in fx_rate %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% endmacro %}
{% macro ferry_row(item) %}
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">
{{ item.from }} → {{ item.to }}
</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item"></div>
<div class="grid-item">{{ item.operator }}</div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{# <div class="grid-item">{{ item | pprint }}</div> #}
{% 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 %}
<div class="trip-conference-card my-1">
<div class="card-body">
<h5 class="card-title">
<a href="{{ item.url }}">{{ item.name }}</a>
<small class="text-muted">
{{ 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 %}
</small>
</h5>
<p class="card-text">
Topic: {{ item.topic }}
| Venue: {{ item.venue }}
| Location: {{ item.location }}
{% if country %}
{{ flag(trip, country.flag) }}
{% elif item.online %}
💻 Online
{% else %}
<span class="text-bg-danger p-2">
country code <strong>{{ item.country }}</strong> not found
</span>
{% endif %}
{% if item.free %}
| <span class="badge bg-success text-nowrap">free to attend</span>
{% elif item.price and item.currency %}
| <span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
{% endif %}
</p>
</div>
</div>
{% 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 %}
<div class="trip-element">
{{ e.get_emoji() }} <strong>{{ item.name }}</strong>
{% if item.operator and item.operator != item.name %}<small class="text-muted">{{ item.operator }}</small>{% endif %}
<small class="text-muted">({% if nights == 1 %}1 night{% else %}{{ nights }} nights{% endif %})</small>
</div>
{% elif e.element_type == "check-out" %}
<div class="trip-checkout">
{{ e.get_emoji() }} Check out: {{ item.name }}
{% if item.operator and item.operator != item.name %}<span class="text-muted small">{{ item.operator }}</span>{% endif %}
</div>
{% elif e.element_type != "conference" %}
{# Transport: flight, train, ferry, coach, bus #}
{% 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 %}
<div class="trip-element">
{% 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 %} <span class="text-muted small">+1 day</span>{% endif %}{% endif %}
{% endif %}
{% if dur_mins %}
{%- set h = dur_mins // 60 %}{%- set m = dur_mins % 60 %}
<span class="text-muted">🕒{% if h %}{{ h }}h {% endif %}{% if m %}{{ m }}m{% endif %}</span>
{% endif %}
{% if e.element_type == "flight" %}
<small class="text-muted">· {{ item.airline_name }} {{ item.airline_code }}{{ item.flight_number }}</small>
{% elif item.operator %}
<small class="text-muted">· {{ item.operator }}</small>
{% endif %}
{% if item.distance %}
<span class="text-muted small">· {{ "{:,.0f} km".format(item.distance) }}</span>
{% endif %}
{% if item.co2_kg is defined and item.co2_kg is not none %}
<span class="text-muted small">CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg</span>
{% endif %}
</div>
{% 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 %}
<div class="trip-card{% if is_current %} trip-current{% endif %}">
<h3 class="trip-name">
{{ trip_link(trip) }}
<small>({{ display_date(trip.start) }})</small></h3>
{% set school_holidays = trip_school_holiday_map.get(trip.start.isoformat(), []) if trip_school_holiday_map is defined else [] %}
{% if school_holidays %}
<div class="school-holiday-info">
<span class="badge bg-warning text-dark">UK school holiday</span>
{% for item in school_holidays %}
<span class="text-muted">{{ item.title }} ({{ display_date_no_year(item.as_date) }} to {{ display_date_no_year(item.end_as_date) }})</span>
{% endfor %}
</div>
{% endif %}
<ul class="list-unstyled trip-countries">
{% for c in trip.countries %}
<li>{{ c.flag }} {{ c.name }}</li>
{% endfor %}
</ul>
{% if end %}
<div class="trip-dates">Dates: {{ display_date_no_year(trip.start) }} to {{ display_date_no_year(end) }}
{% if g.user.is_authenticated and trip.start <= today %}
<a href="https://photos.4angle.com/search?query=%7B%22takenAfter%22%3A%22{{trip.start}}T00%3A00%3A00.000Z%22%2C%22takenBefore%22%3A%22{{end}}T23%3A59%3A59.999Z%22%7D">photos</a>
{% endif %}
</div>
{% else %}
<div>Start: {{ display_date_no_year(trip.start) }} (end date missing)</div>
{% endif %}
<div class="trip-stats">
{% if total_distance %}
<span class="trip-stat">{{ format_distance(total_distance) }}</span>
{% endif %}
{% if distances_by_transport_type %}
{% for transport_type, distance in distances_by_transport_type %}
<span class="trip-stat">{{ transport_type | title }}: {{format_distance(distance) }}</span>
{% endfor %}
{% endif %}
{% if total_co2_kg %}
<span class="trip-stat">CO₂ {{ "{:,.1f}".format(total_co2_kg) }} kg</span>
{% endif %}
{% set co2_by_transport = trip.co2_by_transport_type() %}
{% if co2_by_transport %}
{% for transport_type, co2_kg in co2_by_transport %}
<span class="trip-stat">{{ transport_type | title }} CO₂ {{ "{:,.1f}".format(co2_kg) }} kg</span>
{% endfor %}
{% endif %}
</div>
{% if trip.schengen_compliance %}
<div>
<strong>Schengen:</strong>
{% if trip.schengen_compliance.is_compliant %}
<span class="badge bg-success">✅ Compliant</span>
{% else %}
<span class="badge bg-danger">❌ Non-compliant</span>
{% endif %}
<span class="text-muted small">({{ trip.schengen_compliance.total_days_used }}/90 days used)</span>
</div>
{% 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()) %}
<h4 class="trip-day-header">{{ display_date_no_year(day) }}
{% if weather %}
<span class="trip-weather-inline">
<img src="https://openweathermap.org/img/wn/{{ weather.icon }}.png" alt="{{ weather.status }}" title="{{ weather.detailed_status }}" width="18" height="18">
{{ weather.temp_min }}{{ weather.temp_max }}°C
<span class="fw-normal fst-italic">{{ weather.detailed_status }}</span>
</span>
{% endif %}
{% if g.user.is_authenticated and day <= today %}
<a class="ms-2 small fw-normal" href="https://photos.4angle.com/search?query=%7B%22takenAfter%22%3A%22{{day}}T00%3A00%3A00.000Z%22%2C%22takenBefore%22%3A%22{{day}}T23%3A59%3A59.999Z%22%7D">photos</a>
{% endif %}
</h4>
{% for e in elements %}
{{ render_trip_element(e, trip) }}
{% endfor %}
{% endfor %}
</div>
{% endmacro %}

View file

@ -1,54 +0,0 @@
{% extends "base.html" %}
{% block title %}Meteor Showers 2025 - Edward Betts{% endblock %}
{% block content %}
<div class="container-fluid mt-2">
<h1>Meteor Showers 2025</h1>
<p>Major meteor showers visible throughout 2025. All times are approximate and viewing conditions depend on light pollution, weather, and moon phase.</p>
{% for meteor in meteors %}
<div class="row mb-4">
<div class="col-md-2 text-nowrap text-md-end">
<strong>{{ meteor.peak }}</strong>
<br class="d-none d-md-block"/>
<small class="text-muted">{{ meteor.active }}</small>
</div>
<div class="col-md-2 text-md-nowrap">
<span class="d-md-none">Rate:</span>
{{ meteor.rate }}
<br class="d-none d-md-block"/>
<small class="text-muted">{{ meteor.radiant }}</small>
</div>
<div class="col">
<div>
<h4>{{ meteor.name }}</h4>
<p><strong>Moon Phase:</strong> {{ meteor.moon_phase }}</p>
<p><strong>Best Visibility:</strong> {{ meteor.visibility }}</p>
<p>{{ meteor.description }}</p>
</div>
</div>
</div>
{% endfor %}
<div class="mt-4">
<h3>Viewing Tips</h3>
<ul>
<li>Find a dark location away from city lights</li>
<li>Allow your eyes to adjust to darkness for 20-30 minutes</li>
<li>Look northeast for most showers, but meteors can appear anywhere</li>
<li>Best viewing is typically after midnight</li>
<li>No telescope needed - use naked eye viewing</li>
<li>Check weather conditions and moon phase before planning</li>
</ul>
</div>
<div class="mt-4">
<h3>About Meteor Showers</h3>
<p>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."</p>
<p><strong>Data Sources:</strong> Information compiled from NASA, American Meteor Society, and astronomical observations for 2025.</p>
</div>
</div>
{% endblock %}

View file

@ -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 @@
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
{% for page in pages_before_dropdowns %}
{% for page in pages %}
{% set is_active = request.endpoint == page.endpoint %}
<li class="nav-item">
<a class="nav-link{% if is_active %} border border-white border-2 active{% endif %}" {% if is_active %} aria-current="page"{% endif %} href="{{ url_for(page.endpoint) }}">
@ -48,50 +26,6 @@
</a>
</li>
{% endfor %}
<!-- Trip dropdown -->
{% set trip_active = request.endpoint in ['trip_future_list', 'trip_past_list', 'trip_stats'] %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle{% if trip_active %} border border-white border-2 active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Trips
</a>
<ul class="dropdown-menu">
{% for page in trip_pages %}
{% set is_active = request.endpoint == page.endpoint %}
<li><a class="dropdown-item{% if is_active %} active{% endif %}" href="{{ url_for(page.endpoint) }}">{{ page.label }}</a></li>
{% endfor %}
</ul>
</li>
<!-- Conference dropdown -->
{% set conference_active = request.endpoint in ['conference_list', 'past_conference_list', 'conference_series_list', 'conference_series_page'] %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle{% if conference_active %} border border-white border-2 active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Conferences
</a>
<ul class="dropdown-menu">
{% for page in conference_pages %}
{% set is_active = request.endpoint == page.endpoint %}
<li><a class="dropdown-item{% if is_active %} active{% endif %}" href="{{ url_for(page.endpoint) }}">{{ page.label }}</a></li>
{% endfor %}
</ul>
</li>
{% for page in pages_after_dropdowns %}
{% set is_active = request.endpoint == page.endpoint %}
<li class="nav-item">
<a class="nav-link{% if is_active %} border border-white border-2 active{% endif %}" {% if is_active %} aria-current="page"{% endif %} href="{{ url_for(page.endpoint) }}">
{{ page.label }}
</a>
</li>
{% endfor %}
</ul>
<ul class="navbar-nav ms-auto">
{% if g.user.is_authenticated %}
<li class="nav-item"><a class="nav-link" href="{{ url_for("logout") }}">Logout</a></li>
{% else %}
<li class="nav-item"><a class="nav-link" href="{{ url_for("login", next=request.url) }}">Login</a></li>
{% endif %}
</ul>
</div>
</div>

View file

@ -1,230 +0,0 @@
{% extends "base.html" %}
{% set heading = "Schengen area compliance report" %}
{% block title %}{{ heading }} - Edward Betts{% endblock %}
{% block content %}
<div class="container-fluid mt-2">
<h1>{{ heading }}</h1>
<div class="row">
<div class="col-md-8">
<div class="card mb-4">
<div class="card-header">
<h5 class="card-title">Current Status</h5>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<h6>Compliance Status</h6>
{% if current_compliance.is_compliant %}
<span class="badge bg-success fs-6">✅ COMPLIANT</span>
{% else %}
<span class="badge bg-danger fs-6">❌ NON-COMPLIANT</span>
{% endif %}
</div>
<div class="col-md-6">
<h6>Days Used</h6>
<div class="fs-4">{{ current_compliance.total_days_used }}/90</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-6">
{% if current_compliance.is_compliant %}
<h6>Days Remaining</h6>
<div class="fs-5 text-success">{{ current_compliance.days_remaining }}</div>
{% else %}
<h6>Days Over Limit</h6>
<div class="fs-5 text-danger">{{ current_compliance.days_over_limit }}</div>
{% endif %}
</div>
<div class="col-md-6">
{% if current_compliance.next_reset_date %}
<h6>Next Reset Date</h6>
<div class="fs-6">{{ current_compliance.next_reset_date.strftime('%Y-%m-%d') }}</div>
{% endif %}
</div>
</div>
<div class="mt-3">
<h6>Current 180-day Period</h6>
<div class="text-muted">
{{ current_compliance.current_180_day_period[0].strftime('%Y-%m-%d') }} to
{{ current_compliance.current_180_day_period[1].strftime('%Y-%m-%d') }}
</div>
</div>
</div>
</div>
{% if current_compliance.stays_in_period %}
<div class="card mb-4">
<div class="card-header">
<h5 class="card-title">Stays in Current 180-day Period</h5>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Entry Date</th>
<th>Exit Date</th>
<th>Country</th>
<th>Days</th>
<th>Trip</th>
</tr>
</thead>
<tbody>
{% for stay in current_compliance.stays_in_period %}
<tr>
<td>{{ stay.entry_date.strftime('%Y-%m-%d') }}</td>
<td>
{% if stay.exit_date %}
{{ stay.exit_date.strftime('%Y-%m-%d') }}
{% else %}
<span class="text-muted">ongoing</span>
{% endif %}
</td>
{% set country = get_country(stay.country) %}
<td>{{ country.flag }} {{ country.name }}</td>
<td>{{ stay.days }}</td>
<td>
{% if stay.trip_date and stay.trip_name %}
<a href="{{ url_for('trip_page', start=stay.trip_date.strftime('%Y-%m-%d')) }}" class="text-decoration-none">
{{ stay.trip_name }}
</a>
{% else %}
<span class="text-muted">-</span>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endif %}
</div>
<div class="col-md-4">
{% if warnings %}
<div class="card mb-4">
<div class="card-header">
<h5 class="card-title">Warnings</h5>
</div>
<div class="card-body">
{% for warning in warnings %}
<div class="alert alert-{{ 'danger' if warning.severity == 'error' else 'warning' }} alert-dismissible fade show" role="alert">
<strong>{{ warning.date.strftime('%Y-%m-%d') }}</strong><br>
{{ warning.message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
</div>
</div>
{% endif %}
<div class="card">
<div class="card-header">
<h5 class="card-title">Compliance History</h5>
</div>
<div class="card-body">
<div class="chart-container" style="height: 300px;">
<canvas id="complianceChart"></canvas>
</div>
</div>
</div>
</div>
</div>
{% if trips_with_compliance %}
<div class="card mt-4">
<div class="card-header">
<h5 class="card-title">Trip Compliance History</h5>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Trip Date</th>
<th>Trip Name</th>
<th>Days Used</th>
<th>Days Remaining</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for trip_date, calculation in trips_with_compliance.items() %}
<tr>
<td>{{ trip_date.strftime('%Y-%m-%d') }}</td>
<td>
<a href="{{ url_for('trip_page', start=trip_date.strftime('%Y-%m-%d')) }}" class="text-decoration-none">
{% for trip in trip_list if trip.start == trip_date %}
{{ trip.title }} {{ trip.country_flags }}
{% endfor %}
</a>
</td>
<td>{{ calculation.total_days_used }}/90</td>
<td>{{ calculation.days_remaining }}</td>
<td>
{% if calculation.is_compliant %}
<span class="badge bg-success">Compliant</span>
{% else %}
<span class="badge bg-danger">Non-compliant</span>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endif %}
</div>
{% endblock %}
{% block scripts %}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const ctx = document.getElementById('complianceChart').getContext('2d');
const complianceHistory = {{ compliance_history | tojson }};
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: complianceHistory.map(item => item.date),
datasets: [{
label: 'Days Used',
data: complianceHistory.map(item => item.days_used),
borderColor: 'rgb(75, 192, 192)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
tension: 0.1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
max: 90,
ticks: {
callback: function(value) {
return value + '/90';
}
}
}
},
plugins: {
legend: {
display: true
}
}
}
});
</script>
{% endblock %}

View file

@ -1,7 +1,5 @@
{% extends "base.html" %}
{% block title %}Agenda error - Edward Betts{% endblock %}
{% block style %}
<link rel="stylesheet" href="{{url_for('static', filename='css/exception.css')}}" />
{% endblock %}

View file

@ -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 %}
<style>
.grid-container {
display: grid;
grid-template-columns: repeat({{ flight_column_count }}, auto);
grid-template-columns: repeat(7, auto); /* 7 columns for each piece of information */
gap: 10px;
justify-content: start;
}
.train-grid-container {
display: grid;
grid-template-columns: repeat({{ column_count }}, auto);
grid-template-columns: repeat(6, auto); /* 7 columns for each piece of information */
gap: 10px;
justify-content: start;
}
@ -37,20 +37,27 @@
<h3>flights</h3>
<div class="grid-container">
<div class="grid-item">reference</div>
<div class="grid-item">price</div>
<div class="grid-item text-end">date</div>
<div class="grid-item">route</div>
<div class="grid-item">take-off</div>
<div class="grid-item">land</div>
<div class="grid-item">duration</div>
<div class="grid-item">flight</div>
<div class="grid-item">tracking</div>
<div class="grid-item">distance</div>
<div class="grid-item">CO₂ (kg)</div>
<div class="grid-item">reference</div>
{% for item in flights %}
{{ flight_booking_row(item) }}
{% for item in flights | sort(attribute="depart") if item.arrive %}
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">{{ item.from }} &rarr; {{ item.to }}</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item">{{ item.duration }}</div>
<div class="grid-item">{{ item.airline }}{{ item.flight_number }}</div>
<div class="grid-item">{{ item.booking_reference }}</div>
{% endfor %}
</div>
@ -61,15 +68,21 @@
<div class="grid-item">route</div>
<div class="grid-item">depart</div>
<div class="grid-item">arrive</div>
<div class="grid-item">duration</div>
<div class="grid-item">operator</div>
<div class="grid-item">reference</div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
{% for item in trains | sort(attribute="depart") %}
{{ train_row(item) }}
{% for item in trains | sort(attribute="depart") if item.arrive %}
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">{{ item.from }} &rarr; {{ item.to }}</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item">{{ item.operator }}</div>
<div class="grid-item">{{ item.booking_reference }}</div>
{% endfor %}
</div>

View file

@ -1,113 +0,0 @@
{% extends "base.html" %}
{% from "macros.html" import trip_link, display_date_no_year, display_date, display_datetime, display_time, format_distance, trip_item with context %}
{% block title %}{{ heading }} - Edward Betts{% endblock %}
{% block style %}
<link rel="stylesheet" href="{{ url_for("static", filename="leaflet/leaflet.css") }}">
<link rel="stylesheet" href="{{ url_for("static", filename="css/trips.css") }}">
<style>
body, html {
height: 100%;
margin: 0;
}
.container-fluid {
height: calc(100% - 56px); /* Subtracting the height of the navbar */
}
.text-content {
overflow-y: scroll;
height: 100%;
}
.map-container {
position: sticky;
top: 56px; /* Adjust to be below the navbar */
height: calc(100vh - 56px); /* Subtracting the height of the navbar */
}
#map {
height: 100%;
}
@media (max-width: 767.98px) {
.container-fluid {
display: block;
height: auto;
}
.map-container {
position: relative;
top: 0;
height: 40vh;
}
.text-content {
height: auto;
overflow-y: auto;
}
}
</style>
{% endblock %}
{% macro section(heading, item_list) %}
{% if item_list %}
{% set items = item_list | list %}
<div class="trip-list-summary">
<h2>{{ heading }}</h2>
<p class="mb-2"><a href="{{ url_for("trip_stats") }}">Trip statistics</a> &middot; {{ items | count }} trips</p>
<div class="summary-stats-row">
<div class="summary-stat">
<span class="summary-stat-label">Total distance</span>
<span class="summary-stat-value">{{ format_distance(total_distance) }}</span>
</div>
{% for transport_type, distance in distances_by_transport_type %}
<div class="summary-stat">
<span class="summary-stat-label">{{ transport_type | title }}</span>
<span class="summary-stat-value">{{ format_distance(distance) }}</span>
</div>
{% endfor %}
<div class="summary-stat">
<span class="summary-stat-label">Total CO₂</span>
<span class="summary-stat-value">{{ "{:,.1f}".format(total_co2_kg / 1000.0) }} t</span>
</div>
{% for transport_type, co2_kg in co2_by_transport_type %}
<div class="summary-stat">
<span class="summary-stat-label">{{ transport_type | title }} CO₂</span>
<span class="summary-stat-value">{{ "{:,.1f}".format(co2_kg) }} kg</span>
</div>
{% endfor %}
</div>
</div>
{% for trip in items %}
{{ trip_item(trip) }}
{% endfor %}
{% endif %}
{% endmacro %}
{% block content %}
<div class="container-fluid d-flex flex-column flex-md-row">
<div class="text-content col-12 col-md-6 pe-3">
{{ section(heading, trips) }}
</div>
<div class="map-container col-12 col-md-6">
<div id="map" class="map"></div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script src="{{ url_for("static", filename="leaflet/leaflet.js") }}"></script>
<script src="{{ url_for("static", filename="leaflet-geodesic/leaflet.geodesic.umd.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/map.js") }}"></script>
<script>
var coordinates = {{ coordinates | tojson }};
var routes = {{ routes | tojson }};
build_map("map", coordinates, routes);
</script>
{% endblock %}

View file

@ -1,121 +0,0 @@
{% extends "base.html" %}
{% from "macros.html" import format_distance with context %}
{% set heading = "Trip statistics" %}
{% block title %}{{ heading }} - Edward Betts{% endblock %}
{% macro stat_list(id, label, counter, show_top=5) %}
<div class="mb-2">
<strong>{{ label }}:</strong> {{ counter | count }}
{% if counter | count > 0 %}
<a class="btn btn-sm btn-outline-secondary ms-2" data-bs-toggle="collapse" href="#{{ id }}" role="button" aria-expanded="false">
show/hide
</a>
<div class="collapse mt-2" id="{{ id }}">
<div class="d-flex flex-wrap gap-1">
{% for item, count in counter.most_common() %}
<span class="badge text-bg-secondary">{{ item }} <span class="badge text-bg-light text-dark">{{ count }}</span></span>
{% endfor %}
</div>
</div>
{% endif %}
</div>
{% endmacro %}
{% block content %}
<div class="container-fluid">
<h1>Trip statistics</h1>
<div class="card mb-4">
<div class="card-header"><strong>Overall Summary</strong></div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div>Trips: {{ count }}</div>
<div>Conferences: {{ conferences }}</div>
<div>Total distance: {{ format_distance(total_distance) }}</div>
{% for transport_type, distance in distances_by_transport_type %}
<div class="ms-3">{{ transport_type | title }}: {{ format_distance(distance) }}</div>
{% endfor %}
</div>
<div class="col-md-6">
<div>Flight segments: {{ overall_stats.flight_count }}</div>
<div>Train segments: {{ overall_stats.train_count }}</div>
</div>
</div>
<hr>
{{ stat_list("overall-airlines", "Airlines", overall_stats.airlines) }}
{{ stat_list("overall-airports", "Airports", overall_stats.airports) }}
{{ stat_list("overall-stations", "Stations", overall_stats.stations) }}
</div>
</div>
{% 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([]) %}
<div class="card mb-3">
<div class="card-header">
<strong>{{ year }}</strong>
<span class="text-muted ms-2">{{ year_stats.count }} trips</span>
</div>
<div class="card-body">
<div class="row mb-2">
<div class="col-md-6">
<div>Trips: {{ year_stats.count }}</div>
<div>Conferences: {{ year_stats.conferences }}</div>
<div>Distance: {{ format_distance(year_stats.total_distance or 0) }}</div>
{% if year_stats.distances_by_transport_type %}
{% for transport_type, distance in year_stats.distances_by_transport_type.items() %}
<div class="ms-3">{{ transport_type | title }}: {{ format_distance(distance) }}</div>
{% endfor %}
{% endif %}
</div>
<div class="col-md-6">
<div>Flight segments: {{ year_stats.flight_count or 0 }}</div>
<div>Train segments: {{ year_stats.train_count or 0 }}</div>
{% if year_stats.co2_kg %}
<div>CO₂:
{% if year_stats.co2_kg >= 1000 %}
{{ "{:,.2f}".format(year_stats.co2_kg / 1000.0) }} tonnes
{% else %}
{{ "{:,.0f}".format(year_stats.co2_kg) }} kg
{% endif %}
</div>
{% endif %}
</div>
</div>
<div class="mb-2">
<strong>Countries:</strong> {{ countries | count }}
{% if new_countries %}({{ new_countries | count }} new){% endif %}
<div class="d-flex flex-wrap gap-1 mt-1">
{% for c in countries %}
<span class="badge text-bg-light text-dark border">
{{ c.flag }} {{ c.name }}
{% if c in new_countries %}
<span class="badge text-bg-info">new</span>
{% endif %}
</span>
{% endfor %}
</div>
</div>
{% 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 %}
</div>
</div>
{% endfor %}
</div>
{% endblock %}

View file

@ -1,133 +0,0 @@
{% extends "base.html" %}
{% block title %}Debug: {{ trip.title }} ({{ trip.start }}) - Edward Betts{% endblock %}
{% block style %}
<style>
.data-display {
background-color: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 0.375rem;
padding: 1rem;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 0.875rem;
line-height: 1.4;
white-space: pre-wrap;
overflow-x: auto;
max-height: 80vh;
overflow-y: auto;
}
.debug-header {
background-color: #fff3cd;
border: 1px solid #ffeaa7;
border-radius: 0.375rem;
padding: 1rem;
margin-bottom: 1rem;
}
.debug-header h1 {
color: #856404;
margin-bottom: 0.5rem;
}
.debug-header p {
color: #856404;
margin-bottom: 0.5rem;
}
.debug-header .btn {
margin-right: 0.5rem;
}
/* Basic JSON syntax highlighting using CSS */
.json-display .json-key {
color: #d73a49;
font-weight: bold;
}
.json-display .json-string {
color: #032f62;
}
.json-display .json-number {
color: #005cc5;
}
.json-display .json-boolean {
color: #e36209;
font-weight: bold;
}
.json-display .json-null {
color: #6f42c1;
font-weight: bold;
}
</style>
{% endblock %}
{% block content %}
<div class="container-fluid">
<div class="debug-header">
<h1>🐛 Trip Debug Information</h1>
<p>Raw trip object data for: <strong>{{ trip.title }}</strong></p>
<a href="{{ url_for('trip_page', start=start) }}" class="btn btn-primary">← Back to Trip Page</a>
<button onclick="copyToClipboard('jsonDisplay', event)" class="btn btn-secondary">📋 Copy JSON</button>
<button onclick="copyToClipboard('yamlDisplay', event)" class="btn btn-secondary">📋 Copy YAML</button>
</div>
<div class="row">
<div class="col-12">
<h3>Trip Object (JSON)</h3>
<div class="data-display json-display" id="jsonDisplay">{{ trip_json }}</div>
</div>
<div class="col-12 mt-4">
<h3>Trip Object (YAML)</h3>
<div class="data-display" id="yamlDisplay">{{ trip_yaml }}</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script>
function copyToClipboard(elementId, evt) {
const clipText = document.getElementById(elementId).textContent;
navigator.clipboard.writeText(clipText).then(function() {
// Show a temporary notification
const btn = evt.target;
const originalText = btn.textContent;
btn.textContent = '✅ Copied!';
btn.classList.remove('btn-secondary');
btn.classList.add('btn-success');
setTimeout(function() {
btn.textContent = originalText;
btn.classList.remove('btn-success');
btn.classList.add('btn-secondary');
}, 2000);
}).catch(function(err) {
console.error('Failed to copy: ', err);
alert('Failed to copy to clipboard');
});
}
// Simple JSON syntax highlighting
function highlightJSON() {
const display = document.getElementById('jsonDisplay');
let content = display.textContent;
// Highlight different JSON elements
content = content.replace(/"([^"]+)":/g, '<span class="json-key">"$1":</span>');
content = content.replace(/"([^"]*)"(?=\s*[,\]\}])/g, '<span class="json-string">"$1"</span>');
content = content.replace(/\b(\d+\.?\d*)\b/g, '<span class="json-number">$1</span>');
content = content.replace(/\b(true|false)\b/g, '<span class="json-boolean">$1</span>');
content = content.replace(/\bnull\b/g, '<span class="json-null">null</span>');
display.innerHTML = content;
}
// Apply highlighting when page loads
document.addEventListener('DOMContentLoaded', highlightJSON);
</script>
{% endblock %}

View file

@ -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 %}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
{% set conference_column_count = 7 %}
{% set accommodation_column_count = 7 %}
{% set travel_column_count = 8 %}
<style>
.conferences {
display: grid;
grid-template-columns: repeat({{ conference_column_count }}, auto); /* 7 columns for each piece of information */
gap: 10px;
justify-content: start;
}
.accommodation {
display: grid;
grid-template-columns: repeat({{ accommodation_column_count }}, auto);
gap: 10px;
justify-content: start;
}
.travel {
display: grid;
grid-template-columns: repeat({{ travel_column_count }}, auto);
gap: 10px;
justify-content: start;
}
.grid-item {
/* Additional styling for grid items can go here */
}
.map {
height: 80vh;
}
</style>
{% endblock %}
{% block content %}
<div class="p-2">
<h1>Trips</h1>
<p>{{ future | count }} trips</p>
{% for trip in future %}
{% set end = trip.end %}
<div>
{{ display_date_no_year(trip.start) }} to {{ display_date_no_year(end) }}:
{{ trip.title }} &mdash; {{ trip.locations_str }}
</div>
{% endfor %}
</div>
{% endblock %}

View file

@ -1,535 +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) -%}
{%- 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 -%}
{%- endmacro %}
{% macro next_and_previous() %}
<p>
{% 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 %}
</p>
{% endmacro %}
{% block style %}
{% if coordinates or routes %}
<link rel="stylesheet" href="{{ url_for("static", filename="leaflet/leaflet.css") }}">
{% endif %}
<link rel="stylesheet" href="{{ url_for("static", filename="css/trips.css") }}">
{% set conference_column_count = 7 %}
{% set accommodation_column_count = 7 %}
{% set travel_column_count = 9 %}
<style>
.conferences {
display: grid;
grid-template-columns: repeat({{ conference_column_count }}, auto); /* 7 columns for each piece of information */
gap: 10px;
justify-content: start;
}
.accommodation {
display: grid;
grid-template-columns: repeat({{ accommodation_column_count }}, auto);
gap: 10px;
justify-content: start;
}
.travel {
display: grid;
grid-template-columns: repeat({{ travel_column_count }}, auto);
gap: 10px;
justify-content: start;
}
.grid-item {
/* Additional styling for grid items can go here */
}
.half-map {
height: 90vh;
}
.full-window-map {
position: fixed;
top: 56px;
left: 0;
right: 0;
bottom: 0;
z-index: 9999;
}
#toggleMapSize {
position: fixed;
top: 66px;
right: 10px;
z-index: 10000;
}
@media (max-width: 767.98px) {
#toggleMapSize {
display: none;
}
.half-map {
height: 50vh;
}
}
</style>
{% 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 %}
<div class="row">
<div class="{% if coordinates or routes %}col-md-6{% else %}col-md-12{% endif %} col-sm-12">
<div class="m-3">
<div class="trip-prev-next">{{ next_and_previous() }}</div>
<h1 class="trip-page-title">{{ trip.title }}</h1>
<p class="lead">
{% 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 %}
</p>
<div class="mb-3">
<ul class="list-unstyled trip-countries">
{% for location, country in trip.locations() %}
<li>{{ country.flag if trip.show_flags }} {{ location }}</li>
{% endfor %}
</ul>
{% if destination_times %}
<div class="mt-2">
<strong>Destination time zones</strong>
<table class="table table-sm table-hover w-auto mb-0">
<thead>
<tr>
<th>Destination</th>
<th>Timezone</th>
<th>Difference from UK</th>
</tr>
</thead>
<tbody>
{% for item in destination_times %}
<tr>
<td>{{ item.destination_label }}</td>
<td>{{ item.timezone or "Unknown" }}</td>
<td class="destination-offset">{{ item.offset_display }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
<div class="trip-stats">
{% if total_distance %}
<span class="trip-stat">{{ "{:,.0f} km / {:,.0f} mi".format(total_distance, total_distance / 1.60934) }}</span>
{% endif %}
{% if distances_by_transport_type %}
{% for transport_type, distance in distances_by_transport_type %}
<span class="trip-stat">{{ transport_type | title }}: {{ "{:,.0f} km".format(distance) }}</span>
{% endfor %}
{% endif %}
{% if total_co2_kg %}
<span class="trip-stat">CO₂ {{ "{:,.1f}".format(total_co2_kg) }} kg</span>
{% endif %}
{% if co2_by_transport_type %}
{% for transport_type, co2_kg in co2_by_transport_type %}
<span class="trip-stat">{{ transport_type | title }} CO₂ {{ "{:,.1f}".format(co2_kg) }} kg</span>
{% endfor %}
{% endif %}
</div>
{% set delta = human_readable_delta(trip.start) %}
{% if delta %}
<div>How long until trip: {{ delta }}</div>
{% endif %}
{% if trip.schengen_compliance %}
<div class="mt-3">
<strong>Schengen Compliance:</strong>
{% if trip.schengen_compliance.is_compliant %}
<span class="badge bg-success">✅ Compliant</span>
{% else %}
<span class="badge bg-danger">❌ Non-compliant</span>
{% endif %}
<div class="text-muted small">
{{ 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 %}
</div>
</div>
{% endif %}
</div>
{# ---- Chronological itinerary ---- #}
{% for day, day_elements in trip.elements_grouped_by_day() %}
{% set weather = trip_weather.get(day.isoformat()) if trip_weather else None %}
<h4 class="trip-day-header">
{{ display_date_no_year(day) }}
{% if weather %}
<span class="trip-weather-inline">
<img src="https://openweathermap.org/img/wn/{{ weather.icon }}.png"
alt="{{ weather.status }}" title="{{ weather.detailed_status }}" width="18" height="18">
{{ weather.temp_min }}{{ weather.temp_max }}°C
<span class="fw-normal fst-italic">{{ weather.detailed_status }}</span>
</span>
{% endif %}
{% if g.user.is_authenticated and day <= today %}
<a class="ms-2 small fw-normal" href="https://photos.4angle.com/search?query=%7B%22takenAfter%22%3A%22{{day}}T00%3A00%3A00.000Z%22%2C%22takenBefore%22%3A%22{{day}}T23%3A59%3A59.999Z%22%7D">photos</a>
{% endif %}
</h4>
{% 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 %}
<div class="trip-conference-card my-1">
<div class="card-body">
<h5 class="card-title">
<a href="{{ item.url }}">{{ item.name }}</a>
<small class="text-muted">
{{ 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 %}
</small>
</h5>
<p class="card-text">
<strong>Topic:</strong> {{ item.topic }}
<strong>Venue:</strong> {{ item.venue }}
<strong>Location:</strong> {{ item.location }}
{% if country %}
{{ country.flag if trip.show_flags }}
{% elif item.online %}
💻 Online
{% else %}
<span class="text-bg-danger p-2">country code <strong>{{ item.country }}</strong> not found</span>
{% endif %}
{% if item.free %}
<span class="badge bg-success text-nowrap">free to attend</span>
{% elif item.price and item.currency %}
<span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
{% 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 %}
<span class="badge bg-secondary text-nowrap">{{ days_before }} day{{ 's' if days_before != 1 }} to explore before</span>
{% endif %}
{% if days_after > 0 %}
<span class="badge bg-secondary text-nowrap">{{ days_after }} day{{ 's' if days_after != 1 }} to explore after</span>
{% endif %}
{% endif %}
</p>
</div>
</div>
{% 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 %}
<div class="trip-accommodation-card my-1">
<div class="card-body">
<h5 class="card-title">
{{ e.get_emoji() }}
<a href="{{ item.url }}">{{ item.name }}</a>
{% if item.operator and item.operator != item.name %}<small class="text-muted fw-normal">{{ item.operator }}</small>{% endif %}
<small class="text-muted">({% if nights == 1 %}1 night{% else %}{{ nights }} nights{% endif %})</small>
</h5>
<p class="card-text">
{{ item.location }}
{% if country %}
{{ country.flag if trip.show_flags }}
{% else %}
<span class="text-bg-danger p-2">country code <strong>{{ item.country }}</strong> not found</span>
{% endif %}
{% if item.address %} · {{ item.address }}{% endif %}
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap ms-1">{{ item.price }} {{ item.currency }}</span>
{% endif %}
</p>
</div>
</div>
{% elif e.element_type == "check-out" %}
{% set item = e.detail %}
<div class="trip-checkout">
{{ e.get_emoji() }} Check out: <a href="{{ item.url }}">{{ item.name }}</a>
{% if item.operator and item.operator != item.name %}<span class="text-muted small">{{ item.operator }}</span>{% endif %}
</div>
{% 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 %}
<div class="trip-transport-card my-1">
<div class="card-body">
<h5 class="card-title">
✈️
{{ item.from_airport.name }} ({{ item.from_airport.iata }})
{{ item.to_airport.name }} ({{ item.to_airport.iata }})
</h5>
<div class="card-text">
<div>
<span>{{ item.airline_name }}</span>
<span class="text-muted small">{{ full_flight_number }}</span>
· {{ item.depart.strftime("%H:%M") }}
{% if item.arrive %}
→ {{ item.arrive.strftime("%H:%M") }}{% if is_overnight %} <span class="text-muted small">+1 day</span>{% endif %}
<span class="text-muted">🕒{{ trip_duration(item.depart, item.arrive) }}</span>
{% endif %}
{% if item.distance %}
<span class="text-muted">🌍 {{ "{:,.0f} km".format(item.distance) }}</span>
{% endif %}
{% if item.co2_kg is defined and item.co2_kg is not none %}
<span class="text-muted">CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg</span>
{% endif %}
</div>
<div class="small mt-1">
<a href="https://www.flightradar24.com/data/flights/{{ item.airline_detail.iata | lower }}{{ item.flight_number }}">flightradar24</a>
· <a href="https://uk.flightaware.com/live/flight/{{ full_flight_number }}">FlightAware</a>
· <a href="{{ radarbox_url }}">radarbox</a>
</div>
</div>
</div>
</div>
{% elif e.element_type == "train" %}
{% set item = e.detail %}
{% set depart_date = item.depart.date() if item.depart.hour is defined else item.depart %}
{% set arrive_date = item.arrive.date() if item.arrive.hour is defined else item.arrive %}
{% set is_overnight = depart_date != arrive_date %}
<div class="trip-transport-card my-1">
<div class="card-body">
<h5 class="card-title">
{% if is_overnight %}🌙{% else %}🚆{% endif %}
{{ item.from }} → {{ item.to }}
{% if item.operator %}<small class="text-muted fw-normal">{{ item.operator }}</small>{% endif %}
{% if is_overnight %}<span class="badge bg-secondary text-nowrap ms-1">Night train</span>{% endif %}
</h5>
<p class="card-text">
{{ item.depart.strftime("%H:%M") }}
→ {{ item.arrive.strftime("%H:%M") }}{% if is_overnight %} <span class="text-muted small">+1 day</span>{% endif %}
{% if item.class %}
<span class="badge bg-info text-nowrap">{{ item.class }}</span>
{% endif %}
<span class="text-muted">🕒{{ trip_duration(item.depart, item.arrive) }}</span>
{% if item.distance %}
<span class="text-muted">🛤️ {{ "{:,.0f} km".format(item.distance) }}</span>
{% endif %}
{% if item.co2_kg is defined and item.co2_kg is not none %}
<span class="text-muted">CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg</span>
{% endif %}
{% if item.coach %}
<span class="text-nowrap">🛏️ 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 %}</span>
{% endif %}
</p>
</div>
</div>
{% elif e.element_type in ("coach", "bus") %}
{% set item = e.detail %}
<div class="trip-transport-card my-1">
<div class="card-body">
<h5 class="card-title">
🚌 {{ item.from }} → {{ item.to }}
{% if item.operator %}<small class="text-muted fw-normal">{{ item.operator }}</small>{% endif %}
</h5>
<p class="card-text">
{{ item.depart.strftime("%H:%M") }} → {{ item.arrive.strftime("%H:%M") }}
{% if item.class %}
<span class="badge bg-info text-nowrap">{{ item.class }}</span>
{% endif %}
<span class="text-muted">🕒{{ trip_duration(item.depart, item.arrive) }}</span>
{% if item.distance %}
<span class="text-muted">🛤️ {{ "{:,.0f} km".format(item.distance) }}</span>
{% endif %}
{% if item.co2_kg is defined and item.co2_kg is not none %}
<span class="text-muted">CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg</span>
{% endif %}
</p>
</div>
</div>
{% elif e.element_type == "ferry" %}
{% set item = e.detail %}
<div class="trip-transport-card my-1">
<div class="card-body">
<h5 class="card-title">
⛴️ {{ item.from }} → {{ item.to }}
<small class="text-muted fw-normal">{{ item.operator }}{% if item.ferry %} · {{ item.ferry }}{% endif %}</small>
</h5>
<p class="card-text">
<div>
{{ item.depart.strftime("%H:%M") }} → {{ item.arrive.strftime("%H:%M") }}
<span class="text-muted">🕒{{ trip_duration(item.depart, item.arrive) }}</span>
{% if item.class %}
<span class="badge bg-info text-nowrap">{{ item.class }}</span>
{% endif %}
{% if item.co2_kg is defined and item.co2_kg is not none %}
<span class="text-muted">CO₂ {{ "{:,.1f}".format(item.co2_kg) }} kg</span>
{% endif %}
</div>
{% if item.vehicle %}
<div>🚗 Vehicle: {{ item.vehicle.type }} {% if g.user.is_authenticated %}({{ item.vehicle.registration }}) {% endif %}
{% if item.vehicle.extras %} - Extras: {{ item.vehicle.extras | join(", ") }}{% endif %}
</div>
{% endif %}
{% if g.user.is_authenticated %}
<div>
{% if item.booking_reference %}<strong>Booking reference:</strong> {{ item.booking_reference }}{% endif %}
{% if item.price and item.currency %}<span class="badge bg-info text-nowrap">Price: {{ item.price }} {{ item.currency }}</span>{% endif %}
</div>
{% endif %}
</p>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
{% if trip.flight_bookings %}
<h3 class="trip-section-h">Flight bookings</h3>
{% for item in trip.flight_bookings %}
<div>
{{ item.flights | map(attribute="airline_name") | unique | join(" + ") }}
{% if g.user.is_authenticated and item.booking_reference %}
<strong>booking reference:</strong> {{ item.booking_reference }}
{% endif %}
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
{% endif %}
</div>
{% endfor %}
{% endif %}
{% if trip.events %}
<h4 class="trip-section-h">Events</h4>
{% for item in trip.events %}
{% set country = get_country(item.country) if item.country else None %}
<div class="trip-event-card my-1">
<div class="card-body">
<h5 class="card-title">
<a href="{{ item.url }}">{{ item.title }}</a>
<small class="text-muted">{{ display_date_no_year(item.date) }}</small>
</h5>
<p class="card-text">
Address: {{ item.address }}
| Location: {{ item.location }}
{% if country %}
{{ country.flag if trip.show_flags }}
{% else %}
<span class="text-bg-danger p-2">country code <strong>{{ item.country }}</strong> not found</span>
{% endif %}
{% if g.user.is_authenticated and item.price and item.currency %}
| <span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
{% endif %}
</p>
</div>
</div>
{% endfor %}
{% endif %}
<div class="mt-3">
<h4 class="trip-section-h">Holidays</h4>
{% if holidays %}
<table class="table table-hover w-auto">
{% for item in holidays %}
{% set country = get_country(item.country) %}
<tr>
{% if loop.first or item.date != loop.previtem.date %}
<td class="text-end">{{ display_date(item.date) }}</td>
{% else %}
<td></td>
{% endif %}
<td>{{ country.flag if trip.show_flags }} {{ country.name }}</td>
<td>{{ item.display_name }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No public holidays during trip.</p>
{% endif %}
</div>
<div class="mt-3">
<h4 class="trip-section-h">UK school holidays (Bristol)</h4>
{% if school_holidays %}
<table class="table table-hover w-auto">
{% for item in school_holidays %}
<tr>
<td class="text-end">{{ display_date(item.as_date) }}</td>
<td>to {{ display_date(item.end_as_date) }}</td>
<td>{{ item.title }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No UK school holidays during trip.</p>
{% endif %}
</div>
{{ next_and_previous() }}
</div>
</div>
{% if coordinates or routes %}
<div class="col-md-6 col-sm-12">
<button id="toggleMapSize" class="btn btn-primary mb-2">Toggle map size</button>
<div id="map" class="half-map">
</div>
</div>
{% endif %}
</div>
{% endblock %}
{% block scripts %}
{% if coordinates or routes %}
<script src="{{ url_for("static", filename="leaflet/leaflet.js") }}"></script>
<script src="{{ url_for("static", filename="leaflet-geodesic/leaflet.geodesic.umd.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/map.js") }}"></script>
<script>
var coordinates = {{ coordinates | tojson }};
var routes = {{ routes | tojson }};
build_map("map", coordinates, routes);
</script>
{% endif %}
{% endblock %}

View file

@ -1,71 +0,0 @@
{% extends "base.html" %}
{% block title %}Weekends - Edward Betts{% endblock %}
{% block content %}
<div class="p-2">
<h1>Weekends</h1>
<table class="table table-hover w-auto">
<thead>
<tr>
<th class="text-end">Week</th>
<th class="text-end">Date</th>
<th>Saturday</th>
<th>Saturday Location</th>
<th>Saturday Weather</th>
<th>Sunday</th>
<th>Sunday Location</th>
<th>Sunday Weather</th>
</tr>
</thead>
<tbody>
{% 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 %}
<tr{% if week_number == current_week_number %} class="bg-warning-subtle"{% endif %}>
<td class="text-end{{ extra_class }}">
{{ week_number }}
</td>
<td class="text-end text-nowrap{{ extra_class }}">
{{ weekend.date.strftime("%-d %b %Y") }}
</td>
{% for day in "saturday", "sunday" %}
{% if extra_class %}<td class="{{ extra_class|trim }}">{% else %}<td>{% endif %}
{% if weekend[day] %}
{% for event in weekend[day] %}
<a href="{{ event.url }}">{{ event.title }}</a>{% if not loop.last %},{%endif %}
{% endfor %}
{% else %}
<strong>free</strong>
{% endif %}
</td>
{% if extra_class %}<td class="{{ extra_class|trim }}">{% else %}<td>{% endif %}
{% set city, country = weekend[day + '_location'] %}
{% if city %}
{{ city }}, {{ country.flag }} {{ country.name }}
{% endif %}
</td>
{% if extra_class %}<td class="{{ extra_class|trim }}">{% else %}<td>{% endif %}
{% set w = weekend[day + '_weather'] %}
{% if w %}
<img src="https://openweathermap.org/img/wn/{{ w.icon }}.png" alt="{{ w.status }}" title="{{ w.detailed_status }}" width="25" height="25">
{{ w.temp_min }}{{ w.temp_max }}°C
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}

View file

@ -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)

View file

@ -1,394 +0,0 @@
"""Tests for agenda.add_new_conference."""
from datetime import date, datetime
import typing
import lxml.html # type: ignore[import-untyped]
import pytest
import yaml
from agenda import add_new_conference
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_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
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_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(
(
"<html><body>"
'<a href="https://example.com">Example</a>'
'<a href="https://www.openstreetmap.org/?mlat=51.5&mlon=-0.12">Map</a>'
"</body></html>"
)
)
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(
(
"<html><body>"
'<a href="https://www.openstreetmap.org/?mlat=40.0&mlon=-74.0">Map</a>'
"</body></html>"
)
)
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"
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("<html><body>Conference details</body></html>")
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]

View file

@ -1,130 +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 timezone_transition
from agenda.economist import publication_dates
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
def mock_today() -> datetime.date:
"""Mock the current date for testing purposes."""
def mock_today():
# Mock the current date for testing purposes
return datetime.date(2023, 10, 5)
@pytest.fixture
def mock_now() -> datetime.datetime:
"""Mock the current date and time for testing purposes."""
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_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_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_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_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_timedelta_display() -> None:
"""Test timedelta_display function."""
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_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"
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.

View file

@ -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

View file

@ -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

View file

@ -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 == []

View file

@ -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"

View file

@ -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

View file

@ -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"

View file

@ -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 == []

View file

@ -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)

View file

@ -1,123 +0,0 @@
"""Tests for conference list date handling."""
from datetime import date
import typing
from types import SimpleNamespace
import yaml
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

View file

@ -1,168 +0,0 @@
"""Tests for domain renewal functionality."""
import csv
import os
import tempfile
from datetime import date
from typing import Any
import pytest
from agenda.domains import renewal_dates, url
from agenda.event import Event
class TestRenewalDates:
"""Test the renewal_dates function."""
def test_renewal_dates_empty_directory(self) -> None:
"""Test with empty directory."""
with tempfile.TemporaryDirectory() as temp_dir:
with pytest.raises(ValueError, match="max\\(\\) iterable argument is empty"):
renewal_dates(temp_dir)
def test_renewal_dates_no_matching_files(self) -> None:
"""Test with directory containing no matching files."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create some non-matching files
with open(os.path.join(temp_dir, "other_file.csv"), "w") as f:
f.write("test")
with open(os.path.join(temp_dir, "not_export.csv"), "w") as f:
f.write("test")
with pytest.raises(ValueError, match="max\\(\\) iterable argument is empty"):
renewal_dates(temp_dir)
def test_renewal_dates_single_file(self) -> None:
"""Test with single domain export file."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create a domain export file
filename = "export_domains_01_15_2024_02_30_PM.csv"
filepath = os.path.join(temp_dir, filename)
# Create CSV content
csv_data = [
["fqdn", "date_registry_end_utc"],
["example.com", "2024-06-15T00:00:00Z"],
["test.org", "2024-12-31T00:00:00Z"]
]
with open(filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(csv_data)
result = renewal_dates(temp_dir)
assert len(result) == 2
# Check first domain
assert result[0].name == "domain"
assert result[0].title == "🌐 example.com"
assert result[0].date == date(2024, 6, 15)
assert result[0].url == url + "example.com"
# Check second domain
assert result[1].name == "domain"
assert result[1].title == "🌐 test.org"
assert result[1].date == date(2024, 12, 31)
assert result[1].url == url + "test.org"
def test_renewal_dates_multiple_files_latest_used(self) -> None:
"""Test that the latest file is used when multiple exist."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create older file
older_filename = "export_domains_01_15_2024_02_30_PM.csv"
older_filepath = os.path.join(temp_dir, older_filename)
older_csv_data = [
["fqdn", "date_registry_end_utc"],
["old.com", "2024-06-15T00:00:00Z"]
]
with open(older_filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(older_csv_data)
# Create newer file
newer_filename = "export_domains_01_16_2024_03_45_PM.csv"
newer_filepath = os.path.join(temp_dir, newer_filename)
newer_csv_data = [
["fqdn", "date_registry_end_utc"],
["new.com", "2024-08-20T00:00:00Z"]
]
with open(newer_filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(newer_csv_data)
result = renewal_dates(temp_dir)
# Should only get data from newer file
assert len(result) == 1
assert result[0].title == "🌐 new.com"
assert result[0].date == date(2024, 8, 20)
def test_renewal_dates_empty_csv(self) -> None:
"""Test with empty CSV file."""
with tempfile.TemporaryDirectory() as temp_dir:
filename = "export_domains_01_15_2024_02_30_PM.csv"
filepath = os.path.join(temp_dir, filename)
# Create CSV with only headers
csv_data = [["fqdn", "date_registry_end_utc"]]
with open(filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(csv_data)
result = renewal_dates(temp_dir)
assert result == []
def test_renewal_dates_directory_not_found(self) -> None:
"""Test error handling when directory doesn't exist."""
with pytest.raises(FileNotFoundError):
renewal_dates("/nonexistent/directory")
def test_renewal_dates_malformed_filename(self) -> None:
"""Test with malformed filename that can't be parsed."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create file with name that starts correctly but can't be parsed as date
filename = "export_domains_invalid_date.csv"
filepath = os.path.join(temp_dir, filename)
with open(filepath, "w") as f:
f.write("test")
# Should raise ValueError when trying to parse the malformed date
with pytest.raises(ValueError, match="time data .* does not match format"):
renewal_dates(temp_dir)
def test_renewal_dates_various_date_formats(self) -> None:
"""Test with various date formats in the CSV."""
with tempfile.TemporaryDirectory() as temp_dir:
filename = "export_domains_01_15_2024_02_30_PM.csv"
filepath = os.path.join(temp_dir, filename)
csv_data = [
["fqdn", "date_registry_end_utc"],
["example1.com", "2024-06-15T12:34:56Z"],
["example2.com", "2024-12-31T00:00:00+00:00"],
["example3.com", "2024-03-20T23:59:59.999Z"]
]
with open(filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(csv_data)
result = renewal_dates(temp_dir)
assert len(result) == 3
assert result[0].date == date(2024, 6, 15)
assert result[1].date == date(2024, 12, 31)
assert result[2].date == date(2024, 3, 20)
class TestConstants:
"""Test module constants."""
def test_url_constant(self) -> None:
"""Test that URL constant has expected value."""
expected_url = "https://admin.gandi.net/domain/01578ef0-a84b-11e7-bdf3-00163e6dc886/"
assert url == expected_url

View file

@ -1,49 +0,0 @@
"""Tests for events_yaml functionality."""
from datetime import date, datetime, time
from agenda.events_yaml import midnight
class TestMidnight:
"""Test the midnight function."""
def test_midnight_with_date(self) -> None:
"""Test converting date to midnight datetime."""
test_date = date(2024, 6, 15)
result = midnight(test_date)
assert isinstance(result, datetime)
assert result.date() == test_date
assert result.time() == time(0, 0, 0) # Midnight
assert result == datetime(2024, 6, 15, 0, 0, 0)
def test_midnight_different_dates(self) -> None:
"""Test midnight function with different dates."""
test_cases = [
date(2024, 1, 1), # New Year's Day
date(2024, 12, 31), # New Year's Eve
date(2024, 2, 29), # Leap day
date(2024, 7, 4), # Independence Day
]
for test_date in test_cases:
result = midnight(test_date)
assert result.date() == test_date
assert result.time() == time(0, 0, 0)
assert result.hour == 0
assert result.minute == 0
assert result.second == 0
assert result.microsecond == 0
def test_midnight_preserves_year_month_day(self) -> None:
"""Test that midnight preserves the year, month, and day."""
test_date = date(2023, 11, 27)
result = midnight(test_date)
assert result.year == 2023
assert result.month == 11
assert result.day == 27
assert result.hour == 0
assert result.minute == 0
assert result.second == 0

View file

@ -1,261 +0,0 @@
"""Tests for foreign exchange functionality."""
import json
import os
import tempfile
from datetime import datetime
from decimal import Decimal
from unittest.mock import patch
from agenda.fx import get_rates, read_cached_rates
class TestReadCachedRates:
"""Test the read_cached_rates function."""
def test_read_cached_rates_none_filename(self) -> None:
"""Test with None filename returns empty dict."""
result = read_cached_rates(None, ["USD", "EUR"])
assert result == {}
def test_read_cached_rates_valid_file(self) -> None:
"""Test reading valid exchangerate.host cached rates file."""
currencies = ["USD", "EUR", "JPY"]
data = {
"quotes": {
"GBPUSD": 1.25,
"GBPEUR": 1.15,
"GBPJPY": 150.0,
"GBPCAD": 1.70, # Not requested
}
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(data, f)
filepath = f.name
try:
result = read_cached_rates(filepath, currencies)
assert len(result) == 3
assert result["USD"] == Decimal("1.25")
assert result["EUR"] == Decimal("1.15")
assert result["JPY"] == Decimal("150.0")
assert "CAD" not in result # Not requested
finally:
os.unlink(filepath)
def test_read_cached_rates_frankfurter_file(self) -> None:
"""Test reading valid Frankfurter cached rates file."""
currencies = ["USD", "EUR", "JPY"]
data = [
{"date": "2026-06-20", "base": "GBP", "quote": "USD", "rate": 1.25},
{"date": "2026-06-20", "base": "GBP", "quote": "EUR", "rate": 1.15},
{"date": "2026-06-20", "base": "GBP", "quote": "JPY", "rate": 150.0},
{
"date": "2026-06-20",
"base": "GBP",
"quote": "CAD",
"rate": 1.70,
}, # Not requested
]
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(data, f)
filepath = f.name
try:
result = read_cached_rates(filepath, currencies)
assert len(result) == 3
assert result["USD"] == Decimal("1.25")
assert result["EUR"] == Decimal("1.15")
assert result["JPY"] == Decimal("150.0")
assert "CAD" not in result # Not requested
finally:
os.unlink(filepath)
def test_read_cached_rates_missing_currencies(self) -> None:
"""Test with some currencies missing from data."""
currencies = ["USD", "EUR", "CHF"] # CHF not in data
data = {
"quotes": {
"GBPUSD": 1.25,
"GBPEUR": 1.15,
# GBPCHF missing
}
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(data, f)
filepath = f.name
try:
result = read_cached_rates(filepath, currencies)
assert len(result) == 2
assert result["USD"] == Decimal("1.25")
assert result["EUR"] == Decimal("1.15")
assert "CHF" not in result # Missing from data
finally:
os.unlink(filepath)
def test_read_cached_rates_empty_currencies_list(self) -> None:
"""Test with empty currencies list."""
data = {
"quotes": {
"GBPUSD": 1.25,
"GBPEUR": 1.15,
}
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(data, f)
filepath = f.name
try:
result = read_cached_rates(filepath, [])
assert result == {}
finally:
os.unlink(filepath)
def test_read_cached_rates_no_quotes_key(self) -> None:
"""Test with data missing quotes key."""
currencies = ["USD", "EUR"]
data = {"other_key": "value"} # No quotes key
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(data, f)
filepath = f.name
try:
assert read_cached_rates(filepath, currencies) == {}
finally:
os.unlink(filepath)
def test_read_cached_rates_decimal_precision(self) -> None:
"""Test that rates are returned as Decimal with proper precision."""
currencies = ["USD"]
data = {"quotes": {"GBPUSD": 1.234567890123456789}} # High precision
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(data, f)
filepath = f.name
try:
result = read_cached_rates(filepath, currencies)
assert isinstance(result["USD"], Decimal)
# Should preserve reasonable precision from the JSON
# Python's JSON precision may be limited to float precision
expected = Decimal("1.234567890123456789")
assert abs(result["USD"] - expected) < Decimal("0.0000000000000001")
finally:
os.unlink(filepath)
def test_read_cached_rates_various_currency_codes(self) -> None:
"""Test with various currency codes."""
currencies = ["USD", "EUR", "JPY", "CHF", "CAD", "AUD"]
data = {
"quotes": {
"GBPUSD": 1.25,
"GBPEUR": 1.15,
"GBPJPY": 150.0,
"GBPCHF": 1.12,
"GBPCAD": 1.70,
"GBPAUD": 1.85,
}
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(data, f)
filepath = f.name
try:
result = read_cached_rates(filepath, currencies)
assert len(result) == 6
for currency in currencies:
assert currency in result
assert isinstance(result[currency], Decimal)
finally:
os.unlink(filepath)
def test_get_rates_ignores_old_usage_limit_attempt_when_using_frankfurter() -> None:
"""An old provider usage-limit response should not block Frankfurter refreshes."""
with tempfile.TemporaryDirectory() as temp_dir:
fx_dir = os.path.join(temp_dir, "fx")
os.mkdir(fx_dir)
suffix = "EUR,USD_to_GBP.json"
valid_file = os.path.join(fx_dir, f"2026-06-19_23:35_{suffix}")
usage_limit_file = os.path.join(fx_dir, f"2026-06-20_11:35_{suffix}")
with open(valid_file, "w") as f:
json.dump(
{"success": True, "quotes": {"GBPEUR": 1.15, "GBPUSD": 1.25}},
f,
)
with open(usage_limit_file, "w") as f:
json.dump(
{
"success": False,
"error": {
"code": 104,
"type": "usage_limit_reached",
"info": "Your monthly usage limit has been reached.",
},
},
f,
)
config = {
"CURRENCIES": ["USD", "EUR"],
"DATA_DIR": temp_dir,
"EXCHANGERATE_ACCESS_KEY": "key",
"OFFLINE_MODE": False,
"FX_CACHE_TTL_HOURS": 1,
"FX_FAILURE_RETRY_HOURS": 24,
}
with patch("agenda.fx.datetime") as mock_datetime, patch(
"agenda.fx.httpx.Client"
) as mock_client:
mock_datetime.now.return_value = datetime(2026, 6, 20, 12, 35)
mock_datetime.strptime.side_effect = datetime.strptime
mock_response = (
mock_client.return_value.__enter__.return_value.get.return_value
)
mock_response.text = json.dumps(
[
{
"date": "2026-06-20",
"base": "GBP",
"quote": "EUR",
"rate": 1.16,
},
{
"date": "2026-06-20",
"base": "GBP",
"quote": "USD",
"rate": 1.26,
},
]
)
result = get_rates(config)
assert result == {"USD": Decimal("1.26"), "EUR": Decimal("1.16")}
mock_client.return_value.__enter__.return_value.get.assert_called_once_with(
"https://api.frankfurter.dev/v2/rates",
params={"base": "GBP", "quotes": "EUR,USD"},
timeout=10,
)

Some files were not shown because too many files have changed in this diff Show more