Docker Build & Test / build (push) Canceled after 0s
Docker Build & Test / test (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/amd64) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm/v7) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm64) (push) Canceled after 0s
Build & Push Docker image / merge-images (push) Canceled after 0s
Release / release (push) Canceled after 0s
Release / docker (push) Canceled after 0s
162 lines
7.2 KiB
TypeScript
162 lines
7.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
||
|
||
import { getConfig } from '@/lib/config';
|
||
|
||
// 强制使用 Edge Runtime 以支持 Cloudflare Pages
|
||
export const runtime = 'edge';
|
||
|
||
// ---------- /api/tvbox/all 全源聚合(源码化,原运行时补丁 app-api-tvbox-route.js 的 ALL) ----------
|
||
|
||
const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36';
|
||
|
||
interface SrcCfg { key: string; name: string; api: string; detail?: string; disabled?: boolean }
|
||
|
||
async function subFetch(api: string, query: string): Promise<any> {
|
||
try {
|
||
const ctrl = new AbortController();
|
||
const t = setTimeout(() => ctrl.abort(), 8000);
|
||
const r = await fetch(api + (api.includes('?') ? '&' : '?') + query, { headers: { 'User-Agent': UA, Accept: 'application/json' }, signal: ctrl.signal });
|
||
clearTimeout(t);
|
||
if (!r.ok) return null;
|
||
return await r.json();
|
||
} catch { return null; }
|
||
}
|
||
|
||
function ns(x: any, key: string) { return Object.assign({}, x, { vod_id: String(key) + ':' + String(x.vod_id) }); }
|
||
|
||
// 源站 class 缓存:顶层 9xx → 源站真实 type_id(旧补丁 P() 的 tops 映射逻辑)
|
||
const classCache = new Map<string, [number, any[]]>();
|
||
async function fetchClass(api: string): Promise<any[]> {
|
||
const now = Date.now();
|
||
const c = classCache.get(api);
|
||
if (c && now - c[0] < 600000) return c[1];
|
||
for (const ep of [api, api + (api.includes('?') ? '&' : '?') + 'ac=list']) {
|
||
try {
|
||
const ctrl = new AbortController();
|
||
const t = setTimeout(() => ctrl.abort(), 8000);
|
||
const r = await fetch(ep, { headers: { 'User-Agent': UA, Accept: 'application/json' }, signal: ctrl.signal });
|
||
clearTimeout(t);
|
||
if (!r.ok) continue;
|
||
const j = await r.json();
|
||
if (Array.isArray(j?.class) && j.class.length) {
|
||
classCache.set(api, [now, j.class]);
|
||
return j.class;
|
||
}
|
||
} catch {}
|
||
}
|
||
classCache.set(api, [now, []]);
|
||
return [];
|
||
}
|
||
|
||
// t=901-907(顶层分类)→ 每个源各自的真实 type_id(各源分类表不同,不能拿第一个源的 id 发给全源)
|
||
// t=902(香港剧):源站有“香港剧”类目则直取;没有则取电视剧类目 + area=香港 过滤,保证只出香港剧
|
||
async function resolveTopPerSource(sources: SrcCfg[], sp: URLSearchParams): Promise<{ extraArea: string; nativeByApi: Map<string, string> }> {
|
||
const t = sp.get('t');
|
||
const nativeByApi = new Map<string, string>();
|
||
if (!t || !/^90[1-9]$/.test(t) || sp.get('wd') || sp.get('ids')) return { extraArea: '', nativeByApi };
|
||
const classes = await Promise.all(sources.map((s) => fetchClass(s.api)));
|
||
sources.forEach((s, i) => {
|
||
const byN: Record<string, any> = {};
|
||
(classes[i] || []).forEach((c) => { if (c && c.type_name != null) byN[String(c.type_name).trim()] = c; });
|
||
const fd = (...names: string[]) => { for (const n of names) if (byN[n]) return byN[n]; return null; };
|
||
const mv = fd('电影', '电影片');
|
||
const tv = fd('连续剧', '电视剧', '剧集');
|
||
const vy = fd('综艺', '综艺片');
|
||
const an = fd('动漫', '动漫片');
|
||
const to = fd('动画片', '动画电影', '动漫电影');
|
||
const hk = fd('香港剧', '港澳剧', '港台剧', '港剧');
|
||
let hit = null;
|
||
if (t === '901') hit = mv;
|
||
else if (t === '902') hit = hk || tv;
|
||
else if (t === '903') hit = mv;
|
||
else if (t === '904') hit = tv;
|
||
else if (t === '905') hit = vy;
|
||
else if (t === '906') hit = to;
|
||
else if (t === '907') hit = an;
|
||
if (hit) nativeByApi.set(s.api, String(hit.type_id));
|
||
});
|
||
if (t === '901' || t === '902') return { extraArea: '香港', nativeByApi };
|
||
return { extraArea: '', nativeByApi };
|
||
}
|
||
|
||
async function aggregateAll(sources: SrcCfg[], sp: URLSearchParams) {
|
||
const srcs = sources.filter((s) => !s.disabled);
|
||
const out: any = { code: 1, msg: '数据列表', page: 1, pagecount: 1, limit: 0, total: 0, list: [] };
|
||
|
||
const ids = sp.get('ids');
|
||
const wd = sp.get('wd');
|
||
const t = sp.get('t');
|
||
const pg = sp.get('pg') || '1';
|
||
|
||
if (ids) {
|
||
const byK: Record<string, string[]> = {};
|
||
ids.split(',').forEach((p) => {
|
||
const i = p.indexOf(':');
|
||
if (i > 0) { const k = p.slice(0, i); const id = p.slice(i + 1); (byK[k] = byK[k] || []).push(id); }
|
||
});
|
||
for (const k in byK) {
|
||
const d = await subFetch(srcs.find((s) => s.key === k)?.api || '', 'ac=videolist&ids=' + byK[k].map(encodeURIComponent).join(','));
|
||
if (d && Array.isArray(d.list)) d.list.forEach((x: any) => out.list.push(ns(x, k)));
|
||
}
|
||
out.total = out.list.length; out.limit = out.list.length;
|
||
return out;
|
||
}
|
||
|
||
// 顶层 9xx 分类:每个源用自己的真实 type_id 查询(旧补丁 P() tops 逻辑,源码化时按源分别映射)
|
||
const topT = sp.get('t');
|
||
const isTop = topT && /^90[1-9]$/.test(topT) && !wd && !ids;
|
||
const { extraArea, nativeByApi } = isTop
|
||
? await resolveTopPerSource(srcs, sp)
|
||
: { extraArea: '', nativeByApi: new Map<string, string>() };
|
||
const baseExtra = ['st', 'year', 'area', 'by'].map((k) => { const v = sp.get(k); return v ? '&' + k + '=' + encodeURIComponent(v) : ''; }).join('');
|
||
const queries = srcs.map((s) => {
|
||
let q = 'ac=videolist&pg=' + pg;
|
||
if (wd) q += '&wd=' + encodeURIComponent(wd);
|
||
const nt = isTop ? nativeByApi.get(s.api) : topT;
|
||
if (nt) q += '&t=' + encodeURIComponent(nt);
|
||
q += baseExtra;
|
||
return q;
|
||
});
|
||
|
||
const results = await Promise.all(srcs.map((s, i) => subFetch(s.api, queries[i])));
|
||
const seen: Record<string, number> = {};
|
||
let list: any[] = [];
|
||
results.forEach((d, i) => {
|
||
if (d && Array.isArray(d.list)) {
|
||
const key = srcs[i].key;
|
||
d.list.forEach((x: any) => {
|
||
const nm = String(x.vod_name || '').trim();
|
||
if (nm && !seen[nm]) { seen[nm] = 1; list.push(ns(x, key)); }
|
||
});
|
||
}
|
||
});
|
||
list.sort((a, b) => String(b.vod_time == null ? '' : b.vod_time).localeCompare(String(a.vod_time == null ? '' : a.vod_time)));
|
||
// 902 香港剧硬性过滤:只保留地区含“香港”的条目(兜底:无地区但名字命中的保留,避免误杀)
|
||
if (topT === '902') {
|
||
const hkPat = /港剧|粤语|TVB|邵氏|香港/i;
|
||
list = list.filter((x) => {
|
||
const area = String(x.vod_area || '');
|
||
if (area.indexOf('香港') >= 0 || area === '香港') return true;
|
||
if (!area && hkPat.test(String(x.vod_name || ''))) return true;
|
||
return false;
|
||
});
|
||
}
|
||
const fy = sp.get('year');
|
||
const fa = sp.get('area') || extraArea;
|
||
if (fy) list = list.filter((x) => String(x.vod_year || '').indexOf(fy) >= 0);
|
||
if (fa && topT !== '902') list = list.filter((x) => String(x.vod_area || '').indexOf(fa) >= 0);
|
||
list = list.slice(0, 300);
|
||
out.list = list; out.total = list.length; out.limit = list.length;
|
||
const rep = results.find((d) => d && Array.isArray(d.class) && d.class.length);
|
||
if (rep) { out.class = rep.class; out.filters = rep.filters; }
|
||
return out;
|
||
}
|
||
|
||
export async function GET(request: NextRequest) {
|
||
const { searchParams } = new URL(request.url);
|
||
const config = await getConfig();
|
||
const sourceConfigs: SrcCfg[] = (config.SourceConfig || []).filter((s: any) => !s.disabled);
|
||
const r = await aggregateAll(sourceConfigs, searchParams);
|
||
return NextResponse.json(r, { headers: { 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'public, max-age=60' } });
|
||
}
|