-
Notifications
You must be signed in to change notification settings - Fork 4
/
qbittorrentAPI.js
195 lines (181 loc) · 6.62 KB
/
qbittorrentAPI.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
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');
const ParseTorrent = require('parse-torrent');
const DEFAULT_TRACKER = [
"udp://tracker.opentrackr.org:1337/announce",
"udp://opentracker.i2p.rocks:6969/announce",
"udp://open.demonii.com:1337/announce",
"http://tracker.openbittorrent.com:80/announce",
"udp://tracker.openbittorrent.com:6969/announce",
"udp://open.stealth.si:80/announce",
"udp://tracker.torrent.eu.org:451/announce",
"udp://exodus.desync.com:6969/announce",
"udp://explodie.org:6969/announce",
"udp://uploads.gamecoast.net:6969/announce",
"udp://tracker1.bt.moack.co.kr:80/announce",
"udp://tracker.theoks.net:6969/announce",
"udp://tracker.ccp.ovh:6969/announce",
"udp://tracker.bittor.pw:1337/announce",
"udp://tracker.4.babico.name.tr:3131/announce",
"udp://thouvenin.cloud:6969/announce",
"udp://sanincode.com:6969/announce",
"udp://retracker01-msk-virt.corbina.net:80/announce",
"udp://private.anonseed.com:6969/announce",
"udp://p4p.arenabg.com:1337/announce",
"http://nyaa.tracker.wf:7777/announce"
]
class qbittorrentAPI {
constructor(baseURl, username, password, {...options}) {
this.baseURl = baseURl;
this.auth = `username=${username}&password=${password}`;
this.cookie = null;
this.up_limit = options.UPLOAD_LIMIT;
this.ratio = options.RATIO_LIMIT;
this.include_trackers = options.INCLUDE_TRACKER;
this.block_dl = options.BLOCK_DOWNLOAD;
this.skip_check = options.SKIP_CHECKING;
}
async login() {
return await axios.get(this.baseURl + '?' + this.auth,
{
headers: {
Referer: this.baseURl
}
}).then(res => {
if(res.status == 200) {
this.cookie = res.headers.get('set-cookie')?.join(';');
if(this.cookie)
console.log('Login to qbittorrent success!');
return true;
}
else {
console.log('Login to qbittorrent failed!');
return false;
}
}).catch(err => {
throw new Error('fetch: unknow error on login!', err);
})
}
request(urlPath, options = {}) {
options.headers = {
'Cookie': this.cookie,
...options.headers
}
return axios(this.baseURl + '/api/v2' + urlPath, options);
}
async getTorrentList(options = {
filter: null,
category: null,
tag: null,
sort: null,
reverse: false,
limit: null,
offset: null,
hashes: []
}) {
let data = [];
if(options.filter) data.push('filter=' + options.filter);
if(options.category) data.push('category=' + options.category);
if(options.tag) data.push('tag=' + options.tag);
if(options.sort) data.push('sort=' + options.sort);
if(options.reverse) data.push('reverse=' + options.reverse);
if(options.limit) data.push('limit=' + options.limit);
if(options.offset) data.push('offset=' + options.offset);
if(options.hashes?.length) data.push('hashes=' + options.hashes.join('|'));
return await this.request('/torrents/info?' + encodeURI(data.join('&')))
.then(res => {
if(res.status == 200) {
return res.data;
}
else {
console.log('Get TorrentList Failed!', res.status)
return false;
}
})
.catch(err => {
console.error(err);
})
}
async addTorrentFile(pathFile, saveDir, rename) {
const formData = new FormData();
formData.append('torrents', fs.createReadStream(pathFile));
formData.append('savepath', saveDir);
formData.append('category', 'Stremio Seeds');
formData.append('firstLastPiecePrio', 'true');
if(rename) formData.append('rename', rename);
if(this.up_limit) formData.append('upLimit', this.up_limit);
if(this.ratio) formData.append('ratioLimit', this.ratio);
if(this.block_dl) formData.append('dlLimit', 1024); //qBittorrent only allow set DL Limit to 1KiB/s
if(this.skip_check) formData.append('skip_checking', 'true'); //skip check the torrent hash when added, for performance!
//console.log(formData)
return await this.request('/torrents/add', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data'
},
data: formData
}).then(async res => {
if(res.status == 200) {
if(this.include_trackers) {
const _torrent = ParseTorrent(fs.readFileSync(pathFile));
const _hashes = _torrent.infoHash;
await this.addTrackers(_hashes, DEFAULT_TRACKER);
};
console.log('Add Susceess!');
return true;
}
else {
console.log('Add failed', res.status)
return false;
}
}).catch(err => {
console.error(err);
})
}
async removeTorrents(hashes = Array, deleteFile) {
if(!hashes.length) return;
return await this.request('/torrents/delete', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: 'hashes=' + hashes.join('|') + (deleteFile ? '&deleteFiles=true' : '&deleteFiles=false')
})
.then(res => {
if(res.status == 200) {
console.log('Delete Susceess!');
return true;
}
else {
console.log('Delete Failed!', res.status)
return false;
}
})
.catch(err => {
console.error(err);
})
}
async addTrackers(hashes, trackers = Array) {
if(!trackers.length) return;
return await this.request('/torrents/addTrackers', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: `hash=${hashes}&urls=${trackers.join('\%0A')}`,
}).then(res => {
if(res.status == 200) {
console.log('AddTracker Susceess!');
return true;
}
else {
console.log('AddTracker Failed!', res.status)
return false;
}
}).catch(err => {
console.error(err);
})
}
}
module.exports = qbittorrentAPI;