@ctrl/deluge
Advanced tools
Comparing version 1.3.1 to 1.4.0
/// <reference types="node" /> | ||
import { Response } from 'got'; | ||
import { TorrentSettings } from '@ctrl/shared-torrent'; | ||
import { TorrentSettings, TorrentClient, NormalizedTorrent, AllClientData } from '@ctrl/shared-torrent'; | ||
import { GetHostsResponse, GetHostStatusResponse, DefaultResponse, BooleanStatus, TorrentInfo, ListMethods, UploadResponse, AddTorrentOptions, TorrentListResponse, DelugeSettings, PluginInfo, ConfigResponse, PluginsListResponse, TorrentOptions, TorrentFiles, TorrentStatus, Tracker } from './types'; | ||
export declare class Deluge { | ||
export declare class Deluge implements TorrentClient { | ||
config: TorrentSettings; | ||
@@ -63,3 +63,7 @@ private _msgId; | ||
changePassword(password: string): Promise<BooleanStatus>; | ||
listTorrents(additionalFields?: string[]): Promise<TorrentListResponse>; | ||
getAllData(): Promise<AllClientData>; | ||
listTorrents(additionalFields?: string[], filter?: { | ||
[key: string]: string; | ||
}): Promise<TorrentListResponse>; | ||
getTorrent(id: string): Promise<NormalizedTorrent>; | ||
/** | ||
@@ -90,3 +94,4 @@ * get torrent state/status | ||
request<T extends object>(method: string, params?: any[], needsAuth?: boolean, autoConnect?: boolean): Promise<Response<T>>; | ||
private _normalizeTorrentData; | ||
private _validateAuth; | ||
} |
83
index.js
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const url_1 = require("url"); | ||
const url_join_1 = __importDefault(require("url-join")); | ||
const got_1 = __importDefault(require("got")); | ||
@@ -12,2 +12,3 @@ const tough_cookie_1 = require("tough-cookie"); | ||
const fs_1 = __importDefault(require("fs")); | ||
const shared_torrent_1 = require("@ctrl/shared-torrent"); | ||
const defaults = { | ||
@@ -154,3 +155,3 @@ baseUrl: 'http://localhost:8112/', | ||
} | ||
const url = url_1.resolve(this.config.baseUrl, '../upload'); | ||
const url = url_join_1.default(this.config.baseUrl, '/upload'); | ||
const res = await got_1.default.post(url, { | ||
@@ -169,10 +170,3 @@ headers: form.getHeaders(), | ||
const options = Object.assign({ file_priorities: [], add_paused: false, compact_allocation: false, max_connections: -1, max_download_speed: -1, max_upload_slots: -1, max_upload_speed: -1, prioritize_first_last_pieces: false }, config); | ||
const res = await this.request('web.add_torrents', [ | ||
[ | ||
{ | ||
path, | ||
options, | ||
}, | ||
], | ||
]); | ||
const res = await this.request('web.add_torrents', [[{ path, options }]]); | ||
return res.body; | ||
@@ -202,3 +196,25 @@ } | ||
} | ||
async listTorrents(additionalFields = []) { | ||
async getAllData() { | ||
const listTorrents = await this.listTorrents(); | ||
const results = { | ||
torrents: [], | ||
labels: [], | ||
}; | ||
for (const id of Object.keys(listTorrents.result.torrents)) { | ||
const torrent = listTorrents.result.torrents[id]; | ||
const torrentData = this._normalizeTorrentData(id, torrent); | ||
results.torrents.push(torrentData); | ||
} | ||
if (listTorrents.result.filters.label) { | ||
for (const label of listTorrents.result.filters.label) { | ||
results.labels.push({ | ||
id: label[0], | ||
name: label[0], | ||
count: label[1], | ||
}); | ||
} | ||
} | ||
return results; | ||
} | ||
async listTorrents(additionalFields = [], filter = {}) { | ||
const fields = [ | ||
@@ -228,2 +244,4 @@ 'distributed_copies', | ||
'upload_payload_rate', | ||
// if they don't have the label plugin it shouldn't fail | ||
'label', | ||
...additionalFields, | ||
@@ -233,6 +251,10 @@ ]; | ||
[...new Set(fields)], | ||
{}, | ||
filter, | ||
]); | ||
return req.body; | ||
} | ||
async getTorrent(id) { | ||
const torrentResponse = await this.getTorrentStatus(id); | ||
return this._normalizeTorrentData(id, torrentResponse.result); | ||
} | ||
/** | ||
@@ -279,2 +301,3 @@ * get torrent state/status | ||
'seeds_peers_ratio', | ||
'label', | ||
...additionalFields, | ||
@@ -374,3 +397,3 @@ ]; | ||
}; | ||
const url = url_1.resolve(this.config.baseUrl, this.config.path); | ||
const url = url_join_1.default(this.config.baseUrl, this.config.path); | ||
const options = { | ||
@@ -395,2 +418,36 @@ body: { | ||
} | ||
_normalizeTorrentData(id, torrent) { | ||
const dateAdded = new Date(torrent.time_added * 1000).toISOString(); | ||
// normalize state to enum | ||
let state = shared_torrent_1.TorrentState.unknown; | ||
if (Object.keys(shared_torrent_1.TorrentState).includes(torrent.state.toLowerCase())) { | ||
state = shared_torrent_1.TorrentState[torrent.state.toLowerCase()]; | ||
} | ||
const isCompleted = torrent.progress >= 100; | ||
const result = { | ||
id, | ||
name: torrent.name, | ||
state, | ||
isCompleted, | ||
stateMessage: torrent.state, | ||
progress: torrent.progress, | ||
ratio: torrent.ratio, | ||
dateAdded, | ||
dateCompleted: undefined, | ||
label: torrent.label, | ||
savePath: torrent.save_path, | ||
uploadSpeed: torrent.upload_payload_rate, | ||
downloadSpeed: torrent.download_payload_rate, | ||
eta: torrent.eta, | ||
queuePosition: torrent.queue + 1, | ||
connectedPeers: torrent.num_peers, | ||
connectedSeeds: torrent.num_seeds, | ||
totalPeers: torrent.total_peers, | ||
totalSeeds: torrent.total_seeds, | ||
totalSelected: torrent.total_wanted, | ||
totalUploaded: torrent.total_uploaded, | ||
totalDownloaded: torrent.total_done, | ||
}; | ||
return result; | ||
} | ||
async _validateAuth() { | ||
@@ -397,0 +454,0 @@ let validAuth = await this.checkSession(); |
{ | ||
"name": "@ctrl/deluge", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "TypeScript api wrapper for deluge using got", | ||
@@ -28,6 +28,7 @@ "author": "Scott Cooper <scttcper@gmail.com>", | ||
"dependencies": { | ||
"@ctrl/shared-torrent": "^1.0.3", | ||
"@ctrl/shared-torrent": "^1.1.0", | ||
"form-data": "2.3.3", | ||
"got": "9.6.0", | ||
"tough-cookie": "3.0.1" | ||
"tough-cookie": "3.0.1", | ||
"url-join": "^4.0.0" | ||
}, | ||
@@ -41,5 +42,6 @@ "devDependencies": { | ||
"@types/tough-cookie": "2.3.5", | ||
"@types/url-join": "4.0.0", | ||
"@typescript-eslint/eslint-plugin": "1.4.2", | ||
"@typescript-eslint/parser": "1.4.2", | ||
"eslint": "5.15.0", | ||
"eslint": "5.15.1", | ||
"eslint-config-prettier": "4.1.0", | ||
@@ -46,0 +48,0 @@ "eslint-config-xo-space": "0.21.0", |
export interface DefaultResponse { | ||
/** | ||
* mostly usless id that increments with every request | ||
*/ | ||
id: number; | ||
@@ -12,3 +15,9 @@ error: null | string; | ||
} | ||
/** | ||
* ex - | ||
*/ | ||
export interface UploadResponse { | ||
/** | ||
* ex - `["/tmp/delugeweb-5Q9ttR/tmpL7xhth.torrent"]` | ||
*/ | ||
files: string[]; | ||
@@ -87,2 +96,5 @@ success: boolean; | ||
} | ||
/** | ||
* ['label', 'id'] | ||
*/ | ||
export interface TorrentFilters { | ||
@@ -160,26 +172,3 @@ state: [string, number][]; | ||
export interface TorrentStatus extends DefaultResponse { | ||
result: { | ||
max_download_speed?: number; | ||
stop_ratio?: number; | ||
is_auto_managed?: true; | ||
move_completed_path?: string; | ||
private?: boolean; | ||
stop_at_ratio?: boolean; | ||
max_upload_speed?: number; | ||
remove_at_ratio?: boolean; | ||
max_upload_slots?: number; | ||
prioritize_first_last?: boolean; | ||
move_completed?: boolean; | ||
max_connections?: number; | ||
comment?: string; | ||
name?: string; | ||
total_size?: number; | ||
num_files?: number; | ||
tracker?: string; | ||
save_path?: string; | ||
message?: string; | ||
peers?: TorrentPeers; | ||
trackers?: Tracker; | ||
[key: string]: any; | ||
}; | ||
result: Torrent; | ||
} | ||
@@ -186,0 +175,0 @@ export interface TorrentPeers { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71057
1006
5
22
+ Addedurl-join@^4.0.0
+ Addedurl-join@4.0.1(transitive)
Updated@ctrl/shared-torrent@^1.1.0