import { NextRequest, NextResponse } from 'next/server'; import { getAvailableApiSites, getCacheTime } from '@/lib/config'; import { fetchSiteKind, mapItem, Site } from '@/lib/vodcat'; // ---------- /api/anime 动漫聚合接口(源码化) ---------- // 数据来源:各第三方采集站(maccms 10 协议),非豆瓣精选 // 逻辑:按「动漫/动画/卡通」分类名跨站筛分类 -> 逐类取分页 -> 合并去重(全量) // 参数:region('' | 国产 | 日韩 | 欧美 | 港台 | 海外)、pg const EXCLUDE_SITES = new Set(['xiaojizy', '155api']); // 地区 -> 各站分类名里的关键词(国产动漫 / 中国动漫 / 国产动漫…) const REGION_MAP: Record = { 国产: '国产', 日韩: '日韩', 欧美: '欧美', 港台: '港台', 海外: '海外', }; export async function GET(request: NextRequest) { const { searchParams } = new URL(request.url); const region = searchParams.get('region') || ''; const pg = Math.max(1, parseInt(searchParams.get('pg') || '1', 10)); try { const all = await getAvailableApiSites(true); const sites = all.filter( (s) => !EXCLUDE_SITES.has(s.key) && !String(s.name || '').startsWith('AV-') ) as unknown as Site[]; const cacheTime = await getCacheTime(); const cat = REGION_MAP[region] || ''; const settled = await Promise.all( sites.map(async (s) => { try { return await fetchSiteKind(s, 'anime', cat, pg); } catch { return { items: [] as any[], hasMore: false }; } }) ); const seen = new Set(); const list: any[] = []; let hasMore = false; settled.forEach((r, i) => { if (r.hasMore) hasMore = true; const site = sites[i]; r.items.forEach((it) => { const m = mapItem(it, site); if (!m.title) return; const k = `${m.title}|${m.year}`; if (seen.has(k)) return; seen.add(k); list.push(m); }); }); return NextResponse.json( { source: 'all', region, pg, list, next_pg: pg + 1, has_more: hasMore }, { headers: { 'Cache-Control': `public, max-age=${Math.min(cacheTime, 300)}, s-maxage=${Math.min(cacheTime, 300)}`, }, } ); } catch (e: any) { return NextResponse.json( { error: '动漫聚合请求失败', details: e?.message || String(e) }, { status: 500 } ); } } export async function OPTIONS() { return new NextResponse(null, { status: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', }, }); }