import { VoiranimeScraper } from "@aphrody/voiranime"; import { resolveEmbed } from "../media/resolver.ts"; import type { MediaTransport } from "../media/types.ts"; import { type CommonOptions, EXIT, logger } from "./shared.ts"; interface CliOptions extends CommonOptions { action: "search" | "info" | "resolve"; param: string; profile: "static" | "fast" | "http" | "stealth" | "max"; } const directTransport: MediaTransport = async (request) => { const response = await fetch(request.url, { method: request.method ?? "GET", headers: { ...request.headers, ...(request.referer ? { Referer: request.referer } : {}), }, body: request.body, signal: request.timeoutMs ? AbortSignal.timeout(request.timeoutMs) : undefined, redirect: "follow", }); return { status: response.status, body: await response.text(), url: response.url, }; }; function printUsage(): void { Bun.stdout.write( `bxc voiranime — VoirAnime streaming scraper Usage: bxc voiranime search Search for anime on VoirAnime (e.g. "inazuma") bxc voiranime info Get metadata and episodes list of an anime bxc voiranime resolve Resolve a video embed to its direct streaming link Options: --profile static (default) | http | fast | stealth | max --help, -h this help `, ); } function parseArgs( argv: readonly string[], baseOpts: CommonOptions, ): CliOptions | null { const opts: CliOptions = { ...baseOpts, action: "search", param: "", // Même constat que pour anime-sama : voir-anime est du WordPress rendu // côté serveur, et les profils navigateur n'en rapportent que le shell // pré-JS (37 Ko et zéro ancre, contre 212 Ko et 112 ancres en HTTP // direct). Un navigateur ne gagne rien ici et coûte un Chromium. profile: "static", }; const actionStr = argv[0]; if (actionStr === "search") opts.action = "search"; else if (actionStr === "info") opts.action = "info"; else if (actionStr === "resolve") opts.action = "resolve"; else { logger.error(`Unknown action: ${actionStr}`); return null; } const positional: string[] = []; for (let i = 1; i < argv.length; i++) { const a = argv[i]; switch (a) { case "--profile": { const v = argv[++i] as any; if ( v !== "static" && v !== "fast" && v !== "http" && v !== "stealth" && v !== "max" ) { logger.error(`Invalid profile: ${v}`); return null; } opts.profile = v; break; } case "--help": case "-h": return null; default: if (!a.startsWith("-")) positional.push(a); } } if (positional.length < 1) { logger.error("requires query/slug/URL argument"); return null; } opts.param = positional.join(" "); return opts; } export async function main( argv: readonly string[], baseOpts: CommonOptions, ): Promise { const opts = parseArgs(argv, baseOpts); if (!opts) { printUsage(); process.exit(EXIT.MISUSE); } const scraper = new VoiranimeScraper({ profile: opts.profile, timeoutMs: opts.timeoutMs, }); try { if (opts.action === "search") { const results = await scraper.search(opts.param); Bun.stdout.write(JSON.stringify(results, null, 2) + "\n"); } else if (opts.action === "info") { const info = await scraper.getAnime(opts.param); Bun.stdout.write(JSON.stringify(info, null, 2) + "\n"); } else { let source = await scraper.resolveSource(opts.param, { enumerateQualities: true, }); if (!source.url && /mail\.ru/i.test(opts.param)) { const media = await resolveEmbed(opts.param, { transport: directTransport, referer: "https://voiranime.com/", timeoutMs: opts.timeoutMs, }); source = { provider: media.host, embedUrl: media.embedUrl, type: media.kind, url: media.url, poster: media.poster, headers: media.headers, error: media.error ?? null, }; } Bun.stdout.write(JSON.stringify(source, null, 2) + "\n"); } } catch (err) { logger.error(err instanceof Error ? err.message : String(err)); process.exit(EXIT.DATA_ERR); } finally { await scraper.close().catch(() => {}); } }