From c5b6b4a21a9e0497cb80b81e55d8a1b36cb76a59 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Mon, 30 Sep 2024 14:32:03 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=B7=BB=E5=8A=A0dy?= =?UTF-8?q?=E7=9B=B4=E6=92=AD=E8=A7=A3=E6=9E=90=20[beta]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/tools.js | 45 ++++++++++++++++++++++++++++++++------------- constants/tools.js | 7 +++++++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/apps/tools.js b/apps/tools.js index 8967ddd..2d05215 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -34,7 +34,7 @@ import { BILI_STREAM_INFO, BILI_SUMMARY, DY_COMMENT, - DY_INFO, + DY_INFO, DY_LIVE_INFO, DY_TOUTIAO_INFO, GENERAL_REQ_LINK, HIBI_API_SERVICE, @@ -128,7 +128,7 @@ export class tools extends plugin { fnc: "trans", }, { - reg: "(v.douyin.com)", + reg: "(v.douyin.com|live.douyin.com)", fnc: "douyin", }, { @@ -305,16 +305,27 @@ export class tools extends plugin { // 抖音解析 async douyin(e) { - const urlRex = /(http:|https:)\/\/v.douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/g; - const douUrl = urlRex.exec(e.msg.trim())[0]; - - const res = await this.douyinRequest(douUrl); - // 当前版本需要填入cookie - if (_.isEmpty(this.douyinCookie)) { - e.reply(`检测到没有Cookie,无法解析抖音${HELP_DOC}`); + const urlRex = /(http:\/\/|https:\/\/)(v|live).douyin.com\/[A-Za-z\d._?%&+\-=\/#]*/; + // 检测无效链接,例如:v.douyin.com + if (!urlRex.test(e.msg)) { + e.reply(`检测到这是一个无效链接,无法解析抖音${HELP_DOC}`); + return; + } + // 获取链接 + let douUrl = urlRex.exec(e.msg.trim())[0]; + if (douUrl.includes("v.douyin.com")) { + douUrl = await this.douyinRequest(douUrl) + } + // 获取 ID + const douId = /note\/(\d+)/g.exec(douUrl)?.[1] || + /video\/(\d+)/g.exec(douUrl)?.[1] || + /live.douyin.com\/(\d+)/.exec(douUrl)?.[1] || + /live\/(\d+)/.exec(douUrl)?.[1]; + // 当前版本需要填入cookie + if (_.isEmpty(this.douyinCookie) || _.isEmpty(douId)) { + e.reply(`检测到没有Cookie 或者 这是一个无效链接,无法解析抖音${HELP_DOC}`); return; } - const douId = /note\/(\d+)/g.exec(res)?.[1] || /video\/(\d+)/g.exec(res)?.[1]; // 以下是更新了很多次的抖音API历史,且用且珍惜 // const url = `https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${ douId }`; // const url = `https://www.iesdouyin.com/aweme/v1/web/aweme/detail/?aweme_id=${ douId }&aid=1128&version_name=23.5.0&device_platform=android&os_version=2333`; @@ -325,7 +336,7 @@ export class tools extends plugin { Referer: "https://www.douyin.com/", cookie: this.douyinCookie, }; - const dyApi = DY_INFO.replace("{}", douId); + const dyApi = douUrl.includes("live") ? DY_LIVE_INFO.replaceAll("{}", douId) : DY_INFO.replace("{}", douId); // a-bogus参数 const abParam = aBogus.generate_a_bogus( new URLSearchParams(new URL(dyApi).search).toString(), @@ -333,7 +344,7 @@ export class tools extends plugin { ); // const param = resp.data.result[0].paramsencode; const resDyApi = `${dyApi}&a_bogus=${abParam}`; - headers['Referer'] = `https://www.douyin.com/video/${douId}` + headers['Referer'] = `https://www.douyin.com/` // 定义一个dy请求 const dyResponse = () => axios.get(resDyApi, { headers, @@ -341,7 +352,15 @@ export class tools extends plugin { // 如果失败进行3次重试 try { const data = await retryAxiosReq(dyResponse) - // logger.info(data) + // saveJsonToFile(data); + // 直播数据逻辑 + if (douUrl.includes("live")) { + const item = await data.data.data?.[0]; + const { title, cover, user_count_str } = item; + const dySendContent = `${this.identifyPrefix}识别:抖音直播,${title}` + e.reply([segment.image(cover?.url_list?.[0]), dySendContent, `\n🏄‍♂️在线人数:${user_count_str}人正在观看`]); + return; + } const item = await data.aweme_detail; // await saveJsonToFile(item); // 如果为null则退出 diff --git a/constants/tools.js b/constants/tools.js index da52dc9..471bf94 100644 --- a/constants/tools.js +++ b/constants/tools.js @@ -113,6 +113,13 @@ export const DY_COMMENT = "https://www.douyin.com/aweme/v1/web/comment/list/?dev */ export const DY_TOUTIAO_INFO = "https://aweme.snssdk.com/aweme/v1/play/?video_id={}&ratio=1080p&line=0" + +/** + * DY 直播信息 + * @type {string} + */ +export const DY_LIVE_INFO = "https://live.douyin.com/webcast/room/web/enter/?device_platform=webapp&aid=6383&channel=channel_pc_web&pc_client_type=1&version_code=190500&version_name=19.5.0&cookie_enabled=true&screen_width=1920&screen_height=1080&browser_language=zh-CN&browser_platform=Win32&browser_name=Firefox&browser_version=124.0&browser_online=true&engine_name=Gecko&engine_version=122.0.0.0&os_name=Windows&os_version=10&cpu_core_num=12&device_memory=8&platform=PC&web_rid={}&room_id_str={}"; + /** * X API * @type {string}