-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (50 loc) · 1.47 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
#!/usr/bin/env node
const WebTorrent = require("webtorrent-hybrid");
const fs = require("fs");
const os = require("os");
const hash = process.argv[2];
const client = new WebTorrent();
const cliProgress = require("cli-progress");
const bar = new cliProgress.SingleBar({}, cliProgress.Presets.rect);
if (!hash) {
console.log("You must enter your torrent link as an argument");
console.log("./torrent-downloader-cli LINK");
process.exit();
}
const getDownloadsDirectory = () => {
// return process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
return os.homedir() + "/Downloads";
};
try {
client.add(hash, (torrent) => {
const files = torrent.files;
let length = files.length;
console.log(`Files Count: ${length}`);
console.log(
`Downloading in your Downloads directory: "${getDownloadsDirectory()}"`
);
bar.start(100, 0);
let interval = setInterval(() => {
bar.update(torrent.progress * 100);
}, 1000);
files.forEach((file) => {
const source = file.createReadStream();
const dest = fs.createWriteStream(
`${getDownloadsDirectory()}/${file.name}`
);
source
.on("end", () => {
console.log(`\n----\nfile "${file.name}" downloaded.\n`);
length -= 1;
if (!length) {
bar.stop();
clearInterval(interval);
process.exit();
}
})
.pipe(dest);
});
});
} catch (err) {
console.error(`Error: ${err}`);
}