🎈 pref: 新增dy重试兜底策略

This commit is contained in:
zhiyu1998 2024-05-28 20:28:30 +08:00
parent 479efae0c4
commit cd69d28c64
2 changed files with 62 additions and 44 deletions

View File

@ -36,7 +36,7 @@ import {
downloadImg,
downloadMp3,
formatBiliInfo,
getIdVideo,
getIdVideo, retryAxiosReq,
secondsToTime,
testProxy,
truncateString
@ -262,18 +262,12 @@ export class tools extends plugin {
// const param = resp.data.result[0].paramsencode;
const resDyApi = `${ dyApi }&X-Bogus=${ xbParam }`;
headers['Referer'] = `https://www.douyin.com/video/${ douId }`
axios
.get(resDyApi, {
const dyResponse = () => axios.get(resDyApi, {
headers,
})
.then(async resp => {
// console.log(resp)
if (_.isEmpty(await resp?.data)) {
e.reply("解析失败,请重试!");
return;
}
// console.log(await resp.data)
const item = await resp.data.aweme_detail;
});
const data = await retryAxiosReq(dyResponse)
// logger.info(data)
const item = await data.aweme_detail;
e.reply(`识别:抖音, ${ item.desc }`);
const urlTypeCode = item.aweme_type;
const urlType = douyinTypeMap[urlTypeCode];
@ -306,7 +300,6 @@ export class tools extends plugin {
await this.reply(await Bot.makeForwardMsg(no_watermark_image_list));
}
});
});
return true;
}

View File

@ -353,3 +353,28 @@ export function formatSeconds(seconds) {
const remainingSeconds = seconds % 60;
return `${minutes}${remainingSeconds}`;
}
/**
* 重试 axios 请求
* @param requestFunction
* @param retries
* @param delay
* @returns {*}
*/
export async function retryAxiosReq(requestFunction, retries = 3, delay = 1000) {
try {
const response = await requestFunction();
if (!response.data) {
throw new Error('请求空数据');
}
return response.data;
} catch (error) {
if (retries > 0) {
logger.mark(`[R插件][重试模块]重试中... (${3 - retries + 1}/3) 次`);
await new Promise(resolve => setTimeout(resolve, delay));
return retryAxiosReq(requestFunction, retries - 1, delay);
} else {
throw error;
}
}
}