// A CHANNEL'S LISTING FROM ITS PAGE, IN A BROWSER (release 32 R9). // // yt-dlp's lbry listing (`--flat-playlist` of the channel URL) is a // `claim_search` through Odysee's API, and on 2026-10-16/17 it answered 429 // where the channel page, loaded by a browser, answered 200. This opens the // channel page (ytdlp/odyseePageReader.ts), scrolls it a bounded number of // times so it lists more uploads, and reads the uploads out of the page's own // `claim_search` answers (lib/odyseePageApi.ts listingFromResponses) as the // page URLs yt-dlp's listing prints. // // ADD-ONLY. A page shows the newest uploads, not the whole channel, so this // never decides that something LEFT the listing: the ids join the roster as a // sighting (mergeRosterFile, source "listing" — the paged sync's call), and // the ones `playlist` does not hold yet are put at its top, newest first, // ahead of everything already there. Nothing is removed and nothing is // downloaded: the download lane and download-missing see them as undownloaded // as they see any listed video. Serialised with the channel's sync and // downloads by the job's queue (the platform's download queue). // // FILTERED AS yt-dlp's LISTING WOULD BE: a channel whose `ytdlpExtraArgs` carry // a `--match-filter` lists only the matching titles through yt-dlp; here the // readable form (`title~=''`) is applied, and an unreadable one refuses // the listing (ytdlp/odyseeBrowserLookup.ts browserLookupDecision). The app's // download filters apply too. import path from "node:path"; import { readFile } from "node:fs/promises"; import type { Paths } from "../lib/paths"; import type { ChannelConfig } from "../lib/channelConfig"; import { detectPlatform } from "../lib/platform"; import { extractVideoId } from "../lib/videoId"; import { writeFileAtomic } from "../lib/jsonFile-server"; import { channelClaimFor, listingFromResponses, odyseeInfoFromClaim, odyseePageRef, } from "../lib/odyseePageApi"; import { notePlatformGap, platformGapRemainingMs, waitForPlatformGap } from "../jobs/platformGap"; import { BROWSER_LOOKUP_PLATFORMS, browserLookupDecision, browserLookupGapKey, browserLookupGapMs, type BrowserLookupDeps, } from "../ytdlp/odyseeBrowserLookup"; import { readOdyseePage } from "../ytdlp/odyseePageReader"; import { mergeRosterFile, observationsFromUrls } from "./rosterStore"; export const BROWSER_LISTING_DEFAULT_SCROLLS = 5; export type BrowserListingResult = | { status: "listed"; seen: number; filtered: number; added: string[] } | { status: "refused" | "unreadable" | "not-found"; error: string }; // Before anything is opened: the channel's URL is an Odysee channel page, and // its match filter (if any) is one a browser listing can apply. export function browserListingRefusal(config: ChannelConfig): string | null { const url = config.url ?? ""; const platform = detectPlatform(url); if (!platform || !BROWSER_LOOKUP_PLATFORMS.includes(platform)) { return `A browser listing is for ${BROWSER_LOOKUP_PLATFORMS.join(", ")} channels; this channel's URL is ${url || "unset"}.`; } if (!odyseePageRef(url).channel) return `The channel URL (${url}) names no @channel page.`; const probe = browserLookupDecision(config, { title: "" }, { skipLiveDownloads: false }); if (!probe.ok && !probe.skip) return `Not listed: ${probe.reason}.`; return null; } export async function browserListChannel(opts: { paths: Paths; slug: string; config: ChannelConfig; scrolls?: number; skipLiveDownloads: boolean; onLog: (s: string) => void; signal: AbortSignal; deps?: BrowserLookupDeps; }): Promise { const { paths, slug, config, onLog, signal } = opts; const refusal = browserListingRefusal(config); if (refusal) return { status: "refused", error: refusal }; const pageUrl = config.url!; const platform = detectPlatform(pageUrl)!; const ref = odyseePageRef(pageUrl); const key = browserLookupGapKey(platform); const gapMs = opts.deps?.gapMs ?? browserLookupGapMs(platform); try { await waitForPlatformGap({ label: `A browser listing (${platform})`, remainingMs: () => platformGapRemainingMs(key), signal, onLog: (line) => onLog(`${line}\n`), ...(opts.deps?.sleep ? { sleep: opts.deps.sleep } : {}), }); } catch { return { status: "unreadable", error: "cancelled while waiting for the gap between browser lookups" }; } notePlatformGap(key, gapMs); const reader = opts.deps?.reader ?? readOdyseePage; let read; try { read = await reader({ pageUrl, signal, onLog, scrolls: opts.scrolls ?? BROWSER_LISTING_DEFAULT_SCROLLS }); } finally { notePlatformGap(key, gapMs); } if (!read.ok && read.responses.length === 0) { return { status: "unreadable", error: `the channel page could not be read (${read.error ?? "no API answer"})` }; } // The channel's claim id: the page's resolve of `@name`, else the URL's own. const fromUrl = (() => { try { const seg = decodeURIComponent(new URL(pageUrl).pathname).split("/").find((s) => s.startsWith("@")) ?? ""; const m = /[:#]([0-9a-f]{40})$/.exec(seg); return m ? m[1] : null; } catch { return null; } })(); const channelClaim = ref.channel ? channelClaimFor(read.responses, ref.channel) : null; const listing = listingFromResponses(read.responses, { claimId: channelClaim?.claimId ?? fromUrl, name: ref.channel, }); if (listing.length === 0) { return { status: "not-found", error: `the channel page's API answers (${read.responses.length}) list no uploads of ${ref.channel}` }; } let filtered = 0; const kept: string[] = []; for (const { url, claim } of listing) { const info = odyseeInfoFromClaim(claim, { pageUrl: url, streamingUrl: null }); const d = browserLookupDecision(config, info, { skipLiveDownloads: opts.skipLiveDownloads }); if (d.ok) kept.push(url); else filtered++; } const root = path.join(paths.channelsDir, slug); const playlistPath = path.join(root, "playlist"); const existing = (await readFile(playlistPath, "utf8").catch(() => "")) .split("\n") .map((s) => s.trim()) .filter(Boolean); const have = new Set(existing.map((u) => extractVideoId(u)).filter((id): id is string => Boolean(id))); const added = kept.filter((u) => { const id = extractVideoId(u); return id !== null && !have.has(id); }); if (added.length > 0) { const next = [...added, ...existing]; await writeFileAtomic(playlistPath, next.join("\n") + "\n", { mkdir: true }); } if (kept.length > 0) { await mergeRosterFile(paths, slug, observationsFromUrls(kept), new Date().toISOString(), "listing"); } onLog( `Browser listing of ${ref.channel}: ${listing.length} upload(s) on the page` + (filtered ? `, ${filtered} declined by the channel's filters` : "") + `, ${added.length} new to the playlist.\n`, ); return { status: "listed", seen: listing.length, filtered, added }; }