Skip to content

Commit

Permalink
feat: Add torrentPeers endpoint (#118)
Browse files Browse the repository at this point in the history
fixes #117
  • Loading branch information
scttcper committed May 5, 2023
1 parent 0170b9a commit fbc5fe7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ build
docs
dist
yarn.lock
.DS_Store
16 changes: 16 additions & 0 deletions src/qbittorrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
TorrentFile,
TorrentFilePriority,
TorrentFilters,
TorrentPeersResponse,
TorrentPieceState,
TorrentProperties,
TorrentTrackers,
Expand Down Expand Up @@ -754,6 +755,21 @@ export class QBittorrent implements TorrentClient {
return true;
}

/**
* {@link https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#get-torrent-peers-data}
* @param rid - Response ID. If not provided, rid=0 will be assumed. If the given rid is
* different from the one of last server reply, full_update will be true (see the server reply details for more info)
*/
async torrentPeers(hash: string, rid?: number): Promise<TorrentPeersResponse> {
const params: { hash: string; rid?: number } = { hash };
if (rid) {
params.rid = rid;
}

const res = await this.request<TorrentPeersResponse>('/sync/torrentPeers', 'GET', params);
return res.body;
}

/**
* {@link https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#login}
*/
Expand Down
27 changes: 27 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1259,3 +1259,30 @@ export interface Preferences {
*/
utp_tcp_mixed_mode: number;
}

export interface TorrentPeersResponse {
full_update: boolean;
peers: Peers;
rid: number;
show_flags: boolean;
}

type Peers = Record<string, TorrentPeer>;

export interface TorrentPeer {
client?: string;
connection?: string;
country?: string;
country_code?: string;
dl_speed?: number;
downloaded?: number;
files?: string;
flags?: string;
flags_desc?: string;
ip?: string;
port?: number;
progress?: number;
relevance?: number;
up_speed?: number;
uploaded?: number;
}
7 changes: 7 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ it('should get torrent properties', async () => {
const res = await client.torrentProperties(torrentId);
expect(res.save_path).toEqual(expect.stringMatching(/downloads/i));
});
it('should get torrent peers', async () => {
const client = new QBittorrent({ baseUrl, username, password });
const torrentId = await setupTorrent(client);
const res = await client.torrentPeers(torrentId);
expect(res.full_update).toBe(true);
expect(res.peers).toBeDefined();
});
it('should get torrent trackers', async () => {
const client = new QBittorrent({ baseUrl, username, password });
const torrentId = await setupTorrent(client);
Expand Down

0 comments on commit fbc5fe7

Please sign in to comment.