import { findCopyAction, findWholeCopiesAction } from "../../../channels/[slug]/videos/findCopyAction"; import { FIND_COPY_MAX_LIMIT, GONE_SCOPES, type GoneScope } from "yt-dlp-transcript-common/publish/findCopy"; import { OpsInputError, jobResponse, ops, optBool, optPositiveInt, optString } from "../_lib"; import { findPageCopiesAction } from "../../../channels/[slug]/waybackLiveActions"; import { FIND_PAGE_COPY_MAX_LIMIT } from "yt-dlp-transcript-common/publish/findCopyPages"; export const dynamic = "force-dynamic"; // POST { article } | { siteId } | { articles: true }, dryRun?, retryFailed? -> { ok, jobId } // // A COPY OF EVERY GONE RECORDING THE REPORTS CITE (release 29 E2, // publish/findCopy.ts), as a `find-copy` job: of one article (`article`), // every published article (`articles`), or a site's reports (`siteId`), the // cited windows whose recording is deleted or private ("unfetchable: gone"). // Per YouTube recording, one at a time: the Wayback Machine when it holds the // video (the check yt-dlp makes), then archive.org (a mirror of the upload — // its identifier, a file name or its original URL names the id — whose file // has the recording's length). A copy a fetch failed from at its source // before (404, 410, 403, unreachable — never our own selector) is not picked // again, unless `retryFailed`. The windows of every copy found go to ONE fetch-windows job on the // archive.org clip queue, each with the copy's URL as its page, so the clips // land in the recording's own clips/ with the copy in their provenance. A // recording with no copy is left: its moments show their words only ("source // deleted"). `dryRun` looks and starts no fetch. The job's log ends with // `@@find-copy {recordings: [{slug, id, gone, watchUrl, youtubeId, windows, // failedBefore, tried, copy?, outcome}], items, stopped?, fetchJobId?, // refused?}`; `--wait` follows it. // // POST { slug } | { siteId }, gone: "no-transcript" | "all", dryRun?, max?, // retryFailed? -> { ok, jobId } — THE SCOPE MODE (release 31): every gone // video of the channel (or of the site's channels) with no audio or video // (and, for "no-transcript", no transcript), at most `max` (default 10, at // most 200) looked up per run, the copies' WHOLE audio fetched into each // video's own record by ONE fetch-copies job (archive.org's download queue), // so the transcription lane transcribes it. A terminal attempt (none found, // a failed copy) in the last 30 days keeps a video out unless `retryFailed`. // `dryRun` looks (paced lookups) and records and fetches nothing. The log ends // with `@@find-copy {scope, gone, dryRun, max, channels, eligible, skipped, // looked: [{slug, id, gone, noTranscript, watchUrl, youtubeId, tried, copy?, // via?, outcome}], left, items, stopped?, fetchJobId?, refused?}`. // // POST { article } | { siteId } | { articles: true }, pages: true, channel?, // max?, dryRun? -> { ok, jobId } — THE PAGE MODE (release 37 S5, // publish/findCopyPages.ts): every page the reports cite whose ORIGINAL is // gone at its own site gets the Wayback capture nearest the citation's date // imported into the Wayback pages channel named (`channel`) or the one whose // targets cover it; a cited page record whose original is gone has its // capture imported again when it is not stored. A find-copy job on the // archive.org queue; at most `max` (default 10, at most 100) pages looked up. // The log ends with `@@find-copy {host, dryRun, max, pages, posts, items: // [{citedBy, url, original, channel?, capture?, outcome, detail, pointTo?, // as?, records?}], left, stopped?}` — `pointTo`/`as` name the record a // citation should cite; the report is not rewritten. export async function POST(request: Request) { return ops(request, ["article", "siteId", "articles", "dryRun", "retryFailed", "slug", "gone", "max", "pages", "channel"], async (body) => { const article = optString(body, "article")?.trim(); const siteId = optString(body, "siteId")?.trim(); const articles = optBool(body, "articles") === true; const gone = optString(body, "gone"); if (optBool(body, "pages") === true) { if (gone !== undefined || optString(body, "slug") !== undefined || body.retryFailed !== undefined) { throw new OpsInputError('"pages" is its own mode: send "article", "articles": true or "siteId", with "channel"?, "max"?, "dryRun"? — not "gone", "slug" or "retryFailed"'); } if ([article, siteId, articles || undefined].filter(Boolean).length !== 1) { throw new OpsInputError('send one of "article" (an article id), "articles": true or "siteId"'); } const max = optPositiveInt(body, "max"); if (max !== undefined && max > FIND_PAGE_COPY_MAX_LIMIT) { throw new OpsInputError(`"max" must be at most ${FIND_PAGE_COPY_MAX_LIMIT}`); } const channel = optString(body, "channel")?.trim() || undefined; return jobResponse( await findPageCopiesAction({ ...(article ? { article } : {}), ...(siteId ? { siteId } : {}), ...(channel ? { channel } : {}), ...(max !== undefined ? { max } : {}), dryRun: optBool(body, "dryRun") === true, }), ); } if (optString(body, "channel") !== undefined) throw new OpsInputError('"channel" goes with "pages": true (the page mode)'); if (gone !== undefined) { if (!(GONE_SCOPES as readonly string[]).includes(gone)) { throw new OpsInputError(`"gone" must be one of ${GONE_SCOPES.join(", ")}`); } const slug = optString(body, "slug")?.trim(); if ([slug, siteId].filter(Boolean).length !== 1 || article || articles) { throw new OpsInputError('with "gone", send one of "slug" (a channel) or "siteId" — not "article" or "articles"'); } const max = optPositiveInt(body, "max"); if (max !== undefined && max > FIND_COPY_MAX_LIMIT) { throw new OpsInputError(`"max" must be at most ${FIND_COPY_MAX_LIMIT}`); } return jobResponse( await findWholeCopiesAction({ ...(slug ? { slug } : { siteId: siteId! }), gone: gone as GoneScope, dryRun: optBool(body, "dryRun") === true, ...(max !== undefined ? { max } : {}), retryFailed: optBool(body, "retryFailed") === true, }), ); } if (optString(body, "slug") !== undefined || body.max !== undefined) { throw new OpsInputError('"slug" and "max" go with "gone" (the scope mode)'); } if ([article, siteId, articles || undefined].filter(Boolean).length !== 1) { throw new OpsInputError('send one of "article" (an article id), "articles": true or "siteId"'); } return jobResponse( await findCopyAction({ ...(article ? { article } : {}), ...(siteId ? { siteId } : {}), dryRun: optBool(body, "dryRun") === true, retryFailed: optBool(body, "retryFailed") === true, }), ); }); }