// What a Wayback pages import is asked for, read from what a person types: a // capture's replay URL (web.archive.org/web/[mod]/), or an // original URL and a date (`at`, "YYYYMMDD[hhmmss]") for the capture nearest // it. Client-safe: no node imports. import { isWaybackHost, parseWaybackUrl } from "../wayback"; import { isArchiveTodayHost, parseArchiveTodayUrl } from "../archives/mementos"; // The bodies a page import keeps. Anything else is refused by its mimetype. export const PAGE_MIMETYPES = ["text/html", "text/plain", "application/json"] as const; // The bare mimetype of a Content-Type ("text/html; charset=utf-8" → "text/html"). export function bareMimetype(contentType: string | null | undefined): string { return (contentType ?? "").split(";")[0].trim().toLowerCase(); } export function isPageMimetype(contentType: string | null | undefined): boolean { return (PAGE_MIMETYPES as readonly string[]).includes(bareMimetype(contentType)); } // "YYYYMMDD[hhmmss]" — or any whole-unit prefix of a Wayback timestamp // (yyyy, yyyyMM, … yyyyMMddhhmmss). export function isCaptureAt(v: string): boolean { return /^\d{4}(?:\d{2}){0,5}$/.test(v); } // A Wayback timestamp (4–14 digits) as an ISO-8601 instant, UTC; the missing // units are the start of the period. "" for one that is not a real time. export function captureTsIso(ts: string): string { if (!/^\d{4,14}$/.test(ts)) return ""; const p = ts + "00000101000000".slice(ts.length); const [y, mo, d, h, mi, s] = [p.slice(0, 4), p.slice(4, 6), p.slice(6, 8), p.slice(8, 10), p.slice(10, 12), p.slice(12, 14)]; const iso = `${y}-${mo}-${d}T${h}:${mi}:${s}.000Z`; const ms = Date.parse(iso); if (!Number.isFinite(ms)) return ""; // Date.parse rolls 2014-02-31 over to March; a timestamp that does that is // not a real time. return new Date(ms).toISOString() === iso ? iso : ""; } export type PageCaptureAsk = // One capture, by its timestamp (as the URL gives it: 4–14 digits; the // Wayback Machine answers a short one with the nearest capture). | { kind: "capture"; captureTs: string; originalUrl: string } // The capture of `originalUrl` nearest `at`, found through the CDX API. | { kind: "nearest"; at: string; originalUrl: string }; export const NOT_A_CAPTURE_URL = "give a Wayback Machine capture's URL (https://web.archive.org/web//), " + 'or the page\'s own URL and a date ("at": YYYYMMDD)'; // What `{url, at?}` asks for, or the sentence why it asks for nothing. export function parsePageCaptureAsk( url: string, at?: string, ): { ok: true; ask: PageCaptureAsk } | { ok: false; error: string } { const given = url.trim(); const when = (at ?? "").trim(); if (!given) return { ok: false, error: NOT_A_CAPTURE_URL }; let u: URL; try { u = new URL(given); } catch { return { ok: false, error: `"${given}" is not a URL: ${NOT_A_CAPTURE_URL}` }; } if (u.protocol !== "http:" && u.protocol !== "https:") { return { ok: false, error: `"${given}" is not an http(s) URL: ${NOT_A_CAPTURE_URL}` }; } if (when && !isCaptureAt(when)) { return { ok: false, error: `"at" must be a date as YYYYMMDD (or YYYYMMDDhhmmss), not "${when}"` }; } if (isWaybackHost(u.hostname)) { const ref = parseWaybackUrl(given); if (!ref) { return { ok: false, error: `${given} is a Wayback Machine listing, not one capture: ${NOT_A_CAPTURE_URL}`, }; } if (when) { return { ok: false, error: `"at" goes with a page's own URL; ${given} already names its capture (${ref.captureTs})` }; } return { ok: true, ask: { kind: "capture", captureTs: ref.captureTs, originalUrl: ref.originalUrl } }; } if (!when) { return { ok: false, error: `${given} is not a Wayback Machine capture: ${NOT_A_CAPTURE_URL}` }; } return { ok: true, ask: { kind: "nearest", at: when, originalUrl: given } }; } const MEDIA_FILE_RE = /\.(?:mp4|m4v|webm|mkv|mov|flv|avi|wmv|mpg|mpeg|ts|m3u8|mp3|m4a|aac|ogg|oga|opus|wav|flac)$/i; // The platform when `url` is a VIDEO (or a media file) — what `import-video` // imports from a capture — else null. A channel's page, an About page, a // profile or a post is not a video. export function videoPageOf(url: string): string | null { let u: URL; try { u = new URL(url); } catch { return null; } const host = u.hostname.toLowerCase().replace(/^www\./, ""); const segs = u.pathname.split("/").filter(Boolean); if (MEDIA_FILE_RE.test(u.pathname)) return "a media file"; if (host === "youtu.be") return segs[0] ? "YouTube" : null; if (host === "youtube.com" || host.endsWith(".youtube.com") || host === "youtube-nocookie.com") { if (u.pathname === "/watch" && u.searchParams.get("v")) return "YouTube"; if (["shorts", "embed", "live", "v"].includes(segs[0] ?? "") && segs[1]) return "YouTube"; return null; } if (host.endsWith("rumble.com")) { return /^v[0-9a-z]+-/i.test(segs[0] ?? "") || segs[0] === "embed" ? "Rumble" : null; } if (host.endsWith("odysee.com")) { return segs.length >= 2 && segs[1].includes(":") ? "Odysee" : null; } if (host.endsWith("twitch.tv")) { return segs.includes("videos") || segs.includes("clip") || host === "clips.twitch.tv" ? "Twitch" : null; } if (host.endsWith("kick.com")) { return segs.includes("video") || segs.includes("videos") || segs.includes("clips") ? "Kick" : null; } if (host.endsWith("bitchute.com")) { return segs[0] === "video" || segs[0] === "embed" ? "BitChute" : null; } if (host === "archive.org") return segs[0] === "details" || segs[0] === "download" ? "archive.org" : null; return null; } const X_HOSTS = new Set(["x.com", "twitter.com", "mobile.twitter.com", "mobile.x.com"]); // Whether `url` is on X (x.com, twitter.com, mobile.twitter.com): a capture of // it follows `social.x.visibility` exactly as X posts do. export function isXHostUrl(url: string): boolean { try { return X_HOSTS.has(new URL(url).hostname.toLowerCase().replace(/^www\./, "")); } catch { return false; } } // Whether `url` is on Instagram: a capture of it follows // `social.instagram.visibility` exactly as Instagram posts do. export function isInstagramHostUrl(url: string): boolean { try { return new URL(url).hostname.toLowerCase().replace(/^www\./, "") === "instagram.com"; } catch { return false; } } // Whether a target URL (scheme optional) is on Instagram. export function isInstagramTargetUrl(url: string): boolean { return isInstagramHostUrl(/^https?:\/\//i.test(url) ? url : `https://${url.replace(/^\*\./, "")}`); } // Whether a target URL (scheme optional) is on X. export function isXTargetUrl(url: string): boolean { return isXHostUrl(/^https?:\/\//i.test(url) ? url : `https://${url.replace(/^\*\./, "")}`); } // Whether `url` is a Wayback Machine URL (a capture or a listing) of something // that is NOT a video: what the New channel form makes a Wayback pages channel // of. A capture of a video is import-video's. export function isWaybackPagesUrl(url: string): boolean { let u: URL; try { u = new URL(url.trim()); } catch { return false; } // Release 37 lane F: an archive.today copy is an archived page too (a // short id names no page; one that names a video is import-video's). if (isArchiveTodayHost(u.hostname)) { const ref = parseArchiveTodayUrl(url); return ref !== null && (!ref.originalUrl || videoPageOf(ref.originalUrl) === null); } if (!isWaybackHost(u.hostname)) return false; const m = /^\/(?:web\/)?\d{0,14}\*?(?:[a-z]{2}_)?\/(.+)$/s.exec(u.pathname + u.search); if (!m) return false; let inner = m[1].replace(/\*+$/, "").replace(/^\*\./, ""); inner = inner.replace(/^(https?):\/(?!\/)/i, "$1://"); if (!/^https?:\/\//i.test(inner)) inner = `http://${inner}`; return videoPageOf(inner) === null; }