// A LOOKOUT'S SELECTION, the pure half (lane/lookout stages 1–2): air dates, // the window, and the folding of one recording's several uploads into one read. // No I/O — controller/lookoutPlan.ts reads the channels and calls these. // // AIR DATE, NOT UPLOAD DATE. yt-dlp's `upload_date` is a UTC date, so an // evening stream in the Americas lands on the next day: an election-night // stream that started at 20:20 in New York is 4 Nov by `upload_date` and 3 Nov // on the air. A lookout's days are counted in its own time zone from the // metadata's `release_timestamp` (a stream's or premiere's start), else its // `timestamp` (the upload), and only when neither is there from `upload_date`, // which the plan then marks as such. // // ONE RECORDING, READ ONCE. Two cases, both decided on the cues, because // `transcripts/duplicates.json` is built rarely and does not know this week: // - a DUPLICATE UPLOAD — the same recording on two channels or platforms — // is two videos whose runtimes agree within 2 % and whose normalised titles // match or whose transcripts are near-duplicates (5-word shingle Jaccard ≥ // 0.35, the duplicate detector's own threshold: two ASR engines over the // same audio land at 0.35–0.60). A re-titled mirror is caught by the text. // A title match alone is not enough when both have text: two episodes of // a show with one title and one length share none, so it also needs a // Jaccard of 0.1. // - a CUT-DOWN of a stream — a shorter upload whose text is an excerpt of a // long recording in the window — is a video under 30 minutes at least 40 % // of whose shingles are in one stream (the pilot's measured rule). It is // credited to the stream as an alternate source and not read on its own. // Every folded video stays in the plan, under the recording it was folded into, // with why and its score. import { DEFAULT_NEAR_THRESHOLD, DEFAULT_SHINGLE_SIZE, DEFAULT_TITLE_DURATION_RATIO, UnionFind, durationsCompatible, intersectionSize, jaccard, normalizeTitleKey, platformRank, } from "./duplicates"; // ─── Air dates and the window ─── export type AirDateSource = "release_timestamp" | "timestamp" | "upload_date"; export type AirDate = { /** YYYY-MM-DD in the lookout's time zone (or the UTC upload date, when that is all there is). */ date: string; /** ISO 8601 instant, when a timestamp gave it. */ at: string | null; source: AirDateSource; }; /** The machine's IANA zone (what a lookout's `timeZone: ""` means). */ export function machineTimeZone(): string { return Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC"; } /** `tz`, or the machine's when it is "". */ export function resolveTimeZone(tz: string): string { return tz ? tz : machineTimeZone(); } /** The calendar date (YYYY-MM-DD) of an instant in `tz`. */ export function localDateOf(ms: number, tz: string): string { const parts = new Intl.DateTimeFormat("en-US", { timeZone: tz, year: "numeric", month: "2-digit", day: "2-digit", }).formatToParts(new Date(ms)); const get = (t: string) => parts.find((p) => p.type === t)?.value ?? ""; return `${get("year")}-${get("month")}-${get("day")}`; } /** "20261104" → "2026-11-04"; null for anything else. */ export function isoOfUploadDate(yyyymmdd: string | null | undefined): string | null { if (typeof yyyymmdd !== "string" || !/^\d{8}$/.test(yyyymmdd)) return null; return `${yyyymmdd.slice(0, 4)}-${yyyymmdd.slice(4, 6)}-${yyyymmdd.slice(6, 8)}`; } const ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/; export function isIsoDate(s: unknown): s is string { if (typeof s !== "string" || !ISO_DATE_RE.test(s)) return false; const d = new Date(`${s}T00:00:00Z`); return !Number.isNaN(d.getTime()) && d.toISOString().slice(0, 10) === s; } /** A YYYY-MM-DD date `n` days later (negative: earlier). */ export function addDays(date: string, n: number): string { const d = new Date(`${date}T00:00:00Z`); d.setUTCDate(d.getUTCDate() + n); return d.toISOString().slice(0, 10); } /** Whole days from `a` to `b` (both YYYY-MM-DD). */ export function daysBetween(a: string, b: string): number { return Math.round((Date.parse(`${b}T00:00:00Z`) - Date.parse(`${a}T00:00:00Z`)) / 86_400_000); } // A timestamp further than this from the record's own UTC `upload_date` is not // believed (a nested key read by mistake, a broken record): the upload date is // used instead and the plan says so. const TIMESTAMP_TRUST_DAYS = 2; /** * A video's air date in `tz`: its `release_timestamp`, else its `timestamp` * (seconds since the epoch, as yt-dlp writes them), else its `upload_date`. * Null when it has none of the three. */ export function airDateOf( meta: { releaseTimestamp?: number | null; timestamp?: number | null; uploadDate?: string | null }, tz: string, ): AirDate | null { const upload = isoOfUploadDate(meta.uploadDate); const candidates: [AirDateSource, number | null | undefined][] = [ ["release_timestamp", meta.releaseTimestamp], ["timestamp", meta.timestamp], ]; for (const [source, ts] of candidates) { if (typeof ts !== "number" || !Number.isFinite(ts) || ts <= 0) continue; const ms = ts * 1000; const utc = new Date(ms).toISOString().slice(0, 10); if (upload && Math.abs(daysBetween(upload, utc)) > TIMESTAMP_TRUST_DAYS) continue; return { date: localDateOf(ms, tz), at: new Date(ms).toISOString(), source }; } return upload ? { date: upload, at: null, source: "upload_date" } : null; } export type LookoutWindow = { /** First day read, YYYY-MM-DD, inclusive. */ from: string; /** Last day read, YYYY-MM-DD, inclusive. */ to: string; days: number; timeZone: string; }; /** * The `days` calendar days ending on `until` (default: today in `tz`), both * ends included: days 7 on 11 Nov is 5–11 Nov. */ export function lookoutWindow(o: { days: number; timeZone: string; until?: string | null; now?: Date }): LookoutWindow { const tz = resolveTimeZone(o.timeZone); const to = o.until && isIsoDate(o.until) ? o.until : localDateOf((o.now ?? new Date()).getTime(), tz); const days = Math.max(1, Math.round(o.days)); return { from: addDays(to, -(days - 1)), to, days, timeZone: tz }; } export function inWindow(date: string, w: Pick): boolean { return date >= w.from && date <= w.to; } // ─── One recording, read once ─── export const LOOKOUT_SHINGLE_SIZE = DEFAULT_SHINGLE_SIZE; /** Duplicate uploads: transcripts at least this similar (Jaccard) are one recording. */ export const TWIN_TEXT_THRESHOLD = DEFAULT_NEAR_THRESHOLD; /** Duplicate uploads with the same title: their transcripts still share at least this much (Jaccard), when both have text. */ export const TWIN_TITLE_TEXT_FLOOR = 0.1; /** Duplicate uploads: runtimes within this fraction of the longer. */ export const TWIN_DURATION_RATIO = DEFAULT_TITLE_DURATION_RATIO; /** A recording this long or longer is a stream other uploads may be cut from. */ export const STREAM_MIN_SECONDS = 1800; /** A shorter upload with at least this share of its shingles in a stream is that stream's cut-down. */ export const COVERED_THRESHOLD = 0.4; export type DedupeVideo = { /** `/