UNPKG

1.85 kBJavaScriptView Raw
1const fetch = require('node-fetch');
2const { print, l10n } = require('../..');
3const url = require('url');
4
5module.exports = async (thread, config) => {
6 print.debug('dmhy:downloaders:qbittorrent:thread', thread);
7 print.debug('dmhy:downloaders:qbittorrent:config', config);
8
9 try {
10 await fetch(config['qbittorrent-url']);
11 } catch (e) {
12 print.error(l10n('QBITTORRENT_WEBUI_NOT_AVAILABLE'));
13 print.error(l10n('DOWNLOADER_DL_FAILED', { title: thread.title }));
14 return;
15 }
16 const [username, password] = config['qbittorrent-auth'].split(':');
17 const loginParams = new URLSearchParams();
18 loginParams.append('username', username);
19 loginParams.append('password', password);
20 const loginResponse = await fetch(url.resolve(config['qbittorrent-url'], '/login'), {
21 method: 'POST',
22 body: loginParams.toString(),
23 headers: {
24 'Content-Type': 'application/x-www-form-urlencoded',
25 },
26 });
27 const setCookie = loginResponse.headers.get('set-cookie');
28 if (!setCookie) {
29 print.error(l10n('QBITTORRENT_LOGIN_FAILED'));
30 print.error(l10n('DOWNLOADER_DL_FAILED', { title: thread.title }));
31 return;
32 }
33 const sid = setCookie.split(' ')[0]; // Get the first part "SID=xxxx;"
34 const downloadParams = new URLSearchParams();
35 downloadParams.append('urls', thread.link);
36 downloadParams.append('savepath', config['destination']);
37 const downloadResponse = await fetch(url.resolve(config['qbittorrent-url'], '/command/download'), {
38 method: 'POST',
39 headers: {
40 'Cookie': sid,
41 'Content-Type': 'application/x-www-form-urlencoded',
42 },
43 body: downloadParams.toString(),
44 });
45 if ((await downloadResponse.text()) === 'Ok.') {
46 print.success(l10n('DOWNLOADER_DL_SUCCESS', { title: thread.title }));
47 } else {
48 print.error(l10n('DOWNLOADER_DL_FAILED', { title: thread.title }));
49 }
50};