// site-links — the ONE place a manifest entry's link to its moment on the // archive is built (`/?v=%2F&t=`): the corner QR, the // deck's QR, the YouTube description's sources, lint's QR check, the review // page's cite link, the moment lookup. // // The id is the one the ARCHIVE serves (siteVideoId, common/lib/videoDirId.mjs): // an entry's `siteVideo` when it pins one (a Rumble clip), else its `video` — // but a Twitch VOD's `video` is its directory id `` while the archive keys // the record id `v`. That mapping is decided by the recording's PLATFORM, // never by the id being all digits (an X status id is digits too): // // the entry's own `platform`, else its channel's (config.json `platform`, // else the host of its `url` — channelConfigPlatform, the rule every // resolveVideoDirId caller uses), read once per channel from CHANNELS_DIR. // // With no local channel config (a machine with no corpus) the platform is // unknown and the id goes out as written. The channel is `siteChannel`, else // `channel`, else the provenance's channelSlug. import { readFileSync } from "node:fs"; import path from "node:path"; import { channelConfigPlatform, siteVideoId } from "yt-dlp-transcript-common/lib/videoDirId.mjs"; import { CHANNELS_DIR } from "../lib/paths.mjs"; /** @type {Map} */ const PLATFORMS = new Map(); /** * A channel's platform from its config.json, cached per (dir, slug); null when * there is no readable config. * @param {string | null | undefined} slug * @param {string} [channelsDir] * @returns {string | null} */ export function channelPlatform(slug, channelsDir = CHANNELS_DIR) { if (!slug) return null; const key = `${channelsDir}\u0000${slug}`; if (PLATFORMS.has(key)) return /** @type {string | null} */ (PLATFORMS.get(key)); let platform = null; try { platform = channelConfigPlatform(JSON.parse(readFileSync(path.join(/* turbopackIgnore: true */ channelsDir, slug, "config.json"), "utf8"))); } catch { platform = null; } PLATFORMS.set(key, platform); return platform; } /** Forget the cached platforms (tests). */ export function clearChannelPlatforms() { PLATFORMS.clear(); } /** * The recording's platform: the entry's own `platform`, else its LOCAL * channel's (`channel`, else the provenance's slug — the directory the cues * come from). * @param {any} entry * @param {any} [provenance] * @param {{ channelsDir?: string }} [opts] * @returns {string | null} */ export function entryPlatform(entry, provenance = {}, { channelsDir } = {}) { if (typeof entry?.platform === "string" && entry.platform) return entry.platform; return channelPlatform(entry?.channel ?? provenance?.channelSlug ?? null, channelsDir); } /** * The `/` the archive serves an entry's recording under. * @param {any} entry * @param {any} [provenance] * @param {{ channelsDir?: string, platform?: string | null }} [opts] * @returns {{ channel: string | undefined, video: string }} */ export function siteRefOf(entry, provenance = {}, opts = {}) { // No channel stays `undefined`, as the builders this replaced wrote it (a // schedule pinned byte-for-byte reads "undefined/…"). const channel = entry?.siteChannel ?? entry?.channel ?? provenance?.channelSlug; const platform = opts.platform !== undefined ? opts.platform : entryPlatform(entry, provenance, opts); return { channel, video: siteVideoId(entry, platform) }; } /** * An entry's moment on the archive at `seconds` (floored): * `/?v=%2F&t=`. `origin` is the provenance's * `siteOrigin` unless given; it is used as it is (a missing origin gives the * "undefined/?v=…" the old builders gave — callers that care check first). * @param {any} entry * @param {any} provenance * @param {number} seconds * @param {{ origin?: string | null, channelsDir?: string, platform?: string | null }} [opts] */ export function archiveMomentUrl(entry, provenance, seconds, opts = {}) { const origin = opts.origin !== undefined ? opts.origin : provenance?.siteOrigin; const { channel, video } = siteRefOf(entry, provenance, opts); const at = Number(seconds); return `${origin}/?v=${encodeURIComponent(`${channel}/${video}`)}&t=${Math.floor(Number.isFinite(at) ? at : 0)}`; }