修复豆瓣推荐截断问题
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
import * as cheerio from 'cheerio';
|
import * as cheerio from 'cheerio';
|
||||||
|
import { fetchDoubanData } from '@/lib/douban';
|
||||||
|
|
||||||
export const runtime = 'nodejs';
|
export const runtime = 'nodejs';
|
||||||
|
|
||||||
@@ -10,6 +11,12 @@ interface DoubanRecommendation {
|
|||||||
rating: string;
|
rating: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface DoubanDetailApiResponse {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const doubanId = searchParams.get('id');
|
const doubanId = searchParams.get('id');
|
||||||
@@ -84,9 +91,44 @@ export async function GET(request: NextRequest) {
|
|||||||
|
|
||||||
console.log('解析到推荐数:', recommendations.length);
|
console.log('解析到推荐数:', recommendations.length);
|
||||||
|
|
||||||
|
// 处理标题截断问题
|
||||||
|
const processedRecommendations: DoubanRecommendation[] = [];
|
||||||
|
|
||||||
|
for (const rec of recommendations) {
|
||||||
|
// 检查标题是否被截断(包含三个点)
|
||||||
|
if (rec.title.includes('...')) {
|
||||||
|
console.log(`检测到截断标题: ${rec.title}, ID: ${rec.doubanId}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 调用豆瓣详情接口获取完整名称
|
||||||
|
const detailUrl = `https://m.douban.com/rexxar/api/v2/subject/${rec.doubanId}`;
|
||||||
|
const detailData = await fetchDoubanData<DoubanDetailApiResponse>(detailUrl);
|
||||||
|
|
||||||
|
if (detailData && detailData.title) {
|
||||||
|
console.log(`成功获取完整标题: ${detailData.title}`);
|
||||||
|
processedRecommendations.push({
|
||||||
|
...rec,
|
||||||
|
title: detailData.title,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log(`详情接口未返回标题,移除该视频: ${rec.doubanId}`);
|
||||||
|
// 补充失败,不添加到结果中
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`获取完整标题失败,移除该视频: ${rec.doubanId}`, error);
|
||||||
|
// 补充失败,不添加到结果中
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 标题正常,直接添加
|
||||||
|
processedRecommendations.push(rec);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('处理后的推荐数:', processedRecommendations.length);
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
recommendations,
|
recommendations: processedRecommendations,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
Reference in New Issue
Block a user