This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
271 lines (241 loc) · 8.33 KB
/
index.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const puppeteer = require('puppeteer-extra');
const inquirer = require("inquirer");
const chalk = require("chalk");
const { resolve } = require("path");
const fetch = require("node-fetch");
const fs = require("fs");
const {Headers} = require('node-fetch');
const UserAgent = require('user-agents');
//set a user-agent for fetch & pptr
const headers = new Headers();
const userAgent = new UserAgent({ platform: 'Win32' }).toString();
headers.append('User-Agent', 'TikTok 26.2.0 rv:262018 (iPhone; iOS 14.4.2; en_US) Cronet');
const headersWm = new Headers();
headersWm.append('User-Agent', userAgent);
const getChoice = () => new Promise((resolve, reject) => {
inquirer.prompt([
{
type: "list",
name: "choice",
message: "Choose a option",
choices: ["Mass Download (Username)", "Mass Download (URL)", "Single Download (URL)"]
},
{
type: "list",
name: "type",
message: "Choose a option",
choices: ["With Watermark", "Without Watermark"]
}
])
.then(res => resolve(res))
.catch(err => reject(err));
});
const getInput = (message) => new Promise((resolve, reject) => {
inquirer.prompt([
{
type: "input",
name: "input",
message: message
}
])
.then(res => resolve(res))
.catch(err => reject(err));
});
const generateUrlProfile = (username) => {
var baseUrl = "https://www.tiktok.com/";
if (username.includes("@")) {
baseUrl = `${baseUrl}${username}`;
} else {
baseUrl = `${baseUrl}@${username}`;
}
return baseUrl;
};
const downloadMediaFromList = async (list) => {
const folder = "downloads/"
try {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder)
}
} catch (err) {
console.error(err)
}
list.forEach((item) => {
const fileName = `${item.id}.mp4`
const downloadFile = fetch(item.url)
const file = fs.createWriteStream(folder + fileName)
console.log(chalk.green(`[+] Downloading ${fileName}`))
downloadFile.then(res => {
res.body.pipe(file)
file.on("finish", () => {
file.close()
resolve()
});
file.on("error", (err) => reject(err));
});
});
}
const getVideoWM = async (url) => {
const idVideo = await getIdVideo(url)
const request = await fetch(url, {
method: "GET",
headers:headersWm
});
const res = await request.text()
const urlMedia = res.toString().match(/\{"url":"[^"]*"/g).toString().split('"')[3].replace(/\\u002F/g, "/");
const data = {
url: urlMedia,
id: idVideo
}
return data
}
const getVideoNoWM = async (url) => {
const idVideo = await getIdVideo(url)
const API_URL = `https://api19-core-useast5.us.tiktokv.com/aweme/v1/feed/?aweme_id=${idVideo}&version_code=262&app_name=musical_ly&channel=App&device_id=null&os_version=14.4.2&device_platform=iphone&device_type=iPhone9`;
const request = await fetch(API_URL, {
method: "GET",
headers : headers
});
const body = await request.text();
try {
var res = JSON.parse(body);
} catch (err) {
console.error("Error:", err);
console.error("Response body:", body);
}
// const res = await request.json()
const urlMedia = res.aweme_list[0].video.play_addr.url_list[0]
const data = {
url: urlMedia,
id: idVideo
}
return data
}
//// incase api fails
// const getVideoNoWM = async (url) => {
// const idVideo = await getIdVideo(url)
// var form = new FormData();
// form.append('id',url);
// const ssstik = 'https://ssstik.io/abc?url=dl'
// const request = await fetch(ssstik, {
// method: "POST",
// headers: headers,
// body: form,
// });
// const res = await request.text()
// const urlMedia = await res.match(/(https):\/\/[a-zA-Z0-9./?=_%:-]*/g)[2].toString()
// const data = {
// url: urlMedia,
// id: idVideo
// }
// return data
// }
const getListVideoByUsername = async (username) => {
var baseUrl = await generateUrlProfile(username)
if (baseUrl.includes("tiktok.com/http")){
baseUrl = baseUrl.slice(23)
} else {
baseUrl = baseUrl
}
const browser = await puppeteer.launch({
headless:true,
executablePath:require("puppeteer").executablePath(),
args: ["--no-sandbox"]
})
const page = await browser.newPage()
await page.setRequestInterception(true);
page.on('request', (request) => {
if(['image', 'stylesheet', 'font'].includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
})
page.setUserAgent(userAgent);
await page.goto(baseUrl)
var listVideo = []
console.log(chalk.green("[*] Getting list video from: " + username))
var loop = true
while(loop) {
listVideo = await page.evaluate(() => {
const listVideo = Array.from(document.querySelectorAll(".tiktok-yz6ijl-DivWrapper > a"));
return listVideo.map(item => item.href);
});
console.log(chalk.green(`[*] ${listVideo.length} video found`))
previousHeight = await page.evaluate("document.body.scrollHeight");
await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
await page.waitForFunction(`document.body.scrollHeight > ${previousHeight}`, {timeout: 10000})
.catch(() => {
console.log(chalk.red("[X] No more video found"));
console.log(chalk.green(`[*] Total video found: ${listVideo.length}`))
loop = false
});
await new Promise((resolve) => setTimeout(resolve, 1000));
}
await browser.close()
return listVideo
}
const getRedirectUrl = async (url) => {
if(url.includes("vm.tiktok.com") || url.includes("vt.tiktok.com")) {
url = await fetch(url, {
redirect: "follow",
follow: 10,
});
url = url.url;
console.log(chalk.green("[*] Redirecting to: " + url));
}
return url;
}
const getIdVideo = (url) => {
const matching = url.includes("/video/")
if(!matching){
console.log(chalk.red("[X] Error: URL not found"));
exit();
}
const idVideo = url.substring(url.indexOf("/video/") + 7, url.length);
return (idVideo.length > 19) ? idVideo.substring(0, idVideo.indexOf("?")) : idVideo;
}
(async () => {
const header = "\rTiktokDL by https://github.com/karim0sec \n"
console.log(chalk.magenta(header))
const choice = await getChoice();
var listVideo = [];
var listMedia = [];
// var listVideoDes = []
if (choice.choice === "Mass Download (Username)") {
const usernameInput = await getInput("Enter the username with @ (e.g. @username) : ");
const username = usernameInput.input;
listVideo = await getListVideoByUsername(username);
if(listVideo.length === 0) {
console.log(chalk.yellow("[!] Error: No video found"));
process.exit();
}
} else if (choice.choice === "Mass Download (URL)") {
var urls = [];
const count = await getInput("Enter the number of URL : ");
for(var i = 0; i < count.input; i++) {
const urlInput = await getInput("Enter the URL : ");
urls.push(urlInput.input);
}
for(var i = 0; i < urls.length; i++) {
const url = await getRedirectUrl(urls[i]);
const idVideo = await getIdVideo(url);
listVideo.push(idVideo);
}
} else {
const urlInput = await getInput("Enter the URL : ");
const url = await getRedirectUrl(urlInput.input);
listVideo.push(url);
}
console.log(chalk.green(`[!] Found ${listVideo.length} video`));
for(var i = 0; i < listVideo.length; i++){
var data = (choice.type == "With Watermark") ? await getVideoWM(listVideo[i]) : await getVideoNoWM(listVideo[i]);
listMedia.push(data);
}
downloadMediaFromList(listMedia)
.then(() => {
console.log(chalk.green("[+] Downloaded successfully"));
})
.catch(err => {
console.log(chalk.red("[X] Error: " + err));
});
})();