// THE DATE A COPIES SCAN PAIRS A VIDEO BY (lane copies-dates). Pure. // // The copies scan (controller/copiesScan.ts) pairs two members' videos when // their dates are at most `dateWindowDays` apart, so the date it reads is the // whole of its first stage. A video's `upload_date` is the day THAT COPY went // up: right for a live stream (the video is the stream) and for a creator's // own upload, wrong for a re-upload channel, whose copies go up weeks or years // after the streams. The first real scan answered 0 candidates for exactly // that: a re-upload channel dated by its uploads (all of them months after the // last stream its live mirror held) against the mirror dated by its streams. // // THE ORDER, best evidence first; a video is dated by the first that gives one: // rule the channel's `recordedDate.titlePattern` (lib/recordedDate.ts): // the operator's word, as the index derived it or applied here. With // a rule set nothing else is read off a title: a title the rule does // not read is not guessed past it. // live a finished live stream's own day: its `upload_date`, which yt-dlp // sets from the stream (equal to the UTC day of `release_timestamp` // on 822 of 832 live videos measured, a day apart on the rest — the // date window absorbs it). // title with NO rule set, a date the channel's titles carry in one of the // shapes below, when that shape reads at least TITLE_DATE_MIN_SHARE // of the channel's videos that are not live streams (and at least // TITLE_DATE_MIN_VIDEOS of them). The one shape is applied to the // whole channel, as a rule would be, and the same guards hold: a real // day, no later than the upload. The scan then SAYS the rule is not // set and gives the exact patch that sets it, so every other reader // (coverage, the index) dates the channel the same way. // upload anything else: a not-live video's upload day. On a re-upload // channel that is the copy's day, so such a video is also looked for // by runtime and words (lib/copies/match.ts, runtimeCandidates). // // A date only proposes candidates. What a pair IS stays the matcher's: words // both ways and agreeing runtimes to confirm; a metadata-only pair (one side // without words) is held for a bounded wait, never skipped. import { deriveRecordedDate, recordedDatePatternProblem } from "../recordedDate"; export type DateFrom = "rule" | "live" | "title" | "upload"; export const DATE_FROM_ORDER: readonly DateFrom[] = ["rule", "live", "title", "upload"]; // The title shapes the scan recognises without a rule. Each is a valid // recordedDate.titlePattern (the dating test proves it), so the one detected // is proposed to the operator verbatim. export const TITLE_DATE_SHAPES: readonly { id: string; pattern: string }[] = [ // "2023-01-04 …", "2023 01 04 …", "2023 Jan 04 …", "(2023.01.04) …" { id: "leading", pattern: "^\\W{0,3}(?\\d{4})[-\\s._/](?\\d{1,2}|[a-z]{3,9})[-\\s._/](?\\d{1,2})\\b" }, // "[Mirror] 2023-01-04 …", "[VOD] 2023.01.04 …" { id: "tagged", pattern: "^\\s*\\[[^\\]]{1,40}\\]\\s*(?\\d{4})[-._/](?\\d{1,2})[-._/](?\\d{1,2})\\b" }, // "… stream 2023-01-04 …" { id: "iso", pattern: "\\b(?\\d{4})-(?\\d{2})-(?\\d{2})\\b" }, // "… January 4, 2023 …", "… Jan 4th 2023 …" { id: "month-name", pattern: "\\b(?[a-z]{3,9})\\.? (?\\d{1,2})(?:st|nd|rd|th)?,? (?\\d{4})\\b" }, ]; export const TITLE_DATE_MIN_SHARE = 0.5; export const TITLE_DATE_MIN_VIDEOS = 3; export type DatingRecord = { title: string; // YYYYMMDD, or "" when unknown. uploadDate: string; // The channel rule's date (the index's, or applied by the reader). recordedDate?: string; wasLive: boolean; }; export type DetectedTitleDate = { pattern: string; // Not-live videos the shape reads, of how many. reads: number; of: number; }; const YMD = /^\d{8}$/; /** * The title shape that dates most of a channel's not-live videos, when it * reads enough of them to stand for the channel; with `best`, the best shape * whatever it reads (for advice), null when none reads one. */ export function detectTitleDate( records: readonly DatingRecord[], opts: { best?: boolean } = {}, ): DetectedTitleDate | null { const notLive = records.filter((r) => !r.wasLive); if (notLive.length === 0) return null; let top: DetectedTitleDate | null = null; for (const shape of TITLE_DATE_SHAPES) { if (recordedDatePatternProblem(shape.pattern)) continue; const re = new RegExp(shape.pattern, "i"); let reads = 0; for (const r of notLive) if (deriveRecordedDate(re, r.title, r.uploadDate || undefined)) reads++; if (reads > 0 && (!top || reads > top.reads)) top = { pattern: shape.pattern, reads, of: notLive.length }; } if (!top || opts.best) return top; return top.reads >= TITLE_DATE_MIN_VIDEOS && top.reads >= TITLE_DATE_MIN_SHARE * top.of ? top : null; } /** A video's date and where it came from, by the order above; null: undated. */ export function dateEvidence( r: DatingRecord, detected: RegExp | null, ): { date: string; from: DateFrom } | null { if (r.recordedDate && YMD.test(r.recordedDate)) return { date: r.recordedDate, from: "rule" }; const upload = YMD.test(r.uploadDate) ? r.uploadDate : null; if (r.wasLive && upload) return { date: upload, from: "live" }; if (!r.wasLive && detected) { const d = deriveRecordedDate(detected, r.title, upload ?? undefined); if (d) return { date: d, from: "title" }; } return upload ? { date: upload, from: "upload" } : null; } export type ChannelDating = { videos: number; byFrom: Record; // The span of the dates the scan pairs by (YYYYMMDD), null: none dated. span: { from: string; to: string } | null; // Whether the channel's config sets a recordedDate rule. ruleSet: boolean; // The title shape the scan dated the channel by (no rule set). detectedPattern?: string; }; export type DatedChannel = { dating: ChannelDating; dated: { record: T; date: string | null; from: DateFrom | null }[]; // With no rule set and no shape detected: the best shape and what it reads, // for the advice (null when no shape reads any title). bestShape: DetectedTitleDate | null; }; /** One channel's videos, each dated by the order above, with the tally. */ export function dateChannel(records: readonly T[], ruleSet: boolean): DatedChannel { const detected = ruleSet ? null : detectTitleDate(records); const re = detected ? new RegExp(detected.pattern, "i") : null; const byFrom: Record = { rule: 0, live: 0, title: 0, upload: 0, none: 0 }; let from: string | null = null; let to: string | null = null; const dated = records.map((record) => { const e = dateEvidence(record, re); byFrom[e?.from ?? "none"]++; if (e) { if (!from || e.date < from) from = e.date; if (!to || e.date > to) to = e.date; } return { record, date: e?.date ?? null, from: e?.from ?? null }; }); return { dating: { videos: records.length, byFrom, span: from && to ? { from, to } : null, ruleSet, ...(detected ? { detectedPattern: detected.pattern } : {}), }, dated, bestShape: ruleSet || detected ? null : detectTitleDate(records, { best: true }), }; } /** The ops call that sets a channel's recorded-date rule to `pattern`. */ export function recordedDatePatchCommand(slug: string, pattern: string): string { return `pnpm ops channel-config --json '${JSON.stringify({ slug, patch: { recordedDateTitlePattern: pattern } })}'`; } const ymdShow = (d: string) => `${d.slice(0, 4)}-${d.slice(4, 6)}-${d.slice(6, 8)}`; /** * What the scan says about a channel's dates, as sentences: the rule it lacks * (with the exact patch), the videos it could date only by their upload. */ export function datingAdvice(slug: string, d: DatedChannel, runtimeSearch: string | null): string[] { const out: string[] = []; const { dating } = d; if (dating.detectedPattern) { out.push( `${slug}: ${dating.byFrom.title} of ${dating.videos} videos are dated by a date their titles carry; the channel ` + `sets no recordedDate rule, so the scan read the shape itself. Set it, so the index and coverage date them the ` + `same way: ${recordedDatePatchCommand(slug, dating.detectedPattern)}`, ); } if (dating.byFrom.upload > 0) { let s = `${slug}: ${dating.byFrom.upload} of ${dating.videos} videos have only an upload date (not a live stream, no ` + `date in the title${dating.ruleSet ? " the channel's rule reads" : ""}); on a channel of re-uploads that is the ` + `day of the copy, not of the stream.`; if (runtimeSearch) s += ` ${runtimeSearch}`; // With a rule set, or one proposed above, that is the channel's shape: // these titles simply carry none, and nothing more is proposed. if (!dating.ruleSet && !dating.detectedPattern) { s += d.bestShape ? ` The closest title shape reads ${d.bestShape.reads} of ${d.bestShape.of}; if the titles carry the ` + `stream's day, set a rule: ${recordedDatePatchCommand(slug, d.bestShape.pattern)}` : ` If the titles carry the stream's day, set a rule: ${recordedDatePatchCommand(slug, "")}`; } out.push(s); } if (dating.byFrom.none > 0) out.push(`${slug}: ${dating.byFrom.none} of ${dating.videos} videos have no date at all.`); return out; } /** The sentence for a scan with no date candidate at all, naming each member's span. */ export function noCandidatesAdvice( windowDays: number, spans: readonly { slug: string; span: ChannelDating["span"] }[], ): string { const parts = spans.map((s) => `${s.slug} ${s.span ? `${ymdShow(s.span.from)}…${ymdShow(s.span.to)}` : "undated"}`); return `no two members' videos are dated within ${windowDays} day(s) of each other (${parts.join("; ")}): check the dates above before reading 0 as "no copies".`; }