ryu-js / lib /tiktok /tiktok.js
randydev's picture
Create tiktok/tiktok.js
447b34c verified
raw
history blame
1.83 kB
import got from 'got';
import * as cheerio from 'cheerio';
import { TiktokDlArgsSchema, TiktokDlSchema } from '../types/tiktok-v1.js';
import { DEFAULT_HEADERS } from '../constant.js';
export async function tiktokdl(url) {
TiktokDlArgsSchema.parse(arguments);
const html = await got.post('https://ttsave.app/download', {
headers: {
...DEFAULT_HEADERS,
origin: 'https://ttsave.app'
},
json: {
language_id: '1',
query: url
}
}).text();
const $ = cheerio.load(html);
const $div = $('div.flex');
const nickname = $div.find('h2').text()?.trim() || null;
const username = $div.find('a.font-extrabold').text()?.trim() || null;
const avatar = $div.find('a > img').attr('src') || null;
const description = $div.find('p').text()?.trim() || null;
const $span = $div.find('div.flex > div.flex > span');
const played = $span.eq(0).text()?.trim() || null;
const commented = $span.eq(1).text()?.trim() || null;
const saved = $span.eq(2).text()?.trim() || null;
const shared = $span.eq(3).text()?.trim() || null;
const song = $div.find('div.flex > span').eq(4).text()?.trim() || null;
const $a = $('#button-download-ready > a');
const noWatermark = $a.eq(0).attr('href') || null;
const withWatermark = $a.eq(1).attr('href') || null;
const audio = $a.eq(2).attr('href') || null;
const thumbnail = $a.eq(3).attr('href') || null; // Fixed index
const result = {
nickname,
username,
avatar,
description,
thumbnail,
played,
commented,
saved,
shared,
song,
video: {
noWatermark,
withWatermark
},
audio
};
return TiktokDlSchema.parse(result);
}