-
Notifications
You must be signed in to change notification settings - Fork 21
/
handler.js
215 lines (194 loc) · 8.04 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const { default: axios } = require('axios');
const { createCipheriv } = require('node:crypto')
const fs = require('fs');
const ProgressBar = require('progress')
const path = require('path')
const async = require('async');
const chalk = require('chalk');
const delay = ms => new Promise((resolve) => setTimeout(resolve, ms))
const crypto = require('crypto');
const { TikTokClient } = require('./node_modules/tiktok-private-api/build/index')
global.task
function convertCookie(cookies) {
let cookie;
try {
cookie = JSON.parse(cookies).map(x => `${x.name}=${x.value}`).join('; ')
} catch (error) {
cookie = cookies
}
return cookie
}
let cookie = fs.existsSync('cookie') ? convertCookie(fs.readFileSync('cookie', 'utf-8')) : ''
let headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35',
cookie
}
// https://stackoverflow.com/questions/57362319/how-to-download-files-one-by-one-in-node-js
class Downloader {
constructor(folder) {
this.folder = folder
this.q = async.queue(this.singleFile, 1);
this.q.concurrency = 1
this.q.pause
this.taskDone = 0
this.done = false
// assign a callback
this.q.drain(() => {
console.log('All Videos downloaded successfully');
this.done = true
});
// assign an error callback
this.q.error(function (err, task) {
console.log(err);
console.error('task experienced an error', task);
});
}
async downloadFiles(links) {
for (let link of links) {
this.q.push(link);
}
}
singleFile = async (link, cb) => {
// console.log(this.taskDone);
let foldername = this.folder
const mediaPath = link.type == 'image' ? `download/${foldername}/${link.title}` : `download/${foldername}`
const fileName = link.type == 'image' ? `${link.id}_${link.img_index}.jpeg` : `${link.id}.mp4`
if (!fs.existsSync('download')) fs.mkdirSync('download')
if (!fs.existsSync(mediaPath)) {
fs.mkdirSync(mediaPath)
}
if (fs.existsSync(mediaPath + '/' + fileName)) {
console.log(`[${chalk.hex('#99f2c8')(link.index)}] [ ${chalk.hex('#f12711')(link.id)} ] [ ${chalk.hex('#f12711')('already downloaded!')} ] ==> [${chalk.hex('#7F7FD5')('skipped')}]`);
this.q.remove(x => x.data.id == link.id)
this.q.resume()
} else {
this.q.pause()
try {
const url = link.type == 'video' ? await getDownloadLink(link.id) : link.img_url
await axios({
url,
method: 'GET',
responseType: 'stream'
}).then(async ({ data, headers }) => {
const totalLength = headers['content-length']
let log = `[${chalk.hex('#FCE4EC')(link.index)}] ` +
`[ ${chalk.hex('#00B0FF')(link.id)} ] ` +
`[ ${link.type == 'video' ? chalk.hex('#916eff')('video') : chalk.hex('#aaf255')('image')} ${link.type == 'video' ? '' : `${chalk.hex('#ffff1c')(`${link.img_index}`)}`} ] ` +
`[ ${chalk.hex('#6be585')(':bar')}] ` +
`:percent downloaded in :elapseds`
const progressBar = new ProgressBar(log, {
width: 40,
complete: '<',
incomplete: '•',
renderThrottle: 10,
total: parseInt(totalLength)
})
data.on('data', (chunk) => {
progressBar.tick(chunk.length)
})
data.on('end', () => {
this.q.resume()
this.taskDone = this.taskDone + 1
})
// const writer = fs.createWriteStream(path.resolve(__dirname, 'download', foldername, `${link.id}.mp4`))
const writer = fs.createWriteStream(mediaPath + '/' + fileName)
data.pipe(writer)
})
} catch (error) {
console.log(`[ ${chalk.hex('#f12711')(link.id)} got error while trying to get video data! ] ===== [${chalk.hex('#7F7FD5')('skipped')}]`);
this.q.resume()
}
}
}
}
const formatK = (n) => {
return Number(n).toLocaleString('en', { notation: 'compact' })
}
// fixed by using this code : https://github.com/mominkali/tikdate/blob/3bf790be9ae19f727e2956f0764b0f0fff3bf21e/tikdate.py
async function searchUser(username) {
try {
let ob = {
WebIdLastTime: '1688494715',
aid: '1988',
app_language: 'en',
app_name: 'tiktok_web',
browser_language: 'en-US',
browser_name: 'Mozilla',
browser_online: 'true',
browser_platform: 'Win32',
browser_version: '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
channel: 'tiktok_web',
cookie_enabled: 'true',
cursor: '0',
device_id: '7252029459376145938',
device_platform: 'web_pc',
focus_state: 'true',
from_page: 'search',
history_len: '6',
is_fullscreen: 'false',
is_page_visible: 'true',
keyword: username,
os: 'windows',
priority_region: 'ID',
referer: '',
region: 'ID',
screen_height: '1080',
screen_width: '1920',
tz_name: 'Asia/Jakarta',
}
const { data } = await axios.get(`https://www.tiktok.com/api/search/user/full/?${new URLSearchParams(ob).toString()}`, {
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35',
cookie
},
});
return data.user_list;
} catch (error) {
console.log(error);
}
}
async function getVideoList(secUid, count = 30, cursor = 0) {
try {
const TikTokApi = new TikTokClient();
TikTokApi.state.defaultApiHeaders = {
...TikTokApi.state.defaultApiHeaders,
cookie,
};
const data = await TikTokApi.user.videos('', secUid, count, cursor)
// console.log(data);
return data
} catch (error) {
console.log(error);
}
}
// getVideoList('MS4wLjABAAAAHYX7r5EzjxLQ7whZLAwpscERO7k4L18xgWqt0ShUj29dsupS6eH8LtCVWPVPDOzP').then(x => {
// console.log(x.itemList)
// })
// const { TikTokClient } = require('./node_modules/tiktok-private-api/build/index');
// (async () => {
// const TikTokApi = new TikTokClient();
// TikTokApi.state.defaultHeaders = {
// ...TikTokApi.state.defaultHeaders,
// "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
// "sec-ch-ua": '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
// };
// // TikTokApi.state.defaultApiHeaders.cookie = cookie
// // TikTokApi.state.defaultHeaders.cookie = cookie
// const usr = await TikTokApi.user.info('_nvtaaa.a')
// console.log(usr);
// const data = await TikTokApi.user.videos(usr.userInfo.id, usr.userInfo.secUid, 30, 0)
// console.log(data);
// // TikTokApi.user.videos('MS4wLjABAAAAHYX7r5EzjxLQ7whZLAwpscERO7k4L18xgWqt0ShUj29dsupS6eH8LtCVWPVPDOzP', '0', '0').then(x => console.log(x))
// })();
async function getDownloadLink(id) {
const res = await axios.get('https://api16-normal-c-useast2a.tiktokv.com/aweme/v1/feed/?aweme_id=' + id, { headers })
const filtered = res.data.aweme_list.find(x => x.aweme_id == id)
return filtered.video.play_addr.url_list[0]
}
module.exports = {
Downloader,
formatK,
searchUser,
getVideoList,
getDownloadLink
}