CODEBREAKER / Tiktok Download
Basically a tiktok download snippet
OtherJavaScript
1 views
Basically a tiktok download snippet
const axios = require('axios')
function getId(url) {
const idMatch = url.match(/[0-9]{19}/);
if (idMatch) return idMatch[0];
return null
}
async function tiktok(url) {
try {
const headers = {
'user-agent': 'Mozilla/5.0 (Android 15; Mobile; SM-F958; rv:130.0) Gecko/130.0 Firefox/130.0',
'referer': url,
'origin': 'https://www.tiktok.com',
}
const r = await axios.get(url, {
headers
})
const u = r.request.res.responseUrl
const t = getId(u)
const g = await axios.get(`https://www.tiktok.com/embed/v2/${t}`, {
headers,
params: {
lang: 'id',
referrer: u
}
})
const d = g.data.match(/__FRONTITY_CONNECT_STATE__".*?>([\s\S]*?)<\/script/i)?.[1]
if (!d) throw new Error('Failed found JSON data in embed');
const v = JSON.parse(d).source.data
const b = Object.entries(v).find(([n]) => n.includes(t))?.[1]
if (!b) throw new Error('Failed found metadata in here');
const y = b.videoData
const [_a, _b, _c, _d] = [
y.itemInfos,
y.authorInfos,
y.authorStats,
y.musicInfos
]
const res = {
status: true,
title: _a.text,
created_time: new Date(_a.createTime * 1000).toISOString(),
location: _a.locationCreated,
stats: {
digg_count: _a.diggCount,
share_count: _a.shareCount,
play_count: _a.playCount,
comment_count: _a.commentCount
},
covers: _a.covers[1] || _a.covers[0],
author: {
id: _b.userId,
username: _b.uniqueId,
name: _b.nickName,
verified: _b.verified,
bio: _b.signature,
covers: _b.covers[0],
stats: {
following: _c.followingCount,
followers: _c.followerCount,
heart: Number(_c.heartCount),
digg: _c.diggCount,
total_video: _c.videoCount
}
},
music: {
id: _d.musicId,
name: _d.musicName,
author: _d.authorName,
original: _d.original,
covers: _d.covers[0],
url: _d.playUrl[0]
},
type: _a.video.urls?.[0] ? 'video' : 'slide',
video: _a.video.urls?.[0] || null,
image: y.imagePostInfo && y.imagePostInfo.displayImages.map(r => r.urlList[0])
}
return res
} catch(e) {
return {
status: false,
msg: e.stack
}
}
}