tiktok: improve link parsing

This commit is contained in:
Damir Modyarov 2024-05-21 16:17:29 +03:00
parent 66546ff79b
commit 97850e08af
No known key found for this signature in database

View File

@ -1,14 +1,14 @@
import { genericUserAgent } from "../../config.js"; import { genericUserAgent } from "../../config.js";
import { updateCookie } from "../cookie/manager.js"; import { updateCookie } from "../cookie/manager.js";
import { extract } from "../url.js";
import Cookie from "../cookie/cookie.js"; import Cookie from "../cookie/cookie.js";
const fullDomain = "https://tiktok.com/";
const shortDomain = "https://vt.tiktok.com/"; const shortDomain = "https://vt.tiktok.com/";
export const cookie = new Cookie({}) export const cookie = new Cookie({})
export default async function(obj) { export default async function(obj) {
let postId = obj.postId || false let postId = obj.postId
let username = obj.user || false let username = obj.user
if (!username || !postId) { if (!username || !postId) {
let html = await fetch(`${shortDomain}${obj.id}`, { let html = await fetch(`${shortDomain}${obj.id}`, {
@ -20,16 +20,16 @@ export default async function(obj) {
if (!html) return { error: 'ErrorCouldntFetch' }; if (!html) return { error: 'ErrorCouldntFetch' };
if (html.slice(0, 17) === '<a href="https://') { if (html.startsWith('<a href="https://')) {
const fullLink = html.split('<a href="https://')[1].split('?')[0].split('/') const { patternMatch } = extract(html.split('<a href="https://')[1].split('?')[0])
username = fullLink[1] username = patternMatch.user
postId = fullLink[3] postId = patternMatch.postId
} }
} }
if (!username || !postId) return { error: 'ErrorCantGetID' }; if (!username || !postId) return { error: 'ErrorCantGetID' };
// should always be /video/, even for photos // should always be /video/, even for photos
const res = await fetch(`${fullDomain}${username}/video/${postId}`, { const res = await fetch(`https://tiktok.com/${username}/video/${postId}`, {
headers: { headers: {
"user-agent": genericUserAgent, "user-agent": genericUserAgent,
cookie, cookie,
@ -62,7 +62,7 @@ export default async function(obj) {
} else { } else {
audio = detail.music.playUrl || playAddr; audio = detail.music.playUrl || playAddr;
audioFilename = `${filenameBase}_audio`; audioFilename = `${filenameBase}_audio`;
if (audio.slice(-4) === ".mp3") bestAudio = 'mp3'; if (audio.endsWith(".mp3")) bestAudio = 'mp3';
} }
if (video) return { if (video) return {