|
| 1 | +"""``obehy release feed-list``: the public list of the active release's feeds. |
| 2 | +
|
| 3 | +Caddy serves each static feed under a stable name straight from ``releases/active`` |
| 4 | +(``deploy/Caddyfile``), so the URLs never change; this writes the page that lists them, with |
| 5 | +sizes and the release they come from, as ``index.html`` and ``feeds.json``. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import html |
| 11 | +import json |
| 12 | +from dataclasses import dataclass |
| 13 | +from datetime import UTC, datetime |
| 14 | +from pathlib import Path |
| 15 | +from typing import Any, cast |
| 16 | +from zoneinfo import ZoneInfo |
| 17 | + |
| 18 | +from obehy.pipeline.files import atomic_output_path |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(frozen=True) |
| 22 | +class Feed: |
| 23 | + name: str # the published file name, served next to the list |
| 24 | + source: str # path inside the release directory |
| 25 | + title: str |
| 26 | + |
| 27 | + |
| 28 | +# The names the MOTIS node's /get-feeds/ directory used, so its URLs keep working when it points |
| 29 | +# here. |
| 30 | +STATIC_FEEDS = ( |
| 31 | + Feed("cz-jdf-gtfs.zip", "jdf/gtfs.zip", "Buses (JDF), national"), |
| 32 | + Feed("cz-jdf-filtered-gtfs.zip", "jdf-filtered/gtfs.zip", "Buses (JDF), filtered"), |
| 33 | + Feed("cz-czptt-gtfs.zip", "czptt/gtfs.zip", "Rail (CZPTT), national"), |
| 34 | +) |
| 35 | +REALTIME_FEEDS = (Feed("cz-jdf-gtfs-rt.pb", "", "Buses (JDF), GTFS-RT, updated every 10 s"),) |
| 36 | + |
| 37 | + |
| 38 | +def _built(completed_at: object) -> str: |
| 39 | + """The build time in Prague, as people read it; the raw value if it is not a timestamp.""" |
| 40 | + |
| 41 | + try: |
| 42 | + when = datetime.fromisoformat(str(completed_at)) |
| 43 | + except ValueError: |
| 44 | + return str(completed_at) |
| 45 | + local = when.astimezone(ZoneInfo("Europe/Prague")) |
| 46 | + return f"{local.day}. {local.month}. {local.year} {local:%H:%M}" |
| 47 | + |
| 48 | + |
| 49 | +def _size(size: int) -> str: |
| 50 | + value = float(size) |
| 51 | + for unit in ("B", "kB", "MB", "GB"): |
| 52 | + if value < 1000 or unit == "GB": |
| 53 | + return f"{value:.0f} {unit}" if unit == "B" else f"{value:.1f} {unit}" |
| 54 | + value /= 1000 |
| 55 | + raise AssertionError("unreachable") |
| 56 | + |
| 57 | + |
| 58 | +def feed_list(release_dir: Path) -> dict[str, Any]: |
| 59 | + release = cast( |
| 60 | + dict[str, Any], json.loads((release_dir / "release.json").read_text(encoding="utf-8")) |
| 61 | + ) |
| 62 | + feeds: list[dict[str, Any]] = [] |
| 63 | + for feed in STATIC_FEEDS: |
| 64 | + path = release_dir / feed.source |
| 65 | + if path.is_file(): |
| 66 | + stat = path.stat() |
| 67 | + feeds.append( |
| 68 | + { |
| 69 | + "name": feed.name, |
| 70 | + "title": feed.title, |
| 71 | + "size_bytes": stat.st_size, |
| 72 | + "modified": datetime.fromtimestamp(stat.st_mtime, UTC).isoformat( |
| 73 | + timespec="seconds" |
| 74 | + ), |
| 75 | + } |
| 76 | + ) |
| 77 | + feeds.extend({"name": feed.name, "title": feed.title} for feed in REALTIME_FEEDS) |
| 78 | + return { |
| 79 | + "run_id": release["run_id"], |
| 80 | + "completed_at": release.get("completed_at"), |
| 81 | + "feeds": feeds, |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +def _html(listing: dict[str, Any]) -> str: |
| 86 | + rows = "\n".join( |
| 87 | + '<tr><td><a href="{href}">{name}</a></td><td>{title}</td><td>{size}</td></tr>'.format( |
| 88 | + href=html.escape(feed["name"], quote=True), |
| 89 | + name=html.escape(feed["name"]), |
| 90 | + title=html.escape(feed["title"]), |
| 91 | + size=_size(feed["size_bytes"]) if "size_bytes" in feed else "live", |
| 92 | + ) |
| 93 | + for feed in listing["feeds"] |
| 94 | + ) |
| 95 | + return f"""<!doctype html> |
| 96 | +<html lang="en"> |
| 97 | +<head> |
| 98 | +<meta charset="utf-8"> |
| 99 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 100 | +<title>Oběhy feeds</title> |
| 101 | +<style> |
| 102 | +:root {{ color-scheme: light dark; font-family: system-ui, sans-serif; }} |
| 103 | +body {{ max-width: 44rem; margin: 2rem auto; padding: 0 1rem; line-height: 1.5; }} |
| 104 | +table {{ border-collapse: collapse; width: 100%; }} |
| 105 | +td, th {{ padding: .35rem .6rem; border-bottom: 1px solid #8884; text-align: left; }} |
| 106 | +td:last-child, th:last-child {{ text-align: right; white-space: nowrap; }} |
| 107 | +small {{ opacity: .7; }} |
| 108 | +</style> |
| 109 | +</head> |
| 110 | +<body> |
| 111 | +<h1>Oběhy feeds</h1> |
| 112 | +<p><small>Release {html.escape(listing["run_id"])}, built { |
| 113 | + html.escape(_built(listing["completed_at"])) |
| 114 | + }. File names stay the same across releases. Machine-readable: |
| 115 | +<a href="feeds.json">feeds.json</a>.</small></p> |
| 116 | +<table> |
| 117 | +<thead><tr><th>File</th><th>Feed</th><th>Size</th></tr></thead> |
| 118 | +<tbody> |
| 119 | +{rows} |
| 120 | +</tbody> |
| 121 | +</table> |
| 122 | +</body> |
| 123 | +</html> |
| 124 | +""" |
| 125 | + |
| 126 | + |
| 127 | +def write_feed_list(release_dir: Path, out_dir: Path) -> dict[str, Any]: |
| 128 | + """Write ``index.html`` and ``feeds.json`` for the release into ``out_dir``.""" |
| 129 | + |
| 130 | + listing = feed_list(release_dir) |
| 131 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 132 | + with atomic_output_path(out_dir / "feeds.json") as path: |
| 133 | + path.write_text(json.dumps(listing, indent=2) + "\n", encoding="utf-8") |
| 134 | + with atomic_output_path(out_dir / "index.html") as path: |
| 135 | + path.write_text(_html(listing), encoding="utf-8") |
| 136 | + return listing |
0 commit comments