diff --git a/VERSION.txt b/VERSION.txt index fd540e9..e31f30e 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -20250807215120 \ No newline at end of file +20250807220314 \ No newline at end of file diff --git a/src/app/api/douban/categories/route.ts b/src/app/api/douban/categories/route.ts index ea85bc1..23c345c 100644 --- a/src/app/api/douban/categories/route.ts +++ b/src/app/api/douban/categories/route.ts @@ -1,6 +1,7 @@ import { NextResponse } from 'next/server'; import { getCacheTime } from '@/lib/config'; +import { fetchDoubanData } from '@/lib/douban'; import { DoubanItem, DoubanResult } from '@/lib/types'; interface DoubanCategoryApiResponse { @@ -19,41 +20,6 @@ interface DoubanCategoryApiResponse { }>; } -async function fetchDoubanData( - url: string -): Promise { - // 添加超时控制 - const controller = new AbortController(); - const timeoutId = setTimeout(() => controller.abort(), 10000); // 10秒超时 - - // 设置请求选项,包括信号和头部 - const fetchOptions = { - signal: controller.signal, - headers: { - 'User-Agent': - 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36', - Referer: 'https://movie.douban.com/', - Accept: 'application/json, text/plain, */*', - Origin: 'https://movie.douban.com', - }, - }; - - try { - // 尝试直接访问豆瓣API - const response = await fetch(url, fetchOptions); - clearTimeout(timeoutId); - - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } - - return await response.json(); - } catch (error) { - clearTimeout(timeoutId); - throw error; - } -} - export const runtime = 'edge'; export async function GET(request: Request) { @@ -99,7 +65,7 @@ export async function GET(request: Request) { try { // 调用豆瓣 API - const doubanData = await fetchDoubanData(target); + const doubanData = await fetchDoubanData(target); // 转换数据格式 const list: DoubanItem[] = doubanData.items.map((item) => ({ diff --git a/src/app/api/douban/route.ts b/src/app/api/douban/route.ts index e9eee27..3e82039 100644 --- a/src/app/api/douban/route.ts +++ b/src/app/api/douban/route.ts @@ -1,6 +1,7 @@ import { NextResponse } from 'next/server'; import { getCacheTime } from '@/lib/config'; +import { fetchDoubanData } from '@/lib/douban'; import { DoubanItem, DoubanResult } from '@/lib/types'; interface DoubanApiResponse { @@ -12,38 +13,6 @@ interface DoubanApiResponse { }>; } -async function fetchDoubanData(url: string): Promise { - // 添加超时控制 - const controller = new AbortController(); - const timeoutId = setTimeout(() => controller.abort(), 10000); // 10秒超时 - - // 设置请求选项,包括信号和头部 - const fetchOptions = { - signal: controller.signal, - headers: { - 'User-Agent': - 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36', - Referer: 'https://movie.douban.com/', - Accept: 'application/json, text/plain, */*', - }, - }; - - try { - // 尝试直接访问豆瓣API - const response = await fetch(url, fetchOptions); - clearTimeout(timeoutId); - - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } - - return await response.json(); - } catch (error) { - clearTimeout(timeoutId); - throw error; - } -} - export const runtime = 'edge'; export async function GET(request: Request) { @@ -92,7 +61,7 @@ export async function GET(request: Request) { try { // 调用豆瓣 API - const doubanData = await fetchDoubanData(target); + const doubanData = await fetchDoubanData(target); // 转换数据格式 const list: DoubanItem[] = doubanData.subjects.map((item) => ({ diff --git a/src/lib/douban.ts b/src/lib/douban.ts new file mode 100644 index 0000000..ee5e8f4 --- /dev/null +++ b/src/lib/douban.ts @@ -0,0 +1,52 @@ +/** + * 通用的豆瓣数据获取函数 + * @param url 请求的URL + * @returns Promise 返回指定类型的数据 + */ +export async function fetchDoubanData(url: string): Promise { + // 添加超时控制 + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 10000); // 10秒超时 + + // 设置请求选项,包括信号和头部 + const fetchOptions = { + signal: controller.signal, + headers: { + 'User-Agent': + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36', + Referer: 'https://movie.douban.com/', + Accept: 'application/json, text/plain, */*', + Origin: 'https://movie.douban.com', + }, + }; + + try { + // 尝试直接访问豆瓣API + const response = await fetch(url, fetchOptions); + clearTimeout(timeoutId); + + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + return await response.json(); + } catch (error) { + const fallbackUrl = `https://api.allorigins.win/get?url=${encodeURIComponent( + url + )}`; + try { + const fallbackResponse = await fetch(fallbackUrl, fetchOptions); + if (!fallbackResponse.ok) { + throw new Error(`HTTP error! Status: ${fallbackResponse.status}`); + } + const fallbackData = await fallbackResponse.json(); + if (fallbackData && fallbackData.contents) { + return JSON.parse(fallbackData.contents); + } + throw new Error('Invalid fallback response'); + } catch (fallbackError) { + clearTimeout(timeoutId); + throw fallbackError; + } + } +} diff --git a/src/lib/version.ts b/src/lib/version.ts index 9f95a8c..71c690f 100644 --- a/src/lib/version.ts +++ b/src/lib/version.ts @@ -2,7 +2,7 @@ 'use client'; -const CURRENT_VERSION = '20250807215120'; +const CURRENT_VERSION = '20250807220314'; // 版本检查结果枚举 export enum UpdateStatus {