Repository Public
1import { fetchText } from "../ssrf";23// NASA Image and Video Library (images-api.nasa.gov, public API, no key). Videos are MP4 files read directly from4// NASA's servers. NASA media are generally not copyrighted (https://www.nasa.gov/nasa-brand-center/images-and-media/);5// items that credit a third party are skipped.6export const TOPICS: [string, string][] = [["apollo", "Programme Apollo"], ["artemis", "Artemis"], ["moon", "Lune"], ["mars", "Mars"], ["hubble", "Hubble"], ["webb telescope", "James Webb"], ["international space station tour", "Station spatiale"],7 ["space shuttle", "Navette spatiale"], ["earth from space", "La Terre vue de l'espace"], ["saturn", "Saturne"], ["jupiter", "Jupiter"], ["sun solar", "Le Soleil"], ["sciencecasts", "ScienceCasts"], ["nasa explorers", "NASA Explorers"], ["history", "Histoire de la NASA"], ["launch", "Lancements"]];8const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));9let last = 0;10async function json<T>(url: string): Promise<T> { const w = last + 250 - Date.now(); if (w > 0) await sleep(w); last = Date.now(); return JSON.parse(await fetchText(url, 8 * 1024 * 1024, 40_000)) as T; }1112export interface NasaItem { id: string; title: string; description: string | null; year: number | null; keywords: string[]; thumb: string | null; assets: string; topic: string }13interface Search { collection: { items: { href: string; data: { nasa_id: string; title: string; description?: string; date_created?: string; keywords?: string[]; secondary_creator?: string; photographer?: string }[]; links?: { href: string; rel: string; render?: string }[] }[] } }14/** A raw downlink file name is not a title anyone can browse. */15export const presentable = (title: string) => /\s/.test(title.trim()) && !/^[A-Za-z0-9]+[_-][A-Za-z0-9_-]{12,}$/.test(title.trim()) && title.trim().length >= 8;1617export async function search(query: string, topic: string, pages = 2): Promise<NasaItem[]> {18 const out: NasaItem[] = [];19 for (let page = 1; page <= pages; page++) {20 let d: Search; try { d = await json<Search>(`https://images-api.nasa.gov/search?media_type=video&page_size=100&page=${page}&q=${encodeURIComponent(query)}`); } catch { break; }21 for (const it of d.collection.items) {22 const m = it.data[0]; if (!m?.nasa_id || !m.title || !presentable(m.title)) continue;23 out.push({ id: m.nasa_id, title: m.title.trim(), description: m.description?.trim() || null, year: Number((m.date_created ?? "").slice(0, 4)) || null, keywords: m.keywords ?? [], topic,24 thumb: it.links?.find((l) => l.render === "image" || l.rel === "preview")?.href.replace(/ /g, "%20") ?? null, assets: it.href });25 }26 if (d.collection.items.length < 100) break;27 }28 return out;29}30export interface NasaFiles { video: string; height: number | null; subtitles: string | null }31/** Best browser-playable rendition (large → medium → orig) and its subtitle file, with clean HTTPS addresses. */32export async function files(assets: string): Promise<NasaFiles | null> {33 const list = (await json<string[]>(assets.replace(/ /g, "%20"))).map((u) => u.replace(/^http:/, "https:").replace(/ /g, "%20"));34 const pick = (tag: string) => list.find((u) => u.toLowerCase().endsWith(`~${tag}.mp4`));35 const video = pick("large") ?? pick("medium") ?? pick("orig") ?? pick("small") ?? pick("mobile"); if (!video) return null;36 return { video, height: /~large\.mp4$/i.test(video) ? 720 : /~medium\.mp4$/i.test(video) ? 480 : null, subtitles: list.find((u) => /\.(srt|vtt)$/i.test(u)) ?? null };37}38