Document codebase: comments, per-area READMEs, deployment guide

Readability pass across all four areas of the stack. Comments explain why
rather than what, concentrated at the points where behaviour is non-obvious:
concurrency, caching, auth, scraping and nginx templating.

Backend: doc comments on every exported declaration across handlers,
services, models and resolvers. Resolver commentary lives inside function
bodies so gqlgen does not strip it on regeneration; schema files use #
comments rather than description blocks so introspection output is
unchanged. Extracted the duplicated auth-cookie blocks into helpers.

Frontend: header comments on every component, store and view, including
STATUS: notes marking which of the parallel/legacy views are still live.
Documented the non-reactive animation-state pattern, the homeData hub and
the WebSocket stale-socket guard. Removed a few dead imports and named the
reconnect-backoff and poll-interval constants.

Python: module and function docstrings covering the Travelodge scraping
contract, the deadline threading, the two-executor concurrency and the
NDJSON streaming protocol.

Infra: header blocks on every compose file, nginx template, Dockerfile and
entrypoint, covering the config selection, envsubst allow-lists, rate-limit
semantics and volume persistence. Hoisted the duplicated envsubst variable
list in nginx/entrypoint.sh.

New: backend/README.md, python/README.md, DEPLOYMENT.md, .env.example,
backend/graph/doc.go. Refreshed vue/README.md. Corrected CLAUDE.md, which
stated a 24h access token where main.go sets 7 days.

