diff --git a/apps/tools.js b/apps/tools.js index adc4756..bd6fd93 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -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; } diff --git a/utils/common.js b/utils/common.js index 19227c8..63a6722 100644 --- a/utils/common.js +++ b/utils/common.js @@ -352,4 +352,29 @@ export function formatSeconds(seconds) { const minutes = Math.floor(seconds / 60); 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; + } + } } \ No newline at end of file