forked from robertklep/node-sabnzbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sabnzbd-queue.js
117 lines (99 loc) · 3.23 KB
/
sabnzbd-queue.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
(function() {
var dateutil = require('dateutil');
var Q = require('q');
// dateutil parser for ETA's
dateutil._parsers['sabnzbd'] = {
test : /^\s*[\d:]+\s+[a-zA-Z]{3}\s+\d+\s+[a-zA-Z]{3}\s*$/,
parse : function(str) {
var m = str.match(/^([\d:]+)\s+(.*)/);
var t = [ m[2], (new Date()).getFullYear(), m[1] ].join(" ");
return dateutil.parse(t);
}
};
// SABnzbd queue class
var SABnzbdQueue = function(delegate) {
this.delegate = delegate;
};
// get queue status
SABnzbdQueue.prototype.status = function() {
var delegate = this.delegate;
var this_ = this;
return delegate.cmd('queue').then(function(response) {
var queue = response.queue || { slots: [] };
// normalize queue slots
queue.entries = queue.slots.map(function(slot) {
return this_.normalize(slot);
});
// done
return queue;
})
};
// get only a list of queue entries
SABnzbdQueue.prototype.entries = function() {
return this.status().then(function(queue) {
return queue.entries;
});
};
// add an NZB url to the queue
SABnzbdQueue.prototype.addurl = function(url) {
return this.delegate.cmd('addurl', { name : url });
};
// pause entire queue (with no argument) or a single download (with
// a single 'id' argument)
SABnzbdQueue.prototype.pause = function(id) {
if (id === undefined)
return this.delegate.cmd('pause');
else
return this.delegate.cmd('queue', { name : 'pause', value : id });
};
// resume entire queue (with no argument) or a single download (with
// a single 'id' argument)
SABnzbdQueue.prototype.resume = function(id) {
if (id === undefined)
return this.delegate.cmd('resume');
else
return this.delegate.cmd('queue', { name : 'resume', value : id });
};
// delete (an) item(s) from the queue (or pass 'all' as single argument
// to remove everything from the queue)
SABnzbdQueue.prototype.delete = function() {
var args = Array.prototype.slice.call(arguments);
return this.delegate.cmd('queue', {
name : 'delete',
value : args.join(",")
});
};
// normalize queue slot
SABnzbdQueue.prototype.normalize = function(slot) {
// parse timeleft
var timeleft = slot.timeleft.replace(/^(\d+):(\d+):(\d+)/, function(all, H, M, S) {
return parseInt(H) * 3600 + parseInt(M) * 60 + parseInt(S);
});
// parse ETA
var eta = slot.eta == 'unknown' ? slot.eta : dateutil.parse(slot.eta);
// parse age
var age = slot.avg_age.replace(/^\s*(\d+).*/, function(a, H) {
return parseInt(H) * 3600;
});
// return a normalized object
return {
_queue_slot : true,
age : parseInt(age),
size : slot.mb * 1000 * 1000,
size_left : slot.mbleft * 1000 * 1000,
nzbid : slot.nzo_id,
category : slot.cat,
eta : eta,
name : slot.filename,
nzbname : slot.filename,
percentage : parseInt(slot.percentage),
index : slot.index,
missing : slot.missing,
priority : slot.priority,
status : slot.status,
time_left : parseInt(timeleft)
};
};
// done
module.exports = SABnzbdQueue;
})();