🎈 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, downloadImg,
downloadMp3, downloadMp3,
formatBiliInfo, formatBiliInfo,
getIdVideo, getIdVideo, retryAxiosReq,
secondsToTime, secondsToTime,
testProxy, testProxy,
truncateString truncateString
@ -262,50 +262,43 @@ export class tools extends plugin {
// const param = resp.data.result[0].paramsencode; // const param = resp.data.result[0].paramsencode;
const resDyApi = `${ dyApi }&X-Bogus=${ xbParam }`; const resDyApi = `${ dyApi }&X-Bogus=${ xbParam }`;
headers['Referer'] = `https://www.douyin.com/video/${ douId }` headers['Referer'] = `https://www.douyin.com/video/${ douId }`
axios const dyResponse = () => axios.get(resDyApi, {
.get(resDyApi, { headers,
headers, });
}) const data = await retryAxiosReq(dyResponse)
.then(async resp => { // logger.info(data)
// console.log(resp) const item = await data.aweme_detail;
if (_.isEmpty(await resp?.data)) { e.reply(`识别:抖音, ${ item.desc }`);
e.reply("解析失败,请重试!"); const urlTypeCode = item.aweme_type;
return; const urlType = douyinTypeMap[urlTypeCode];
} if (urlType === "video") {
// console.log(await resp.data) const resUrl = item.video.play_addr.url_list[0].replace(
const item = await resp.data.aweme_detail; "http",
e.reply(`识别:抖音, ${ item.desc }`); "https",
const urlTypeCode = item.aweme_type; );
const urlType = douyinTypeMap[urlTypeCode]; const path = `${ this.getCurDownloadPath(e) }/temp.mp4`;
if (urlType === "video") { await this.downloadVideo(resUrl).then(() => {
const resUrl = item.video.play_addr.url_list[0].replace( this.sendVideoToUpload(e, path)
"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));
}
}); });
} 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; return true;
} }

View File

@ -352,4 +352,29 @@ export function formatSeconds(seconds) {
const minutes = Math.floor(seconds / 60); const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60; const remainingSeconds = seconds % 60;
return `${minutes}${remainingSeconds}`; 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;
}
}
} }