An open API service indexing awesome lists of open source software.

https://github.com/robertklep/qbittorrent

Node.js qBittorrent API client
https://github.com/robertklep/qbittorrent

Last synced: over 1 year ago
JSON representation

Node.js qBittorrent API client

Awesome Lists containing this project

README

          

# qBittorrent v2 API client

Implements almost all of the methods of the current (v2.8.3) qBittorrent [WebUI API](https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)).

Disclaimer: not all of the API methods have been tested.

## Installation

```
npm install @robertklep/qbittorrent
```

## Examples

#### Instantiating

TypeScript:
```typescript
import { qBittorrentClient } from '@robertklep/qbittorrent';

const client = new qBittorrentClient('http://127.0.0.1:8080', 'username', 'password');
```

JavaScript:
```javascript
const { qBittorrentClient } = require('@robertklep/qbittorrent');

const client = new qBittorrentClient('http://127.0.0.1:8080', 'username', 'password');
```

There is no need to explicitly call `client.auth.login()`, the client will log in automatically on the first API call.

#### Getting the server version

```typescript
const info = await client.app.version();
```

#### Adding torrents

Add a single torrent by URL:
```typescript
const res = await client.torrents.add('https://distrowatch.com/dwres/torrents/xubuntu-20.04.4-desktop-amd64.iso.torrent');

// also works for Magnet URI's
const res = await client.torrents.add('magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent')
```

Add multiple torrents by URL:
```typescript
const res = await client.torrents.add([
'https://distrowatch.com/dwres/torrents/xubuntu-20.04.4-desktop-amd64.iso.torrent',
'https://distrowatch.com/dwres/torrents/ubuntu-budgie-20.04.4-desktop-amd64.iso.torrent'
]);
```

Add by file contents:
```typescript
import { qBittorrentClient, TorrentAddParameters } from '@robertklep/qbittorrent';
import { readFile } from 'fs/promises';

const res = await client.torrents.add({
torrents: { // TorrentFile[] | TorrentFile
buffer : await readFile('xubuntu-20.04.4-desktop-amd64.iso.torrent')
},
// start this torrent in a paused state (see Torrent type for options)
paused: true
});
```

#### Searching torrents

(your qBittorrent installation needs to support search for this to work)

```typescript
const wait = (ms : number) => new Promise(r => setTimeout(r, ms));

// start a search
const id = await client.search.start('linux');

// run the search for 20 seconds, check status every second
for (let i = 0; i < 20; i++) {
const { status, total } = await client.search.status(id) as TorrentSearchStatus;
console.log(`Status: ${ status }, total number of results so far: ${ total }`);
await wait(1000);
}

// retrieve the results
console.log( await client.search.results(id) );

// delete the search
await client.search.delete(id);
```

## Documentation

The structure of this module follows the structure and naming of the [qBittorrent WebUI API](https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)) methods.

