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