mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-03-14 01:57:29 +08:00
first commit
This commit is contained in:
36
src/lib/douban.ts
Normal file
36
src/lib/douban.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 通用的豆瓣数据获取函数
|
||||
* @param url 请求的URL
|
||||
* @returns Promise<T> 返回指定类型的数据
|
||||
*/
|
||||
export async function fetchDoubanData<T>(url: string): Promise<T> {
|
||||
// 添加超时控制
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user