tiktok: minor cleanup

This commit is contained in:
wukko 2024-03-15 16:00:02 +06:00
parent 11330589bf
commit fe055abe17

View file

@ -4,11 +4,13 @@ const userAgent = genericUserAgent.split(' Chrome/1')[0],
config = { config = {
tiktok: { tiktok: {
short: "https://vt.tiktok.com/", short: "https://vt.tiktok.com/",
api: "https://api22-normal-c-useast2a.tiktokv.com/aweme/v1/feed/?aweme_id={postId}&version_code=262&app_name=musical_ly&channel=App&device_id=null&os_version=14.4.2&device_platform=iphone&device_type=iPhone9&region=US&carrier_region=US" api: "https://api22-normal-c-useast2a.tiktokv.com/aweme/v1/feed/?aweme_id={postId}&version_code=262&app_name=musical_ly&channel=App&device_id=null&os_version=14.4.2&device_platform=iphone&device_type=iPhone9&region=US&carrier_region=US",
userAgent: "TikTok 26.2.0 rv:262018 (iPhone; iOS 14.4.2; en_US) Cronet"
}, },
douyin: { douyin: {
short: "https://v.douyin.com/", short: "https://v.douyin.com/",
api: "https://www.iesdouyin.com/aweme/v1/web/aweme/detail/?aweme_id={postId}" 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"
} }
} }
@ -17,10 +19,10 @@ function selector(j, h, id) {
let t; let t;
switch (h) { switch (h) {
case "tiktok": case "tiktok":
t = j["aweme_list"].filter(v => v["aweme_id"] === id)[0]; t = j.aweme_list.filter(v => v.aweme_id === id)[0];
break; break;
case "douyin": case "douyin":
t = j['aweme_detail']; t = j.aweme_detail;
break; break;
} }
if (t?.length < 3) return false; if (t?.length < 3) return false;
@ -31,7 +33,7 @@ export default async function(obj) {
let postId = obj.postId ? obj.postId : false; let postId = obj.postId ? obj.postId : false;
if (!postId) { if (!postId) {
let html = await fetch(`${config[obj.host]["short"]}${obj.id}`, { let html = await fetch(`${config[obj.host].short}${obj.id}`, {
redirect: "manual", redirect: "manual",
headers: { "user-agent": userAgent } headers: { "user-agent": userAgent }
}).then((r) => { return r.text() }).catch(() => { return false }); }).then((r) => { return r.text() }).catch(() => { return false });
@ -47,8 +49,10 @@ export default async function(obj) {
if (!postId) return { error: 'ErrorCantGetID' }; if (!postId) return { error: 'ErrorCantGetID' };
let detail; let detail;
detail = await fetch(config[obj.host]["api"].replace("{postId}", postId), { detail = await fetch(config[obj.host].api.replace("{postId}", postId), {
headers: {"user-agent": "TikTok 26.2.0 rv:262018 (iPhone; iOS 14.4.2; en_US) Cronet"} headers: {
"user-agent": config[obj.host].userAgent
}
}).then((r) => { return r.json() }).catch(() => { return false }); }).then((r) => { return r.json() }).catch(() => { return false });
detail = selector(detail, obj.host, postId); detail = selector(detail, obj.host, postId);
@ -56,24 +60,24 @@ export default async function(obj) {
let video, videoFilename, audioFilename, isMp3, audio, images, filenameBase = `${obj.host}_${postId}`; let video, videoFilename, audioFilename, isMp3, audio, images, filenameBase = `${obj.host}_${postId}`;
if (obj.host === "tiktok") { 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 { } else {
images = detail["images"] ? detail["images"] : false images = detail.images ? detail.images : false
} }
if (!obj.isAudioOnly && !images) { if (!obj.isAudioOnly && !images) {
video = obj.host === "tiktok" ? detail["video"]["download_addr"]["url_list"][0] : detail["video"]["play_addr"]["url_list"][2].replace("/play/", "/playwm/"); video = obj.host === "tiktok" ? detail.video.download_addr.url_list[0] : detail.video.play_addr.url_list[2].replace("/play/", "/playwm/");
videoFilename = `${filenameBase}_video.mp4`; videoFilename = `${filenameBase}_video.mp4`;
if (obj.noWatermark) { if (obj.noWatermark) {
video = obj.host === "tiktok" ? detail["video"]["play_addr"]["url_list"][0] : detail["video"]["play_addr"]["url_list"][0]; video = obj.host === "tiktok" ? detail.video.play_addr.url_list[0] : detail.video.play_addr.url_list[0];
videoFilename = `${filenameBase}_video_nw.mp4` // nw - no watermark videoFilename = `${filenameBase}_video_nw.mp4` // nw - no watermark
} }
} else { } else {
let fallback = obj.host === "douyin" ? detail["video"]["play_addr"]["url_list"][0].replace("playwm", "play") : detail["video"]["play_addr"]["url_list"][0]; let fallback = obj.host === "douyin" ? detail.video.play_addr.url_list[0].replace("playwm", "play") : detail.video.play_addr.url_list[0];
audio = fallback; audio = fallback;
audioFilename = `${filenameBase}_audio_fv`; // fv - from video audioFilename = `${filenameBase}_audio_fv`; // fv - from video
if (obj.fullAudio || fallback.includes("music")) { if (obj.fullAudio || fallback.includes("music")) {
audio = detail["music"]["play_url"]["url_list"][0] audio = detail.music.play_url.url_list[0]
audioFilename = `${filenameBase}_audio` audioFilename = `${filenameBase}_audio`
} }
if (audio.slice(-4) === ".mp3") isMp3 = true; if (audio.slice(-4) === ".mp3") isMp3 = true;
@ -92,7 +96,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 = obj.host === "tiktok" ? images[i].display_image.url_list : images[i].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]})
} }