fix:抽卡记录请求

This commit is contained in:
别调P 2024-07-20 01:41:29 +08:00
parent c059050b8b
commit cf4c27da44
2 changed files with 21 additions and 3 deletions

View file

@ -1,9 +1,9 @@
import fetch from 'node-fetch';
import { SingleGachaLog, ZZZGachaLogResp } from '../model/gacha.js';
import { sleep } from '../utils/time.js';
import { rank } from './convert.js';
import { getGachaLog, saveGachaLog } from './db.js';
import { ZZZ_GET_GACHA_LOG_API } from './mysapi/api.js';
import { fetchWithRetry } from '../utils/network.js';
// 池子代码
export const gacha_type_meta_data = {
@ -119,7 +119,7 @@ export const getZZZGachaLogByAuthkey = async (
endId
);
// 发送请求
const response = await fetch(link, {
const response = await fetchWithRetry(link, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
@ -202,7 +202,7 @@ export const updateGachaLog = async (authKey, uid) => {
endId = log.list[log.list.length - 1]?.id || endId;
page++;
// 防止请求过快
await sleep(400);
await sleep(1000);
}
}
// 合并新数据和之前的数据

View file

@ -1,5 +1,6 @@
import http from 'http';
import https from 'https';
import fetch from 'node-fetch';
export async function checkLatency(url) {
let request = http;
if (url.startsWith('https')) {
@ -46,3 +47,20 @@ export function getQueryVariable(url, variable) {
return null;
}
}
export async function fetchWithRetry(url, options, retry = 3) {
let err;
const _fetch = async (url, options, retryCount = 0) => {
if (retryCount > retry) {
throw new Error('Retry limit reached', err);
}
try {
return await fetch(url, options);
} catch (error) {
logger.debug(`Fetch error: ${error.message}`);
err = error;
return _fetch(url, options, retryCount + 1);
}
};
return _fetch(url, options);
}