// WHAT A PAGE IMPORT IS ASKED FOR, across archive sources (release 37 lane F): // a copy's URL on any source, or a page's own URL with a date and (optionally) // the source to look in. Client-safe (no node imports). // // {url: } that capture (lib/waybackPages/urls.ts) // {url: , at} the Wayback capture nearest `at` // {url: } that copy (archive.ph/, //, /newest/) // {url: , at, source: "archive.today"} the archive.today copy nearest `at` (its timemap) import { parsePageCaptureAsk, isCaptureAt, NOT_A_CAPTURE_URL, type PageCaptureAsk } from "../waybackPages/urls"; import { isArchiveTodayUrl, parseArchiveTodayUrl } from "./mementos"; import { isArchiveSourceId, type ArchiveMementoRef, type ArchiveSourceId } from "./types"; export type ArchiveCaptureAsk = | (PageCaptureAsk & { source: "wayback" }) // One archive.today copy by its URL. `originalUrl` when the URL names the // page (//, /newest/); a short id names none. | { kind: "memento"; source: "archive.today"; ref: ArchiveMementoRef; originalUrl?: string } // The archive.today copy of `originalUrl` nearest `at`. | { kind: "nearest"; source: "archive.today"; at: string; originalUrl: string }; export const NOT_A_COPY_URL = `${NOT_A_CAPTURE_URL}; or an archive.today copy's URL (https://archive.ph/), ` + 'or the page\'s own URL, a date and "source": "archive.today"'; export function parseArchiveCaptureAsk( url: string, at?: string, source?: string, ): { ok: true; ask: ArchiveCaptureAsk } | { ok: false; error: string } { const given = url.trim(); const when = (at ?? "").trim(); const want = (source ?? "").trim(); if (want && !isArchiveSourceId(want)) { return { ok: false, error: `"source" must be "wayback" or "archive.today", not "${want}"` }; } const asked = (want || undefined) as ArchiveSourceId | undefined; if (given && isArchiveTodayUrl(given)) { if (asked && asked !== "archive.today") { return { ok: false, error: `${given} is an archive.today copy, not the ${asked === "wayback" ? "Wayback Machine's" : asked}` }; } const ref = parseArchiveTodayUrl(given); if (!ref) { return { ok: false, error: `${given} is an archive.today listing or link, not one copy: ${NOT_A_COPY_URL}` }; } if (when) { return { ok: false, error: `"at" goes with a page's own URL; ${given} already names its copy` }; } return { ok: true, ask: { kind: "memento", source: "archive.today", ref, ...(ref.originalUrl ? { originalUrl: ref.originalUrl } : {}) }, }; } if (asked === "archive.today") { let u: URL; try { u = new URL(given); } catch { return { ok: false, error: `"${given}" is not a URL: ${NOT_A_COPY_URL}` }; } if (u.protocol !== "http:" && u.protocol !== "https:") return { ok: false, error: `"${given}" is not an http(s) URL` }; const wb = parsePageCaptureAsk(given); if (wb.ok) return { ok: false, error: `${given} is a Wayback Machine capture, not an archive.today copy` }; if (!when) { return { ok: false, error: `give "at" (YYYYMMDD) with a page's own URL to import its archive.today copy nearest that date` }; } if (!isCaptureAt(when)) return { ok: false, error: `"at" must be a date as YYYYMMDD (or YYYYMMDDhhmmss), not "${when}"` }; return { ok: true, ask: { kind: "nearest", source: "archive.today", at: when, originalUrl: given } }; } const wb = parsePageCaptureAsk(given, at); if (!wb.ok) return wb; return { ok: true, ask: { ...wb.ask, source: "wayback" } }; }