tiktok: clean up

This commit is contained in:
wukko 2024-03-29 06:28:52 +06:00
parent 79772d45d7
commit 18545e7c91
2 changed files with 19 additions and 46 deletions

View file

@ -83,10 +83,8 @@ export default async function(host, patternMatch, url, lang, obj) {
id: patternMatch.id id: patternMatch.id
}); });
break; break;
case "douyin":
case "tiktok": case "tiktok":
r = await tiktok({ r = await tiktok({
host: host,
postId: patternMatch.postId, postId: patternMatch.postId,
id: patternMatch.id, id: patternMatch.id,
fullAudio: obj.isTTFullAudio, fullAudio: obj.isTTFullAudio,

View file

@ -1,33 +1,8 @@
import { genericUserAgent } from "../../config.js"; import { genericUserAgent } from "../../config.js";
const userAgent = genericUserAgent.split(' Chrome/1')[0], const shortDomain = "https://vt.tiktok.com/";
config = { const apiPath = "https://api22-normal-c-alisg.tiktokv.com/aweme/v1/feed/?region=US&carrier_region=US";
tiktok: { const apiUserAgent = "TikTok/338014 CFNetwork/1410.1 Darwin/22.6.0";
short: "https://vt.tiktok.com/",
api: "https://api22-normal-c-alisg.tiktokv.com/aweme/v1/feed/?aweme_id={postId}&region=US&carrier_region=US",
userAgent: "TikTok/338014 CFNetwork/1410.1 Darwin/22.6.0"
},
douyin: {
short: "https://v.douyin.com/",
api: "https://www.iesdouyin.com/aweme/v1/web/aweme/detail/?aweme_id={postId}",
userAgent: "TikTok 26.2.0 rv:262018 (iPhone; iOS 14.4.2; en_US) Cronet"
}
}
function selector(j, h, id) {
if (!j) return false;
let t;
switch (h) {
case "tiktok":
t = j.aweme_list.filter(v => v.aweme_id === id)[0];
break;
case "douyin":
t = j.aweme_detail;
break;
}
if (t?.length < 3) return false;
return t;
}
export default async function(obj) { export default async function(obj) {
let postId = obj.postId ? obj.postId : false; let postId = obj.postId ? obj.postId : false;
@ -35,9 +10,11 @@ export default async function(obj) {
if (!process.env.TIKTOK_DEVICE_INFO) return { error: 'ErrorCouldntFetch' }; if (!process.env.TIKTOK_DEVICE_INFO) return { error: 'ErrorCouldntFetch' };
if (!postId) { if (!postId) {
let html = await fetch(`${config[obj.host].short}${obj.id}`, { let html = await fetch(`${shortDomain}${obj.id}`, {
redirect: "manual", redirect: "manual",
headers: { "user-agent": userAgent } headers: {
"user-agent": genericUserAgent.split(' Chrome/1')[0]
}
}).then((r) => { return r.text() }).catch(() => { return false }); }).then((r) => { return r.text() }).catch(() => { return false });
if (!html) return { error: 'ErrorCouldntFetch' }; if (!html) return { error: 'ErrorCouldntFetch' };
@ -53,24 +30,22 @@ export default async function(obj) {
let deviceInfo = JSON.parse(process.env.TIKTOK_DEVICE_INFO); let deviceInfo = JSON.parse(process.env.TIKTOK_DEVICE_INFO);
deviceInfo = new URLSearchParams(deviceInfo).toString(); deviceInfo = new URLSearchParams(deviceInfo).toString();
let detail; let apiURL = new URL(apiPath);
detail = await fetch(`${config[obj.host].api.replace("{postId}", postId)}&${deviceInfo}`, { apiURL.searchParams.append("aweme_id", postId);
let detail = await fetch(`${apiURL.href}&${deviceInfo}`, {
headers: { headers: {
"user-agent": config[obj.host].userAgent "user-agent": apiUserAgent
} }
}).then((r) => { return r.json() }).catch(() => { return false }); }).then((r) => { return r.json() }).catch(() => { return false });
detail = selector(detail, obj.host, postId); detail = detail?.aweme_list?.filter(v => v.aweme_id === postId)[0];
if (!detail) return { error: 'ErrorCouldntFetch' }; if (!detail) return { error: 'ErrorCouldntFetch' };
let video, videoFilename, audioFilename, isMp3, audio, images, let video, videoFilename, audioFilename, isMp3, audio, images,
filenameBase = `${obj.host}_${detail.author.unique_id}_${postId}`; filenameBase = `tiktok_${detail.author.unique_id}_${postId}`;
if (obj.host === "tiktok") { images = detail.image_post_info ? detail.image_post_info.images : false;
images = detail.image_post_info ? detail.image_post_info.images : false
} else {
images = detail.images ? detail.images : false
}
if (!obj.isAudioOnly && !images) { if (!obj.isAudioOnly && !images) {
video = detail.video.play_addr.url_list[0]; video = detail.video.play_addr.url_list[0];
@ -99,7 +74,7 @@ export default async function(obj) {
if (images) { if (images) {
let imageLinks = []; let imageLinks = [];
for (let i in images) { for (let i in images) {
let sel = obj.host === "tiktok" ? images[i].display_image.url_list : images[i].url_list; let sel = images[i].display_image.url_list;
sel = sel.filter(p => p.includes(".jpeg?")) sel = sel.filter(p => p.includes(".jpeg?"))
imageLinks.push({url: sel[0]}) imageLinks.push({url: sel[0]})
} }