🐞 fix: V1.3.2 修复tiktok短链接解析问题

新增tiktok短链接(类似:https://www.tiktok.com/t/xxxx/)解析,感谢群友(MiX提供)
This commit is contained in:
zhiyu 2024-02-05 21:55:25 +08:00
parent 8133ac018e
commit 9769dce7a4
4 changed files with 48 additions and 32 deletions

View File

@ -31,6 +31,7 @@ import { BILI_SUMMARY } from "../constants/bili.js";
import { XHS_VIDEO } from "../constants/xhs.js"; import { XHS_VIDEO } from "../constants/xhs.js";
import child_process from 'node:child_process' import child_process from 'node:child_process'
import { getAudio, getVideo } from "../utils/y2b.js"; import { getAudio, getVideo } from "../utils/y2b.js";
import { processTikTokUrl } from "../utils/tiktok.js";
export class tools extends plugin { export class tools extends plugin {
constructor() { constructor() {
@ -227,37 +228,11 @@ export class tools extends plugin {
// tiktok解析 // tiktok解析
async tiktok(e) { 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(); const isOversea = await this.isOverseasServer();
// 短号处理 // 处理链接
if (url.includes("vt.tiktok")) { let url = await processTikTokUrl(e.msg.trim(), isOversea);
const temp_url = urlShortRex.exec(url)[0]; // 处理ID
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 tiktokVideoId = await getIdVideo(url); let tiktokVideoId = await getIdVideo(url);
tiktokVideoId = tiktokVideoId.replace(/\//g, ""); tiktokVideoId = tiktokVideoId.replace(/\//g, "");
// API链接 // API链接

View File

@ -1,5 +1,5 @@
- { - {
version: 1.3.1, version: 1.3.2,
data: data:
[ [
新增<span class="cmd">油管解析</span>功能, 新增<span class="cmd">油管解析</span>功能,

View File

@ -105,7 +105,6 @@ export function downloadPDF (url, filename) {
export async function getIdVideo(url) { export async function getIdVideo(url) {
const matching = url.includes("/video/"); const matching = url.includes("/video/");
if (!matching) { if (!matching) {
this.e.reply("没找到,正在获取随机视频!");
return null; return null;
} }
const idVideo = url.substring(url.indexOf("/video/") + 7, url.length); const idVideo = url.substring(url.indexOf("/video/") + 7, url.length);

42
utils/tiktok.js Normal file
View File

@ -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;
};