// The Wayback pages channel's live panel's state (release 37 S5, // components/WaybackLivePanel.tsx), from its page records and the // availability sidecar: one row per original URL. import type { Post, PostAvailabilityMap } from "yt-dlp-transcript-common/lib/posts"; import { groupByOriginalUrl } from "yt-dlp-transcript-common/lib/waybackPages/liveness"; import type { WaybackLiveRow, WaybackLiveState } from "./components/WaybackLivePanel"; const ROWS = 20; export function waybackLiveState( records: ReadonlyArray, availability: PostAvailabilityMap, spn: WaybackLiveState["spn"], ): WaybackLiveState { const groups = groupByOriginalUrl(records); const byId = new Map(records.map((r) => [r.id, r])); let checked = 0; let gone = 0; let unavailable = 0; const goneRows: WaybackLiveRow[] = []; const liveRows: WaybackLiveRow[] = []; for (const [url, ids] of groups) { // The newest capture of the page stands for it. const newest = ids .map((id) => byId.get(id)!) .sort((a, b) => b.page!.captureTs.padEnd(14, "0").localeCompare(a.page!.captureTs.padEnd(14, "0")))[0]!; const av = ids.map((id) => availability[id]).find(Boolean); const row: WaybackLiveRow = { id: newest.id, title: newest.page!.title || newest.text.split("\n")[0]!.slice(0, 120) || url, originalUrl: url, waybackUrl: newest.page!.waybackUrl, ...(av ? { availability: av.availability, checkedAt: av.checkedAt, ...(av.reason ? { reason: av.reason } : {}) } : {}), }; if (av) checked++; if (av?.availability === "deleted") gone++; if (av?.availability === "account_unavailable") unavailable++; if (av && (av.availability === "deleted" || av.availability === "account_unavailable")) goneRows.push(row); else if (av?.availability === "available") liveRows.push(row); } return { originals: groups.size, checked, gone, unavailable, goneRows: goneRows.slice(0, ROWS), liveRows: liveRows.slice(0, ROWS), spn, }; }