From c0d78f2b016231d29af606dc177d0441f910a2f9 Mon Sep 17 00:00:00 2001 From: wukko Date: Fri, 28 Oct 2022 18:32:51 +0600 Subject: [PATCH] changed the way watermarked tiktok videos are downloaded --- package.json | 2 +- src/modules/services/tiktok.js | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 597ccbc..93dc9c3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cobalt", "description": "save what you love", - "version": "4.1", + "version": "4.1.1", "author": "wukko", "exports": "./src/cobalt.js", "type": "module", diff --git a/src/modules/services/tiktok.js b/src/modules/services/tiktok.js index 05e4db2..53b9ddf 100644 --- a/src/modules/services/tiktok.js +++ b/src/modules/services/tiktok.js @@ -41,16 +41,20 @@ export default async function(obj) { detail = selector(JSON.parse(detail.body), obj.host, obj.postId); if (!detail) throw new Error() } catch (e) { - if (obj.host === "tiktok") { - let html = await got.get(`https://tiktok.com/@video/video/${obj.postId}`, { headers: { "user-agent": userAgent } }); - html = html.body; - if (html.includes(',"preloadList":[{"url":"')) { + try { + if (obj.host === "tiktok") { + let webAppDetail = await got.get(`https://m.tiktok.com/api/item/detail/?itemId=${obj.postId}`, { headers: { "user-agent": userAgent } }); + webAppDetail = JSON.parse(webAppDetail.body); return { - urls: unicodeDecode(html.split(',"preloadList":[{"url":"')[1].split('","id":"')[0].trim()), + urls: webAppDetail.itemInfo.itemStruct.video.downloadAddr, filename: `${obj.host}_${obj.postId}_video.mp4` } - } else throw new Error() - } else throw new Error() + } else { + return { error: loc(obj.lang, 'ErrorCouldntFetch') } + } + } catch (e) { + return { error: loc(obj.lang, 'ErrorCouldntFetch') } + } } let video, videoFilename, audioFilename, isMp3, audio, images, filenameBase = `${obj.host}_${obj.postId}`; @@ -64,10 +68,12 @@ export default async function(obj) { videoFilename = `${filenameBase}_video_nw.mp4` // nw - no watermark if (!obj.noWatermark) { if (obj.host === "tiktok") { - let html = await got.get(`https://tiktok.com/@video/video/${obj.postId}`, { headers: { "user-agent": userAgent } }); - html = html.body; - if (html.includes(',"preloadList":[{"url":"')) { - video = unicodeDecode(html.split(',"preloadList":[{"url":"')[1].split('","id":"')[0].trim()) + try { + let webAppDetail = await got.get(`https://m.tiktok.com/api/item/detail/?itemId=${obj.postId}`, { headers: { "user-agent": userAgent } }); + webAppDetail = JSON.parse(webAppDetail.body); + video = webAppDetail.itemInfo.itemStruct.video.downloadAddr; + } catch (e) { + video = detail["video"]["download_addr"]["url_list"][0] } } else { video = detail['video']['play_addr']['url_list'][0]