-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebtorrent.js
49 lines (40 loc) · 1.27 KB
/
webtorrent.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
import fs from "node:fs";
import Webtorrent from "webtorrent";
import { ProgressBar } from "@opentf/cli-pbar";
import { RarFilesPackage } from "rar-stream";
import prettyBytes from "pretty-bytes";
const client = new Webtorrent();
const magnetURI = process.argv[2];
const pBar = new ProgressBar({
prefix: "Downloading:",
});
client.add(magnetURI, { path: "." }, async (torrent) => {
// Got torrent metadata!
console.log("Client is downloading:", torrent.infoHash);
const rarStreamPackage = new RarFilesPackage(torrent.files);
rarStreamPackage.on("file-parsed", (file) =>
console.log(`Parsed file: ${file.name}`),
);
const innerFiles = await rarStreamPackage.parse();
const [innerFile] = innerFiles.filter(
(inner) => inner.name.indexOf("mkv") !== -1,
);
console.log(`Found file: ${innerFile.name}`);
pBar.start({ total: innerFile.length - 1 });
const fileStream = innerFile
.createReadStream({
start: 0,
end: innerFile.length - 1,
})
.pipe(fs.createWriteStream(innerFile.name));
setInterval(() => {
pBar.update({
value: fileStream.bytesWritten,
suffix: `${prettyBytes(client.downloadSpeed, {space: false})}/s`,
});
}, 500);
fileStream.on("close", () => {
pBar.stop();
process.exit();
});
});