forked from Tubbebubbe/transmission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmission.go
253 lines (212 loc) · 6.25 KB
/
transmission.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
package transmission
import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"sort"
)
//TransmissionClient to talk to transmission
type TransmissionClient struct {
apiclient ApiClient
}
type Command struct {
Method string `json:"method,omitempty"`
Arguments arguments `json:"arguments,omitempty"`
Result string `json:"result,omitempty"`
}
type arguments struct {
Fields []string `json:"fields,omitempty"`
Torrents Torrents `json:"torrents,omitempty"`
Ids []int `json:"ids,omitempty"`
DeleteData bool `json:"delete-local-data,omitempty"`
DownloadDir string `json:"download-dir,omitempty"`
MetaInfo string `json:"metainfo,omitempty"`
Filename string `json:"filename,omitempty"`
TorrentAdded TorrentAdded `json:"torrent-added"`
}
//Torrent struct for torrents
type Torrent struct {
ID int `json:"id"`
Name string `json:"name"`
Status int `json:"status"`
AddedDate int `json:"addedDate"`
LeftUntilDone int `json:"leftUntilDone"`
Eta int `json:"eta"`
UploadRatio float64 `json:"uploadRatio"`
RateDownload int `json:"rateDownload"`
RateUpload int `json:"rateUpload"`
DownloadDir string `json:"downloadDir"`
IsFinished bool `json:"isFinished"`
PercentDone float64 `json:"percentDone"`
SeedRatioMode int `json:"seedRatioMode"`
HashString string `json:"hashString"`
Error int `json:"error"`
ErrorString string `json:"errorString"`
}
// Torrents represent []Torrent
type Torrents []Torrent
// sorting types
type (
byID Torrents
byName Torrents
byAddedDate Torrents
)
func (t byID) Len() int { return len(t) }
func (t byID) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t byID) Less(i, j int) bool { return t[i].ID < t[j].ID }
func (t byName) Len() int { return len(t) }
func (t byName) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t byName) Less(i, j int) bool { return t[i].Name < t[j].Name }
func (t byAddedDate) Len() int { return len(t) }
func (t byAddedDate) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t byAddedDate) Less(i, j int) bool { return t[i].AddedDate < t[j].AddedDate }
// methods of 'Torrents' to sort by ID, Name or AddedDate
func (t Torrents) SortByID(reverse bool) {
if reverse {
sort.Sort(sort.Reverse(byID(t)))
return
}
sort.Sort(byID(t))
}
func (t Torrents) SortByName(reverse bool) {
if reverse {
sort.Sort(sort.Reverse(byName(t)))
return
}
sort.Sort(byName(t))
}
func (t Torrents) SortByAddedDate(reverse bool) {
if reverse {
sort.Sort(sort.Reverse(byAddedDate(t)))
return
}
sort.Sort(byAddedDate(t))
}
//TorrentAdded data returning
type TorrentAdded struct {
HashString string `json:"hashString"`
ID int `json:"id"`
Name string `json:"name"`
}
//New create new transmission torrent
func New(url string, username string, password string) TransmissionClient {
apiclient := NewClient(url, username, password)
tc := TransmissionClient{apiclient: apiclient}
return tc
}
//GetTorrents get a list of torrents
func (ac *TransmissionClient) GetTorrents() (Torrents, error) {
cmd, err := NewGetTorrentsCmd()
out, err := ac.ExecuteCommand(cmd)
if err != nil {
return nil, err
}
return out.Arguments.Torrents, nil
}
//StartTorrent start the torrent
func (ac *TransmissionClient) StartTorrent(id int) (string, error) {
return ac.sendSimpleCommand("torrent-start", id)
}
//StopTorrent start the torrent
func (ac *TransmissionClient) StopTorrent(id int) (string, error) {
return ac.sendSimpleCommand("torrent-stop", id)
}
func NewGetTorrentsCmd() (*Command, error) {
cmd := &Command{}
cmd.Method = "torrent-get"
cmd.Arguments.Fields = []string{"id", "name", "hashString",
"status", "addedDate", "leftUntilDone", "eta", "uploadRatio",
"rateDownload", "rateUpload", "downloadDir", "isFinished",
"percentDone", "seedRatioMode", "error", "errorString"}
return cmd, nil
}
func NewAddCmd() (*Command, error) {
cmd := &Command{}
cmd.Method = "torrent-add"
return cmd, nil
}
func NewAddCmdByMagnet(magnetLink string) (*Command, error) {
cmd, _ := NewAddCmd()
cmd.Arguments.Filename = magnetLink
return cmd, nil
}
func NewAddCmdByURL(url string) (*Command, error) {
cmd, _ := NewAddCmd()
cmd.Arguments.Filename = url
return cmd, nil
}
func NewAddCmdByFilename(filename string) (*Command, error) {
cmd, _ := NewAddCmd()
cmd.Arguments.Filename = filename
return cmd, nil
}
func NewAddCmdByFile(file string) (*Command, error) {
cmd, _ := NewAddCmd()
fileData, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
cmd.Arguments.MetaInfo = base64.StdEncoding.EncodeToString(fileData)
return cmd, nil
}
func (cmd *Command) SetDownloadDir(dir string) {
cmd.Arguments.DownloadDir = dir
}
func NewDelCmd(id int, removeFile bool) (*Command, error) {
cmd := &Command{}
cmd.Method = "torrent-remove"
cmd.Arguments.Ids = []int{id}
cmd.Arguments.DeleteData = removeFile
return cmd, nil
}
func (ac *TransmissionClient) ExecuteCommand(cmd *Command) (*Command, error) {
out := &Command{}
body, err := json.Marshal(cmd)
if err != nil {
return out, err
}
output, err := ac.apiclient.Post(string(body))
if err != nil {
return out, err
}
err = json.Unmarshal(output, &out)
if err != nil {
return out, err
}
return out, nil
}
func (ac *TransmissionClient) ExecuteAddCommand(addCmd *Command) (TorrentAdded, error) {
outCmd, err := ac.ExecuteCommand(addCmd)
if err != nil {
return TorrentAdded{}, err
}
return outCmd.Arguments.TorrentAdded, nil
}
func encodeFile(file string) (string, error) {
fileData, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(fileData), nil
}
func (ac *TransmissionClient) sendSimpleCommand(method string, id int) (result string, err error) {
cmd := Command{Method: method}
cmd.Arguments.Ids = []int{id}
resp, err := ac.sendCommand(cmd)
return resp.Result, err
}
func (ac *TransmissionClient) sendCommand(cmd Command) (response Command, err error) {
body, err := json.Marshal(cmd)
if err != nil {
return
}
output, err := ac.apiclient.Post(string(body))
if err != nil {
return
}
err = json.Unmarshal(output, &response)
if err != nil {
return
}
return response, nil
}