import { extractVideoId } from "../lib/videoId"; import { recordIdOfVideoDir } from "../lib/videoDir-server.mjs"; // WHICH CANDIDATES A LISTING NAMES (lane verify-resume). The pre-clean check // (verifyBeforeClean) may clean a candidate only when a listing ACTUALLY // RETURNED it, so the match is exact and by id, never by substring: // // - every platform: the canonical id of each returned URL (extractVideoId, // the id the quick availability check diffs against data-dir names) equals // the candidate's directory name — the full check's own rule; // - Rumble also has a second id per video (AGENTS.md: the archive keys by // the EMBED id, a directory is named for the URL SLUG). A returned // `rumble.com/embed//` URL names the embed id, which is matched against // the candidate's metadata.info.json `id` — and only against that: an // embed id is never compared with a directory (slug) name, nor a slug with // an embed id, so two different videos whose two kinds of id happen to be // spelled alike can never vouch for each other. export type ListingIndex = { // Canonical ids of the returned page (watch) URLs. ids: Set; // Rumble only: ids of returned embed URLs. embedIds: Set; // How many entries were returned (de-duplicated by id). entries: number; }; function isRumbleEmbedUrl(url: string): boolean { try { const u = new URL(url); return u.hostname.toLowerCase().endsWith("rumble.com") && u.pathname.split("/").filter(Boolean)[0] === "embed"; } catch { return false; } } export function indexListing(urls: Iterable): ListingIndex { const ids = new Set(); const embedIds = new Set(); for (const url of urls) { const id = extractVideoId(url); if (!id) continue; (isRumbleEmbedUrl(url) ? embedIds : ids).add(id); } return { ids, embedIds, entries: ids.size + embedIds.size }; } // A candidate's ids: its directory, and on Rumble its embed id when the // metadata names one that is not the directory's own. export type CandidateIds = { dir: string; embed?: string }; export async function candidateIdsOf( channelsDir: string, slug: string, dirId: string, platform: string | null, ): Promise { if (platform !== "rumble") return { dir: dirId }; const record = await recordIdOfVideoDir(channelsDir, slug, dirId, { platform }).catch(() => dirId); return record && record !== dirId ? { dir: dirId, embed: record } : { dir: dirId }; } export function isListed(index: ListingIndex, c: CandidateIds): boolean { if (index.ids.has(c.dir)) return true; return c.embed !== undefined && index.embedIds.has(c.embed); } // The candidates (by directory id) a listing names. export async function listedCandidates( opts: { channelsDir: string; slug: string; platform: string | null }, candidateIds: readonly string[], index: ListingIndex, ): Promise> { const out = new Set(); if (index.entries === 0) return out; for (const id of candidateIds) { if (isListed(index, await candidateIdsOf(opts.channelsDir, opts.slug, id, opts.platform))) out.add(id); } return out; }