forked from wahdalo/tiktok-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (62 loc) · 2.26 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
import { TiktokDL } from './lib/ttapi.js'
import { ReelsUpload } from './lib/browserHandler.js'
import axios from 'axios'
import ProgressBar from 'progress'
import chalk from 'chalk'
import path from 'path'
import fs from 'fs'
import readlineSync from 'readline-sync'
async function downloadAndUpload(url) {
try {
const result = await TiktokDL(url)
const video = result.result.video[0]
const namafile = result.result.id
const caption = result.result.description
if (fs.existsSync(path.resolve('download', `${namafile}.mp4`))) {
console.log(`[ ${chalk.hex('#f12711')(namafile)} already downloaded! ] ===== [${chalk.hex('#7F7FD5')('skipped')}]`);
// await ReelsUpload(namafile, caption)
} else {
await axios({
url: video,
method: 'GET',
responseType: 'stream'
}).then(async ({ data, headers }) => {
if (!fs.existsSync('download')) fs.mkdirSync('download')
const totalLength = headers['content-length']
const progressBar = new ProgressBar(`[ ${chalk.hex('#ffff1c')("Proses Download")} ] [${chalk.hex('#6be585')(':bar')}] :percent downloaded in :elapseds`, {
width: 40,
complete: '<',
incomplete: '•',
renderThrottle: 1,
total: parseInt(totalLength)
})
data.on('data', (chunk) => {
progressBar.tick(chunk.length)
})
const writer = fs.createWriteStream(path.resolve('download', `${namafile}.mp4`))
data.pipe(writer)
data.on('end', async () => {
await ReelsUpload(namafile, caption)
})
})
}
} catch (err) {
console.log(err)
}
}
function processUrlList(filePath) {
const urls = fs.readFileSync(filePath, 'utf8').split('\n');
for (const url of urls) {
if (url) downloadAndUpload(url.trim());
}
}
const choice = readlineSync.question('Do you want to enter a single URL or a list of URLs? (single/list): ');
if (choice.toLowerCase() === 'single') {
const url = readlineSync.question('Enter the TikTok URL: ');
downloadAndUpload(url);
} else if (choice.toLowerCase() === 'list') {
const filePath = readlineSync.question('Enter the path to your list file: ');
processUrlList(filePath);
} else {
console.log('Invalid input. Please enter "single" or "list".');
}