🎈 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,50 +262,43 @@ 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, {
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;
e.reply(`识别:抖音, ${ item.desc }`);
const urlTypeCode = item.aweme_type;
const urlType = douyinTypeMap[urlTypeCode];
if (urlType === "video") {
const resUrl = item.video.play_addr.url_list[0].replace(
"http",
"https",
);
const path = `${ this.getCurDownloadPath(e) }/temp.mp4`;
await this.downloadVideo(resUrl).then(() => {
this.sendVideoToUpload(e, path)
});
} else if (urlType === "image") {
// 无水印图片列表
let no_watermark_image_list = [];
// 有水印图片列表
// let watermark_image_list = [];
for (let i of item.images) {
// 无水印图片列表
no_watermark_image_list.push({
message: segment.image(i.url_list[0]),
nickname: this.e.sender.card || this.e.user_id,
user_id: this.e.user_id,
});
// 有水印图片列表
// watermark_image_list.push(i.download_url_list[0]);
// e.reply(segment.image(i.url_list[0]));
}
// console.log(no_watermark_image_list)
await this.reply(await Bot.makeForwardMsg(no_watermark_image_list));
}
const dyResponse = () => axios.get(resDyApi, {
headers,
});
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];
if (urlType === "video") {
const resUrl = item.video.play_addr.url_list[0].replace(
"http",
"https",
);
const path = `${ this.getCurDownloadPath(e) }/temp.mp4`;
await this.downloadVideo(resUrl).then(() => {
this.sendVideoToUpload(e, path)
});
} else if (urlType === "image") {
// 无水印图片列表
let no_watermark_image_list = [];
// 有水印图片列表
// let watermark_image_list = [];
for (let i of item.images) {
// 无水印图片列表
no_watermark_image_list.push({
message: segment.image(i.url_list[0]),
nickname: this.e.sender.card || this.e.user_id,
user_id: this.e.user_id,
});
// 有水印图片列表
// watermark_image_list.push(i.download_url_list[0]);
// e.reply(segment.image(i.url_list[0]));
}
// console.log(no_watermark_image_list)
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;
}
}
}