import { NextResponse } from "next/server"; import { WAYBACK_IMPORT_BODY_KEYS } from "yt-dlp-transcript-common/controller/waybackImport"; import { CAPTURE_SHOW_VALUES, type CaptureShow } from "yt-dlp-transcript-common/lib/waybackPages/scanView"; import { importCapturesAction } from "../../../channels/[slug]/captures/actions"; import { OpsInputError, ops, opsFail, optBool, optString, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, keys? | year?, prefix?, q?, show?, dryRun? } -> { ok, slug, // dryRun, chosen, queued: [{key, url, jobId?}], skipped: [{key, why}], left } // // Imports chosen rows of a Wayback pages channel's scan (release 37 S2): one // `import-page` job per row, each the capture the row's content was first // listed at, queued on archive.org's queue (they run one at a time at its // pace). `keys` names rows as the Captures tab does (` `); // without it, every row the tab's filter keeps — `year` (first seen), `prefix` // ("blog.example/post/": host first, no scheme), `q` (text in the URL), `show` // (all | new | not-imported | imported) — is Import all in range. A row already // imported is skipped; at most 500 are queued a call (`left` says how many // remain). `dryRun` says what would be queued and queues nothing. // // AN ADAPTER: the refusals are the controller's (controller/waybackImport.ts). export async function POST(request: Request) { return ops(request, WAYBACK_IMPORT_BODY_KEYS, async (body) => { const slug = reqSlug(body, "slug"); const keys = body.keys; if (keys !== undefined && (!Array.isArray(keys) || keys.some((k) => typeof k !== "string" || !k.trim()))) { throw new OpsInputError('"keys" must be a list of row keys (" ")'); } const show = optString(body, "show"); if (show !== undefined && !(CAPTURE_SHOW_VALUES as readonly string[]).includes(show)) { throw new OpsInputError(`"show" must be one of ${CAPTURE_SHOW_VALUES.join(", ")}`); } const year = optString(body, "year"); if (year !== undefined && !/^\d{4}$/.test(year)) throw new OpsInputError('"year" must be YYYY'); const filterKeys = ["year", "prefix", "q", "show"].filter((k) => body[k] !== undefined); if (keys !== undefined && filterKeys.length) throw new OpsInputError(`send "keys" or a range (${filterKeys.join(", ")}), not both`); const prefix = optString(body, "prefix"); const q = optString(body, "q"); const r = await importCapturesAction( slug, keys !== undefined ? { keys: keys as string[] } : { filter: { ...(year ? { year } : {}), ...(prefix ? { prefix } : {}), ...(q ? { q } : {}), ...(show ? { show: show as CaptureShow } : {}) } }, { dryRun: optBool(body, "dryRun") === true, requestedBy: "ops" }, ); if (!r.ok) return opsFail(r.error ?? "the import was refused", 409, { queued: r.queued, skipped: r.skipped }); return NextResponse.json(r); }); }