forked from phillipj/node-transmission
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
71 lines (61 loc) · 1.33 KB
/
example.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
var Transmission = require('./');
var ProgressBar = require('progress');
var transmission = new Transmission({
port : 9091,
host : '127.0.0.1'
});
function get(hash, cb) {
transmission.get(hash, function(err, result) {
if (err) {
throw err;
}
cb(null, result.torrents[0]);
});
}
function watch(hash) {
get(hash, function(err, torrent) {
if (err) {
throw err;
}
var downloadedEver = 0;
var WatchBar = new ProgressBar(' downloading [:bar] :percent :etas', {
complete : '=',
incomplete : ' ',
width : 35,
total : torrent.sizeWhenDone
});
function tick(err, torrent) {
if (err) {
throw err;
}
var downloaded = torrent.downloadedEver - downloadedEver;
downloadedEver = torrent.downloadedEver;
WatchBar.tick(downloaded);
if (torrent.sizeWhenDone === torrent.downloadedEver) {
return remove(hash);
}
setTimeout(function() {
get(hash, tick);
}, 1000);
}
get(hash, tick);
});
}
function remove(hash) {
transmission.remove(hash, function(err) {
if (err) {
throw err;
}
console.log('torrent was removed');
});
}
var sample = 'http://releases.ubuntu.com/14.04.1/ubuntu-14.04.1-desktop-amd64.iso.torrent';
transmission.addUrl(sample, {
//options
}, function(err, result) {
if (err) {
return console.log(err)
}
var hash = result.hashString;
watch(hash);
});