// `archilyzer corpus grep --regex [--channels a,b] [--since YYYY-MM-DD] // [--until YYYY-MM-DD] [--window 30] [--limit N] [--case-sensitive] [--json]` // // A regex over the LIVE corpus's transcripts (controller/corpusGrep.ts): every // held video of the named channels (default: every channel that is not social), // dated by metadata.info.json's upload_date, its cues read as the ops // transcript route reads them. Prints each hit's date, channel, id, start // second, title and window text, then how many videos in range have no // transcript yet, and lists them. Needs no editor and no index; writes nothing. // The corpus is TRANSCRIPTS_DIR, else the checkout's transcripts/ (from a // worktree, point TRANSCRIPTS_DIR at the primary checkout's). import { getPaths } from "../lib/paths"; import { corpusGrep, parseGrepDate, type CorpusGrepResult } from "../controller/corpusGrep"; import type { FlagValue } from "./_parseFlags"; function str(v: FlagValue | undefined): string | undefined { return typeof v === "string" ? v : undefined; } function clock(s: number): string { const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); const sec = s % 60; const mm = String(m).padStart(2, "0"); const ss = String(sec).padStart(2, "0"); return h ? `${h}:${mm}:${ss}` : `${m}:${ss}`; } export function formatCorpusGrep(r: CorpusGrepResult): string { const lines: string[] = []; for (const h of r.hits) { lines.push(`${h.date || "undated"} ${h.slug} ${h.id} ${h.start}s (${clock(h.start)}) ${h.title}`); lines.push(` ${h.text}`); } const range = r.since || r.until ? ` uploaded ${r.since ? `from ${r.since}` : ""}${r.since && r.until ? " " : ""}${r.until ? `to ${r.until}` : ""}` : ""; lines.push(""); lines.push( `${r.totalHits} hit${r.totalHits === 1 ? "" : "s"}${r.hits.length < r.totalHits ? ` (${r.hits.length} shown)` : ""} ` + `for /${r.pattern}/ in ${r.videosSearched} transcribed video${r.videosSearched === 1 ? "" : "s"}${range} ` + `across ${r.channels.length} channel${r.channels.length === 1 ? "" : "s"} (${r.windowSeconds} s windows).`, ); if (r.since || r.until) { if (r.undated) lines.push(`${r.undated} video${r.undated === 1 ? "" : "s"} with no metadata.info.json (no date) left out.`); } lines.push(`${r.noTranscript.length} video${r.noTranscript.length === 1 ? "" : "s"} in range with no transcript yet${r.noTranscript.length ? ":" : "."}`); for (const p of r.noTranscript) { const dur = p.duration !== undefined ? ` ${clock(Math.round(p.duration))}` : ""; lines.push(` ${p.date || "undated"} ${p.slug} ${p.id}${dur} ${p.title}`); } for (const s of r.skippedChannels) lines.push(`skipped ${s.slug}: ${s.reason}`); for (const e of r.errors) lines.push(`unreadable ${e.slug}/${e.id}: ${e.error}`); return lines.join("\n"); } export async function main(flags: Record): Promise { const source = str(flags.regex); if (!source) { console.error("corpus grep: --regex is required"); return 2; } let pattern: RegExp; try { pattern = new RegExp(source, flags["case-sensitive"] === true ? "" : "i"); } catch (err) { console.error(`corpus grep: --regex does not parse: ${(err as Error).message}`); return 2; } const dates: Record<"since" | "until", string | undefined> = { since: undefined, until: undefined }; for (const key of ["since", "until"] as const) { const raw = flags[key]; if (raw === undefined) continue; const day = typeof raw === "string" ? parseGrepDate(raw) : null; if (!day) { console.error(`corpus grep: --${key} is a date, YYYY-MM-DD`); return 2; } dates[key] = day; } const num = (key: string, min: number): number | undefined | null => { const raw = flags[key]; if (raw === undefined) return undefined; const n = typeof raw === "string" ? Number(raw) : NaN; return Number.isInteger(n) && n >= min ? n : null; }; const windowSeconds = num("window", 1); if (windowSeconds === null) { console.error("corpus grep: --window is a whole number of seconds, 1 or more"); return 2; } const limit = num("limit", 0); if (limit === null) { console.error("corpus grep: --limit is a whole number"); return 2; } const channelsRaw = flags.channels; if (channelsRaw === true) { console.error("corpus grep: --channels takes a comma-separated list of slugs"); return 2; } const channels = channelsRaw ?.split(",") .map((s) => s.trim()) .filter(Boolean); let result: CorpusGrepResult; try { result = await corpusGrep({ paths: getPaths(), pattern, ...(channels?.length ? { channels } : {}), ...(dates.since ? { since: dates.since } : {}), ...(dates.until ? { until: dates.until } : {}), ...(windowSeconds !== undefined ? { windowSeconds } : {}), ...(limit !== undefined ? { limit } : {}), }); } catch (err) { console.error(`corpus grep: ${(err as Error).message}`); return 1; } console.log(flags.json === true ? JSON.stringify(result, null, 2) : formatCorpusGrep(result)); return 0; }