// THE DOWNLOAD LANE PICKS HELD-PLATFORM WORK FROM STORED URLS (release 32 R9). // // The lane drops every pending video of a platform that is cooling down or // held (autoRunner's platformSkip): asking the platform again would only earn // another 429. Release 32 R7 gave a download a way round the LOOKUP — the media // URL the record's metadata.info.json already stores, read the way the player // reads it — but the lane never offered such a video, so the held Odysee // backlog waited for the hold to lift. // // Now a channel that opted in (`storedUrlDownloads: "only" | "fallback"`, // transcribe handling, a STORED_URL_PLATFORMS platform) keeps the pending // videos of a cooling or held platform whose record stores a trusted URL — // and only when the platform is not busy and the platform's stored-URL rest // (jobs/storedUrlPace.ts, the gap shared with clip cuts) has elapsed, so the // lane takes one new file per rest. Such a unit downloads as `only` (no page // or API request: the lookup is the limited thing), `storedUrlHeld` on a held // platform, and settles nothing (viaStoredUrl, jobs/unitOutcome.ts). // // "Does the record store a URL" is a file read, so it is remembered per video // for the runner's life (a record's stored format does not change without a // new lookup, which is exactly what is not happening) and at most // STORED_URL_LANE_PROBES_PER_TICK unknown videos are read per tick; a video is // offered from the tick after its read. import path from "node:path"; import type { ChannelConfig } from "../lib/channelConfig"; import { isStoredUrlPlatform, type StoredMediaUrl } from "../lib/storedMediaUrl"; import { storedMediaUrl } from "../lib/storedMediaUrl-server"; export const STORED_URL_LANE_PROBES_PER_TICK = 40; // The channel's stored-URL mode when the lane may take its videos past a // cooling or held platform, else null. export function storedUrlLaneMode( config: Pick, platform: string | null | undefined, ): "only" | "fallback" | null { const mode = config.storedUrlDownloads; if (mode !== "only" && mode !== "fallback") return null; if (config.handling !== "transcribe") return null; if (!isStoredUrlPlatform(platform)) return null; return mode; } export type StoredUrlLaneMemo = { // true / false once read; undefined = not read yet. known(slug: string, id: string): boolean | undefined; // Read up to `limit` of `ids` (unknown ones), remembering each answer. probe(channelsDir: string, ids: ReadonlyArray<{ slug: string; id: string }>, limit?: number): Promise; }; export function makeStoredUrlLaneMemo( read: (videoDir: string) => Promise = storedMediaUrl, ): StoredUrlLaneMemo { const memo = new Map(); const key = (slug: string, id: string) => `${slug}\0${id}`; return { known: (slug, id) => memo.get(key(slug, id)), async probe(channelsDir, ids, limit = STORED_URL_LANE_PROBES_PER_TICK) { const todo = ids.filter((x) => !memo.has(key(x.slug, x.id))).slice(0, limit); await Promise.all( todo.map(async ({ slug, id }) => { const s = await read(path.join(channelsDir, slug, "data", id)).catch(() => null); memo.set(key(slug, id), s !== null); }), ); return todo.length; }, }; } // THE FILTER (pure): which skipped videos stay pending because they can be // downloaded from their stored URL. `rescuable` = the platforms skipped ONLY // for a cooldown or a hold (not busy) whose stored-URL rest has elapsed. export function storedUrlRescue(args: { pending: Record; owner: ReadonlyMap; platformSkip: ReadonlySet; rescuable: ReadonlySet; platformOf: (slug: string) => string; optedIn: (slug: string) => boolean; known: (slug: string, id: string) => boolean | undefined; }): { pending: Record; storedIds: Set; unknown: { slug: string; id: string }[] } { const storedIds = new Set(); const unknown: { slug: string; id: string }[] = []; const seenUnknown = new Set(); const out: Record = {}; for (const [leafId, ids] of Object.entries(args.pending)) { out[leafId] = ids.filter((id) => { const slug = args.owner.get(id); if (!slug) return true; const pf = args.platformOf(slug); if (!args.platformSkip.has(pf)) return true; if (!args.rescuable.has(pf) || !args.optedIn(slug)) return false; const k = args.known(slug, id); if (k === true) { storedIds.add(id); return true; } if (k === undefined && !seenUnknown.has(id)) { seenUnknown.add(id); unknown.push({ slug, id }); } return false; }); } return { pending: out, storedIds, unknown }; }