import { NextResponse } from "next/server"; import { ArchiveOrgRequestError } from "yt-dlp-transcript-common/lib/archiveOrgClient"; import { isWaybackExcluded } from "yt-dlp-transcript-common/lib/waybackClient"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { readChannelConfig } from "yt-dlp-transcript-common/controller/channels"; import { isSourcelessPostsChannel, SOURCELESS_POSTS_CHANNEL } from "yt-dlp-transcript-common/lib/sourcelessPosts"; import { recordWebpageUrl, waybackLookup } from "yt-dlp-transcript-common/publish/findCopy"; import { OpsInputError, ops, opsFail, optBool, optPositiveInt, optString, reqSlug, reqVideoId } from "../_lib"; export const dynamic = "force-dynamic"; // POST { url } | { slug, id }, okOnly?, limit? -> { ok: true, url, found, // captures: [{ timestamp, status, mimetype?, replayUrl }] } // // The Wayback Machine's captures of a page (its CDX API, release 29 E2): a // URL as given, or a record's page — its metadata's webpage_url, else the one // its availability check recorded, else the playlist's. Oldest first; // `okOnly` keeps only the captures that recorded a 200; `limit` at most 500 // (default 100). ONE request through the editor's Wayback client, with // archive.org's manners (one at a time, a 2 s gap, the project's User-Agent, // 429s waited out a few times, then a 502). Read only; nothing is fetched. // `find-copy` is what fetches a gone recording's windows from a capture. // Release 37 lane F: a URL the Wayback Machine answers 403 for is EXCLUDED // ("excluded from the Wayback Machine (cause unknown)"): a 403 with // `refusal: "excluded"`. `archive-lookup` asks every archive source. const MAX_LIMIT = 500; export async function POST(request: Request) { return ops(request, ["url", "slug", "id", "okOnly", "limit"], async (body) => { const given = optString(body, "url")?.trim(); const hasRecord = body.slug !== undefined || body.id !== undefined; if (given && hasRecord) throw new OpsInputError('send either "url" or "slug" and "id", not both'); let url: string; if (given) { if (!/^https?:\/\/[^\s]+$/.test(given)) throw new OpsInputError(`"url" must be an http(s) URL`); url = given; } else if (hasRecord) { const slug = reqSlug(body, "slug"); const id = reqVideoId(body, "id"); // Posts published from a private library have no page of their own to // look up (review 1 S1): refused by name, before any address is built. if (isSourcelessPostsChannel(await readChannelConfig(getPaths(), slug))) return opsFail(SOURCELESS_POSTS_CHANNEL(slug)); const found = await recordWebpageUrl(getPaths(), slug, id); if (!found) return opsFail(`no page is known for ${slug}/${id} (no metadata, availability check or playlist entry names one)`, 404); url = found; } else { throw new OpsInputError('"url" (a page) or "slug" and "id" (a record) is required'); } const limit = optPositiveInt(body, "limit") ?? 100; if (limit > MAX_LIMIT) throw new OpsInputError(`"limit" is at most ${MAX_LIMIT}`); try { const r = await waybackLookup(url, { okOnly: optBool(body, "okOnly") === true, limit }); return NextResponse.json({ ok: true, url, found: r.captures.length, captures: r.captures }); } catch (e) { // Release 37 lane F: a 403 for this URL while the archive answers is an // exclusion — a fact about the URL, not an outage. if (isWaybackExcluded(e)) return opsFail((e as Error).message, 403, { refusal: "excluded" }); if (e instanceof ArchiveOrgRequestError) return opsFail(e.message, 502); throw e; } }); }