forked from robertklep/node-sabnzbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sabnzbd.js
136 lines (117 loc) · 3.66 KB
/
sabnzbd.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
(function() {
var request = require('request');
var URL = require('url');
var util = require('util');
var QS = require('querystring');
var Q = require('q');
// helper classes
var SABnzbdQueue = require('./sabnzbd-queue');
var SABnzbdHistory = require('./sabnzbd-history');
// main SABnzbd class
var SABnzbd = function(url, apikey, verbose) {
this.apikey = apikey;
this.verbose = verbose === true;
this.invalid = false;
// instantiate queue/history classes
this.queue = new SABnzbdQueue(this);
this.history = new SABnzbdHistory(this);
// attach API endpoint to url
if (url.indexOf('/sabnzbd/api') == -1)
url = url.replace(/\/?$/, '/sabnzbd/api');
this.url = url;
// check for valid endpoint
var _this = this;
this.version().then(function(version) {
if (_this.verbose)
util.log('SABnzbd version: ' + version);
}).fail(function(error) {
util.log('Version check failed: ' + error);
_this.invalid = true;
});
// check for valid API key
this.cmd('get_config')
.then(function(response) {
if (response.status === false)
{
util.log("Supplied API key was not accepted by the server");
_this.invalid = true;
}
else
if (_this.verbose)
util.log('SABnzbd accepted supplied API key.');
});
};
// perform command request
SABnzbd.prototype.cmd = function(command, args) {
if (this.invalid)
{
util.log('SABnzbd API invalid because of connection/API key issues');
return false;
}
// build url for request
var url = this.url + '?' + QS.stringify({
mode : command,
apikey : this.apikey,
output : 'json'
});
// tack on any passed arguments
if (args)
url += '&' + QS.stringify(args);
if (this.verbose)
util.log("Retrieving url `" + url + "'");
// perform request
var defer = Q.defer();
request.get(url, function(error, response, body) {
if (error)
defer.reject(error);
else
{
// JSON response?
if (response.headers['content-type'].indexOf('application/json') != -1)
body = JSON.parse(body);
defer.resolve(body);
}
});
// return deferred object
return defer.promise;
};
// get server version
SABnzbd.prototype.version = function() {
return this.cmd('version').then(function(r) {
return r.version;
});
};
// get both queue and history status, merged
SABnzbd.prototype.status = function() {
return Q.all([ this.queue.status(), this.history.status() ]).spread(function(queue, history) {
// merge slots
if (! queue.slots)
queue.slots = [];
history.slots.forEach(function(slot) { queue.slots.push(slot) });
// merge entries
if (! queue.entries)
queue.entries = [];
history.entries.forEach(function(entry) { queue.entries.push(entry) });
return queue;
});
};
// get both queue and history entries
SABnzbd.prototype.entries = function() {
return this.status().then(function(response) {
return response.entries;
});
};
// delete (an) item(s) from both queue and history (or pass 'all' as
// single argument to remove everything)
SABnzbd.prototype.delete = function() {
return Q.all([
this.queue.delete.apply(this.queue, arguments),
this.history.delete.apply(this.history, arguments)
])
.spread(function(queue_status, history_status) {
return { status : queue_status.status || history_status.status };
});
};
// done
module.exports = SABnzbd;
})();