mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
feat: GitHub请求优化
This commit is contained in:
@@ -80,7 +80,6 @@ export async function getTrendingRepos(
|
|||||||
type: TrendingType | string = "daily",
|
type: TrendingType | string = "daily",
|
||||||
ttl = 60 * 60 * 24,
|
ttl = 60 * 60 * 24,
|
||||||
): Promise<TrendingRepoInfo> {
|
): Promise<TrendingRepoInfo> {
|
||||||
// 拼接 Trending 地址,可根据需要调整时间维度
|
|
||||||
const url = `https://github.com/trending?since=${type}`;
|
const url = `https://github.com/trending?since=${type}`;
|
||||||
// 先从缓存中取
|
// 先从缓存中取
|
||||||
const cachedData = await getCache(url);
|
const cachedData = await getCache(url);
|
||||||
@@ -93,17 +92,42 @@ export async function getTrendingRepos(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
logger.info(`🌐 [GET] ${url}`);
|
logger.info(`🌐 [GET] ${url}`);
|
||||||
console.log("获取 github信息 url", url);
|
|
||||||
|
|
||||||
// 添加浏览器请求头
|
// 更新请求头
|
||||||
const headers = {
|
const headers = {
|
||||||
"User-Agent":
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
||||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 14_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/605.1.15",
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
||||||
|
'Accept-Language': 'en-US,en;q=0.5',
|
||||||
|
'Accept-Encoding': 'gzip, deflate, br',
|
||||||
|
'Connection': 'keep-alive',
|
||||||
|
'Upgrade-Insecure-Requests': '1',
|
||||||
|
'Sec-Fetch-Dest': 'document',
|
||||||
|
'Sec-Fetch-Mode': 'navigate',
|
||||||
|
'Sec-Fetch-Site': 'none',
|
||||||
|
'Sec-Fetch-User': '?1',
|
||||||
|
'Cache-Control': 'max-age=0',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 添加重试逻辑
|
||||||
|
const maxRetries = 3;
|
||||||
|
let lastError;
|
||||||
|
|
||||||
|
for (let i = 0; i < maxRetries; i++) {
|
||||||
try {
|
try {
|
||||||
// 添加请求头到 fetch 请求中
|
// 设置超时时间为 20 秒
|
||||||
const response = await fetch(url, { headers });
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(() => controller.abort(), 20000);
|
||||||
|
|
||||||
|
const response = await fetch(url, {
|
||||||
|
headers,
|
||||||
|
signal: controller.signal
|
||||||
|
});
|
||||||
|
clearTimeout(timeout);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
const html = await response.text();
|
const html = await response.text();
|
||||||
// 1. 加载 HTML
|
// 1. 加载 HTML
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
@@ -159,10 +183,24 @@ export async function getTrendingRepos(
|
|||||||
|
|
||||||
await setCache(url, { data, updateTime }, ttl);
|
await setCache(url, { data, updateTime }, ttl);
|
||||||
// 返回数据
|
// 返回数据
|
||||||
logger.info(`✅ [${response?.status}] request was successful`);
|
logger.info(`✅ [${response?.status}] 请求成功!`);
|
||||||
return { fromCache: false, updateTime, data };
|
return { fromCache: false, updateTime, data };
|
||||||
} catch (error) {
|
} catch (error: Error | unknown) {
|
||||||
logger.error("❌ [ERROR] request failed");
|
lastError = error;
|
||||||
throw error;
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||||
|
logger.error(`❌ [ERROR] 第 ${i + 1} 请求失败: ${errorMessage}`);
|
||||||
|
|
||||||
|
// 如果是最后一次重试,则抛出错误
|
||||||
|
if (i === maxRetries - 1) {
|
||||||
|
logger.error("❌ [ERROR] 所有尝试请求失败!");
|
||||||
|
throw lastError;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待一段时间后重试 (1秒、2秒、4秒...)
|
||||||
|
await new Promise(resolve => setTimeout(resolve, Math.pow(2, i) * 1000));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new Error("请求失败!");
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ import type { RouterData } from "../types.js";
|
|||||||
import type { RouterType } from "../router.types.js";
|
import type { RouterType } from "../router.types.js";
|
||||||
import { get } from "../utils/getData.js";
|
import { get } from "../utils/getData.js";
|
||||||
|
|
||||||
|
// 类目接口
|
||||||
|
// https://api.juejin.cn/tag_api/v1/query_category_briefs
|
||||||
|
|
||||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||||
const listData = await getList(noCache);
|
const listData = await getList(noCache);
|
||||||
const routeData: RouterData = {
|
const routeData: RouterData = {
|
||||||
|
|||||||
Reference in New Issue
Block a user