// Synthetic odysee.com page reads (release 32 R9): what a headless browser's // load of a video page or a channel page would keep of the page's own API // answers — the LBRY SDK's JSON-RPC through Odysee's proxy (`resolve`, // `claim_search`, `get`), in the shape yt-dlp's lbry extractor reads // (yt_dlp/extractor/lbry.py). Invented claims, channels and hashes: nothing // recorded from the real site. No browser and no network: `fakePageReader` // answers a PageApiReader call from these. // // const ch = syntheticChannel({ name: "@synthchan", title: "Synth Channel" }); // const v = syntheticClaim({ name: "a-synthetic-stream", title: "…", channel: ch, releaseTime: 1700000000 }); // const read = videoPageRead({ claim: v, related: [other], streamingUrl: cdnUrlOf(v) }); // const reader = fakePageReader({ [pageUrlOf(v)]: read }); // reader.calls lists every call const PROXY = "https://api.na-backend.odysee.com/api/v1/proxy"; function hex40(seed) { let h = ""; let x = 0x9e3779b9 ^ [...String(seed)].reduce((a, c) => (a * 31 + c.charCodeAt(0)) >>> 0, 7); while (h.length < 40) { x = (x * 1103515245 + 12345) >>> 0; h += (x >>> 8).toString(16).padStart(6, "0"); } return h.slice(0, 40); } export function syntheticChannel({ name, title, claimId }) { return { claim_id: claimId ?? hex40(`channel:${name}`), name, value_type: "channel", timestamp: 1690000000, value: { title: title ?? name.slice(1) }, }; } export function syntheticClaim({ name, title, channel, claimId, releaseTime = 1700000000, timestamp, duration = 3600, size = 123456789, sdHash, streamType = "video", mediaType = "video/mp4", height = 720, width = 1280, description = "A synthetic stream.", tags = ["synthetic"], }) { const id = claimId ?? hex40(`stream:${name}`); return { claim_id: id, name, value_type: "stream", timestamp: timestamp ?? releaseTime + 60, canonical_url: `lbry://${channel.name}#${channel.claim_id.slice(0, 1)}/${name}#${id.slice(0, 1)}`, permanent_url: `lbry://${name}#${id}`, signing_channel: { claim_id: channel.claim_id, name: channel.name, value: { title: channel.value.title } }, value: { title, description, license: "None", // The SDK answers these as strings. release_time: String(releaseTime), tags, stream_type: streamType, source: { media_type: mediaType, name: `${name}.mp4`, sd_hash: sdHash ?? hex40(`sd:${name}`) + hex40(`sd2:${name}`).slice(0, 56), size: String(size), }, thumbnail: { url: `https://thumbs.odycdn.com/${hex40(`th:${name}`).slice(0, 32)}.webp` }, [streamType]: streamType === "video" ? { duration, height, width } : { duration }, }, }; } export const pageUrlOf = (claim) => `https://odysee.com/${claim.name}:${claim.claim_id}`; export const channelUrlOf = (channel) => `https://odysee.com/${channel.name}`; export const cdnUrlOf = (claim) => `https://player.odycdn.com/v6/streams/${claim.claim_id}/${claim.value.source.sd_hash.slice(0, 6)}.mp4`; const rpc = (method, params, result, status = 200) => ({ url: `${PROXY}?m=${method}`, status, request: { jsonrpc: "2.0", method, params, id: 1 }, body: status === 200 ? { jsonrpc: "2.0", result, id: 1 } : { error: { code: status, message: "Too Many Requests" } }, }); export const resolveAnswer = (claims) => rpc( "resolve", { urls: claims.map((c) => c.permanent_url ?? `lbry://${c.name}#${c.claim_id}`) }, Object.fromEntries(claims.map((c) => [c.permanent_url ?? `lbry://${c.name}#${c.claim_id}`, c])), ); export const claimSearchAnswer = (claims, page = 1) => rpc("claim_search", { page, page_size: claims.length, no_totals: true }, { items: claims, page, page_size: claims.length }); export const getAnswer = (claim, streamingUrl) => rpc("get", { uri: claim.permanent_url, save_file: false }, { streaming_url: streamingUrl, claim_id: claim.claim_id }); export const refusedAnswer = (method) => rpc(method, {}, null, 429); // A video page: its resolve (the claim plus recommendations), the related // claim_search, the player's get and its CDN request. export function videoPageRead({ claim, related = [], streamingUrl, mediaUrls, get = true }) { const url = streamingUrl === undefined ? cdnUrlOf(claim) : streamingUrl; return { ok: true, responses: [ resolveAnswer([claim]), ...(related.length ? [claimSearchAnswer(related)] : []), ...(get && url ? [getAnswer(claim, url)] : []), ], mediaUrls: mediaUrls ?? (url ? [`${url}?range=0`] : []), }; } // A channel page: its resolve, and one claim_search per page of uploads. export function channelPageRead({ channel, pages }) { return { ok: true, responses: [resolveAnswer([channel]), ...pages.map((p, i) => claimSearchAnswer(p, i + 1))], mediaUrls: [], }; } export function fakePageReader(reads) { const calls = []; const reader = async ({ pageUrl, play, scrolls }) => { calls.push({ pageUrl, play: Boolean(play), scrolls: scrolls ?? 0 }); const hit = reads[pageUrl] ?? reads["*"]; if (!hit) return { ok: false, responses: [], mediaUrls: [], error: `no recorded read for ${pageUrl}` }; return typeof hit === "function" ? hit({ pageUrl, play, scrolls }) : hit; }; reader.calls = calls; return reader; }