From 679c8deacaf53315db10c4e45351b9c3942471b3 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Mon, 22 Jul 2024 12:06:32 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E5=A2=9E=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?dy=E7=9A=84=E6=97=B6=E9=95=BF=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 增加对dy的时长限制 2. 增加一个json-debug函数,方便调试 --- apps/tools.js | 38 +++++++++++++++++++++++++++++--------- utils/common.js | 23 +++++++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/apps/tools.js b/apps/tools.js index cd34e15..8f6d900 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -289,13 +289,30 @@ export class tools extends plugin { // 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") { // logger.info(item.video); // 多位面选择:play_addr、play_addr_265、play_addr_h264 - const { play_addr: { uri: videoAddrURI } } = item.video; + const { play_addr: { uri: videoAddrURI }, duration, cover } = item.video; + // 进行时间判断,如果超过时间阈值就不发送 + const dyDuration = Math.trunc(duration / 1000); + const durationThreshold = this.biliDuration; + if (dyDuration >= durationThreshold) { + // 超过阈值,不发送的情况 + const dyCover = cover.url_list?.pop(); + // logger.info(cover.url_list); + e.reply([segment.image(dyCover) ,`识别:抖音, ${ item.desc }\n + ${ DIVIDING_LINE.replace('{}', '限制说明') }\n当前视频时长约:${ Math.trunc(dyDuration / 60) }分钟,\n大于管理员设置的最大时长 ${ durationThreshold / 60 } 分钟!`]) + // 如果开启评论的就调用 + await this.douyinComment(e, douId); + return; + } else { + // 正常发送 + e.reply(`识别:抖音, ${ item.desc }`); + } + // 分辨率判断是否压缩 const resolution = this.douyinCompression ? "720p" : "1080p"; // 使用今日头条 CDN 进一步加快解析速度 const resUrl = DY_TOUTIAO_INFO.replace("1080p", resolution).replace("{}", videoAddrURI); @@ -318,6 +335,8 @@ export class tools extends plugin { this.sendVideoToUpload(e, path) }); } else if (urlType === "image") { + // 发送描述 + e.reply(`识别:抖音, ${ item.desc }`); // 无水印图片列表 let no_watermark_image_list = []; // 有水印图片列表 @@ -337,10 +356,7 @@ export class tools extends plugin { await this.reply(await Bot.makeForwardMsg(no_watermark_image_list)); } // 如果开启评论的就调用 - if (this.douyinComments) { - const comments = await this.douyinComment(douId); - e.reply(await Bot.makeForwardMsg(comments)); - } + await this.douyinComment(e, douId); } catch (err) { logger.error(err); e.reply(`Cookie 过期或者 Cookie 没有填写,请参考\n${HELP_DOC}\n尝试无效后可以到官方QQ群[575663150]提出 bug 等待解决`) @@ -350,10 +366,13 @@ export class tools extends plugin { /** * 获取 DY 评论,暂时由群友 @慢热 提供 + * @param e * @param douId - * @returns {Promise<*>} */ - async douyinComment(douId) { + async douyinComment(e, douId) { + if (!this.douyinComments) { + return; + } const commentsResp = await axios.get(DY_COMMENT.replace("{}", douId), { headers: { "User-Agent": @@ -361,13 +380,14 @@ export class tools extends plugin { } }) const comments = commentsResp.data.data.comments; - return comments.map(item => { + const replyComments = comments.map(item => { return { message: item.text, nickname: this.e.sender.card || this.e.user_id, user_id: this.e.user_id, } }) + e.reply(await Bot.makeForwardMsg(replyComments)); } // tiktok解析 diff --git a/utils/common.js b/utils/common.js index 4909037..466e68d 100644 --- a/utils/common.js +++ b/utils/common.js @@ -426,4 +426,27 @@ export function checkCommandExists(command) { } }); }); +} + +/** + * debug:将 JSON 数据保存到本地文件 + * eg. saveJsonToFile(data, 'data.json', (err) => {}) + * @param {Object} jsonData - 要保存的 JSON 数据 + * @param {string} filename - 目标文件名 + * @param {function} callback - 可选的回调函数,处理写入完成后的操作 + */ +function saveJsonToFile(jsonData, filename, callback) { + // 转换 JSON 数据为字符串 + const jsonString = JSON.stringify(jsonData, null, 2); // 第二个参数是 replacer,第三个参数是缩进 + + // 保存到文件 + fs.writeFile(filename, jsonString, 'utf8', (err) => { + if (err) { + console.error('Error writing file', err); + if (callback) callback(err); + } else { + console.log('File successfully written'); + if (callback) callback(null); + } + }); } \ No newline at end of file