-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmission.10s.js
executable file
·214 lines (187 loc) · 6.41 KB
/
transmission.10s.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
// <bitbar.title>Transmission Monitor</bitbar.title>
// <bitbar.author>Jul11Co</bitbar.author>
// <bitbar.author.github>Jul11Co</bitbar.author.github>
// <bitbar.desc>Monitor Tranmission torrent downloads</bitbar.desc>
// <bitbar.dependencies>node, npm, npm/bytes, npm/transmission</bitbar.dependencies>
// To install
// npm install bytes transmission
var bytes = require('bytes');
var Transmission = require('transmission');
var transmission = new Transmission({
host: 'localhost', // default: 'localhost'
port: 9091, // default: 9091
username: 'transmission', // default: blank
password: '12345678' // default: blank
});
var cheek_free_space = false;
var remote_path = '/REMOTE/PATH/TO/CHECK';
var free_space_str = '';
// console.log("🐶");console.log('---');
// Get torrent state
function getStatusType(type){
if(type === 0){
return 'STOPPED';
} else if(type === 1){
return 'CHECK_WAIT';
} else if(type === 2){
return 'CHECK';
} else if(type === 3){
return 'DOWNLOAD_WAIT';
} else if(type === 4){
return 'DOWNLOAD';
} else if(type === 5){
return 'SEED_WAIT';
} else if(type === 6){
return 'SEED';
} else if(type === 7){
return 'ISOLATED';
}
// return transmission.statusArray[type]
}
function trimText(input, max_length) {
if (!input || input == '') return '';
max_length = max_length || 60;
var output = input.trim();
if (output.length > max_length) {
output = output.substring(0, max_length) + '...';
}
return output;
}
function getAllTorrents(options, callback){
if (typeof options == 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(err) {};
transmission.get(function(err, result){
if (err){
// console.log(err);
return callback(err);
}
else {
// console.log(result);
if (result) return callback(null, result.torrents || []);
else return callback(new Error('Invalid result'));
}
});
}
function getFreeSpace(remote_path, options, callback) {
if (typeof options == 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(err) {};
// get Free Space
transmission.freeSpace(remote_path, function(err, result){
if (err){
return callback(err);
}
callback(null, result);
});
}
///
function printActiveTorrents(torrents) {
var active_torrents = torrents.filter(function(torrent) {
return (torrent.status >= 3 && torrent.status <= 6);
});
active_torrents.sort(function(a,b) {
if (a.percentDone>b.percentDone) return -1;
if (a.percentDone<b.percentDone) return 1;
return 0;
});
active_torrents.sort(function(a,b) {
if (a.status>b.status) return 1;
if (a.status<b.status) return -1;
return 0;
});
active_torrents.forEach(function(torrent, idx) {
var percentage = (torrent.percentDone*100).toFixed(1);
var prefix = '';
if (idx >= 10) prefix = '--';
if (idx == 10 && active_torrents.length > 10) {
console.log('More (' + (active_torrents.length-10) + ')...');
}
if (torrent.status == 3 && torrent.error) {
console.log(prefix + '⏸ ' + bytes(torrent.totalSize) + ' ' + percentage + '% '
+ trimText(torrent.name,30) + ' | color=red');
console.log(prefix + ' ' + bytes(torrent.totalSize) + ', ' + percentage + '% | color=grey');
console.log(prefix + "Error: "+ trimText(torrent.errorString,40) + ' | color=grey');
}
else if (torrent.status == 3) {
console.log(prefix + '⏸ ' + trimText(torrent.name,30) + ' | color=orange');
console.log(prefix + ' ' + bytes(torrent.totalSize) + ', ' + percentage + '% | color=grey');
}
else if (torrent.status == 4) {
console.log(prefix + '⏬ ' + trimText(torrent.name,30) + ' | color=blue');
console.log(prefix + ' ' + bytes(torrent.totalSize) + ', ' + percentage + '%, '
+ " ▼ " + bytes(torrent.rateDownload) + '/s, ▲ ' + bytes(torrent.rateUpload) + '/s | color=grey');
}
else if (torrent.status == 6) {
console.log(prefix + '⏫ ' + trimText(torrent.name,30) + ' | color=green');
console.log(prefix + ' ' + bytes(torrent.totalSize) + ', ' + percentage + '%, '
+ " ▼ " + bytes(torrent.rateDownload) + '/s, ▲ ' + bytes(torrent.rateUpload) + '/s | color=grey');
}
else if (torrent.status == 5) {
console.log(prefix + '⏸ ' + trimText(torrent.name,30) + ' | color=green');
console.log(prefix + ' ' + bytes(torrent.totalSize) + ', ' + percentage + '% | color=grey');
}
console.log(prefix+'---');
});
}
function update(done) {
// get all torrents
getAllTorrents(function(err, torrents) {
if (err) {
// console.log(err);
return done(err);
} else if (!torrents || torrents.length == 0) {
console.log("�");console.log('---');
if (free_space_str) {
console.log(free_space_str);
console.log('---');
}
console.log('No torrents | color=black');
return done();
}
var torrent_stats = {};
torrents.forEach(function(torrent) {
if (torrent.status >= 0 && torrent.status <= 7) {
var status = getStatusType(torrent.status);
torrent_stats[status] = torrent_stats[status] || [];
torrent_stats[status].push(torrent);
}
});
var downloading = torrent_stats['DOWNLOAD'] ? torrent_stats['DOWNLOAD'].length : 0;
var seeding = torrent_stats['SEED'] ? torrent_stats['SEED'].length : 0;
console.log("🐶 " + downloading + '/' + seeding);console.log('---');
if (free_space_str) {
console.log(free_space_str);
console.log('---');
}
for (var status in torrent_stats) {
console.log(status + ': ' + torrent_stats[status].length + '');
torrent_stats[status].forEach(function(torrent) {
var percentage = (torrent.percentDone*100).toFixed(1);
console.log('--[' + bytes(torrent.totalSize) + '] ' + percentage + '% '
+ trimText(torrent.name,30) + ' | color=black');
});
// console.log('---');
}
console.log('---');
printActiveTorrents(torrents);
return done();
});
}
if (cheek_free_space) {
getFreeSpace(remote_path, function(err, result) {
if (result && result['size-bytes']) {
free_space_str = 'Free Space: ' + bytes(result['size-bytes']);
}
update(function(err) {});
});
} else {
update(function(err) {});
}