rc-plugin/utils/tiktok.js

44 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import HttpProxyAgent from "https-proxy-agent";
/**
* 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;
};