From 9769dce7a4217fac4e3bdcdf1cc9a0980796e9e9 Mon Sep 17 00:00:00 2001 From: zhiyu <542716863@qq.com> Date: Mon, 5 Feb 2024 21:55:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20V1.3.2=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8Dtiktok=E7=9F=AD=E9=93=BE=E6=8E=A5=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增tiktok短链接(类似:https://www.tiktok.com/t/xxxx/)解析,感谢群友(MiX提供) --- apps/tools.js | 35 +++++------------------------------ config/version.yaml | 2 +- utils/common.js | 1 - utils/tiktok.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 utils/tiktok.js diff --git a/apps/tools.js b/apps/tools.js index b184ffb..60c73f5 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -31,6 +31,7 @@ import { BILI_SUMMARY } from "../constants/bili.js"; import { XHS_VIDEO } from "../constants/xhs.js"; import child_process from 'node:child_process' import { getAudio, getVideo } from "../utils/y2b.js"; +import { processTikTokUrl } from "../utils/tiktok.js"; export class tools extends plugin { constructor() { @@ -227,37 +228,11 @@ export class tools extends plugin { // tiktok解析 async tiktok(e) { - const urlRex = /(http:|https:)\/\/www.tiktok.com\/[A-Za-z\d._?%&+\-=\/#@]*/g; - const urlShortRex = /(http:|https:)\/\/vt.tiktok.com\/[A-Za-z\d._?%&+\-=\/#]*/g; - const urlShortRex2 = /(http:|https:)\/\/vm.tiktok.com\/[A-Za-z\d._?%&+\-=\/#]*/g; - let url = e.msg.trim(); - // 判断是否是海外服务器 + // 判断海外 const isOversea = await this.isOverseasServer(); - // 短号处理 - if (url.includes("vt.tiktok")) { - const temp_url = urlShortRex.exec(url)[0]; - await fetch(temp_url, { - redirect: "follow", - follow: 10, - timeout: 10000, - agent: isOversea ? '' : new HttpProxyAgent(this.myProxy), - }).then(resp => { - url = resp.url; - }); - } else if (url.includes("vm.tiktok")) { - const temp_url = urlShortRex2.exec(url)[0]; - await fetch(temp_url, { - headers: { "User-Agent": "facebookexternalhit/1.1" }, - redirect: "follow", - follow: 10, - timeout: 10000, - agent: isOversea ? '' : new HttpProxyAgent(this.myProxy), - }).then(resp => { - url = resp.url; - }); - } else { - url = urlRex.exec(url)[0]; - } + // 处理链接 + let url = await processTikTokUrl(e.msg.trim(), isOversea); + // 处理ID let tiktokVideoId = await getIdVideo(url); tiktokVideoId = tiktokVideoId.replace(/\//g, ""); // API链接 diff --git a/config/version.yaml b/config/version.yaml index d1f53c2..71b98af 100644 --- a/config/version.yaml +++ b/config/version.yaml @@ -1,5 +1,5 @@ - { - version: 1.3.1, + version: 1.3.2, data: [ 新增油管解析功能, diff --git a/utils/common.js b/utils/common.js index 1324aae..ec0ef90 100644 --- a/utils/common.js +++ b/utils/common.js @@ -105,7 +105,6 @@ export function downloadPDF (url, filename) { export async function getIdVideo(url) { const matching = url.includes("/video/"); if (!matching) { - this.e.reply("没找到,正在获取随机视频!"); return null; } const idVideo = url.substring(url.indexOf("/video/") + 7, url.length); diff --git a/utils/tiktok.js b/utils/tiktok.js new file mode 100644 index 0000000..083ca43 --- /dev/null +++ b/utils/tiktok.js @@ -0,0 +1,42 @@ +/** + * Tiktok专属解析链接的Fetch + * @param url 地址 + * @param isOversea 是否是海外 + */ +const fetchTiktokUrl = async (url, isOversea) => { + const proxyAgent = isOversea ? '' : new HttpProxyAgent(this.myProxy); + // 处理特殊情况 & 非特殊情况的header + const headers = url.includes("vm.tiktok") || url.includes("tiktok.com/t") + ? { "User-Agent": "facebookexternalhit/1.1" } + : {}; + + return fetch(url, { + headers, + redirect: "follow", + follow: 10, + timeout: 10000, + agent: proxyAgent, + }).then(resp => resp.url); +}; + +/** + * 处理Tiktok链接 + * @param url 用户发送的链接,可能存在一些问题,需要正则匹配处理 + * @param isOversea 是否是海外 + */ +export const processTikTokUrl = async (url, isOversea) => { + // 合并正则表达式 + // const urlShortRex = /(http:|https:)\/\/vt.tiktok.com\/[A-Za-z\d._?%&+\-=\/#]*/g; + // const urlShortRex2 = /(http:|https:)\/\/vm.tiktok.com\/[A-Za-z\d._?%&+\-=\/#]*/g; + // const urlShortRex3 = /(http:|https:)\/\/www.tiktok.com\/t\/[A-Za-z\d._?%&+\-=\/#]*/g; + const tikTokRegex = /(http:|https:)\/\/(www\.tiktok\.com\/|vt\.tiktok\.com\/|vm\.tiktok\.com\/www\.tiktok\.com\/t\/)[A-Za-z\d._?%&+\-=\/#@]*/g; + const match = tikTokRegex.exec(url); + + if (match) {// 如果URL匹配任何TikTok相关的模式,则进行处理 + url = await fetchTiktokUrl(match[0], isOversea); + } + + // 这里可以处理其他逻辑,例如更新URL、记录日志等 + // 或者其他处理结果 + return url; +}; \ No newline at end of file