-
Notifications
You must be signed in to change notification settings - Fork 51
/
app.js
executable file
·126 lines (107 loc) · 2.72 KB
/
app.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
#!/usr/bin/env node
var optimist = require("optimist");
var rc = require("rc");
var clivas = require("clivas");
var numeral = require("numeral");
var progress = require("progress");
var colors = require("colors");
var parsetorrent = require("parse-torrent");
var torrentStream = require("torrent-stream");
var argv = rc(
"tget",
{},
optimist.usage("Usage: $0 magnet-link-or-torrent [,...]").argv
);
var input = argv._;
if (input.length == 0) {
optimist.showHelp();
process.exit(1);
}
var totalLength = null;
var downLength = null;
var bar = null;
var timerId = null;
var prevLength = null;
var engine = null;
var verified = 0;
var downloadedPercentage = 0;
var bytes = function(num) {
return numeral(num).format("0.0b");
};
var setupEngine = function(torrent) {
engine = torrentStream(torrent, {
connections: 100,
path: "."
});
engine.on("verify", function() {
verified++;
downloadedPercentage = Math.floor(
(verified / engine.torrent.pieces.length) * 100
);
});
engine.on("ready", function() {
engine.files.forEach(function(file) {
file.select();
});
totalLengthBytes = engine.files.reduce(function(prevLength, currFile) {
return prevLength + currFile.length;
}, 0);
totalLength = engine.torrent.pieces.length;
speed = bytes(engine.swarm.downloadSpeed()) + "/s";
bar = new progress(
" downloading " +
engine.files.length +
" files (" +
bytes(totalLengthBytes) +
") [:bar] :percent :etas :speed :peers",
{
complete: "=",
incomplete: " ",
width: 30,
total: totalLength
}
);
timerId = setInterval(draw, 500);
});
engine.on("idle", function() {
engine.destroy();
clearInterval(timerId);
clivas.clear();
console.log("------------------");
engine.files.forEach(function(file) {
console.log(file.name + " " + bytes(file.length));
});
console.log("------------------");
console.log(
" downloaded " +
engine.files.length +
" files (" +
bytes(totalLength) +
")"
);
});
};
for (let arg of input) {
parsetorrent.remote(arg, function(err, parsedtorrent) {
if (err) {
console.error(err.message);
process.exit(1);
}
setupEngine(parsedtorrent);
});
};
var draw = function() {
_speed = bytes(engine.swarm.downloadSpeed()) + "/s";
speed = "{green:" + _speed + "}";
peers = engine.swarm.wires.length + " peers";
if (verified >= engine.torrent.pieces.length) {
clivas.clear();
clivas.line(" downloading last few pieces " + speed + " " + peers);
} else {
bar.tick(verified - prevLength, {
speed: _speed.green,
peers: peers
});
}
prevLength = verified;
};