No behaviour change. go build/vet, vite build, py_compile and docker
compose config all pass.

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
2026-09-16 15:57:48 +01:00
co-authored by Claude Opus 5
parent a1102b4aa1
commit 64df70e5d2
183 changed files with 4691 additions and 204 deletions
+26
View File
@@ -1,3 +1,29 @@
# =============================================================================
# FastAPI side-service image.
#
# requirements.txt is copied and installed before the app source so the
# dependency layer is cached across code changes. --no-cache-dir keeps pip's
# download cache out of the image (this one is a plain layer, not a BuildKit
# cache mount, because the dependency set is small).
#
# PYTHONDONTWRITEBYTECODE stops .pyc files being written into the image and the
# dev bind mount; PYTHONUNBUFFERED makes logs appear in `docker logs`
# immediately instead of sitting in a buffer — both are standard for
# containerised Python and the second one matters for debugging.
#
# ROOT_PATH=/py: nginx proxies /py/ and STRIPS the prefix, so the app's routes
# are written relative to /. --root-path tells FastAPI it is nonetheless
# mounted under a prefix, so the OpenAPI schema and Swagger UI emit correct
# /py/... URLs. Without it, /py/docs loads but cannot call anything.
#
# The CMD is a `sh -c` form on purpose: it needs the shell to expand
# ${PYTHON_PORT} and ${ROOT_PATH} at runtime, which the exec form cannot do.
# Both have defaults here so the image runs standalone; docker compose normally
# supplies them.
#
# EXPOSE 8000 is documentation only — the port is not published; nginx reaches
# it over app-network.
# =============================================================================
FROM python:3.12-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
+157
View File
@@ -0,0 +1,157 @@
# Python API service
A small FastAPI app (`app/main.py`) served by uvicorn on port 8000 inside the
`python` container. Most of it is one feature: **cheap Travelodge finder** —
given a London location and dates it pulls live room prices from Travelodge and
adds the cost of getting there from a chosen station on TfL, so you can rank
hotels by what the night *actually* costs, not just the room.
## Layout
| File | Role |
| --- | --- |
| `app/main.py` | HTTP skin: request models/validation, response cache, one-at-a-time guard, NDJSON framing, route handlers |
| `app/travelodge.py` | All the real work: upstream calls, caching, rate limiting, fare maths, the two search generators |
| `app/__init__.py` | Empty; makes `app` a package so `app.main:app` resolves |
`travelodge.py` never imports FastAPI and can be driven from a plain script.
## Endpoints
Paths below are as the service sees them; from a browser prefix everything with
`/py` (see *Behind nginx*).
| Method | Path | What it does |
| --- | --- | --- |
| GET | `/` | Index message |
| GET | `/health` | Liveness: `{"status": "ok", "time": ...}` |
| GET | `/hello/{name}` | Demo route |
| GET | `/hotels/origins` | The built-in origin stations the UI offers |
| POST | `/hotels/search` | Blocking search; returns once every shortlisted hotel is priced |
| POST | `/hotels/search/stream` | Same search as NDJSON progress lines |
| POST | `/hotels/scan/stream` | Cheapest night across a date range, NDJSON |
Interactive schema: `/py/docs` (Swagger UI), `/py/openapi.json`.
### Streaming format
The stream endpoints return `application/x-ndjson`: one JSON object per line,
no SSE framing. Read a line, parse it, apply it. `exclude_none=True` is set, so
**absent keys mean null**.
`/hotels/search/stream` emits:
1. `hotels` — every shortlisted hotel with its room price and an estimated
cycling time; transport columns are still null and `route` reads `pricing…`.
2. `transport` — one line per hotel as TfL answers; patch that row by `code`.
3. `done` — final summary plus all rows sorted by total.
`/hotels/scan/stream` emits `scan` (all candidate nights, pending), then a
`night` per date, then a `transport` per **unique hotel code** (apply it to
every night containing that code — transport is priced once and reused), then
`done`.
An `error` line ends a failed stream. Streams are always HTTP 200: the response
starts before the work does, so failures cannot become a status code. The
blocking `/hotels/search` is the exception — it returns 429 when a search is
already running and 502 on upstream failure.
Only **one** live search or scan runs process-wide at a time (the TfL budget
can't be shared), and identical requests are cached for 10 minutes.
## Behind nginx
`nginx/nginx.conf.template` proxies `/py/` to this container and **strips the
prefix**, so routes here are written relative to `/`. `--root-path /py` (set on
the uvicorn command line via `ROOT_PATH`) is what keeps the generated docs and
OpenAPI URLs correct behind that rewrite.
The hotel routes get their own nginx location with `proxy_buffering off` and a
300s read timeout, because otherwise nginx would hold the whole NDJSON stream
until it finished. The app also sends `X-Accel-Buffering: no` as a belt-and-
braces version of the same thing. The internal deadlines in `travelodge.py`
(200s for a search, 240s for a scan) exist to finish *before* that 300s cut, and
return partial results with `summary.truncated = true` instead of dying.
Env vars (from `docker-compose.yml`): `PYTHON_PORT`, `ROOT_PATH`, and the
optional `TFL_APP_KEY` (see below). Dev mode mounts `python/app` and runs
uvicorn with `--reload`.
## Running locally
Inside the whole stack:
```
docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build
# http://localhost/py/docs
```
Or just this service:
```
cd python
python3 -m venv .venv && . .venv/bin/activate
pip install -r requirements.txt
uvicorn app.main:app --reload --port 8000
# http://localhost:8000/docs (no /py prefix when run directly)
```
Dependencies are `fastapi`, `uvicorn[standard]` and `requests` — nothing else.
## How the pricing works
1. Search Travelodge for the location and dates, following pagination, and drop
hotels with no availability, no price, or no coordinates.
2. Keep the cheapest `top` (search) or `per_night` (scan) of them.
3. For each, ask TfL for the fastest route *and* a bus-only route at the given
departure time. Pick the cheapest that isn't slower than `max_travel_min`.
4. Apply a Railcard discount (1/3 off, rounded to 5p) to off-peak rail/tube
fares only — never buses. The return leg is assumed to be the same journey
the next morning at off-peak rates.
5. Rank by room + round-trip transport. Cycling time is shown alongside.
### Fragility — read this before debugging "no prices"
Both upstreams are JSON APIs, not HTML, so there are no CSS selectors to break —
but the **key names play exactly that role** and neither API is a public,
documented contract for this use:
- Travelodge's `/api/v2/hotel` is the feed its own search page calls. The query
parameters (`q`, `checkIn`, `rooms[0][adults]`, the undocumented
`action=hotel_amend`, and `pagination=false`, which only hides the pagination
metadata and does *not* return everything in one page) and the response keys
read in `normalise_hotel` are whatever the site sends today.
- A browser `User-Agent` is sent because the edge throttles obviously scripted
clients. If everything starts coming back empty or 403, bump it first.
- Every field read is tolerant — a renamed key yields `None`, not an exception.
So a shape change looks like *"every hotel is unavailable"*, not a 502.
- TfL fares come from `fare.fares[].cost/peak/offPeak/chargeLevel`. Off-peak is
detected by substring-matching `chargeLevel` for "off" *or* the cost equalling
the off-peak price, because that field's spelling has moved around.
- Pagination stops when a batch of pages returns nothing new or comes back
short, with `TL_MAX_START` as a hard backstop.
There is no scraping of pages behind a login and no attempt to book anything,
but this is still an undocumented internal API being polled from a public web
page — check Travelodge's terms before pointing it at anything heavier.
### TfL rate limit
Anonymous callers get 50 requests/min from a single IP; with a `TFL_APP_KEY` it
is 500/min. A search spends 2–3 requests per hotel, so the anonymous tier is the
binding constraint and is what the single-search-slot guard protects. Setting
`TFL_APP_KEY` also switches cycling times from a distance-based estimate to real
TfL cycle routing (`cycle_source` tells you which you're looking at).
## Frontend
Consumed by `vue/src/views/hotels/`:
- `SingleStayPanel.vue` → `POST /py/hotels/search/stream`
- `CheapestNightPanel.vue` → `POST /py/hotels/scan/stream`
- `useHotelForm.js` → `GET /py/hotels/origins`
- `hotelsStream.js` — the NDJSON reader (splits on newlines, skips a malformed
line rather than aborting the search)
The Pydantic field names and JSON keys in `main.py` are that contract; renaming
one breaks the UI silently.
+138 -7
View File
@@ -4,6 +4,21 @@ Nginx proxies https://www.<DOMAIN>/py/... to this container and strips the
/py prefix, so routes here are defined relative to "/". FastAPI's root_path
(set via --root-path on the uvicorn command line) keeps the generated docs
at /py/docs working behind the proxy.
Endpoints
GET / service index
GET /health liveness probe
GET /hello/{name} demo route
GET /hotels/origins the known travel origins the UI offers
POST /hotels/search blocking cheapest-Travelodge search
POST /hotels/search/stream same search, streamed as NDJSON
POST /hotels/scan/stream cheapest-night-in-a-range scan, NDJSON
All the hotel work lives in `travelodge`; this module is the HTTP skin around
it - request validation, a response cache, the one-search-at-a-time guard, and
the NDJSON framing. Consumed by vue/src/views/hotels/ (SingleStayPanel.vue,
CheapestNightPanel.vue, useHotelForm.js), which fetch these paths with the /py
prefix. Field names in the models below are part of that contract.
"""
import json
@@ -28,26 +43,33 @@ app = FastAPI(
class Health(BaseModel):
"""Liveness payload: a fixed status string and the server's UTC time."""
status: str
time: datetime
class Greeting(BaseModel):
"""A single human-readable message."""
message: str
@app.get("/", response_model=Greeting, summary="Service index")
def index() -> Greeting:
"""Point a curious caller at the docs (which live at /py/docs via nginx)."""
return Greeting(message="Python API is running. See /py/docs for the schema.")
@app.get("/health", response_model=Health, summary="Liveness check")
def health() -> Health:
"""Liveness check; always 200 while the process is up."""
return Health(status="ok", time=datetime.now(timezone.utc))
@app.get("/hello/{name}", response_model=Greeting, summary="Greet a caller")
def hello(name: str) -> Greeting:
"""Echo a greeting back. Demo route, nothing depends on it."""
return Greeting(message=f"Hello, {name}!")
@@ -60,12 +82,18 @@ MAX_CHILDREN = 8
MAX_SCAN_DAYS = travelodge.SCAN_MAX_DAYS
CACHE_TTL_S = 10 * 60
# Only one live search or scan may run at a time, process-wide. A run can spend
# the entire anonymous TfL budget (50 requests/min), so two concurrent runs
# would simply starve each other into rate-limit backoff. Callers that find the
# slot taken get an immediate "try again" rather than queueing.
_search_slot = threading.Semaphore(1)
_cache_lock = threading.Lock()
_cache: dict = {} # key -> (expires_at, response dict)
class Origin(BaseModel):
"""One selectable travel origin, as offered to the UI's dropdown."""
key: str
name: str
lat: float
@@ -120,6 +148,12 @@ class HotelRequestBase(BaseModel):
return v.strip()
def shared_kwargs(self) -> dict:
"""Translate the validated request into travelodge.run_* keyword args.
Parsing runs a second time here (the validators above only checked
validity and discarded the result), which is cheap and keeps the model
holding exactly what the client sent.
"""
origin_name, origin = travelodge.parse_origin(self.origin)
return {
"location": self.location,
@@ -134,6 +168,8 @@ class HotelRequestBase(BaseModel):
class HotelSearchRequest(HotelRequestBase):
"""One stay on known dates: rank hotels by room price plus transport."""
checkin: date = Field(..., description="Check-in date (today or later)")
checkout: Optional[date] = Field(None, description="Check-out date; overrides nights")
nights: int = Field(1, ge=1, le=14, description="Used when checkout is not given")
@@ -141,6 +177,14 @@ class HotelSearchRequest(HotelRequestBase):
@model_validator(mode="after")
def _check_dates(self):
"""Resolve checkout/nights into a consistent pair and bound the stay.
`checkout` wins when both are given; otherwise it is derived from
`nights`. Both fields are then written back so downstream code can
trust either one. `date.today()` is the server's local date, so a user
in another timezone can be refused a checkin that is still valid for
them.
"""
if self.checkin < date.today():
raise ValueError("checkin must be today or later")
checkout = self.checkout or self.checkin + timedelta(days=self.nights)
@@ -154,6 +198,8 @@ class HotelSearchRequest(HotelRequestBase):
class HotelScanRequest(HotelRequestBase):
"""A range of candidate check-in dates: find the cheapest night to stay."""
start: date = Field(..., description="First candidate check-in date (today or later)")
end: date = Field(..., description=f"Last candidate check-in date, at most {MAX_SCAN_DAYS} days after start")
weekdays: list[int] = Field(
@@ -166,6 +212,11 @@ class HotelScanRequest(HotelRequestBase):
@field_validator("weekdays")
@classmethod
def _check_weekdays(cls, v: list[int]) -> list[int]:
"""Validate and de-duplicate the weekday filter, preserving input order.
Order is preserved (rather than using a set) so the echoed summary and
the cache key stay deterministic for a given request body.
"""
days = []
for d in v:
if d < 0 or d > 6:
@@ -178,6 +229,12 @@ class HotelScanRequest(HotelRequestBase):
@model_validator(mode="after")
def _check_dates(self):
"""Bound the range and reject one that no candidate date can satisfy.
The final check actually enumerates the candidates, so a range like
"Mondays only" over a Tuesday-to-Thursday window fails here with a clear
message instead of producing an empty scan.
"""
if self.start < date.today():
raise ValueError("start must be today or later")
if self.end < self.start:
@@ -190,6 +247,14 @@ class HotelScanRequest(HotelRequestBase):
class HotelRow(BaseModel):
"""One hotel in a result list.
Prices are pounds. Transport fields are None until the matching "transport"
event arrives, and `route` carries the reason when they stay None ("pricing…",
"no TfL fare found", "not priced (time limit)"). Field names are consumed
verbatim by the Vue frontend - do not rename.
"""
code: Optional[str] = None
name: Optional[str] = None
lat: Optional[float] = None
@@ -216,6 +281,9 @@ class HotelRow(BaseModel):
class HotelSearchSummary(BaseModel):
"""Counts and echoed parameters for a search; `truncated` means the time
budget ran out before every shortlisted hotel was priced."""
location: str
checkin: date
checkout: date
@@ -238,6 +306,8 @@ class HotelSearchSummary(BaseModel):
class HotelSearchResponse(BaseModel):
"""Final result of the blocking search endpoint."""
summary: HotelSearchSummary
rows: list[HotelRow]
cached: bool = False
@@ -245,6 +315,7 @@ class HotelSearchResponse(BaseModel):
@app.get("/hotels/origins", response_model=list[Origin], summary="Known origin stations")
def hotel_origins() -> list[Origin]:
"""List the built-in origin stations the UI offers (plus free "lat,lon")."""
return [Origin(key=k, name=n, lat=lat, lon=lon) for k, (n, lat, lon) in travelodge.ORIGINS.items()]
@@ -277,6 +348,8 @@ class TransportFields(BaseModel):
class NightRow(BaseModel):
"""One candidate night in a scan, with its cheapest hotels."""
checkin: date
checkout: date
status: str = Field("pending", description='"pending", "ok", "failed" or "skipped"')
@@ -289,6 +362,9 @@ class NightRow(BaseModel):
class HotelScanSummary(BaseModel):
"""Counts and echoed parameters for a scan; `truncated` means some nights
were skipped or some hotels left unpriced when time ran out."""
location: str
start: date
end: date
@@ -326,7 +402,8 @@ class HotelScanEvent(BaseModel):
detail: Optional[str] = None
def _cache_get(key):
def _cache_get(key: str):
"""Return a cached response payload for `key`, or None if absent/expired."""
now = time.monotonic()
with _cache_lock:
hit = _cache.get(key)
@@ -335,7 +412,12 @@ def _cache_get(key):
return None
def _cache_put(key, result):
def _cache_put(key: str, result: dict) -> None:
"""Cache a completed response for CACHE_TTL_S, sweeping expired entries.
The sweep is the only eviction: the cache is unbounded between sweeps, which
is fine because the one-at-a-time guard caps how fast entries can be added.
"""
now = time.monotonic()
with _cache_lock:
expired = [k for k, (exp, _) in _cache.items() if exp <= now]
@@ -370,7 +452,14 @@ def _guarded(run, make_error):
def _search_events(req: HotelSearchRequest):
"""Yield HotelSearchEvent objects for a request, serving from cache when possible."""
"""Yield HotelSearchEvent objects for a request, serving from cache when possible.
A cache hit replays a two-line stream ("hotels" then "done") carrying the
finished result, so the client's streaming reader needs no special case; the
`cached` flag is the only difference it can observe. The key is the
canonical JSON of the whole validated request, which makes it exact but also
means a cosmetically different body (a different `top`, say) misses.
"""
key = "search:" + json.dumps(req.model_dump(mode="json"), sort_keys=True)
hit = _cache_get(key)
if hit:
@@ -379,6 +468,9 @@ def _search_events(req: HotelSearchRequest):
return
def run():
# run_search yields 3-tuples except for "transport", which is a 2-tuple
# of (kind, row) - hence the shape test before unpacking. Only the final
# "done" event is worth caching.
gen = travelodge.run_search(
checkin=req.checkin,
checkout=req.checkout,
@@ -398,7 +490,11 @@ def _search_events(req: HotelSearchRequest):
def _scan_events(req: HotelScanRequest):
"""Yield HotelScanEvent objects for a scan, serving from cache when possible."""
"""Yield HotelScanEvent objects for a scan, serving from cache when possible.
As with searches, a cache hit is replayed as a "scan" line followed by
"done" so the client parses it exactly like a live stream.
"""
key = "scan:" + json.dumps(req.model_dump(mode="json"), sort_keys=True)
hit = _cache_get(key)
if hit:
@@ -442,7 +538,18 @@ _SCAN_DESCRIPTION = (
)
def _ndjson(events):
def _ndjson(events) -> StreamingResponse:
"""Frame an iterable of Pydantic events as newline-delimited JSON.
NDJSON rather than SSE: the client just splits on "\n" and JSON-parses each
line, with no event-type framing to strip. `exclude_none=True` keeps the
lines small, so absent keys must be treated as null by the reader.
X-Accel-Buffering: no is essential - without it nginx buffers the whole
response and the user sees nothing until the search finishes, defeating the
point of streaming. Cache-Control: no-cache stops any intermediary storing
a partial stream.
"""
return StreamingResponse(
(ev.model_dump_json(exclude_none=True) + "\n" for ev in events),
media_type="application/x-ndjson",
@@ -458,6 +565,17 @@ def _ndjson(events):
responses={429: {"description": "Another search is already running"}, 502: {"description": "Upstream API failed"}},
)
def hotel_search(req: HotelSearchRequest) -> HotelSearchResponse:
"""Run a search to completion and return the final result.
Drains the same event generator the streaming endpoint uses and keeps only
the "done" event. Because the status code must be chosen before anything is
written, an "error" event becomes an HTTPException here: 429 when the single
search slot was busy, 502 for anything upstream.
Raises:
HTTPException: 429 (busy) or 502 (upstream failure, or a stream that
ended without a result).
"""
final = None
for ev in _search_events(req):
if ev.event == "error":
@@ -482,7 +600,13 @@ def hotel_search(req: HotelSearchRequest) -> HotelSearchResponse:
response_class=StreamingResponse,
responses={200: {"content": {"application/x-ndjson": {"schema": HotelSearchEvent.model_json_schema()}}}},
)
def hotel_search_stream(req: HotelSearchRequest):
def hotel_search_stream(req: HotelSearchRequest) -> StreamingResponse:
"""Stream the search as NDJSON.
Always 200: the response starts before the work does, so failures (including
the busy-slot case that the blocking endpoint reports as 429) arrive as a
final "error" line instead of a status code.
"""
return _ndjson(_search_events(req))
@@ -499,5 +623,12 @@ def hotel_search_stream(req: HotelSearchRequest):
response_class=StreamingResponse,
responses={200: {"content": {"application/x-ndjson": {"schema": HotelScanEvent.model_json_schema()}}}},
)
def hotel_scan_stream(req: HotelScanRequest):
def hotel_scan_stream(req: HotelScanRequest) -> StreamingResponse:
"""Stream the cheapest-night scan as NDJSON.
Transport is emitted once per unique hotel code, not once per row: the
client is expected to apply each "transport" line to every night containing
that code. Like the search stream this is always 200, with failures carried
as a final "error" line.
"""
return _ndjson(_scan_events(req))
+406 -43
View File
@@ -28,6 +28,33 @@ Method
of the outbound.
5. Rank by room + round-trip transport. Cycling time is reported separately
(TfL cycle routing with an app key, otherwise a distance-based estimate).
Fragility
Neither API is a documented public contract for this use. Travelodge's
/api/v2/hotel endpoint is the JSON feed its own search page calls; the query
parameter names ("q", "checkIn", "rooms[0][adults]", the undocumented
"action=hotel_amend") and the response field names consumed in
`normalise_hotel` are whatever the site happens to send today. If Travelodge
reshapes its search page, every price here silently becomes None rather than
raising. Same for TfL's fare JSON: `price_journey` reads `fare.fares[].cost`
/ `peak` / `offPeak` / `chargeLevel`, and falls back to "no fare found" if the
shape changes. There is no HTML parsing anywhere in this module - both
upstreams are JSON - so there are no CSS/XPath selectors to break, but the
key names play the same role.
Concurrency
Everything here is thread-based (requests is blocking), not asyncio. Module
state - the two response caches, the rate limiters and the shared
requests.Session - is process-global and guarded by plain locks, so it is
shared across concurrent FastAPI requests. main.py additionally allows only
one live search at a time, which is what keeps the TfL budget spendable by a
single run.
Time budgets
Every network path takes an optional `deadline` (a time.monotonic() value).
Passing it down instead of using a wall-clock timeout lets a long run degrade
gracefully: work already done is returned with summary["truncated"] = True
instead of the whole request dying at the nginx 300s proxy timeout.
"""
import math
@@ -43,6 +70,11 @@ import requests
TL_API = "https://www.travelodge.co.uk/api/v2/hotel"
TL_SITE = "https://www.travelodge.co.uk"
TFL_API = "https://api.tfl.gov.uk/Journey/JourneyResults/{frm}/to/{to}"
# A browser User-Agent is sent because Travelodge's edge rejects or throttles
# obviously-scripted clients (python-requests/x.y). The Chrome version is
# arbitrary and is never checked for freshness; if requests start coming back
# as 403 or as a challenge page, this is the first thing to bump. The same
# headers are reused for TfL, where they are harmless.
HEADERS = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0 Safari/537.36",
"Accept": "application/json",
@@ -101,6 +133,11 @@ TFL_CACHE_TTL_S = 2 * 60 * 60
CYCLE_ROUTE_FACTOR = 1.3
CYCLE_KMH = 14.0
# One process-wide Session so TLS handshakes and any cookies the upstreams set
# are reused across requests (a fresh Session per call roughly doubles latency).
# pool_maxsize must cover the widest possible fan-out - TfL workers plus every
# scan date worker running its own batch of Travelodge page fetches - otherwise
# urllib3 starts discarding connections and logging pool-full warnings.
_session = requests.Session()
_session.mount(
"https://",
@@ -115,8 +152,23 @@ class UpstreamError(Exception):
# --------------------------------------------------------------------------- #
# Input parsing
# --------------------------------------------------------------------------- #
def parse_guests(spec):
"""'2,2+1' -> [(2, 0), (2, 1)] (list of (adults, children) per room)."""
def parse_guests(spec: str) -> list[tuple[int, int]]:
"""Parse the compact guests string into per-room occupancy.
Comma separates rooms, "+" separates adults from children within a room,
so '2,2+1' -> [(2, 0), (2, 1)]: two rooms, the second with one child.
Args:
spec: user-supplied guests string, e.g. "1", "2+1", "2,2+2".
Returns:
One (adults, children) tuple per room, in the order given. The index of
each tuple becomes the ``rooms[i][...]`` index in the Travelodge query.
Raises:
ValueError: empty spec, non-numeric part, a room with no adult, or a
negative child count.
"""
rooms = []
for part in spec.split(","):
part = part.strip()
@@ -134,8 +186,25 @@ def parse_guests(spec):
return rooms
def parse_origin(spec):
"""Return (name, (lat, lon)) for an ORIGINS key or a 'lat,lon' string."""
def parse_origin(spec: str) -> tuple[str, tuple[float, float]]:
"""Resolve an origin to a display name and coordinates.
Args:
spec: either a key of :data:`ORIGINS` (case-insensitive) or a raw
"lat,lon" pair.
Returns:
(display name, (lat, lon)). For raw coordinates the display name is the
coordinates re-formatted to 4 decimal places (~11m), which is both the
label shown to the user and a stable cache-key component.
Raises:
ValueError: unknown station, unparseable coordinates, or coordinates
outside :data:`LONDON_BOX`. The box check exists because TfL's
journey planner will happily route from anywhere in the country and
burn the rate-limit budget on journeys this tool cannot price
sensibly.
"""
key = spec.strip().lower()
if key in ORIGINS:
name, lat, lon = ORIGINS[key]
@@ -155,7 +224,24 @@ def parse_origin(spec):
# --------------------------------------------------------------------------- #
# Travelodge
# --------------------------------------------------------------------------- #
def _fetch_page(params, start, deadline=None):
def _fetch_page(params: dict, start: int, deadline: float | None = None) -> list[dict]:
"""Fetch one page of Travelodge search results.
Args:
params: the shared query parameters built by :func:`fetch_hotels`.
start: zero-based offset of the first result wanted; the API paginates
by offset, not page number, in steps of :data:`TL_PAGE_SIZE`.
deadline: time.monotonic() value; the socket timeout is shortened so a
hung page cannot outlive the caller's budget.
Returns:
The raw ``results`` array, or [] when the key is missing - an empty page
is the only signal the API gives that pagination is exhausted.
Raises:
UpstreamError: transport failure, non-2xx status, or a body that is not
JSON (which in practice means a challenge/error HTML page).
"""
p = dict(params, start=start)
timeout = 30
if deadline is not None:
@@ -165,14 +251,33 @@ def _fetch_page(params, start, deadline=None):
r.raise_for_status()
return r.json().get("results") or []
except (requests.RequestException, ValueError, AttributeError) as e:
# ValueError covers r.json() on a non-JSON body; AttributeError covers a
# JSON body that is a list or scalar rather than an object (no .get).
raise UpstreamError(f"Travelodge search failed: {e}") from e
def fetch_hotels(location, checkin, checkout, rooms, deadline=None):
"""Return list of hotel dicts from the Travelodge search API (all pages).
def fetch_hotels(location, checkin, checkout, rooms, deadline=None) -> list[dict]:
"""Return the raw hotel dicts for a search, following pagination.
rooms: list of (adults, children) tuples, one per room.
deadline: time.monotonic() value after which page requests are cut short.
Args:
location: free-text search string, passed straight through as ``q``.
checkin, checkout: :class:`datetime.date`; the API wants ISO dates.
rooms: list of (adults, children) tuples from :func:`parse_guests`.
deadline: time.monotonic() value limiting each page request.
Returns:
Hotel dicts in API order, de-duplicated by ``code``. Order matters: it
is the site's own relevance ranking and later sorts are stable, so ties
on price keep the site's ordering.
Raises:
UpstreamError: propagated from :func:`_fetch_page`.
Note:
``action=hotel_amend`` is an undocumented parameter copied from the
site's own XHR; without it the endpoint answers differently. Likewise
``pagination=false`` does *not* disable paging - it only suppresses the
pagination metadata block - which is why the offset loop below exists.
"""
params = {
"pagination": "false",
@@ -185,9 +290,11 @@ def fetch_hotels(location, checkin, checkout, rooms, deadline=None):
params[f"rooms[{i}][adults]"] = adults
params[f"rooms[{i}][children]"] = children
hotels, seen = [], set()
hotels: list[dict] = []
seen: set[str] = set()
def absorb(results):
def absorb(results: list[dict]) -> bool:
"""Append hotels not seen before; return True if any were new."""
new = [h for h in results if h.get("code") and h.get("code") not in seen]
for h in new:
seen.add(h["code"])
@@ -199,6 +306,11 @@ def fetch_hotels(location, checkin, checkout, rooms, deadline=None):
if not absorb(first) or len(first) < TL_PAGE_SIZE:
return hotels
# Fetch TL_PAGE_BATCH pages at a time and stop on the first batch that is
# either short (a page with fewer than TL_PAGE_SIZE results is the last one)
# or wholly duplicate. Requesting speculatively overshoots by up to a batch,
# but a strictly sequential walk of ~50 pages would blow the time budget.
# TL_MAX_START is a hard stop in case the API ever pages forever.
start = TL_PAGE_SIZE
with ThreadPoolExecutor(max_workers=TL_PAGE_BATCH) as ex:
while start <= TL_MAX_START:
@@ -217,7 +329,17 @@ def fetch_hotels(location, checkin, checkout, rooms, deadline=None):
return hotels
def normalise_hotel(h):
def normalise_hotel(h: dict) -> dict:
"""Map one raw Travelodge hotel dict onto this service's stable field names.
This is the single place where upstream key names are read, so it is the
only file location that needs changing when Travelodge renames a field.
Every lookup is tolerant: a renamed or missing key yields None (or False)
rather than an exception, so a shape change degrades into "no price"
instead of a 502. ``minPrice`` is the cheapest bookable room for the whole
stay in pounds (already a number, no currency string to parse), and
``hotelUrl`` is site-relative so :data:`TL_SITE` is prefixed here.
"""
return {
"code": h.get("code"),
"name": h.get("title"),
@@ -242,6 +364,9 @@ def hotels_for(location, checkin, checkout, rooms, deadline=None):
Failures are not cached. Callers must treat the returned dicts as read-only.
"""
# Case/whitespace-insensitive location so "Central London" and
# "central london " share one cache entry; rooms must be hashable, hence
# the tuple.
key = (location.strip().lower(), checkin, checkout, tuple(rooms))
now = time.monotonic()
with _tl_cache_lock:
@@ -249,6 +374,9 @@ def hotels_for(location, checkin, checkout, rooms, deadline=None):
if hit and hit[0] > now:
return hit[1]
# Deliberately outside the lock: a slow upstream fetch must not block other
# threads reading the cache. Two threads racing on the same key both fetch
# and the second write wins, which is harmless.
hotels = [normalise_hotel(h) for h in fetch_hotels(location, checkin, checkout, rooms, deadline=deadline)]
with _tl_cache_lock:
@@ -256,8 +384,10 @@ def hotels_for(location, checkin, checkout, rooms, deadline=None):
expired = [k for k, (exp, _) in _tl_cache.items() if exp <= now]
for k in expired:
del _tl_cache[k]
# Bounded, not LRU: dicts preserve insertion order, so evicting the
# first key drops the oldest *written* entry regardless of recent reads.
while len(_tl_cache) >= TL_CACHE_MAX:
del _tl_cache[next(iter(_tl_cache))] # oldest insertion
del _tl_cache[next(iter(_tl_cache))]
_tl_cache[key] = (now + TL_CACHE_TTL_S, hotels)
return hotels
@@ -265,15 +395,27 @@ def hotels_for(location, checkin, checkout, rooms, deadline=None):
# --------------------------------------------------------------------------- #
# Geometry
# --------------------------------------------------------------------------- #
def haversine_km(a, b):
def haversine_km(a: tuple[float, float], b: tuple[float, float]) -> float:
"""Great-circle distance in km between two (lat, lon) points in degrees.
6371.0 km is the mean Earth radius; at London scale the error from assuming
a sphere is far smaller than the road-winding fudge applied on top.
"""
lat1, lon1 = map(math.radians, a)
lat2, lon2 = map(math.radians, b)
h = math.sin((lat2 - lat1) / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin((lon2 - lon1) / 2) ** 2
return 2 * 6371.0 * math.asin(math.sqrt(h))
def estimate_cycle(origin, dest):
"""(minutes, km) for cycling from origin to dest, from straight-line distance."""
def estimate_cycle(origin: tuple[float, float], dest: tuple[float, float]) -> tuple[int, float]:
"""Estimate (minutes, km) for a cycle journey without calling TfL.
Straight-line distance is inflated by :data:`CYCLE_ROUTE_FACTOR` (roads do
not go in straight lines) and divided by :data:`CYCLE_KMH`, a relaxed urban
pace that absorbs traffic lights. Both constants are hand-tuned guesses, not
measurements; they are only used when no TFL_APP_KEY is configured. Minutes
are clamped to at least 1 so a hotel next to the origin never reads "0 min".
"""
km = haversine_km(origin, dest) * CYCLE_ROUTE_FACTOR
return max(1, round(km / CYCLE_KMH * 60)), round(km, 1)
@@ -281,7 +423,14 @@ def estimate_cycle(origin, dest):
# --------------------------------------------------------------------------- #
# TfL
# --------------------------------------------------------------------------- #
def round_5p(pence):
def round_5p(pence: float) -> int:
"""Round a pence amount to the nearest 5p.
TfL quotes every discounted fare in whole 5p steps, so a raw 1/3-off
multiplication has to be snapped back onto that grid to match what the
passenger is actually charged. Python's banker's rounding applies at exact
half-steps (2.5p), which is a sub-penny discrepancy and ignored.
"""
return int(5 * round(pence / 5.0))
@@ -292,6 +441,18 @@ def price_journey(journey, adults=1, railcard_holders=1):
adults: number of fare-paying adults. railcard_holders: how many of them
get 1/3 off off-peak rail fares (0 = nobody has a railcard).
All money is integer pence, straight from TfL - no currency strings are
parsed and no floats are introduced until the final division by 100 in
:func:`_transport_fields`, which keeps the arithmetic exact.
Children are not counted: the caller passes adults only, on the assumption
that under-11s travel free on TfL.
The return leg is modelled, not fetched. It is assumed to be the same route
made the next morning at an off-peak time, so it is priced at the outbound
fare's ``offPeak`` value. That is optimistic for an early-morning return
into zone 1, which is a real peak fare.
"""
railcard_holders = max(0, min(adults, railcard_holders))
full_fare = adults - railcard_holders
@@ -304,6 +465,9 @@ def price_journey(journey, adults=1, railcard_holders=1):
if not non_walk: # walking only
return 0, 0, "walk"
# TfL omits the fare block entirely for some journeys (e.g. ones involving
# a mode it cannot price). Returning None makes the caller drop this option
# rather than treat it as free.
if not fare or not fare.get("fares"):
return None
@@ -311,6 +475,11 @@ def price_journey(journey, adults=1, railcard_holders=1):
for f in fare["fares"]:
cost = f.get("cost") or 0
peak, offpeak = f.get("peak") or 0, f.get("offPeak") or 0
# Two independent bus tests, because TfL is inconsistent about which
# it provides: a fare with neither a peak nor an off-peak price is a
# flat bus fare by construction, and otherwise the tap records name the
# mode outright. Getting this wrong would apply a Railcard discount to
# a bus fare, which does not exist.
is_bus = (peak == 0 and offpeak == 0) or any(
(t.get("tapDetails") or {}).get("modeType") == "Bus" for t in (f.get("taps") or [])
)
@@ -319,6 +488,13 @@ def price_journey(journey, adults=1, railcard_holders=1):
out += cost * adults
ret += cost * adults
else:
# Railcards give 1/3 off *off-peak* rail/tube fares only, so the
# outbound is discounted only when this fare is off-peak. That is
# detected two ways because chargeLevel is free text that has
# historically read "Off Peak", "Off-peak" or been absent: a
# substring match on "off", or the cost simply equalling the
# off-peak price. The return is always treated as off-peak (see the
# docstring), so it is always discounted for railcard holders.
level = (f.get("chargeLevel") or "").lower()
out_full = cost
ret_full = offpeak or cost # next-day return assumed off-peak
@@ -327,7 +503,9 @@ def price_journey(journey, adults=1, railcard_holders=1):
out += out_full * full_fare + out_disc * railcard_holders
ret += ret_full * full_fare + ret_disc * railcard_holders
# short human summary of the route
# Short human summary of the route, e.g. "rail > tube Victoria". Walking
# legs are dropped as noise, and "national-rail" is shortened purely to fit
# the column in the UI.
parts = []
for l in legs:
m = (l.get("mode") or {}).get("id")
@@ -342,13 +520,28 @@ def price_journey(journey, adults=1, railcard_holders=1):
class RateLimiter:
"""Sliding-window limiter: at most `per_minute` acquisitions in any 60s."""
def __init__(self, per_minute):
def __init__(self, per_minute: int) -> None:
"""Args: per_minute: maximum acquisitions allowed in any 60s window."""
self.per_minute = per_minute
self._times = deque()
self._lock = threading.Lock()
def acquire(self, deadline=None):
"""Block until a slot is free. Returns False if `deadline` would pass first."""
def acquire(self, deadline: float | None = None) -> bool:
"""Block until a slot is free.
Args:
deadline: time.monotonic() value; if the wait would run past it,
give up immediately instead of sleeping.
Returns:
True when a slot was taken, False when the deadline would pass
first (the caller should then abandon the request).
The sleep is capped at 1s per iteration rather than sleeping the full
computed wait, so that a slot freed by another thread finishing early is
picked up promptly, and so the deadline is re-checked often. The lock is
released before sleeping - holding it would serialise every waiter.
"""
while True:
with self._lock:
now = time.monotonic()
@@ -367,7 +560,12 @@ _limiters = {}
_limiters_lock = threading.Lock()
def _limiter(app_key):
def _limiter(app_key: str | None) -> RateLimiter:
"""Return the shared limiter for the rate that `app_key` unlocks.
Limiters are keyed by rate, not by key, so every caller at a given tier
shares one 60s window - which is the point, since TfL counts per source IP.
"""
rate = TFL_RATE_KEYED if app_key else TFL_RATE_ANON
with _limiters_lock:
lim = _limiters.get(rate)
@@ -389,8 +587,28 @@ def sleep_within(seconds, deadline):
return True
def tfl_journeys(frm, to, when, extra=None, app_key=None, retries=3, deadline=None):
"""Journeys from TfL for one origin/destination/time/mode, cached and rate limited."""
def tfl_journeys(frm, to, when, extra=None, app_key=None, retries=3, deadline=None) -> list[dict]:
"""Journeys from TfL for one origin/destination/time/mode, cached and rate limited.
Args:
frm, to: (lat, lon) tuples; TfL accepts raw coordinates in the path.
when: naive :class:`datetime` of the intended departure, London local
time (TfL interprets it as such; no timezone is sent).
extra: additional query parameters, e.g. ``{"mode": "bus,walking"}`` to
force a bus-only route or ``{"mode": "cycle"}`` for cycle routing.
app_key: TfL API key from the TFL_APP_KEY env var, or None for the
anonymous tier.
retries: attempts before giving up.
deadline: time.monotonic() value bounding the whole call.
Returns:
The ``journeys`` array, or [] on any failure. This function never
raises: a missing route and a dead API look the same to the caller, who
simply ends up with no priced option for that hotel. That is deliberate
- one flaky lookup should not abort a 40-hotel search - but it does mean
a total TfL outage shows up in the UI as "no TfL fare found" rather than
as an error.
"""
params = {
"date": when.strftime("%Y%m%d"),
"time": when.strftime("%H%M"),
@@ -399,6 +617,9 @@ def tfl_journeys(frm, to, when, extra=None, app_key=None, retries=3, deadline=No
}
if extra:
params.update(extra)
# Coordinates are rounded to 5dp (~1m) so that floating-point noise in
# otherwise identical requests still hits the cache. app_key is excluded
# from the key on purpose: the route and fare do not depend on it.
key = (round(frm[0], 5), round(frm[1], 5), round(to[0], 5), round(to[1], 5), params["date"], params["time"],
tuple(sorted((extra or {}).items())))
now = time.monotonic()
@@ -412,6 +633,14 @@ def tfl_journeys(frm, to, when, extra=None, app_key=None, retries=3, deadline=No
url = TFL_API.format(frm=f"{frm[0]},{frm[1]}", to=f"{to[0]},{to[1]}")
limiter = _limiter(app_key)
journeys = None
# Retry policy, all deadline-aware (a skipped sleep means the deadline is
# too close, so stop rather than hammer):
# network error -> wait 2s
# 429 rate limited -> linear backoff, 5s then 10s then 15s
# 5xx -> wait 2s
# any other non-200 (notably 300 "disambiguation", meaning TfL wants the
# caller to pick between candidate locations) -> treat as "no route",
# do not retry, and cache the empty result
for attempt in range(retries):
remaining = TFL_TIMEOUT_S if deadline is None else deadline - time.monotonic()
if remaining < 1 or not limiter.acquire(deadline):
@@ -439,9 +668,15 @@ def tfl_journeys(frm, to, when, extra=None, app_key=None, retries=3, deadline=No
journeys = []
break
# journeys is still None only if every attempt was abandoned; return empty
# without caching so a later, less rushed call can try again. An empty list
# from a *successful* response is cached - "no route" is a real answer.
if journeys is None:
return [] # gave up: don't cache a failure
return []
with _tfl_cache_lock:
# Sweep expired entries only once the cache is large, so the common path
# stays O(1); TFL_CACHE_TTL_S is long because journey times and fares
# for a fixed date/time do not change.
if len(_tfl_cache) > 5000:
expired = [k for k, (exp, _) in _tfl_cache.items() if exp <= now]
for k in expired:
@@ -450,14 +685,40 @@ def tfl_journeys(frm, to, when, extra=None, app_key=None, retries=3, deadline=No
return journeys
# Two journey queries per hotel, because TfL's default "LeastTime" answer is
# usually a tube fare and the cheapest option is very often a slower bus. The
# bus query has to name "walking" alongside "bus" or TfL refuses routes that
# need a walk to the stop.
TRANSPORT_QUERIES = (("fast", None), ("bus", {"mode": "bus,walking"}))
CYCLE_QUERY = {"mode": "cycle"}
def best_transport(origin, hotel, when, adults, railcard_holders, max_minutes, app_key=None, deadline=None, pool=None):
"""Cheapest acceptable round-trip transport to a hotel. Returns dict or None.
"""Cheapest acceptable round-trip transport to a hotel.
Runs the fast and bus-only TfL queries concurrently on `pool` when given.
Args:
origin: (lat, lon) of the journey start.
hotel: normalised hotel dict; only lat/lon are read.
when: departure datetime for the outbound leg.
adults, railcard_holders: party makeup, passed to :func:`price_journey`.
max_minutes: routes slower than this are rejected - unless *every*
route is slower, in which case the fastest is kept so the hotel
still gets a price rather than vanishing from the results.
app_key: TfL key or None.
deadline: time.monotonic() budget.
pool: optional executor; the two queries are independent, so running
them concurrently halves the latency per hotel. Note this pool is
shared with the caller's other work, so it must never be sized 1 or
the map below can deadlock behind its own parent task.
Returns:
The chosen option as a dict with keys ``kind`` ("fast"/"bus"),
``outbound_p``, ``return_p``, ``total_p`` (integer pence), ``minutes``,
``route``, plus ``fastest_*`` fields describing the quickest option so
the UI can show the speed/price trade-off. None if nothing was priceable.
Ties are broken by duration, so two equally cheap routes resolve to the
quicker one.
"""
dest = (hotel["lat"], hotel["lon"])
@@ -498,9 +759,23 @@ def best_transport(origin, hotel, when, adults, railcard_holders, max_minutes, a
def cycle_time(origin, hotel, when, app_key, deadline=None, journeys=None):
"""(minutes, km, source) for cycling to the hotel. TfL routing only with an app key.
"""(minutes, km, source) for cycling to the hotel.
`journeys` may carry an already-fetched TfL cycle response to avoid a second call.
Real TfL cycle routing is used only when an app key is configured, because
it costs a third rate-limited request per hotel and the anonymous 50/min
budget is already spent on fares; without a key this falls straight back to
:func:`estimate_cycle`. ``source`` is "tfl" or "estimate" so the UI can say
which it is showing.
Args:
journeys: an already-fetched cycle response, so the caller can start the
request early and overlap it with the fare lookups.
Returns:
(minutes, km, source). The km comes from summing per-leg ``distance``
values, which TfL reports in metres; if that sum is zero (the field is
sometimes absent) the straight-line estimate is used for distance while
keeping TfL's duration.
"""
dest = (hotel["lat"], hotel["lon"])
est_min, est_km = estimate_cycle(origin, dest)
@@ -518,6 +793,9 @@ def cycle_time(origin, hotel, when, app_key, deadline=None, journeys=None):
# --------------------------------------------------------------------------- #
# Search
# --------------------------------------------------------------------------- #
# Every transport column blanked out. Splatted into a row to reset it, so
# adding a transport field means adding it here too or it will leak a stale
# value from a previous night when rows are reused in a scan.
_UNPRICED = {
"transport_out": None,
"transport_return": None,
@@ -532,7 +810,14 @@ _UNPRICED = {
TIME_LIMIT_ROUTE = "not priced (time limit)"
def _transport_fields(hotel, t):
def _transport_fields(hotel: dict, t: dict | None) -> dict:
"""Convert a :func:`best_transport` result into the row's output columns.
Pence become pounds here (the only place), and ``total`` is room price plus
round-trip transport, rounded to 2dp for display. When `t` is None every
transport column is None and ``route`` carries the reason, which is what the
UI renders in place of a price.
"""
if not t:
return dict(_UNPRICED, route="no TfL fare found")
return {
@@ -548,13 +833,31 @@ def _transport_fields(hotel, t):
}
def sort_rows(rows):
def sort_rows(rows: list[dict]) -> None:
"""Sort rows in place by total cost, unpriced rows last.
``r["total"] is None`` sorts False(0) before True(1), so priced rows come
first; ``or 0`` then keeps the comparison from hitting None. Room price is
the tiebreak.
"""
rows.sort(key=lambda r: (r["total"] is None, r["total"] or 0, r["room_price"]))
def _shortlist(hotels, max_miles, top):
"""(available, shortlist): bookable, priced, geocoded hotels within max_miles
sorted by room price, and the cheapest `top` of them."""
def _shortlist(hotels: list[dict], max_miles: float | None, top: int) -> tuple[list[dict], list[dict]]:
"""Filter and rank hotels, then take the cheapest `top`.
Hotels are dropped unless they are bookable, have a price, and have
coordinates - the last because a hotel without lat/lon cannot be routed and
would crash the TfL step. ``distance_from_search_mi`` is Travelodge's own
distance from the searched location (miles, not from the travel origin),
and a missing value is treated as 0 so it survives the filter rather than
being silently excluded.
Returns:
(all qualifying hotels sorted by room price, the cheapest `top` of them).
Only the shortlist gets transport priced, which is what bounds the
number of TfL calls.
"""
avail = [h for h in hotels if h["available"] and h["room_price"] and h["lat"] is not None and h["lon"] is not None]
if max_miles is not None:
avail = [h for h in avail if (h["distance_from_search_mi"] or 0) <= max_miles]
@@ -562,8 +865,15 @@ def _shortlist(hotels, max_miles, top):
return avail, avail[:top]
def _make_row(h, origin):
"""Result row for a hotel: unpriced transport plus an estimated cycle time."""
def _make_row(h: dict, origin: tuple[float, float]) -> dict:
"""Build the initial result row for a hotel.
Transport columns start blank with route "pricing…" - the stream emits these
rows immediately so the UI can render the hotel list while TfL is still
being queried, then patches each row in place when its "transport" event
arrives. The cycle estimate is cheap and local, so it is filled in up front
and only overwritten later if TfL cycle routing is available.
"""
row = dict(h, **_UNPRICED, route="pricing…")
est_min, est_km = estimate_cycle(origin, (h["lat"], h["lon"]))
row.update({"cycle_minutes": est_min, "cycle_km": est_km, "cycle_source": "estimate"})
@@ -576,6 +886,22 @@ def _price_hotels(hotels, origin, depart_dt, adults, railcard_holders, max_trave
Generator of (code, transport_or_None, cycle_or_None, timed_out) in the order
TfL answers. Owns the worker pools; leftover work is cancelled if the
consumer stops early.
Two pools, not one: `hotel_pool` runs at most TFL_HOTEL_WORKERS hotels at a
time and each of those tasks submits its own sub-queries to the larger
`tfl_pool`. Sharing one pool would let the hotel tasks fill every slot and
then block waiting for sub-tasks that can never be scheduled - a classic
executor deadlock.
`timed_out` distinguishes "TfL has no fare for this hotel" from "we ran out
of time before asking", which the caller surfaces as summary["truncated"].
Note it is inferred from the clock at completion time rather than reported
by the worker, so it can misattribute a genuine no-fare result that happens
to land after the deadline.
The cancel loop in `finally` is best-effort - it only stops tasks that have
not started - and the `with` blocks still join running workers, so an
abandoned stream takes up to one in-flight TfL timeout to unwind.
"""
with ThreadPoolExecutor(max_workers=TFL_WORKERS) as tfl_pool, \
ThreadPoolExecutor(max_workers=TFL_HOTEL_WORKERS) as hotel_pool:
@@ -605,7 +931,8 @@ def _price_hotels(hotels, origin, depart_dt, adults, railcard_holders, max_trave
f.cancel()
def _apply_transport(row, t, cyc, timed_out):
def _apply_transport(row: dict, t: dict | None, cyc: tuple | None, timed_out: bool) -> dict:
"""Merge one hotel's transport and cycle results into its row, in place."""
row.update(_transport_fields(row, t))
if timed_out:
row["route"] = TIME_LIMIT_ROUTE
@@ -614,13 +941,24 @@ def _apply_transport(row, t, cyc, timed_out):
return row
def _party(rooms, railcard_holders):
def _party(rooms: list[tuple[int, int]], railcard_holders: int) -> tuple[int, int, int]:
"""Totals for the booking: (adults, children, railcard holders).
Railcard holders are clamped to the number of adults so an over-stated
value cannot discount more fares than there are payers.
"""
adults = sum(a for a, _ in rooms)
children = sum(c for _, c in rooms)
return adults, children, max(0, min(adults, railcard_holders))
def _depart_dt(day, depart):
def _depart_dt(day: date, depart: str) -> datetime:
"""Combine a check-in date with an "HH:MM" string into a naive datetime.
Naive on purpose: TfL reads the date/time as London local time, so
attaching a timezone here would only invite a double conversion. The format
is already validated by the Pydantic model in main.py.
"""
hh, mm = map(int, depart.split(":"))
return datetime(day.year, day.month, day.day, hh, mm)
@@ -699,9 +1037,15 @@ def run_search(
# --------------------------------------------------------------------------- #
# Date-range scan
# --------------------------------------------------------------------------- #
def candidate_checkins(start, end, weekdays, nights):
"""[(checkin, checkout)] for every day start..end (inclusive) whose weekday
(0=Mon) is in `weekdays`, each for a stay of `nights` nights."""
def candidate_checkins(start: date, end: date, weekdays, nights: int) -> list[tuple[date, date]]:
"""Enumerate candidate (checkin, checkout) pairs for a date-range scan.
Every day from `start` to `end` inclusive whose weekday (0=Mon, matching
:meth:`date.weekday`) is in `weekdays` becomes a check-in, with check-out
`nights` days later. Note the checkout may fall past `end` - `end` bounds
the check-in date, not the stay. main.py also calls this during validation
purely to reject a range that matches no weekday.
"""
out = []
day = start
while day <= end:
@@ -711,7 +1055,13 @@ def candidate_checkins(start, end, weekdays, nights):
return out
def _copy_night(n):
def _copy_night(n: dict) -> dict:
"""Two-level copy of a night before yielding it.
The generator keeps mutating `night_list` after each event, so both the
night dict and its hotel rows must be copied or the caller's already-emitted
JSON would change underneath it.
"""
return dict(n, hotels=[dict(r) for r in n["hotels"]])
@@ -840,6 +1190,10 @@ def run_scan(
# Phase 2: one TfL price per unique hotel. Hotels that are cheapest on some
# night go first, since they are the likely winners if time runs out.
# A hotel can appear on many nights; it is priced once. Its ordering key is
# its best (lowest) position in any night's list, tiebroken by price, so
# hotels that top some night get their fares first and a truncated scan
# still has the likely winners priced.
index = {} # code -> ((best rank, min room price), row)
for n in night_list:
for rank, row in enumerate(n["hotels"]):
@@ -855,6 +1209,9 @@ def run_scan(
ordered, origin, depart_dt, adults, railcard_holders, max_travel_min, app_key, deadline
):
base = index[code][1]
# Transport here is night-independent, so there is no room price to add:
# a dummy 0 is passed and the resulting "total" discarded. Each night
# computes its own total in phase 3.
fields = _transport_fields({"room_price": 0}, t)
fields.pop("total", None)
if timed_out:
@@ -863,6 +1220,8 @@ def run_scan(
if cyc:
fields["cycle_minutes"], fields["cycle_km"], fields["cycle_source"] = cyc
else:
# No TfL cycle result: keep the local estimate already on the row,
# since these fields are copied wholesale onto every night's copy.
fields.update({k: base[k] for k in ("cycle_minutes", "cycle_km", "cycle_source")})
fields["code"] = code
transport[code] = fields
@@ -875,6 +1234,7 @@ def run_scan(
continue
for row in n["hotels"]:
fields = transport.get(row["code"])
# Missing means phase 2 ran out of time before reaching this hotel.
if fields is None:
row["route"] = TIME_LIMIT_ROUTE
continue
@@ -888,6 +1248,9 @@ def run_scan(
n["best_code"] = best["code"]
n["best_room"] = min(r["room_price"] for r in n["hotels"])
# Cheapest night first; nights with no total (failed, skipped, or unpriced)
# fall to the bottom, ordered by best room price and then by date so the
# list is still deterministic.
night_list.sort(
key=lambda n: (
n["best_total"] is None,