-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
98 lines (93 loc) · 3.54 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
import puppeteer from 'puppeteer'
import inquirer from 'inquirer'
import fs from 'fs-extra'
import axios from 'axios'
import chalk from 'chalk'
(async () => {
const browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--aggressive-cache-discard',
'--disable-cache',
'--disable-application-cache',
'--disable-offline-load-stale-cache',
'--disk-cache-size=0',
'--incognito',
]
});
const info = chalk.hex('#FFFF00');
console.log(chalk.green('Tiktok Mass Download - No Watermark'));
console.log(info("by xtrvts"));
console.log(`===================================`);
const username = await inquirer
.prompt([{
name: 'tiktokTarget',
message: 'Target username?'
}, ])
.then(answers => {
return answers.tiktokTarget.replace('@','');
});
const [page] = await browser.pages();
page.setDefaultNavigationTimeout(0);
await page.goto(`https://www.tiktok.com/@${username}`, {waitUntil: 'networkidle2'});
await page.setViewport({
width: 1200,
height: 800
});
console.log(info(`Get tiktok videos from @${username}`));
console.log(info("Getting all videos..."));
await autoScroll(page);
console.log(chalk.green("Done!"));
let media = await page.$x('//*[@id="app"]/div[2]/div[2]/div/div[2]/div[2]/div/div')
console.log(chalk.green("\nTotal videos:", media.length));
if (!fs.existsSync(`videos`)) fs.mkdirSync(`videos`)
if (!fs.existsSync(`videos/${username}`)) fs.mkdirSync(`videos/${username}`)
for (let i = 1; i <= media.length; i++) {
const link = await page.evaluate(el => el.href, (await page.$x(`//*[@id="app"]/div[2]/div[2]/div/div[2]/div[2]/div/div[${i}]/div[1]/div/div/a`))[0])
console.log(`[${i}] ${link}`)
let filename = link.split("/").pop()+".mp4";
//const getTiktokID = /tiktok\.com(.*)\/video\/(\d+)/gm.exec(link);
let getNowm = await axios.get(`https://api.douyin.wtf/api?url=${link}`).then(resp => {
if(resp.data.status === "success") {
let nowm = resp.data.video_data.nwm_video_url_HQ || resp.data.video_data.nwm_video_url;
return nowm;
}
});
if(getNowm){
await axios.get(getNowm, {
responseType: "stream"
})
.then(response => {
response.data.pipe(fs.createWriteStream(`videos/${username}/${filename}`));
console.log(chalk.green(`[+] Download successfully (${filename})\n`));
})
.catch(error => {
console.log(chalk.red("[!] Failed to download video.\n"));
});
} else {
console.log(chalk.red("[!] Failed to get video link without watermark.\n"));
}
}
await browser.close();
console.log(info("All done!"));
process.exit(0);
})();
async function autoScroll(page){
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
let totalHeight = 0;
let distance = 100;
let timer = setInterval(() => {
let scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 100);
});
});
}