Deploy with Docker Compose / deploy (push) Successful in 52s
A second "Cheapest night" tab on /hotels scans every candidate check-in between two dates (filtered by weekday, 1-7 nights, up to ~3 months), keeps the N cheapest hotels per night, and prices TfL transport once per unique hotel so the whole range fits in the streaming window. Backend - travelodge.py: extract _shortlist/_make_row/_price_hotels/_apply_transport from run_search; add a 30-minute per-(location, dates, rooms) Travelodge result cache, a deadline on page fetches, and run_scan (parallel date fetches with per-date failure isolation, one TfL lookup per hotel). - main.py: HotelRequestBase shared by search and scan requests, HotelScanRequest with weekday/range validation, scan event models, _guarded slot wrapper, POST /hotels/scan/stream (NDJSON). - nginx: the streaming location now matches /py/hotels/(search|scan). Frontend - Split CheapHotels.vue into SingleStayPanel, CheapestNightPanel, SharedFields, HotelRows, a shared form composable, stream/format helpers and hotels-shared.css; shared fields keep their values across tabs. - Vite dev server proxies /py to the Python API. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01H5P3C1HbbEwLZEYsjwr7uV
67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
// POST a JSON body to a Python API endpoint and feed each NDJSON line to onEvent.
|
|
|
|
export function describeError(status, payload) {
|
|
if (status === 429)
|
|
return "Someone else is searching right now, try again shortly.";
|
|
if (status === 503)
|
|
return "Too many searches from your address, wait a minute and retry.";
|
|
const detail = payload?.detail;
|
|
if (Array.isArray(detail))
|
|
return detail
|
|
.map((d) => `${(d.loc || []).slice(-1)[0] ?? ""}: ${d.msg}`)
|
|
.join("; ");
|
|
if (typeof detail === "string") return detail;
|
|
return `Search failed (${status}).`;
|
|
}
|
|
|
|
async function readNdjson(res, onEvent) {
|
|
const decoder = new TextDecoder();
|
|
let buffer = "";
|
|
const handleChunk = (text, final) => {
|
|
buffer += text;
|
|
const lines = buffer.split("\n");
|
|
buffer = final ? "" : lines.pop();
|
|
for (const line of lines) {
|
|
if (!line.trim()) continue;
|
|
let ev;
|
|
try {
|
|
ev = JSON.parse(line);
|
|
} catch {
|
|
continue; // skip a malformed line rather than abort the whole search
|
|
}
|
|
onEvent(ev);
|
|
}
|
|
};
|
|
if (!res.body?.getReader) {
|
|
handleChunk(await res.text(), true);
|
|
return;
|
|
}
|
|
const reader = res.body.getReader();
|
|
for (;;) {
|
|
const { value, done } = await reader.read();
|
|
if (done) break;
|
|
handleChunk(decoder.decode(value, { stream: true }), false);
|
|
}
|
|
handleChunk(decoder.decode(), true);
|
|
}
|
|
|
|
/** Throws an Error with a user-facing message on a non-2xx response. */
|
|
export async function runStream(url, body, { signal, onEvent }) {
|
|
const res = await fetch(url, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
signal,
|
|
});
|
|
if (!res.ok) {
|
|
let payload = null;
|
|
try {
|
|
payload = await res.json();
|
|
} catch {
|
|
/* non-JSON body (e.g. nginx error page) */
|
|
}
|
|
throw new Error(describeError(res.status, payload));
|
|
}
|
|
await readNdjson(res, onEvent);
|
|
}
|