forked from karim0sec/Bulk-Tiktoks-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallvideosuser.js
138 lines (115 loc) · 4.79 KB
/
allvideosuser.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
const { url } = require("inspector");
const { abort, nextTick } = require("process");
const puppeteer = require("puppeteer-extra");
const { PassThrough } = require("stream");
const stealthPlugin = require("puppeteer-extra-plugin-stealth")();
["chrome.runtime", "navigator.languages"].forEach(a =>
stealthPlugin.enabledEvasions.delete(a)
);
puppeteer.use(stealthPlugin);
main();
async function main() {
const browser = await puppeteer.launch( { headless: true } );
const page = await browser.newPage();
await page.evaluateOnNewDocument(() => {
delete navigator.__proto__.webdriver;
});
//We stop images and stylesheet to save data
await page.setRequestInterception(true);
page.on('request', (request) => {
if(['image', 'stylesheet', 'font'].includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
})
const args = process.argv.slice(2)
const userLink = args[0]
await page.goto(userLink); //change this to user url page
let username = page.url().slice(23,).replace(/[-:.\/*<>|?]/g,"");
//scroll down until no more videos
await autoScroll(page);
const urls = await page.evaluate(() =>
Array.from(document.querySelectorAll('div.tiktok-1qb12g8-DivThreeColumnContainer > div > div > div > div > div > a'), element => element.href));
var videoDes = await page.evaluate(() =>Array.from(document.querySelectorAll('div.tiktok-1qb12g8-DivThreeColumnContainer.eegew6e2 > div > div > div > a')).map((items) => items.innerText))
for (var i=videoDes.length; i--;) {
videoDes[i] = videoDes[i] + ' #shorts' + "\r\n" ;}; //append #shorts for each video title
const fs = require('fs');
fs.appendFile('names.txt', videoDes + '', function (err) {
if (err) throw err;
console.log('Descriptions Saved!');
});
console.log('now it downloading ' +urls.length+ ' video' )
//loop on snaptik for no watermark tiktok videos
//becareful that can be alot of gigas if profile has a lot of videos
for (var i=0;i<urls.length;i++) //you can limit number of videos by replace url.length by number
{
function getRandomNumber() {
var random = Math.floor(Math.random() * (500 - 300 + 1)) + 300;
return random;
};
function getHighNumber() {
var random = Math.floor(Math.random() * (500 - 300 + 1)) + 1150;
return random;
};
await page.waitForTimeout(getHighNumber());
await page.goto('https://snaptik.app/');
await page.waitForTimeout(getRandomNumber());
await page.waitForSelector('input[name="url"]');
await page.type('input[name="url"]' , (urls[i]) , {delay: 100}); //type result of links
let link = (urls[i]).slice(-19)
await page.waitForTimeout(getRandomNumber());
// await page.keyboard.press('Enter');
await page.click('#submiturl')
await page.waitForTimeout(getHighNumber());
await page.waitForXPath('//*[@id="download-block"]/div/a[1]');
const snaptik = await page.$eval("#download-block > div > a:nth-child(1)", (elm) => elm.href);
const param = snaptik.split(".");
const decode = Buffer.from(param[3], 'base64').toString('ascii').split('"');
const content = decodeURIComponent(decode[3]);
const https = require('https');
const ds = require('fs');
// link to file you want to download
const path = './'+username+'/'; // location to save videos
try {
if (!ds.existsSync(path)) {
ds.mkdirSync(path)
}
} catch (err) {
console.error(err)
}
const request = https.get(content, function(response) {
if (response.statusCode === 200) {
var file = ds.createWriteStream(path+link+'.mp4');
response.pipe(file);
console.log(file.path + ' Saved!')
const fs = require('fs');
fs.appendFile('names.txt',file.path + "\r\n" , function (err) {
if (err) throw err;
console.log('Done');
});
}
// request.setTimeout(60000, function() { // if after 60s file not downlaoded, we abort a request
// request.destroy();
// });
});
;};
browser.close();
}
async function autoScroll(page){
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
var totalHeight = 0;
var distance = 100;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 100);
});
});
}