/
torrent.ts
299 lines (261 loc) · 5.2 KB
/
torrent.ts
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { Model } from "../core/model";
import { ITorrent } from "../types";
export enum RemoveFlag {
JobOnly = 0,
WithTorrent = 1,
WithData = 2
}
export class Torrent extends Model
{
/**
* Store all of the torrent information
*/
private __torrent!: ITorrent;
/**
* Update the corrent information
*/
public setData (info: any) {
this.__torrent = {
hash : info[0],
status : info[1],
name : info[2],
size : info[3],
progress : info[4] / 1000,
downloaded : info[5],
uploaded : info[6],
ratio : info[7] / 1000,
upload_speed : info[8],
download_speed : info[9],
time_remaining : info[10],
label : info[11],
peers_connected: info[12],
peers_in_swarm : info[13],
seeds_connected: info[14],
seeds_in_swarm : info[15],
availability : info[16],
queue_order : info[18],
download_url : info[19],
rss_feed_url : info[20],
status_message : info[21],
stream_id : info[22],
date_added : info[23],
date_completed : info[24],
app_update_url : info[25]
};
}
/**
* Execute a generic action
*/
protected execute (action: string) {
// Don't return the response of the execution.
return new Promise((resolve, reject) => {
this.utorrent().execute(action, {hash: this.__torrent.hash})
.then(() => { resolve(); }).catch(reject);
});
}
// Methods -------------------------------------------------------------------------------------
/**
* Fetch the files associated with the torrent
*/
public files () {}
/**
* Pause the torrent
*/
public pause () {
return this.execute("pause");
}
/**
* Refresh the current torrent
*/
public refresh () {
return new Promise((resolve, reject) => {
this.utorrent().list().then(() => {
resolve();
}).catch(reject);
});
}
/**
* Remove the torrent from uTorrent
*/
public remove (flags: RemoveFlag = RemoveFlag.JobOnly) {
switch (flags) {
case RemoveFlag.WithTorrent:
return this.execute("removetorrent");
case RemoveFlag.WithData:
return this.execute("removedata");
case RemoveFlag.WithTorrent | RemoveFlag.WithData:
return this.execute("removetorrentdata");
default:
return this.execute("remove");
}
}
/**
* Start the torrent
*/
public start (force: boolean = false) {
return this.execute(`${force ? "force" : ""}"start"`);
}
/**
* Stop the torrent
*/
public stop () {
return this.execute("stop");
}
/**
* Unpause the torrent
*/
public unpause () {
return this.execute("unpause");
}
// Accessors -----------------------------------------------------------------------------------
/**
* Get the app update URL (whatever that is...)
*/
appUpdateUrl () {
return this.__torrent.app_update_url;
}
/**
* Get the availability of the torrent (whatever that is as well)
*/
availability () {
return this.__torrent.availability;
}
/**
* Get the date the torrent was added
*/
dateAdded () {
return this.__torrent.date_added;
}
/**
* Get the date the torrent was completed
*/
dateCompleted () {
return this.__torrent.date_completed;
}
/**
* The amount of data downloaded (bytes)
*/
downloaded () {
return this.__torrent.downloaded;
}
/**
* The download speed (bytes per second)
*/
downloadSpeed () {
return this.__torrent.download_speed;
}
/**
* Get the download URL if it exists
*/
downloadUrl () {
return this.__torrent.download_url || null;
}
/**
* Get the estimated time remaining (seconds)
*/
eta () {
return this.__torrent.time_remaining;
}
/**
* The torrent's hash
*/
hash () {
return this.__torrent.hash;
}
/**
* The torrent's label if set
*/
label () {
return this.__torrent.label || null;
}
/**
* The name of the torrent
*/
name () {
return this.__torrent.name;
}
/**
* The number of peers connected
*/
peersConnected () {
return this.__torrent.peers_connected;
}
/**
* The peers in the swarm
*/
peersInSwarm () {
return this.__torrent.peers_in_swarm;
}
/**
* The progress of the torrent
*/
progress () {
return this.__torrent.progress;
}
/**
* The queue order of the torrent
*/
queueOrder () {
return this.__torrent.queue_order;
}
/**
* The upload/download ratio of the torrent
*/
ratio () {
return this.__torrent.ratio;
}
/**
* The RSS feed URL
*/
rssFeedUrl () {
return this.__torrent.rss_feed_url;
}
/**
* The number of seeds connected
*/
seedsConnected () {
return this.__torrent.seeds_connected;
}
/**
* The number of seeds in the swarm
*/
seedsInSwarm () {
return this.__torrent.seeds_in_swarm;
}
/**
* The total size of the files
*/
size () {
return this.__torrent.size;
}
/**
* The status of the torrent
*/
status () {
return this.__torrent.status;
}
/**
* The status message of the torrent
*/
statusMessage () {
return this.__torrent.status_message;
}
/**
* The stream ID of the torrent
*/
streamId () {
return this.__torrent.stream_id;
}
/**
* The amount of data uploaded (bytes)
*/
uploaded () {
return this.__torrent.uploaded;
}
/**
* The upload speed (bytes per second)
*/
uploadSpeed () {
return this.__torrent.upload_speed;
}
}