// `umtool phrases` (release 34): the phrase finder from a terminal, and the // page's job. Prints, per phrase, how many times it was said once folded, by // year, from the preferred channel or another, and how many need an ear. // // umtool phrases "" ["" …] --channels a,b [--prefer c] [--exclude RE] // [--words | --regex] [--samples N] [--no-cache] [--json | --full] // umtool phrases --spec FILE --out FILE [--progress] // // A phrase is plain words (a tolerant pattern: any spaces, commas or hyphens // between the words, an optional plural) unless it holds a regular // expression's characters; --words or --regex says which for all of them. // --json prints the summaries, --full every instance. The second form is what // the page's job runs: the settings in a JSON file, the whole answer written to // --out, and with --progress one JSON line of progress per few hundred videos. import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; import path from "node:path"; import { runPhraseScan } from "./scan.mjs"; import { summarisePhrase } from "./summary.mjs"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; function parse(args) { const o = { phrases: [], channels: [], prefer: null, exclude: null, mode: "auto", samples: 10, cache: true, json: false, full: false, spec: null, out: null, progress: false }; for (let i = 0; i < args.length; i++) { const a = args[i]; const v = () => { const x = args[++i]; if (x === undefined) throw new Error(`${a} needs a value`); return x; }; if (a === "--channels") o.channels.push(...v().split(",")); else if (a === "--prefer") o.prefer = v(); else if (a === "--exclude") o.exclude = v(); else if (a === "--words") o.mode = "words"; else if (a === "--regex") o.mode = "regex"; else if (a === "--samples") o.samples = Math.max(0, Number(v()) || 0); else if (a === "--no-cache") o.cache = false; else if (a === "--json") o.json = true; else if (a === "--full") o.full = true; else if (a === "--spec") o.spec = v(); else if (a === "--out") o.out = v(); else if (a === "--progress") o.progress = true; else if (a === "--help" || a === "-h") o.help = true; else if (a.startsWith("--")) throw new Error(`unknown option ${a}`); else o.phrases.push(a); } return o; } const USAGE = `umtool phrases "" ["" …] --channels a,b [--prefer c] [--exclude RE] [--words | --regex] [--samples N] [--no-cache] [--json | --full] umtool phrases --spec FILE --out FILE [--progress]`; /** @param {string[]} args @returns {Promise} the exit code */ export async function phrasesCommand(args) { let o; try { o = parse(args); } catch (e) { console.error(`${e.message}\n\n${USAGE}`); return 2; } if (o.help) { console.log(USAGE); return 0; } try { const spec = o.spec ? JSON.parse(await readFile(o.spec, "utf8")) : { rows: o.phrases.map((text, k) => ({ id: `p${k + 1}`, text, mode: o.mode })), channels: o.channels, prefer: o.prefer, exclude: o.exclude }; const onProgress = o.progress ? (p) => console.log(JSON.stringify({ progress: p })) : undefined; const answer = await runPhraseScan(spec, { ...(o.cache ? {} : { cacheDir: null }), onProgress }); if (o.out) { await mkdir(path.dirname(o.out), { recursive: true }); const tmp = tmpPathFor(o.out); await writeFile(tmp, JSON.stringify(answer), "utf8"); await rename(tmp, o.out); if (o.progress) console.log(JSON.stringify({ done: true, out: o.out, seconds: answer.seconds })); return 0; } if (o.full) { console.log(JSON.stringify(answer, null, 1)); return 0; } const rows = answer.rows.map((r) => ({ id: r.id, text: r.text, searched: r.source, as: r.used, ...summarisePhrase(r.result, { prefer: answer.spec.prefer, samples: o.samples }) })); if (o.json) { console.log(JSON.stringify({ scanned: answer.scanned, seconds: answer.seconds, spec: answer.spec, rows }, null, 2)); return 0; } console.log(`${answer.scanned} transcripts read in ${answer.seconds} s`); for (const r of rows) { const span = r.first ? `${String(r.first).slice(0, 4)}–${String(r.last).slice(0, 4)}` : ""; const split = answer.spec.prefer ? `${answer.spec.prefer} ${r.preferred} · other ${r.other}` : `rumble ${r.rumble} · other ${r.notRumble}`; console.log(""); console.log(`${r.text} ${r.instances} time(s) ${span} ${r.spark}`); console.log(` ${split} · speaker changes ${r.speakerChecks}${r.dropped ? ` · excluded ${r.dropped}` : ""}`); console.log(` searched as /${r.searched}/ (${r.as})`); for (const s of r.samples) console.log(` ${s.date ?? "?"} ${s.slug}/${s.videoId} @ ${Math.round(s.start)}s ${s.context.slice(0, 100)}`); } return 0; } catch (e) { console.error(e instanceof Error ? e.message : String(e)); return 1; } }