import { NextResponse } from "next/server"; import { ArchiveOrgRequestError } from "yt-dlp-transcript-common/lib/archiveOrgClient"; import { ARCHIVE_ORG_SEARCH_MAX_ROWS, searchArchiveOrgItems, } from "yt-dlp-transcript-common/controller/archiveOrgImport"; import { findArchiveOrgMirrors, isYoutubeId } from "yt-dlp-transcript-common/publish/findCopy"; import { OpsInputError, ops, opsFail, optPositiveInt, optString, reqString } from "../_lib"; export const dynamic = "force-dynamic"; // POST { query, limit? } -> { ok: true, query, found, items: [{ identifier, title? }] } // | { youtubeId, limit? } -> { ok: true, youtubeId, query, found, // items: [{ identifier, title?, file, url, source, size? }], // skipped: [{ identifier, why }] } // // An archive.org search (its advanced-search syntax), read only: ONE request // through the editor's polite archive.org client — the same client, gap and // back-off an import uses (controller/archiveOrgImport.ts) — the first // `limit` items (default 50, at most 500) in identifier order. Nothing is // written; `import-archive-org` with `query` imports what a search finds. // // `youtubeId` (release 29 E2) looks for MIRRORS of one YouTube upload // (publish/findCopy.ts): the search finds items mentioning the id, each is // read for its files (one request each), and only an item OF the upload — its // identifier, a file's name or its `originalurl`/`source` names the id, not // merely its title or description — is a mirror, answered with its direct // video file URL (`https://archive.org/download//`, an // original preferred). Anything else is `skipped`, with why. `limit` // defaults to 10 here. (find-copy also checks the file's length against the // recording's.) const ARCHIVE_SEARCH_DEFAULT_LIMIT = 50; const MIRROR_DEFAULT_LIMIT = 10; export async function POST(request: Request) { return ops(request, ["query", "limit", "youtubeId"], async (body) => { const youtubeId = optString(body, "youtubeId")?.trim(); if (youtubeId !== undefined && body.query !== undefined) { throw new OpsInputError('send either "query" or "youtubeId", not both'); } if (youtubeId !== undefined) { if (!isYoutubeId(youtubeId)) throw new OpsInputError(`"${youtubeId}" is not a YouTube id`); const limit = optPositiveInt(body, "limit") ?? MIRROR_DEFAULT_LIMIT; if (limit > ARCHIVE_ORG_SEARCH_MAX_ROWS) throw new OpsInputError(`"limit" is at most ${ARCHIVE_ORG_SEARCH_MAX_ROWS}`); try { const r = await findArchiveOrgMirrors(youtubeId, { limit }); return NextResponse.json({ ok: true, youtubeId, query: r.query, found: r.found, items: r.mirrors, skipped: r.skipped }); } catch (e) { if (e instanceof ArchiveOrgRequestError) return opsFail(e.message, 502); throw e; } } const query = reqString(body, "query"); const limit = optPositiveInt(body, "limit") ?? ARCHIVE_SEARCH_DEFAULT_LIMIT; if (limit > ARCHIVE_ORG_SEARCH_MAX_ROWS) { throw new OpsInputError(`"limit" is at most ${ARCHIVE_ORG_SEARCH_MAX_ROWS}`); } try { const r = await searchArchiveOrgItems(query, { rows: limit }); return NextResponse.json({ ok: true, query, found: r.found, items: r.items }); } catch (e) { if (e instanceof ArchiveOrgRequestError) return opsFail(e.message, 502); throw e; } }); }