-
Notifications
You must be signed in to change notification settings - Fork 11
/
torrent.go
417 lines (391 loc) · 9.63 KB
/
torrent.go
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package transmission
const (
// StatusStopped stopped
StatusStopped = 0
// StatusCheckPending check pending
StatusCheckPending = 1
// StatusChecking checking
StatusChecking = 2
// StatusDownloadPending download pending
StatusDownloadPending = 3
// StatusDownloading downloading
StatusDownloading = 4
// StatusSeedPending seed pending
StatusSeedPending = 5
// StatusSeeding seeding
StatusSeeding = 6
)
var torrentGetFields = []string{
"activityDate",
"addedDate",
"bandwidthPriority",
"comment",
"corruptEver",
"creator",
"dateCreated",
"desiredAvailable",
"doneDate",
"downloadDir",
"downloadedEver",
"downloadLimit",
"downloadLimited",
"error",
"errorString",
"eta",
"etaIdle",
"files",
"fileStats",
"hashString",
"haveUnchecked",
"haveValid",
"honorsSessionLimits",
"id",
"labels",
"isFinished",
"isPrivate",
"isStalled",
"leftUntilDone",
"magnetLink",
"manualAnnounceTime",
"maxConnectedPeers",
"metadataPercentComplete",
"name",
"peer",
"peers",
"peersConnected",
"peersFrom",
"peersGettingFromUs",
"peersSendingToUs",
"percentDone",
"pieces",
"pieceCount",
"pieceSize",
"priorities",
"queuePosition",
"rateDownload",
"rateUpload",
"recheckProgress",
"secondsDownloading",
"secondsSeeding",
"seedIdleLimit",
"seedIdleMode",
"seedRatioLimit",
"seedRatioMode",
"sizeWhenDone",
"startDate",
"status",
"trackers",
"trackerStats",
"totalSize",
"torrentFile",
"uploadedEver",
"uploadLimit",
"uploadLimited",
"uploadRatio",
"wanted",
"webseeds",
"webseedsSendingToUs",
}
// Torrents a list of Torrents
type Torrents struct {
Torrents []*Torrent `json:"torrents"`
}
// TorrentMap is a map of Torrents indexed by torrent hash.
type TorrentMap map[string]*Torrent
// SetTorrentArg arguments for Torrent.Set method
type SetTorrentArg struct {
BandwidthPriority int `json:"bandwidthPriority,omitempty"`
DownloadLimit int `json:"downloadLimit,omitempty"`
DownloadLimited bool `json:"downloadLimited,omitempty"`
FilesWanted []int `json:"files-wanted,omitempty"`
FilesUnwanted []int `json:"files-unwanted,omitempty"`
HonorsSessionLimits bool `json:"honorsSessionLimits,omitempty"`
Ids int `json:"ids"`
Labels []string `json:"labels,omitempty"`
Location string `json:"location,omitempty"`
PeerLimit int `json:"peer-limit,omitempty"`
PriorityHigh []int `json:"priority-high,omitempty"`
PriorityLow []int `json:"priority-low,omitempty"`
PriorityNormal []int `json:"priority-normal,omitempty"`
QueuePosition int `json:"queuePosition,omitempty"`
SeedIdleLimit int `json:"seedIdleLimit,omitempty"`
SeedIdleMode int `json:"seedIdleMode,omitempty"`
SeedRatioLimit float64 `json:"seedRatioLimit,omitempty"`
SeedRatioMode int `json:"seedRatioMode,omitempty"`
TrackerAdd []string `json:"trackerAdd,omitempty"`
TrackerRemove []int `json:"trackerRemove,omitempty"`
// TrackerReplace `json:"trackerReplace,omitempty"`
UploadLimit int `json:"uploadLimit,omitempty"`
UploadLimited bool `json:"uploadLimited,omitempty"`
}
// Torrent represent a torrent present in transmission
type Torrent struct {
Client *Client `json:"-"`
ActivityDate int
AddedDate int
BandwidthPriority int
Comment string
CorruptEver int64
Creator string
DateCreated int
DesiredAvailable int64
DoneDate int
DownloadDir string
DownloadedEver int64
DownloadLimit int
DownloadLimited bool
Error int
ErrorString string
Eta int
EtaIdle int
Files *[]File
FileStats *[]FileStats
HashString string
HaveUnchecked int64
HaveValid int64
HonorsSessionLimits bool
ID int
IsFinished bool
IsPrivate bool
IsStalled bool
Labels []string
LeftUntilDone int64
MagnetLink string
ManualAnnounceTime int
MaxConnectedPeers int
MetadataPercentComplete float64
Name string
Peerlimit int
Peers *[]Peers
PeersConnected int
PeersFrom PeersFrom
PeersGettingFromUs int
PeersSendingToUs int
PercentDone float64
Pieces string
PieceCount int
PieceSize int
Priorities []int
QueuePosition int
RateDownload int
RateUpload int
RecheckProgress float64
SecondsDownloading int
SecondsSeeding int
SeedIdleLimit int
SeedIdleMode int
SeedRatioLimit float64
SeedRatioMode int
SizeWhenDone int64
StartDate int
Status int
Trackers *[]Trackers
TrackerStats *[]TrackerStats
TotalSize int64
TorrentFile string
UploadedEver int64
UploadLimit int
UploadLimited bool
UploadRatio float64
Wanted []int
Webseeds []string
WebseedsSendingToUs int
}
// File transmission API response
type File struct {
BytesCompleted int64
Length int64
Name string
}
// FileStats transmission API response
type FileStats struct {
BytesCompleted int64
Wanted bool
Priority int
}
// Peers transmission API response
type Peers struct {
Address string
ClientName string
ClientIsChoked bool
ClientIsInterested bool
FlagStr string
IsDownloadingFrom bool
IsEncrypted bool
IsIncoming bool
IsUploadingTo bool
IsUTP bool
PeerIsChoked bool
PeerIsInterested bool
Port int
Progress float64
RateToClient int
RateToPeer int
}
// PeersFrom transmission API response
type PeersFrom struct {
FromCache int
FromDht int
FromIncoming int
FromLpd int
FromLtep int
FromPex int
FromTracker int
}
// TrackerStats transmission API response
type TrackerStats struct {
Announce string
AnnounceState int
DownloadCount int
HasAnnounced bool
HasScraped bool
Host string
ID int
IsBackup bool
LastAnnouncePeerCount int
LastAnnounceResult string
LastAnnounceStartTime int
LastAnnounceSucceeded bool
LastAnnounceTime int
LastAnnounceTimedOut bool
LastScrapeResult string
LastScrapeStartTime int
LastScrapeSucceeded bool
LastScrapeTime int
LastScrapeTimedOut bool
LeecherCount int
NextAnnounceTime int
NextScrapeTim int
Scrap string
ScrapeState int
SeederCount int
Tier int
}
// Trackers from transmission API response
type Trackers struct {
Announce string
ID int
Scrape string
Tier int
}
func (t *Torrent) torrentAction(method string) error {
type Arg struct {
Ids int `json:"ids"`
}
tReq := &Request{
Arguments: Arg{
Ids: t.ID,
},
Method: method,
}
r := &Response{}
err := t.Client.request(tReq, r)
if err != nil {
return err
}
return nil
}
// Start torrent
func (t *Torrent) Start() error {
return t.torrentAction("torrent-start")
}
// StartNow torrent
func (t *Torrent) StartNow() error {
return t.torrentAction("torrent-start-now")
}
// Stop torrent
func (t *Torrent) Stop() error {
return t.torrentAction("torrent-stop")
}
// Verify torrent
func (t *Torrent) Verify() error {
return t.torrentAction("torrent-verify")
}
// Reannounce torrent
func (t *Torrent) Reannounce() error {
return t.torrentAction("torrent-reannounce")
}
// PathRename renames a file or directory in a torrent.
func (t *Torrent) PathRename(path string, newPath string) error {
type arg struct {
Ids []int `json:"ids,string"`
Path string `json:"path"`
Name string `json:"name"`
}
tReq := &Request{
Arguments: arg{
Ids: []int{t.ID},
Path: path,
Name: newPath,
},
Method: "torrent-rename-path",
}
r := &Response{}
err := t.Client.request(tReq, r)
if err != nil {
return err
}
return nil
}
// SetLocation moves a Torrent
// move if true, move from previous location.
// otherwise, search "location" for files
func (t *Torrent) SetLocation(path string, move bool) error {
type arg struct {
Ids []int `json:"ids,string"`
Location string `json:"location"`
Move bool `json:"move,omitempty"`
}
tReq := &Request{
Arguments: arg{
Ids: []int{t.ID},
Location: path,
Move: move,
},
Method: "torrent-set-location",
}
r := &Response{}
err := t.Client.request(tReq, r)
if err != nil {
return err
}
return nil
}
// Set changes torrent param see SetTorrentArg
func (t *Torrent) Set(arg SetTorrentArg) error {
arg.Ids = t.ID
tReq := &Request{
Arguments: arg,
Method: "torrent-set",
}
r := &Response{}
err := t.Client.request(tReq, r)
if err != nil {
return err
}
return nil
}
// Update torrent information from transmission
func (t *Torrent) Update() error {
type Arg struct {
Ids int `json:"ids"`
Fields []string `json:"fields,omitempty"`
}
tReq := &Request{
Arguments: Arg{
Ids: t.ID,
Fields: torrentGetFields,
},
Method: "torrent-get",
}
r := &Response{
Arguments: &Torrents{
Torrents: []*Torrent{t},
},
}
err := t.Client.request(tReq, r)
if err != nil {
return err
}
return nil
}