视频源权重

This commit is contained in:
mtvpls
2026-01-28 18:21:08 +08:00
parent 7c62e1612c
commit ada7d12fa3
6 changed files with 151 additions and 9 deletions

View File

@@ -9,7 +9,7 @@ import { db } from '@/lib/db';
export const runtime = 'nodejs';
// 支持的操作类型
type Action = 'add' | 'disable' | 'enable' | 'delete' | 'sort' | 'batch_disable' | 'batch_enable' | 'batch_delete' | 'toggle_proxy_mode';
type Action = 'add' | 'disable' | 'enable' | 'delete' | 'sort' | 'batch_disable' | 'batch_enable' | 'batch_delete' | 'toggle_proxy_mode' | 'update_weight';
interface BaseBody {
action?: Action;
@@ -37,7 +37,7 @@ export async function POST(request: NextRequest) {
const username = authInfo.username;
// 基础校验
const ACTIONS: Action[] = ['add', 'disable', 'enable', 'delete', 'sort', 'batch_disable', 'batch_enable', 'batch_delete', 'toggle_proxy_mode'];
const ACTIONS: Action[] = ['add', 'disable', 'enable', 'delete', 'sort', 'batch_disable', 'batch_enable', 'batch_delete', 'toggle_proxy_mode', 'update_weight'];
if (!username || !action || !ACTIONS.includes(action)) {
return NextResponse.json({ error: '参数格式错误' }, { status: 400 });
}
@@ -254,6 +254,20 @@ export async function POST(request: NextRequest) {
entry.proxyMode = !entry.proxyMode;
break;
}
case 'update_weight': {
const { key, weight } = body as { key?: string; weight?: number };
if (!key)
return NextResponse.json({ error: '缺少 key 参数' }, { status: 400 });
if (weight === undefined || weight === null)
return NextResponse.json({ error: '缺少 weight 参数' }, { status: 400 });
if (typeof weight !== 'number' || weight < 0 || weight > 100)
return NextResponse.json({ error: '权重必须是 0-100 之间的数字' }, { status: 400 });
const entry = adminConfig.SourceConfig.find((s) => s.key === key);
if (!entry)
return NextResponse.json({ error: '源不存在' }, { status: 404 });
entry.weight = weight;
break;
}
default:
return NextResponse.json({ error: '未知操作' }, { status: 400 });
}

View File

@@ -36,6 +36,12 @@ export async function GET(request: NextRequest) {
const config = await getConfig();
const apiSites = await getAvailableApiSites(authInfo.username);
// 创建权重映射表
const weightMap = new Map<string, number>();
config.SourceConfig.forEach(source => {
weightMap.set(source.key, source.weight ?? 0);
});
// 检查是否配置了 OpenList
const hasOpenList = !!(
config.OpenListConfig?.Enabled &&
@@ -191,6 +197,14 @@ export async function GET(request: NextRequest) {
return !yellowWords.some((word: string) => typeName.includes(word));
});
}
// 按权重降序排序
flattenedResults.sort((a, b) => {
const weightA = weightMap.get(a.source) ?? 0;
const weightB = weightMap.get(b.source) ?? 0;
return weightB - weightA;
});
const cacheTime = await getCacheTime();
if (flattenedResults.length === 0) {

View File

@@ -33,6 +33,19 @@ export async function GET(request: NextRequest) {
const config = await getConfig();
const apiSites = await getAvailableApiSites(authInfo.username);
// 创建权重映射表
const weightMap = new Map<string, number>();
config.SourceConfig.forEach(source => {
weightMap.set(source.key, source.weight ?? 0);
});
// 按权重降序排序 apiSites
const sortedApiSites = [...apiSites].sort((a, b) => {
const weightA = weightMap.get(a.key) ?? 0;
const weightB = weightMap.get(b.key) ?? 0;
return weightB - weightA;
});
// 检查是否配置了 OpenList
const hasOpenList = !!(
config.OpenListConfig?.Enabled &&
@@ -89,7 +102,7 @@ export async function GET(request: NextRequest) {
const startEvent = `data: ${JSON.stringify({
type: 'start',
query,
totalSources: apiSites.length + (hasOpenList ? 1 : 0) + embySourcesCount,
totalSources: sortedApiSites.length + (hasOpenList ? 1 : 0) + embySourcesCount,
timestamp: Date.now()
})}\n\n`;
@@ -294,7 +307,7 @@ export async function GET(request: NextRequest) {
}
// 为每个源创建搜索 Promise
const searchPromises = apiSites.map(async (site) => {
const searchPromises = sortedApiSites.map(async (site) => {
try {
// 添加超时控制
const searchPromise = Promise.race([
@@ -363,7 +376,7 @@ export async function GET(request: NextRequest) {
}
// 检查是否所有源都已完成
if (completedSources === apiSites.length + (hasOpenList ? 1 : 0) + embySourcesCount) {
if (completedSources === sortedApiSites.length + (hasOpenList ? 1 : 0) + embySourcesCount) {
if (!streamClosed) {
// 发送最终完成事件
const completeEvent = `data: ${JSON.stringify({