// SPDX-License-Identifier: Apache-2.0 import { DRAGONBALL_SOURCES, dragonBallUrlFromOrigin, } from "../sources/dragonball.ts"; export const DRAGONBALLFR_ORIGIN = DRAGONBALL_SOURCES.shenronSite.origin; export const SHENRON_API_ORIGIN = DRAGONBALL_SOURCES.shenronApi.origin; export type DragonBallFrLinkKind = "episode" | "movie"; export interface DragonBallFrTrainingLink { kind: DragonBallFrLinkKind; id: number; slug?: string; title: string; series: string; pageUrl: string; apiUrl: string; } interface EpisodeListItem { id: number; title: string; series: string; } interface EpisodeListResponse { episodes: EpisodeListItem[]; total: number; } interface MovieListItem { id: number; slug: string; title: string; series: string; } interface MovieListResponse { movies: MovieListItem[]; } export interface ResolveDragonBallFrOptions { fetch?: typeof globalThis.fetch; apiOrigin?: string; siteOrigin?: string; pageSize?: number; } async function fetchJson( fetcher: typeof globalThis.fetch, url: string, ): Promise { const response = await fetcher(url, { headers: { accept: "application/json" }, }); if (!response.ok) throw new Error(`Shenron API ${response.status}: ${url}`); return (await response.json()) as T; } export async function resolveDragonBallFrTrainingLinks( options: ResolveDragonBallFrOptions = {}, ): Promise { const fetcher = options.fetch ?? globalThis.fetch; const apiOrigin = (options.apiOrigin ?? SHENRON_API_ORIGIN).replace( /\/$/, "", ); const siteOrigin = (options.siteOrigin ?? DRAGONBALLFR_ORIGIN).replace( /\/$/, "", ); const pageSize = Math.min(200, Math.max(1, options.pageSize ?? 200)); const episodes: EpisodeListItem[] = []; for ( let offset = 0, total = Number.POSITIVE_INFINITY; offset < total; offset += pageSize ) { const url = dragonBallUrlFromOrigin( apiOrigin, `/api/public/wiki/episodes?limit=${pageSize}&offset=${offset}`, ); const page = await fetchJson(fetcher, url); total = page.total; episodes.push(...page.episodes); if (page.episodes.length === 0) break; } const movieUrl = dragonBallUrlFromOrigin(apiOrigin, "/api/public/wiki/movies?limit=200&offset=0"); const { movies } = await fetchJson(fetcher, movieUrl); const links: DragonBallFrTrainingLink[] = [ ...episodes.map((episode) => ({ kind: "episode" as const, id: episode.id, title: episode.title, series: episode.series, pageUrl: dragonBallUrlFromOrigin(siteOrigin, `/wiki/episodes/${episode.id}`), apiUrl: dragonBallUrlFromOrigin(apiOrigin, `/api/public/wiki/episodes/${episode.id}`), })), ...movies.map((movie) => ({ kind: "movie" as const, id: movie.id, slug: movie.slug, title: movie.title, series: movie.series, pageUrl: dragonBallUrlFromOrigin(siteOrigin, `/wiki/films/${movie.slug}`), apiUrl: dragonBallUrlFromOrigin(apiOrigin, `/api/public/wiki/movies/${movie.slug}`), })), ]; const unique = new Map(links.map((link) => [link.pageUrl, link])); if (unique.size !== links.length) throw new Error("Shenron API returned duplicate wiki links"); return [...unique.values()]; } export function extractDragonBallFrWikiLinks(html: string): string[] { const links = html.matchAll( /href=["'](\/wiki\/(?:episodes\/\d+|films\/[a-z0-9-]+))["']/g, ); return [ ...new Set([...links].map((match) => dragonBallUrlFromOrigin(DRAGONBALLFR_ORIGIN, match[1]))), ].sort(); }