-
Notifications
You must be signed in to change notification settings - Fork 18
/
torrent_status.go
205 lines (185 loc) · 5.79 KB
/
torrent_status.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
// go-libdeluge v0.5.6 - a native deluge RPC client library
// Copyright (C) 2015~2023 gdm85 - https://github.com/gdm85/go-libdeluge/
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package delugeclient
import (
"github.com/gdm85/go-rencode"
)
// TorrentStatus contains commonly used torrent attributes, as reported
// by the deluge server.
// The full list of potentially available attributes can be found here:
// https://github.com/deluge-torrent/deluge/blob/deluge-2.0.3/deluge/core/torrent.py#L1033-L1143
// If a new field is added to this struct it should also be added to the statusKeys map.
type TorrentStatus struct {
ActiveTime int64
CompletedTime int64 `rencode:"v2only"`
TimeAdded float32 // most times an integer
LastSeenComplete float32 `rencode:"v2only"`
DistributedCopies float32
ETA float32 // most times an integer
Progress float32 // max is 100
Ratio float32
IsFinished bool
IsSeed bool
Private bool
SavePath string
DownloadLocation string `rencode:"v2only"`
DownloadPayloadRate int64
Name string
NextAnnounce int64
NumPeers int64
NumPieces int64
NumSeeds int64
PieceLength int64
SeedingTime int64
State string
TotalDone int64
TotalPeers int64
TotalSeeds int64
TotalSize int64
TrackerHost string
TrackerStatus string
UploadPayloadRate int64
Files []File
Peers []Peer
FilePriorities []int64
FileProgress []float32
}
type TorrentState string
// See all defined torrent states here: https://github.com/deluge-torrent/deluge/blob/deluge-2.0.3/deluge/common.py#L70-L78
// Plus the special 'Active' state.
const (
StateUnspecified TorrentState = ""
StateActive TorrentState = "Active"
StateAllocating TorrentState = "Allocating"
StateChecking TorrentState = "Checking"
StateDownloading TorrentState = "Downloading"
StateSeeding TorrentState = "Seeding"
StatePaused TorrentState = "Paused"
StateError TorrentState = "Error"
StateQueued TorrentState = "Queued"
StateMoving TorrentState = "Moving"
)
var (
// each of the available fields in a torrent status
// fields differ from v1/v2
// See current list at https://github.com/deluge-torrent/deluge/blob/deluge-2.0.3/deluge/core/torrent.py#L1033-L1143
commonStatusKeys = []interface{}{
"state",
"tracker_host",
"tracker_status",
"next_announce",
"name",
"total_size",
"progress",
"num_seeds",
"total_seeds",
"num_peers",
"total_peers",
"eta",
"download_payload_rate",
"upload_payload_rate",
"ratio",
"distributed_copies",
"num_pieces",
"piece_length",
"total_done",
"files",
"file_priorities",
"file_progress",
"peers",
"is_seed",
"is_finished",
"active_time",
"seeding_time",
"time_added",
"private",
"save_path", // supported by both v1 and v2
}
statusKeysV1 = rencode.NewList(commonStatusKeys...)
statusKeysV2 = rencode.NewList(append(commonStatusKeys[:],
"download_location", // v2-only; v1 will not return it if queried to do so
"completed_time", // v2-only
"last_seen_complete", // v2-only
)...)
)
// TorrentStatus returns the status of the torrent with specified hash.
func (c *Client) TorrentStatus(hash string) (*TorrentStatus, error) {
var args rencode.List
args.Add(hash)
if !c.v2daemon {
args.Add(statusKeysV1)
} else {
args.Add(statusKeysV2)
}
rd, err := c.rpcWithDictionaryResult("core.get_torrent_status", args, rencode.Dictionary{})
if err != nil {
return nil, err
}
var ts TorrentStatus
err = rd.ToStruct(&ts, c.excludeTag)
if err != nil {
return nil, err
}
// on v2 both fields SavePath and DownloadLocation are already set to the correct values
if !c.v2daemon {
// on v1 be forward-compatible with v2
ts.DownloadLocation = ts.SavePath
}
return &ts, nil
}
// TorrentsStatus returns the status of torrents matching the specified state and list of hashes.
// Both state and list of hashes are optional.
func (c *Client) TorrentsStatus(state TorrentState, hashes []string) (map[string]*TorrentStatus, error) {
var args rencode.List
var filterDict rencode.Dictionary
if len(hashes) != 0 {
filterDict.Add("id", sliceToRencodeList(hashes))
}
if state != StateUnspecified {
filterDict.Add("state", string(state))
}
args.Add(filterDict)
if !c.v2daemon {
args.Add(statusKeysV1)
} else {
args.Add(statusKeysV2)
}
rd, err := c.rpcWithDictionaryResult("core.get_torrents_status", args, rencode.Dictionary{})
if err != nil {
return nil, err
}
d, err := rd.Zip()
if err != nil {
return nil, err
}
result := map[string]*TorrentStatus{}
for k, rv := range d {
v, ok := rv.(rencode.Dictionary)
if !ok {
return nil, ErrInvalidDictionaryResponse
}
var ts TorrentStatus
err = v.ToStruct(&ts, c.excludeTag)
if err != nil {
return nil, err
}
// on v2 both fields SavePath and DownloadLocation are already set to the correct values
if !c.v2daemon {
// on v1 be forward-compatible with v2
ts.DownloadLocation = ts.SavePath
}
result[k] = &ts
}
return result, nil
}