// A MIRROR CHANNEL AND THE CREATOR IT COPIES (release 31, lane D). // // A channel that mirrors another creator's streams (a VOD archive on another // platform: config `mirror.of` names the creator's channels) holds copies whose // originals the creator may have deleted, made private, or never uploaded at // all. The duplicate finder (lib/duplicates.ts) clusters only short videos, so // a 1–5 h stream never folds with its original; this module is the one place // that pairs a mirror's videos with the creator's, in BOTH directions, from the // same matches — so "the mirror holds N of the creator's removed videos" and // "M of the mirror's videos are the only copy" are counted off one set. // // PURE: no fs, no clock. The records come from controller/mirrorScan.ts (the // index, else metadata on disk) with their gone state already read through the // one gone reading (lib/sourceGone-server.ts, `sourceGoneState`). // // ── A MATCH ── // // Each mirror video is dated by its RECORDED date (`recordedDate`, the // channel's title rule — lib/recordedDate.ts), else its upload date; a creator // video the same way (`contentDate`). A creator video is a CANDIDATE when its // date is within MIRROR_DAYS_BEFORE days before to MIRROR_DAYS_AFTER days // after the mirror video's (an upload lands the day of the stream or a day or // two later; a mirror's title date is sometimes the day after a late stream). // A candidate MATCHES by the first rule that holds: // // "title" title Jaccard ≥ MIRROR_TITLE_JACCARD // "title-duration" title Jaccard ≥ MIRROR_WEAK_TITLE_JACCARD and the // durations agree // "live-duration" the durations agree, the creator video is a stream // (`wasLive`) and longer than MIRROR_LIVE_MIN_SECONDS // // The title Jaccard is over word sets (lowercase `[a-z0-9']+`, words of one // character and MIRROR_STOP_WORDS dropped). The mirror's side is its TOPIC: the // title after the recorded date its rule reads (the creator's name and the // date are not the topic), or before it when nothing follows; the whole title // when the rule does not match. Durations AGREE when both are known and differ // by less than MIRROR_DURATION_TOLERANCE of the mirror's. // // A match is STRONG when its score (the Jaccard to two decimals) is ≥ // MIRROR_STRONG_TITLE_JACCARD, or ≥ MIRROR_STRONG_DURATION_TITLE_JACCARD with // the durations agreeing. Every match is kept in the file; only STRONG ones // decide a status or hold a removed upload. (The research scripts this // generalises: ~/reports/quartering-cornbreadman match.py = the rules, // status.py = strong; the thresholds are theirs.) // // ── A MIRROR VIDEO'S STATUS ── // // `presence` per creator PLATFORM (the platforms of the `mirror.of` channels): // "public" when a strong match on that platform is not gone, else "gone" when a // strong match there is gone (deleted or private), else "none". Then the // status, the first that holds: // // only-copy no platform is "public": the mirror holds the only public // copy (the creator removed it, or never uploaded it, or both) // creator-deleted a platform is "gone" (the creator removed a copy) and // another is "public" // never-uploaded a platform is "none" (never uploaded there) and the rest // are "public" // creator-public every creator platform has it public // // A mirror video with no date has no candidates: only-copy. // // ── THE CREATOR'S REMOVED UPLOADS ── // // The other direction: every creator video that is gone and whose date lies in // the mirror's WINDOW (the first to the last date of the mirror's dated // videos). One is HELD by the mirror when it is a strong match of a mirror // video; `heldBy` names them. import type { GoneState } from "../sourceGone"; export const MIRROR_DAYS_BEFORE = 1; export const MIRROR_DAYS_AFTER = 2; export const MIRROR_TITLE_JACCARD = 0.25; export const MIRROR_WEAK_TITLE_JACCARD = 0.12; export const MIRROR_DURATION_TOLERANCE = 0.25; export const MIRROR_LIVE_MIN_SECONDS = 3000; export const MIRROR_STRONG_TITLE_JACCARD = 0.5; export const MIRROR_STRONG_DURATION_TITLE_JACCARD = 0.25; export const MIRROR_MIN_WORD_LENGTH = 2; export const MIRROR_STOP_WORDS: ReadonlySet = new Set( "the a an and of to in is on for w with & more much by it at as be are his her i my me you we our this that gets got".split(" "), ); export const MIRROR_STATUSES = ["only-copy", "creator-deleted", "never-uploaded", "creator-public"] as const; export type MirrorStatus = (typeof MIRROR_STATUSES)[number]; export const MIRROR_STATUS_LABELS: Readonly> = Object.freeze({ "only-copy": "Only copy", "creator-deleted": "Creator deleted", "never-uploaded": "Never uploaded", "creator-public": "Creator public", }); export type MirrorRule = "title" | "title-duration" | "live-duration"; export type Presence = "public" | "gone" | "none"; // One video, as the matcher reads it. export type MirrorRecord = { id: string; // The data// it lives in, when that is not `id` (a Rumble embed id). dir?: string; title: string; // Seconds; 0 = unknown. duration: number; // YYYYMMDD, or "" when unknown. uploadDate: string; recordedDate?: string; wasLive: boolean; // The gone reading (sourceGone.ts); null = not gone (or never checked). gone: GoneState | null; }; export type MirrorChannelInput = { slug: string; platform: string; records: MirrorRecord[]; }; export type MirrorMatchInput = { mirror: MirrorChannelInput & { // The mirror's recorded-date title rule, for the topic; null = none. titleRule?: RegExp | null; }; creators: MirrorChannelInput[]; }; export type MirrorMatch = { channel: string; id: string; dir?: string; title: string; date: string; duration: number; wasLive: boolean; rule: MirrorRule; // The title Jaccard, to two decimals. score: number; durationAgrees: boolean; strong: boolean; gone?: GoneState; }; export type MirrorVideo = { id: string; dir?: string; title: string; // The date it was matched by (recorded, else upload); null = undated. date: string | null; recordedDate?: string; duration: number; status: MirrorStatus; presence: Record; // Strongest first. matches: MirrorMatch[]; }; export type RemovedUpload = { channel: string; id: string; dir?: string; title: string; date: string; duration: number; gone: GoneState; // Mirror video ids holding it (strong matches); empty = not held. heldBy: string[]; }; export type MirrorTotals = { videos: number; dated: number; // Mirror videos with at least one strong match. matched: number; byStatus: Record; byPlatform: Record>; creatorRemoved: { inWindow: number; held: number; byChannel: Record; }; }; export type MirrorMatchResult = { window: { from: string; to: string } | null; platforms: Record; videos: MirrorVideo[]; removed: RemovedUpload[]; totals: MirrorTotals; }; const YMD = /^\d{8}$/; // The date a record is matched by: recorded, else upload; null when neither is // a YYYYMMDD. export function contentDate(r: { uploadDate: string; recordedDate?: string }): string | null { if (r.recordedDate && YMD.test(r.recordedDate)) return r.recordedDate; return YMD.test(r.uploadDate) ? r.uploadDate : null; } // A YYYYMMDD moved by `days` (UTC calendar days). export function addDays(ymd: string, days: number): string { const d = new Date(Date.UTC(Number(ymd.slice(0, 4)), Number(ymd.slice(4, 6)) - 1, Number(ymd.slice(6, 8)) + days)); return `${d.getUTCFullYear()}${String(d.getUTCMonth() + 1).padStart(2, "0")}${String(d.getUTCDate()).padStart(2, "0")}`; } // The words a title is compared on. export function titleWords(s: string): Set { const out = new Set(); for (const w of (s ?? "").toLowerCase().match(/[a-z0-9']+/g) ?? []) { if (w.length >= MIRROR_MIN_WORD_LENGTH && !MIRROR_STOP_WORDS.has(w)) out.add(w); } return out; } export function titleJaccard(a: ReadonlySet, b: ReadonlySet): number { let inter = 0; for (const w of a) if (b.has(w)) inter++; const union = a.size + b.size - inter; return inter / Math.max(1, union); } const SEPARATORS = /^[\s\-–—|:•·,.]+|[\s\-–—|:•·,.]+$/g; // A mirror title's topic: what follows the date its rule reads (else what // precedes it), or the whole title when the rule reads none. export function mirrorTopic(title: string, rule: RegExp | null | undefined): string { if (!rule) return title; const m = new RegExp(rule.source, rule.flags.replace("g", "")).exec(title); if (!m) return title; const after = title.slice(m.index + m[0].length).replace(SEPARATORS, ""); if (titleWords(after).size > 0) return after; return title.slice(0, m.index).replace(SEPARATORS, ""); } export function durationsAgree(mirrorSeconds: number, creatorSeconds: number): boolean { return mirrorSeconds > 0 && creatorSeconds > 0 && Math.abs(creatorSeconds - mirrorSeconds) < MIRROR_DURATION_TOLERANCE * mirrorSeconds; } // The rule a candidate matches by, or null. export function matchRule(j: number, durationAgrees: boolean, creator: { duration: number; wasLive: boolean }): MirrorRule | null { if (j >= MIRROR_TITLE_JACCARD) return "title"; if (j >= MIRROR_WEAK_TITLE_JACCARD && durationAgrees) return "title-duration"; if (durationAgrees && creator.duration > MIRROR_LIVE_MIN_SECONDS && creator.wasLive) return "live-duration"; return null; } export const roundScore = (j: number): number => Math.round(j * 100) / 100; export function isStrong(score: number, durationAgrees: boolean): boolean { return score >= MIRROR_STRONG_TITLE_JACCARD || (durationAgrees && score >= MIRROR_STRONG_DURATION_TITLE_JACCARD); } export function statusOf(presence: Record): MirrorStatus { const p = Object.values(presence); if (!p.includes("public")) return "only-copy"; if (p.includes("gone")) return "creator-deleted"; if (p.includes("none")) return "never-uploaded"; return "creator-public"; } type Candidate = { channel: string; platform: string; rec: MirrorRecord; date: string; words: Set }; const byIdThenChannel = (a: { id: string; channel?: string }, b: { id: string; channel?: string }) => (a.channel ?? "").localeCompare(b.channel ?? "") || a.id.localeCompare(b.id); export function matchMirror(input: MirrorMatchInput): MirrorMatchResult { const platforms: Record = {}; for (const c of input.creators) platforms[c.slug] = c.platform; const platformList = [...new Set(input.creators.map((c) => c.platform))].sort(); // The creator's videos by date. const byDate = new Map(); for (const c of input.creators) { for (const rec of c.records) { const date = contentDate(rec); if (!date) continue; const list = byDate.get(date) ?? []; list.push({ channel: c.slug, platform: c.platform, rec, date, words: titleWords(rec.title) }); byDate.set(date, list); } } const rule = input.mirror.titleRule ?? null; const videos: MirrorVideo[] = []; // creator channel + id → mirror ids holding it (strong). const held = new Map(); let dated = 0; let from: string | null = null; let to: string | null = null; for (const m of input.mirror.records) { const date = contentDate(m); const matches: (MirrorMatch & { platform: string })[] = []; if (date) { dated++; if (!from || date < from) from = date; if (!to || date > to) to = date; const words = titleWords(mirrorTopic(m.title, rule)); for (let k = -MIRROR_DAYS_BEFORE; k <= MIRROR_DAYS_AFTER; k++) { for (const cand of byDate.get(addDays(date, k)) ?? []) { const j = titleJaccard(words, cand.words); const agrees = durationsAgree(m.duration, cand.rec.duration); const how = matchRule(j, agrees, cand.rec); if (!how) continue; const score = roundScore(j); const strong = isStrong(score, agrees); matches.push({ channel: cand.channel, platform: cand.platform, id: cand.rec.id, ...(cand.rec.dir && cand.rec.dir !== cand.rec.id ? { dir: cand.rec.dir } : {}), title: cand.rec.title, date: cand.date, duration: cand.rec.duration, wasLive: cand.rec.wasLive, rule: how, score, durationAgrees: agrees, strong, ...(cand.rec.gone ? { gone: cand.rec.gone } : {}), }); if (strong) { const key = `${cand.channel}\x00${cand.rec.id}`; const list = held.get(key) ?? []; if (!list.includes(m.id)) list.push(m.id); held.set(key, list); } } } } matches.sort((a, b) => b.score - a.score || Number(b.strong) - Number(a.strong) || byIdThenChannel(a, b)); const presence: Record = {}; for (const p of platformList) { const strong = matches.filter((x) => x.strong && x.platform === p); presence[p] = strong.some((x) => !x.gone) ? "public" : strong.length > 0 ? "gone" : "none"; } videos.push({ id: m.id, ...(m.dir && m.dir !== m.id ? { dir: m.dir } : {}), title: m.title, date, ...(m.recordedDate ? { recordedDate: m.recordedDate } : {}), duration: m.duration, status: statusOf(presence), presence, matches: matches.map(({ platform: _p, ...rest }) => rest), }); } videos.sort((a, b) => (a.date ?? "").localeCompare(b.date ?? "") || a.id.localeCompare(b.id)); const removed: RemovedUpload[] = []; if (from && to) { for (const c of input.creators) { for (const rec of c.records) { const date = contentDate(rec); if (!rec.gone || !date || date < from || date > to) continue; removed.push({ channel: c.slug, id: rec.id, ...(rec.dir && rec.dir !== rec.id ? { dir: rec.dir } : {}), title: rec.title, date, duration: rec.duration, gone: rec.gone, heldBy: [...(held.get(`${c.slug}\x00${rec.id}`) ?? [])].sort(), }); } } } removed.sort((a, b) => a.date.localeCompare(b.date) || byIdThenChannel(a, b)); const byStatus = Object.fromEntries(MIRROR_STATUSES.map((s) => [s, 0])) as Record; const byPlatform: Record> = {}; for (const p of platformList) byPlatform[p] = { public: 0, gone: 0, none: 0 }; let matched = 0; for (const v of videos) { byStatus[v.status]++; if (v.matches.some((x) => x.strong)) matched++; for (const [p, pr] of Object.entries(v.presence)) byPlatform[p][pr]++; } const byChannel: Record = {}; for (const c of input.creators) byChannel[c.slug] = { inWindow: 0, held: 0 }; for (const r of removed) { byChannel[r.channel].inWindow++; if (r.heldBy.length > 0) byChannel[r.channel].held++; } return { window: from && to ? { from, to } : null, platforms, videos, removed, totals: { videos: videos.length, dated, matched, byStatus, byPlatform, creatorRemoved: { inWindow: removed.length, held: removed.filter((r) => r.heldBy.length > 0).length, byChannel, }, }, }; }