52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
export const runtime = 'nodejs'
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { scrapeDouyin, ScrapeError } from '.';
|
|
|
|
async function handleDouyinScrape(req: NextRequest) {
|
|
const { searchParams } = new URL(req.url);
|
|
const videoUrl = searchParams.get('url');
|
|
|
|
if (!videoUrl) {
|
|
return NextResponse.json(
|
|
{ error: '缺少视频URL', code: 'MISSING_URL' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
// 调用爬虫函数
|
|
const result = await scrapeDouyin(videoUrl);
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: result
|
|
});
|
|
} catch (error) {
|
|
// 处理自定义的 ScrapeError
|
|
if (error instanceof ScrapeError) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: error.message,
|
|
code: error.code
|
|
},
|
|
{ status: error.statusCode }
|
|
);
|
|
}
|
|
|
|
// 处理未知错误
|
|
console.error('未捕获的错误:', error);
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: '服务器内部错误',
|
|
code: 'INTERNAL_ERROR'
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export const GET = handleDouyinScrape
|