For example, the [Authentication `login` method](https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#authentication) is accessible as `client.auth.login()`

#### Types
```
export declare type TorrentFile = {
filename?: string;
name?: string;
buffer: Buffer | string;
content_type: string;
};
export declare type TorrentAddParameters = {
urls: string[] | string;
torrents: TorrentFile[] | TorrentFile;
savepath: string;
cookie: string;
category: string;
tags: string[] | string;
skip_checking: boolean;
paused: boolean;
root_folder: string;
rename: string;
upLimit: number;
dlLimit: number;
ratioLimit: number;
seedingTimeLimit: number;
autoTMM: number;
sequentialDownload: boolean;
firstLastPiecePrio: boolean;
};
export declare type TorrentInfoParameters = {
filter: 'all' | 'downloading' | 'seeding' | 'completed' | 'paused' | 'active' | 'inactive' | 'resumed' | 'stalled' | 'stalled_uploading' | 'stalled_downloading' | 'errored';
category: string;
tag: string;
sort: string;
reverse: boolean;
limit: number;
offset: number;
hashes: string[] | string;
};
export declare type TorrentSearchStatus = {
id: number;
status: string;
total: number;
};
export declare type TorrentSearchResult = {
descrLink: string;
fileName: string;
fileSize: number;
fileUrl: string;
nbLeechers: number;
nbSeeders: number;
siteUrl: string;
};
export declare class qBittorrentClient {
#private;
auth: qBittorrentAuthClient;
app: qBittorrentAppClient;
log: qBittorrentLogClient;
sync: qBittorrentSyncClient;
transfer: qBittorrentTransferClient;
torrents: qBittorrentTorrentsClient;
search: qBittorrentSearchClient;
constructor(url: string, username?: string, password?: string);
request(method: string, data?: RequestData, options?: RequestOptions): Promise;
}
declare class qBittorrentSubClient {
protected client: qBittorrentClient;
constructor(client: qBittorrentClient);
}
declare class qBittorrentAuthClient extends qBittorrentSubClient {
login(): Promise;
logout(): Promise;
}
declare class qBittorrentAppClient extends qBittorrentSubClient {
version(): Promise;
webapiVersion(): Promise;
buildInfo(): Promise;
shutdown(): Promise;
preferences(): Promise;
setPreferences(prefs: RequestData): Promise;
defaultSavePath(): Promise;
}
declare class qBittorrentLogClient extends qBittorrentSubClient {
main(params: RequestData): Promise;
peers(last_known_id?: number): Promise;
}
declare class qBittorrentSyncClient extends qBittorrentSubClient {
maindata(rid?: number): Promise;
torrentPeers(hash: string, rid?: number): Promise;
}
declare class qBittorrentTransferClient extends qBittorrentSubClient {
info(): Promise;
speedLimitsMode(): Promise;
toggleSpeedLimitsMode(): Promise;
downloadLimit(): Promise;
setDownloadLimit(limit: number): Promise;
uploadLimit(): Promise;
setUploadLimit(limit: number): Promise;
banPeers(peers: string[] | string): Promise;
}
declare class qBittorrentTorrentsClient extends qBittorrentSubClient {
info(params?: Partial): Promise;
properties(hash: string): Promise;
trackers(hash: string): Promise;
webseeds(hash: string): Promise;
files(hash: string, indexes?: string[] | string | number): Promise;
pieceStates(hash: string): Promise;
pieceHashes(hash: string): Promise;
pause(hashes: string[] | string): Promise;
resume(hashes: string[] | string): Promise;
delete(hashes: string[] | string, deleteFiles?: boolean): Promise;
recheck(hashes: string[] | string): Promise;
reannounce(hashes: string[] | string): Promise;
editTracker(hash: string, origUrl: string, newUrl: string): Promise;
removeTracker(hash: string, urls: string[] | string): Promise;
addPeers(hashes: string[] | string, peers: string[] | string): Promise;
increasePrio(hashes: string[] | string): Promise;
decreasePrio(hashes: string[] | string): Promise;
topPrio(hashes: string[] | string): Promise;
bottomPrio(hashes: string[] | string): Promise;
filePrio(hash: string, id: string[] | string, priority: number): Promise;
downloadLimit(hashes: string[] | string): Promise;
setDownloadLimit(hashes: string[] | string, limit: number): Promise;
setShareLimits(hashes: string[] | string, ratioLimit?: number, seedingTimeLimit?: number): Promise;
uploadLimit(hashes: string[] | string): Promise;
setUploadLimit(hashes: string[] | string, limit: number): Promise;
setLocation(hashes: string[] | string, location: string): Promise;
rename(hash: string, name: string): Promise;
setCategory(hashes: string[] | string, category: string): Promise;
categories(): Promise;
createCategory(category: string, savePath: string): Promise;
editCategory(category: string, savePath: string): Promise;
removeCategories(categories: string[] | string): Promise;
addTags(hashes: string[] | string, tags: string[] | string): Promise;
removeTags(hashes: string[] | string, tags: string[] | string): Promise;
tags(): Promise;
createTags(tags: string[] | string): Promise;
deleteTags(tags: string[] | string): Promise;
setAutoManagement(hashes: string[] | string, enable?: boolean): Promise;
toggleSequentialDownload(hashes: string[]): Promise;
toggleFirstLastPiecePrio(hashes: string[]): Promise;
setForceStart(hashes: string[] | string, value?: boolean): Promise;
setSuperSeeding(hashes: string[] | string, value?: boolean): Promise;
renameFile(hash: string, oldPath: string, newPath: string): Promise;
renameFolder(hash: string, oldPath: string, newPath: string): Promise;
add(torrent: TorrentAddParameters | string[] | string): Promise;
}
declare class qBittorrentSearchClient extends qBittorrentSubClient {
start(pattern: string, plugins?: string[] | string, category?: string[] | string): Promise;
stop(id: number): Promise;
status(id?: number): Promise;
results(id: number, limit?: number, offset?: number): Promise;
delete(id: number): Promise;
plugins(): Promise;
installPlugin(sources: string[] | string): Promise;
uninstallPlugin(names: string[] | string): Promise;
enablePlugin(names: string[] | string, enable: boolean): Promise;
updatePlugins(): Promise;
}
```

#### Methods

Most methods that accept parameters that are either a single ór multiple values, like `hashes`, will accept both strings and arrays of strings. When passing as a string, make sure you use the correct separator when passing multiple values (usually `|`), or just pass the values as an array and the client will take care of concatenating the values properly.

All methods are async (return a Promise) and will thrown an error when the backend returns an non-200 status code. Please refer to the WebUI API documentation for full explanation of every method and their parameters.

List of implemented methods:
```
client.auth.login()
client.auth.logout()

client.app.version()
client.app.webapiVersion()
client.app.buildInfo()
client.app.shutdown()
client.app.preferences()
client.app.setPreferences(prefs : RequestData)
client.app.defaultSavePath()

client.log.main(params : RequestData)
client.log.peers(last_known_id = -1)

client.sync.maindata(rid = 0)
client.sync.torrentPeers(hash : string, rid = 0)

client.transfer.info()
client.transfer.speedLimitsMode()
client.transfer.toggleSpeedLimitsMode()
client.transfer.downloadLimit()
client.transfer.setDownloadLimit(limit : number)
client.transfer.uploadLimit()
client.transfer.setUploadLimit(limit : number)
client.transfer.banPeers(peers : string[] | string)

info(params : Partial = {}) {
client.torrents.properties(hash : string)
client.torrents.trackers(hash : string)
client.torrents.webseeds(hash : string)
client.torrents.files(hash : string, indexes : string[] | string = '')
client.torrents.pieceStates(hash : string)
client.torrents.pieceHashes(hash : string)
client.torrents.pause(hashes : string[] | string)
client.torrents.resume(hashes : string[] | string)
client.torrents.delete(hashes : string[] | string, deleteFiles = false)
client.torrents.recheck(hashes : string[] | string)
client.torrents.reannounce(hashes : string[] | string)
client.torrents.editTracker(hash : string, origUrl : string, newUrl : string)
client.torrents.removeTracker(hash : string, urls : string[] | string)
client.torrents.addPeers(hashes : string[] | string, peers : string[] | string)
client.torrents.increasePrio(hashes : string[] | string)
client.torrents.decreasePrio(hashes : string[] | string)
client.torrents.topPrio(hashes : string[] | string)
client.torrents.bottomPrio(hashes : string[] | string)
client.torrents.filePrio(hash : string, id : string[] | string, priority : number)
client.torrents.downloadLimit(hashes : string[] | string)
client.torrents.setDownloadLimit(hashes : string[] | string, limit : number)
client.torrents.setShareLimits(hashes : string[] | string, ratioLimit = -1, seedingTimeLimit = -1)
client.torrents.uploadLimit(hashes : string[] | string)
client.torrents.setUploadLimit(hashes : string[] | string, limit : number)
client.torrents.setLocation(hashes : string[] | string, location : string)
client.torrents.rename(hash : string, name : string)
client.torrents.setCategory(hashes : string[] | string, category : string)
client.torrents.categories()
client.torrents.createCategory(category : string, savePath : string)
client.torrents.editCategory(category : string, savePath : string)
client.torrents.removeCategories(categories : string[] | string)
client.torrents.addTags(hashes : string[] | string, tags : string[] | string)
client.torrents.removeTags(hashes : string[] | string, tags : string[] | string)
client.torrents.tags()
client.torrents.createTags(tags : string[] | string)
client.torrents.deleteTags(tags : string[] | string)
client.torrents.setAutoManagement(hashes : string[] | string, enable = false)
client.torrents.toggleSequentialDownload(hashes : string[])
client.torrents.toggleFirstLastPiecePrio(hashes : string[])
client.torrents.setForceStart(hashes : string[] | string, value = false)
client.torrents.setSuperSeeding(hashes : string[] | string, value = false)
client.torrents.renameFile(hash : string, oldPath : string, newPath : string)
client.torrents.renameFolder(hash : string, oldPath : string, newPath : string)
client.torrents.add(torrent : Torrent | string[] | string)
// TODO: client.torrents.addTrackers()

client.search.start( pattern : string, plugins : string[] | string = 'all', category : string[] | string = 'all' )
client.search.stop( id : number )
client.search.status( id? : number )
client.search.results( id : number, limit = 0, offset = 0 )
client.search.delete( id : number )
client.search.plugins()
client.search.installPlugin(sources : string[] | string)
client.search.uninstallPlugin(names : string[] | string)
client.search.enablePlugin(names : string[] | string, enable : boolean)
client.search.updatePlugins()
```