randydev commited on
Commit
447b34c
·
verified ·
1 Parent(s): f2a748a

Create tiktok/tiktok.js

Browse files
Files changed (1) hide show
  1. lib/tiktok/tiktok.js +60 -0
lib/tiktok/tiktok.js ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import got from 'got';
2
+ import * as cheerio from 'cheerio';
3
+ import { TiktokDlArgsSchema, TiktokDlSchema } from '../types/tiktok-v1.js';
4
+ import { DEFAULT_HEADERS } from '../constant.js';
5
+
6
+ export async function tiktokdl(url) {
7
+ TiktokDlArgsSchema.parse(arguments);
8
+
9
+ const html = await got.post('https://ttsave.app/download', {
10
+ headers: {
11
+ ...DEFAULT_HEADERS,
12
+ origin: 'https://ttsave.app'
13
+ },
14
+ json: {
15
+ language_id: '1',
16
+ query: url
17
+ }
18
+ }).text();
19
+
20
+ const $ = cheerio.load(html);
21
+ const $div = $('div.flex');
22
+
23
+ const nickname = $div.find('h2').text()?.trim() || null;
24
+ const username = $div.find('a.font-extrabold').text()?.trim() || null;
25
+ const avatar = $div.find('a > img').attr('src') || null;
26
+ const description = $div.find('p').text()?.trim() || null;
27
+
28
+ const $span = $div.find('div.flex > div.flex > span');
29
+ const played = $span.eq(0).text()?.trim() || null;
30
+ const commented = $span.eq(1).text()?.trim() || null;
31
+ const saved = $span.eq(2).text()?.trim() || null;
32
+ const shared = $span.eq(3).text()?.trim() || null;
33
+ const song = $div.find('div.flex > span').eq(4).text()?.trim() || null;
34
+
35
+ const $a = $('#button-download-ready > a');
36
+ const noWatermark = $a.eq(0).attr('href') || null;
37
+ const withWatermark = $a.eq(1).attr('href') || null;
38
+ const audio = $a.eq(2).attr('href') || null;
39
+ const thumbnail = $a.eq(3).attr('href') || null; // Fixed index
40
+
41
+ const result = {
42
+ nickname,
43
+ username,
44
+ avatar,
45
+ description,
46
+ thumbnail,
47
+ played,
48
+ commented,
49
+ saved,
50
+ shared,
51
+ song,
52
+ video: {
53
+ noWatermark,
54
+ withWatermark
55
+ },
56
+ audio
57
+ };
58
+
59
+ return TiktokDlSchema.parse(result);
60
+ }