import { NextRequest, NextResponse } from 'next/server'; import { getAvailableApiSites, getCacheTime } from '@/lib/config'; import { fetchSiteKind, mapItem, Site } from '@/lib/vodcat'; // ---------- /api/browse 第三方分类聚合接口(源码化) ---------- // 数据来源:各第三方采集站(maccms 10 协议),按分类名跨站聚合,全部数据而非精选 // 参数:kind=tv|show|movie|anime(剧集/综艺/电影/动漫)、cat=细分分类名(可空)、pg=页码 const EXCLUDE_SITES = new Set(['xiaojizy', '155api']); export async function GET(request: NextRequest) { const { searchParams } = new URL(request.url); const kind = (searchParams.get('kind') || 'tv') as string; const cat = searchParams.get('cat') || ''; const area = searchParams.get('area') || ''; 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 settled = await Promise.all( sites.map(async (s) => { try { return await fetchSiteKind(s, kind, cat, pg); } catch { return { items: [] as any[], hasMore: false }; } }) ); // area 支持多选('中国|大陆|香港'),命中任一即保留 const areaKeys = area ? area.split('|').filter(Boolean) : []; 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; if (areaKeys.length) { const ar = m.area || ''; if (!ar || !areaKeys.some((k) => ar.includes(k))) return; } const k = `${m.title}|${m.year}`; if (seen.has(k)) return; seen.add(k); list.push(m); }); }); return NextResponse.json( { kind, cat, area, 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', }, }); }