新增弹幕功能

This commit is contained in:
mtvpls
2025-12-02 01:00:42 +08:00
parent 106e966931
commit 9bccf564ee
19 changed files with 2094 additions and 26 deletions

View File

@@ -39,6 +39,8 @@ export async function POST(request: NextRequest) {
DoubanImageProxy,
DisableYellowFilter,
FluidSearch,
DanmakuApiBase,
DanmakuApiToken,
} = body as {
SiteName: string;
Announcement: string;
@@ -50,6 +52,8 @@ export async function POST(request: NextRequest) {
DoubanImageProxy: string;
DisableYellowFilter: boolean;
FluidSearch: boolean;
DanmakuApiBase: string;
DanmakuApiToken: string;
};
// 参数校验
@@ -63,7 +67,9 @@ export async function POST(request: NextRequest) {
typeof DoubanImageProxyType !== 'string' ||
typeof DoubanImageProxy !== 'string' ||
typeof DisableYellowFilter !== 'boolean' ||
typeof FluidSearch !== 'boolean'
typeof FluidSearch !== 'boolean' ||
typeof DanmakuApiBase !== 'string' ||
typeof DanmakuApiToken !== 'string'
) {
return NextResponse.json({ error: '参数格式错误' }, { status: 400 });
}
@@ -93,6 +99,8 @@ export async function POST(request: NextRequest) {
DoubanImageProxy,
DisableYellowFilter,
FluidSearch,
DanmakuApiBase,
DanmakuApiToken,
};
// 写入数据库

View File

@@ -0,0 +1,102 @@
// 获取弹幕 API 路由
import { NextRequest, NextResponse } from 'next/server';
import { getConfig } from '@/lib/config';
export const runtime = 'nodejs';
// 解析弹幕 XML 为 JSON
function parseXmlDanmaku(xmlText: string): Array<{ p: string; m: string; cid: number }> {
const comments: Array<{ p: string; m: string; cid: number }> = [];
// 使用正则表达式提取所有 <d> 标签
const dTagRegex = /<d\s+p="([^"]+)"[^>]*>([^<]*)<\/d>/g;
let match;
while ((match = dTagRegex.exec(xmlText)) !== null) {
const p = match[1];
const m = match[2];
// 从 p 属性中提取 cid弹幕ID
const pParts = p.split(',');
const cid = pParts[7] ? parseInt(pParts[7]) : 0;
comments.push({
p,
m,
cid,
});
}
return comments;
}
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams;
const episodeId = searchParams.get('episodeId');
const url = searchParams.get('url');
// 至少需要一个参数
if (!episodeId && !url) {
return NextResponse.json(
{
count: 0,
comments: [],
},
{ status: 400 }
);
}
// 从数据库读取弹幕配置
const config = await getConfig();
const { DanmakuApiBase, DanmakuApiToken } = config.SiteConfig;
// 构建 API URL
const baseUrl =
DanmakuApiToken === '87654321'
? DanmakuApiBase
: `${DanmakuApiBase}/${DanmakuApiToken}`;
let apiUrl: string;
if (episodeId) {
// 通过剧集 ID 获取弹幕 - 使用 XML 格式
apiUrl = `${baseUrl}/api/v2/comment/${episodeId}?format=xml`;
} else {
// 通过视频 URL 获取弹幕 - 使用 XML 格式
apiUrl = `${baseUrl}/api/v2/comment?url=${encodeURIComponent(url!)}&format=xml`;
}
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Accept': 'application/xml, text/xml',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// 获取 XML 文本
const xmlText = await response.text();
// 解析 XML 为 JSON
const comments = parseXmlDanmaku(xmlText);
return NextResponse.json({
count: comments.length,
comments,
});
} catch (error) {
console.error('获取弹幕代理错误:', error);
return NextResponse.json(
{
count: 0,
comments: [],
},
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,72 @@
// 获取剧集列表 API 路由
import { NextRequest, NextResponse } from 'next/server';
import { getConfig } from '@/lib/config';
export const runtime = 'nodejs';
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams;
const animeId = searchParams.get('animeId');
if (!animeId) {
return NextResponse.json(
{
errorCode: -1,
success: false,
errorMessage: '缺少动漫ID参数',
bangumi: {
bangumiId: '',
animeTitle: '',
episodes: [],
},
},
{ status: 400 }
);
}
// 从数据库读取弹幕配置
const config = await getConfig();
const { DanmakuApiBase, DanmakuApiToken } = config.SiteConfig;
// 构建 API URL
const baseUrl =
DanmakuApiToken === '87654321'
? DanmakuApiBase
: `${DanmakuApiBase}/${DanmakuApiToken}`;
const apiUrl = `${baseUrl}/api/v2/bangumi/${animeId}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('获取剧集列表代理错误:', error);
return NextResponse.json(
{
errorCode: -1,
success: false,
errorMessage:
error instanceof Error ? error.message : '获取剧集列表失败',
bangumi: {
bangumiId: '',
animeTitle: '',
episodes: [],
},
},
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,66 @@
// 自动匹配 API 路由
import { NextRequest, NextResponse } from 'next/server';
import { getConfig } from '@/lib/config';
export const runtime = 'nodejs';
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { fileName } = body;
if (!fileName) {
return NextResponse.json(
{
errorCode: -1,
success: false,
errorMessage: '缺少文件名参数',
isMatched: false,
matches: [],
},
{ status: 400 }
);
}
// 从数据库读取弹幕配置
const config = await getConfig();
const { DanmakuApiBase, DanmakuApiToken } = config.SiteConfig;
// 构建 API URL
const baseUrl =
DanmakuApiToken === '87654321'
? DanmakuApiBase
: `${DanmakuApiBase}/${DanmakuApiToken}`;
const apiUrl = `${baseUrl}/api/v2/match`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ fileName }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('自动匹配代理错误:', error);
return NextResponse.json(
{
errorCode: -1,
success: false,
errorMessage: error instanceof Error ? error.message : '匹配失败',
isMatched: false,
matches: [],
},
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,63 @@
// 弹幕搜索 API 路由
import { NextRequest, NextResponse } from 'next/server';
import { getConfig } from '@/lib/config';
export const runtime = 'nodejs';
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams;
const keyword = searchParams.get('keyword');
if (!keyword) {
return NextResponse.json(
{
errorCode: -1,
success: false,
errorMessage: '缺少关键词参数',
animes: [],
},
{ status: 400 }
);
}
// 从数据库读取弹幕配置
const config = await getConfig();
const { DanmakuApiBase, DanmakuApiToken } = config.SiteConfig;
// 构建 API URL
const baseUrl =
DanmakuApiToken === '87654321'
? DanmakuApiBase
: `${DanmakuApiBase}/${DanmakuApiToken}`;
const apiUrl = `${baseUrl}/api/v2/search/anime?keyword=${encodeURIComponent(keyword)}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('弹幕搜索代理错误:', error);
return NextResponse.json(
{
errorCode: -1,
success: false,
errorMessage: error instanceof Error ? error.message : '搜索失败',
animes: [],
},
{ status: 500 }
);
}
}