// READ AN ODYSEE PAGE THE WAY A BROWSER LOADS IT (release 32 R9). // // The live finding (2026-10-17): a headless Chromium page load of odysee.com // gets 200 from Odysee's API where yt-dlp's lbry extractor gets 429. So when // the lookup is refused, the page itself is opened — a fresh context, no // cookies, the same headless Playwright as the player-page opener // (storedUrlPageOpen.ts) — and every JSON answer of the API hosts // (lib/odyseePageApi.ts ODYSEE_API_HOSTS) is kept with its request body, plus // the URLs the player asks the CDN for. lib/odyseePageApi.ts reads them. // // A VIDEO page is played for a few seconds (so the player asks `get` and the // CDN for the file — which is also the page open that unlocks the CDN for a // stored-URL read that follows). A CHANNEL page is scrolled `scrolls` times, a // few seconds apart, so it lists more of its uploads. // // INJECTED: callers take a `PageApiReader`; tests pass a fake built from // recorded answers and never launch a browser. Under a test runner // (NODE_TEST_CONTEXT) the real reader refuses. On the e2e server, // E2E_ODYSEE_PAGE_FIXTURE names a JSON file of recorded reads keyed by page URL // (`"*"` for any), answered instead of a browser. import { readFile } from "node:fs/promises"; import { importPlaywright } from "../social/playwrightRuntime"; import { isOdyseeApiUrl, type PageApiResponse } from "../lib/odyseePageApi"; export type PageApiRead = { ok: boolean; responses: PageApiResponse[]; // The CDN URLs the player requested, in order. mediaUrls: string[]; error?: string; }; export type PageApiReader = (opts: { pageUrl: string; signal: AbortSignal; onLog: (s: string) => void; // A video page: press play and let it play (the player asks for its file). play?: boolean; // A channel page: scroll to the end this many times. scrolls?: number; }) => Promise; export const PAGE_READ_SETTLE_MS = 10_000; export const PAGE_READ_PLAY_MS = 8_000; export const PAGE_READ_SCROLL_MS = 3_000; export const PAGE_READ_GOTO_TIMEOUT_MS = 45_000; // A bound on one channel read: a page that keeps loading forever is not read // forever. export const PAGE_READ_MAX_SCROLLS = 40; function wait(ms: number, signal: AbortSignal): Promise { return new Promise((resolve) => { if (signal.aborted) return resolve(); const t = setTimeout(() => { signal.removeEventListener("abort", done); resolve(); }, ms); function done() { clearTimeout(t); resolve(); } signal.addEventListener("abort", done, { once: true }); }); } // The e2e server's recorded reads (E2E_ODYSEE_PAGE_FIXTURE): `{ "": // PageApiRead, "*": PageApiRead }`. Read on every call (a spec writes it). async function fixtureRead(file: string, pageUrl: string): Promise { try { const all = JSON.parse(await readFile(file, "utf8")) as Record; const hit = all[pageUrl] ?? all["*"]; if (hit) return { ok: hit.ok !== false, responses: hit.responses ?? [], mediaUrls: hit.mediaUrls ?? [] }; return { ok: false, responses: [], mediaUrls: [], error: `no recorded read for ${pageUrl}` }; } catch (err) { return { ok: false, responses: [], mediaUrls: [], error: (err as Error).message }; } } // The real reader. Never throws: a page that could not be read is `ok: false` // with the reason, and the caller decides. export const readOdyseePage: PageApiReader = async ({ pageUrl, signal, onLog, play, scrolls }) => { const responses: PageApiResponse[] = []; const mediaUrls: string[] = []; const fixture = process.env.E2E_ODYSEE_PAGE_FIXTURE; if (fixture) { onLog(`Reading ${pageUrl} from the recorded page answers (E2E_ODYSEE_PAGE_FIXTURE).\n`); return fixtureRead(fixture, pageUrl); } if (process.env.NODE_TEST_CONTEXT) { onLog(`Not opening ${pageUrl} in a browser under a test runner.\n`); return { ok: false, responses, mediaUrls, error: "not opened under a test runner" }; } let browser: { close: () => Promise } | null = null; const pending: Promise[] = []; try { const { chromium } = await importPlaywright(); const b = await chromium.launch({ headless: true }); browser = b; const ctx = await b.newContext({}); const page = await ctx.newPage(); type Resp = { url: () => string; status: () => number; headers: () => Record; json: () => Promise; request: () => { postData: () => string | null; url: () => string }; }; page.on("request", ((req: { url: () => string }) => { try { const u = new URL(req.url()); if (u.hostname.endsWith(".odycdn.com") && u.pathname.includes("/streams/")) mediaUrls.push(req.url()); } catch { /* not a URL */ } }) as never); page.on("response", ((r: Resp) => { const url = r.url(); if (!isOdyseeApiUrl(url)) return; if (!/json/i.test(r.headers()["content-type"] ?? "")) return; pending.push( (async () => { let body: unknown; try { body = await r.json(); } catch { return; } let request: unknown; try { const raw = r.request().postData(); request = raw ? JSON.parse(raw) : undefined; } catch { request = undefined; } responses.push({ url, status: r.status(), ...(request !== undefined ? { request } : {}), body }); })(), ); }) as never); onLog(`Opening ${pageUrl} in a headless browser to read its own API answers.\n`); await page.goto(pageUrl, { waitUntil: "domcontentloaded", timeout: PAGE_READ_GOTO_TIMEOUT_MS }).catch(() => {}); await wait(PAGE_READ_SETTLE_MS, signal); if (play && !signal.aborted) { await page .evaluate( `(() => { const b = document.querySelector(".vjs-big-play-button, [aria-label*='Play']"); if (b) { b.click(); return; } const v = document.querySelector("video"); if (v) { v.muted = true; v.play().catch(() => {}); } })()`, ) .catch(() => {}); await wait(PAGE_READ_PLAY_MS, signal); } const n = Math.min(Math.max(0, scrolls ?? 0), PAGE_READ_MAX_SCROLLS); for (let i = 0; i < n && !signal.aborted; i++) { await page.evaluate("window.scrollTo(0, document.body.scrollHeight)").catch(() => {}); await wait(PAGE_READ_SCROLL_MS, signal); } await Promise.allSettled(pending); onLog( `Read ${pageUrl}: ${responses.length} API answer(s) ` + `(${responses.filter((r) => r.status >= 200 && r.status < 300).length} ok), ` + `${mediaUrls.length} media request(s).\n`, ); return { ok: responses.some((r) => r.status >= 200 && r.status < 300), responses, mediaUrls }; } catch (err) { const error = (err as Error).message; onLog(`Could not read ${pageUrl} in a browser: ${error}\n`); await Promise.allSettled(pending); return { ok: false, responses, mediaUrls, error }; } finally { await browser?.close().catch(() => {}); } };