From 18545e7c910976d1c376713e10bdf93d289effbd Mon Sep 17 00:00:00 2001 From: wukko Date: Fri, 29 Mar 2024 06:28:52 +0600 Subject: [PATCH] tiktok: clean up --- src/modules/processing/match.js | 2 - src/modules/processing/services/tiktok.js | 63 +++++++---------------- 2 files changed, 19 insertions(+), 46 deletions(-) diff --git a/src/modules/processing/match.js b/src/modules/processing/match.js index 41690571..6f715ef7 100644 --- a/src/modules/processing/match.js +++ b/src/modules/processing/match.js @@ -83,10 +83,8 @@ export default async function(host, patternMatch, url, lang, obj) { id: patternMatch.id }); break; - case "douyin": case "tiktok": r = await tiktok({ - host: host, postId: patternMatch.postId, id: patternMatch.id, fullAudio: obj.isTTFullAudio, diff --git a/src/modules/processing/services/tiktok.js b/src/modules/processing/services/tiktok.js index daea8c4b..ef74c0b9 100644 --- a/src/modules/processing/services/tiktok.js +++ b/src/modules/processing/services/tiktok.js @@ -1,33 +1,8 @@ import { genericUserAgent } from "../../config.js"; -const userAgent = genericUserAgent.split(' Chrome/1')[0], - config = { - tiktok: { - short: "https://vt.tiktok.com/", - api: "https://api22-normal-c-alisg.tiktokv.com/aweme/v1/feed/?aweme_id={postId}®ion=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; -} +const shortDomain = "https://vt.tiktok.com/"; +const apiPath = "https://api22-normal-c-alisg.tiktokv.com/aweme/v1/feed/?region=US&carrier_region=US"; +const apiUserAgent = "TikTok/338014 CFNetwork/1410.1 Darwin/22.6.0"; export default async function(obj) { 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 (!postId) { - let html = await fetch(`${config[obj.host].short}${obj.id}`, { + let html = await fetch(`${shortDomain}${obj.id}`, { redirect: "manual", - headers: { "user-agent": userAgent } + headers: { + "user-agent": genericUserAgent.split(' Chrome/1')[0] + } }).then((r) => { return r.text() }).catch(() => { return false }); if (!html) return { error: 'ErrorCouldntFetch' }; @@ -53,24 +30,22 @@ export default async function(obj) { let deviceInfo = JSON.parse(process.env.TIKTOK_DEVICE_INFO); deviceInfo = new URLSearchParams(deviceInfo).toString(); - let detail; - detail = await fetch(`${config[obj.host].api.replace("{postId}", postId)}&${deviceInfo}`, { - headers: { - "user-agent": config[obj.host].userAgent - } - }).then((r) => { return r.json() }).catch(() => { return false }); + let apiURL = new URL(apiPath); + apiURL.searchParams.append("aweme_id", postId); - detail = selector(detail, obj.host, postId); + let detail = await fetch(`${apiURL.href}&${deviceInfo}`, { + headers: { + "user-agent": apiUserAgent + } + }).then((r) => { return r.json() }).catch(() => { return false }); + + detail = detail?.aweme_list?.filter(v => v.aweme_id === postId)[0]; if (!detail) return { error: 'ErrorCouldntFetch' }; 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 - } else { - images = detail.images ? detail.images : false - } + images = detail.image_post_info ? detail.image_post_info.images : false; if (!obj.isAudioOnly && !images) { video = detail.video.play_addr.url_list[0]; @@ -99,7 +74,7 @@ export default async function(obj) { if (images) { let imageLinks = []; 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?")) imageLinks.push({url: sel[0]}) }