-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmission-restart-errors.10s.js
executable file
·298 lines (262 loc) · 8.47 KB
/
transmission-restart-errors.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/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/async, npm/bytes, npm/transmission</bitbar.dependencies>
// To install
// npm install async bytes transmission
var async = require('async');
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 = '/INPUT/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){
return callback(err);
}
else {
if (result) return callback(null, result.torrents || []);
else return callback(new Error('Invalid result'));
}
});
}
// To start a paused / stopped / error torrent which is still in queue
function startTorrent(id, options, callback){
if (typeof options == 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(err) {};
// start
transmission.start(id, function(err, result){
if (err){
return callback(err);
}
setTimeout(function() {
// recheck
transmission.get(id, function(err, result) {
if (err){
return callback(err);
}
else if (result.torrents && result.torrents.length) {
if (result.torrents[0].status == 3 || result.torrents[0].status == 4) {
// Torrent started
}
}
return callback();
});
}, 2000);
});
}
// To start a paused / stopped / error torrent which is still in queue
function startTorrentNow(id, options, callback){
if (typeof options == 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(err) {};
// start
transmission.startNow(id, function(err, result){
if (err){
return callback(err);
}
setTimeout(function() {
// recheck
transmission.get(id, function(err, result) {
if (err){
// console.log(err);
return callback(err);
}
else if (result.torrents && result.torrents.length) {
if (result.torrents[0].status == 3 || result.torrents[0].status == 4) {
// Torrent started
}
}
return callback();
});
}, 2000);
});
}
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 restartErrorTorrents(torrents, callback) {
var error_torrents = torrents.filter(function(torrent) {
return (torrent.status == 0 && torrent.error);
});
if (error_torrents.length == 0) {
return callback();
}
async.eachSeries(error_torrents, function(torrent, cb) {
// start error torrent
startTorrent(torrent.id, function(err) {
cb(err);
});
}, function(err) {
callback(err);
});
}
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);
// Restart error torrents
restartErrorTorrents(torrents, 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) {});
}