import { NextResponse } from "next/server"; import { savePageNowBatchAction, savePageNowBatchDryRunAction } from "../../../channels/[slug]/waybackLiveActions"; import { OpsInputError, ops, opsFail, optBool, optPositiveInt, optString, reqStringArray } from "../_lib"; export const dynamic = "force-dynamic"; // POST { urls: [...] | file: | batch: , skipArchived?: true, // sources?: ["wayback", "archive.today"], evidence?: , // dailyCap?, dryRun? } -> { ok: true, jobId, batch } | (dryRun) the plan // // SAVE PAGE NOW, A LIST (lane spn-batch, controller/savePageNowBatch.ts): ONE // resumable save-page-now-batch job. Each URL in turn: looked up (the Wayback // Machine's CDX, archive.today's timemap; skipped when a copy shows it), else // saved — anonymously without archive.org keys, SPN2 with them — one at a // time, ≥ 60 s apart anonymous (≥ 10 s with keys), resting after a 429/503, // at most `dailyCap` saves a day (150 anonymous; archive.org allows 200). The // state is /.spn-batches/.json; the same request again (or // {batch}) continues it. The evidence JSON + CSV is `evidence` (default // beside the state). archive.today and Ghostarchive are looked up, never // asked to save. The log ends with `@@save-page-now-batch {batch, mode, // counts, run, evidence, statePath, etaSeconds, stopped?}`. export async function POST(request: Request) { return ops(request, ["urls", "file", "batch", "skipArchived", "sources", "evidence", "dailyCap", "dryRun"], async (body) => { const urls = body.urls !== undefined ? reqStringArray(body, "urls") : undefined; const file = optString(body, "file")?.trim() || undefined; const batch = optString(body, "batch")?.trim() || undefined; if ([urls, file, batch].filter((v) => v !== undefined).length !== 1) { throw new OpsInputError(`give exactly one of "urls", "file" or "batch"`); } const sources = body.sources !== undefined ? reqStringArray(body, "sources") : undefined; const evidence = optString(body, "evidence")?.trim() || undefined; const skipArchived = optBool(body, "skipArchived"); const dailyCap = optPositiveInt(body, "dailyCap"); const req = { ...(urls ? { urls } : {}), ...(file ? { file } : {}), ...(batch ? { batch } : {}), ...(sources ? { sources } : {}), ...(evidence ? { evidence } : {}), ...(skipArchived !== undefined ? { skipArchived } : {}), ...(dailyCap !== undefined ? { dailyCap } : {}), }; if (optBool(body, "dryRun") === true) { const r = await savePageNowBatchDryRunAction(req); if (!r.ok) return opsFail(r.error, 400); return NextResponse.json(r); } const res = await savePageNowBatchAction(req, "ops"); if (!res.ok) return opsFail(res.error, 400); void res.stream.cancel(); return NextResponse.json({ ok: true, jobId: res.jobId, batch: res.batch }); }); }