-
Notifications
You must be signed in to change notification settings - Fork 7
/
qbtAPI.js
139 lines (132 loc) · 3.7 KB
/
qbtAPI.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
const request = require("request")
const config = require("./config")
const logger = require("./logger")
class QbtAPI {
constructor() {
this.apiUser = config.qbt.user
this.apiPass = config.qbt.pass
this.apiHost = config.qbt.host
this.apiPort = config.qbt.port
}
/**
* Call to an API command
* @param {string} method HTTP method, like GET or POST
* @param {string} path URL path to the command
* @param {object} data
* @returns {Promise}
*/
call(method, path, data, headers) {
let baseUrl = "http://" + this.apiHost + ":" + this.apiPort
let url = baseUrl + path
// prepare options
let options = {
method: method,
url: url,
headers: {
Referer: baseUrl,
Cookie: this.cookie || ""
}
}
if (method === "POST") {
options.headers["Content-Type"] = "multipart/form-data"
options.formData = data
}
// use parameters headers, if defined
if (typeof headers !== "undefined") {
options.headers = Object.assign(options.headers, headers)
}
// create the return promise
return new Promise((resolve, reject) => {
// call the API
logger.info("Calling: " + options.url)
request(options, (err, res, body) => {
if (err) {
logger.error("API call failed:", err)
reject(err)
return
}
resolve(body)
})
})
}
/**
* Authenticate against qBittorrent API
*/
async authenticate() {
let self = this
let baseUrl = "http://" + this.apiHost + ":" + this.apiPort
let url = baseUrl + "/api/v2/auth/login"
// prepare options
let options = {
method: "POST",
url: url,
headers: {
Referer: baseUrl,
"Content-Type": "application/x-www-form-urlencoded"
},
//formData: "username=" + this.apiUser + "&password=" + this.apiPass
form: {
username: this.apiUser,
password: this.apiPass
}
}
return new Promise(async (resolve, reject) => {
// call the API
logger.info("Calling: " + options.url)
request(options, (err, res, body) => {
if (err) {
logger.error("API call failed:", err)
reject(err)
return
}
// check the headers to see if the Cookie is in them
if (
!res.headers.hasOwnProperty("Set-Cookie") &&
!res.headers.hasOwnProperty("set-cookie")
) {
logger.error("Login error.", body)
reject(body)
} else {
let cookie = res.headers["Set-Cookie"] || res.headers["set-cookie"]
// get the raw string
if (Array.isArray(cookie)) {
cookie = cookie[0]
}
// now remove the unnecessary part
self.cookie = cookie.substring(0, cookie.indexOf(";"))
logger.info("Auth response.", self.cookie)
resolve({ cookie: self.cookie })
}
})
})
}
/**
* Gets the qBittorrent version of the server
*/
getVersion() {
return this.call("GET", "/version/qbittorrent")
}
/**
* This method can add torrents from URLs. http://, https://, magnet: and bc://bt/ links are supported.
* @param {string} urls
* @param {object} params
*/
async download(urls, params) {
// make sure I have an array of URLs
if (typeof urls === "string" || urls instanceof String) {
urls = [urls]
}
if (urls.length > 0) {
let apiData = {
urls: urls.join("\n"),
savepath: params.savepath,
category: params.category || ""
}
await this.call("POST", "/api/v2/torrents/add", apiData)
} else {
// no URLs to download
logger.info("No URLs to download.")
}
}
}
module.exports = QbtAPI