tmdb兼容cf
This commit is contained in:
+41
-40
@@ -8,6 +8,43 @@ import { getNextApiKey } from './tmdb.client';
|
||||
// TMDB API 默认 Base URL(不包含 /3/,由程序拼接)
|
||||
const DEFAULT_TMDB_BASE_URL = 'https://api.themoviedb.org';
|
||||
|
||||
/**
|
||||
* 检测是否在 Cloudflare 环境中运行
|
||||
*/
|
||||
function isCloudflareEnvironment(): boolean {
|
||||
return process.env.CF_PAGES === '1' || process.env.BUILD_TARGET === 'cloudflare';
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一的 fetch 函数,根据环境选择使用 node-fetch 或原生 fetch
|
||||
*/
|
||||
async function universalFetch(url: string, proxy?: string): Promise<Response> {
|
||||
const isCloudflare = isCloudflareEnvironment();
|
||||
|
||||
if (isCloudflare) {
|
||||
// Cloudflare 环境:使用原生 fetch,忽略 proxy 参数
|
||||
const response = await fetch(url, {
|
||||
signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
return response as unknown as Response;
|
||||
} else {
|
||||
// Node.js 环境:使用 node-fetch,支持 proxy
|
||||
const fetchOptions: any = proxy
|
||||
? {
|
||||
agent: new HttpsProxyAgent(proxy, {
|
||||
timeout: 30000,
|
||||
keepAlive: false,
|
||||
}),
|
||||
signal: AbortSignal.timeout(30000),
|
||||
}
|
||||
: {
|
||||
signal: AbortSignal.timeout(15000),
|
||||
};
|
||||
|
||||
return nodeFetch(url, fetchOptions) as unknown as Response;
|
||||
}
|
||||
}
|
||||
|
||||
export interface TMDBSearchResult {
|
||||
id: number;
|
||||
title?: string; // 电影
|
||||
@@ -52,20 +89,8 @@ export async function searchTMDB(
|
||||
url += `&year=${year}`;
|
||||
}
|
||||
|
||||
const fetchOptions: any = proxy
|
||||
? {
|
||||
agent: new HttpsProxyAgent(proxy, {
|
||||
timeout: 30000,
|
||||
keepAlive: false,
|
||||
}),
|
||||
signal: AbortSignal.timeout(30000),
|
||||
}
|
||||
: {
|
||||
signal: AbortSignal.timeout(15000),
|
||||
};
|
||||
|
||||
// 使用 node-fetch 而不是原生 fetch,因为原生 fetch 不支持 agent 选项
|
||||
const response = await nodeFetch(url, fetchOptions);
|
||||
// 使用统一的 fetch 函数
|
||||
const response = await universalFetch(url, proxy);
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('TMDB 搜索失败:', response.status, response.statusText);
|
||||
@@ -138,19 +163,7 @@ export async function getTVSeasons(
|
||||
const baseUrl = reverseProxyBaseUrl || DEFAULT_TMDB_BASE_URL;
|
||||
const url = `${baseUrl}/3/tv/${tvId}?api_key=${actualKey}&language=zh-CN`;
|
||||
|
||||
const fetchOptions: any = proxy
|
||||
? {
|
||||
agent: new HttpsProxyAgent(proxy, {
|
||||
timeout: 30000,
|
||||
keepAlive: false,
|
||||
}),
|
||||
signal: AbortSignal.timeout(30000),
|
||||
}
|
||||
: {
|
||||
signal: AbortSignal.timeout(15000),
|
||||
};
|
||||
|
||||
const response = await nodeFetch(url, fetchOptions);
|
||||
const response = await universalFetch(url, proxy);
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('TMDB 获取电视剧详情失败:', response.status, response.statusText);
|
||||
@@ -191,19 +204,7 @@ export async function getTVSeasonDetails(
|
||||
const baseUrl = reverseProxyBaseUrl || DEFAULT_TMDB_BASE_URL;
|
||||
const url = `${baseUrl}/3/tv/${tvId}/season/${seasonNumber}?api_key=${actualKey}&language=zh-CN`;
|
||||
|
||||
const fetchOptions: any = proxy
|
||||
? {
|
||||
agent: new HttpsProxyAgent(proxy, {
|
||||
timeout: 30000,
|
||||
keepAlive: false,
|
||||
}),
|
||||
signal: AbortSignal.timeout(30000),
|
||||
}
|
||||
: {
|
||||
signal: AbortSignal.timeout(15000),
|
||||
};
|
||||
|
||||
const response = await nodeFetch(url, fetchOptions);
|
||||
const response = await universalFetch(url, proxy);
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('TMDB 获取季度详情失败:', response.status, response.statusText);
|
||||
|
||||
Reference in New Issue
Block a user