forked from gdm85/go-libdeluge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
155 lines (131 loc) · 3.64 KB
/
plugins.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
// go-libdeluge v0.5.3 - a native deluge RPC client library
// Copyright (C) 2015~2020 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"
)
// LabelPlugin exposes label plugin methods.
type LabelPlugin struct {
*Client
}
// LabelPlugin returns the label plugin if enabled or nil.
// An error is returned if enabled plugins could not be retrieved.
func (c *Client) LabelPlugin() (*LabelPlugin, error) {
plugins, err := c.GetEnabledPlugins()
if err != nil {
return nil, err
}
for _, p := range plugins {
if p == "Label" {
return &LabelPlugin{
Client: c,
}, nil
}
}
return nil, nil
}
// SetTorrentLabel adds or replaces the label for the specified torrent.
func (p LabelPlugin) SetTorrentLabel(hash, label string) error {
var args rencode.List
args.Add(hash, label)
resp, err := p.rpc("label.set_torrent", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return nil
}
// AddLabel adds a new label definition.
func (p LabelPlugin) AddLabel(label string) error {
var args rencode.List
args.Add(label)
resp, err := p.rpc("label.add", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return nil
}
// RemoveLabel removes a label definition.
func (p LabelPlugin) RemoveLabel(label string) error {
var args rencode.List
args.Add(label)
resp, err := p.rpc("label.remove", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return nil
}
// GetTorrentLabel returns the label of the specified torrent.
func (p LabelPlugin) GetTorrentLabel(hash string) (string, error) {
var args rencode.List
args.Add(hash)
args.Add(rencode.NewList("label"))
rd, err := p.rpcWithDictionaryResult("core.get_torrent_status", args, rencode.Dictionary{})
if err != nil {
return "", err
}
var s struct {
Label string
}
err = rd.ToStruct(&s, "")
if err != nil {
return "", err
}
return s.Label, nil
}
// GetTorrentsLabels filters torrents by state and/or IDs and returns their label.
func (p LabelPlugin) GetTorrentsLabels(state TorrentState, ids []string) (map[string]string, error) {
var args rencode.List
var filterDict rencode.Dictionary
if len(ids) != 0 {
filterDict.Add("id", sliceToRencodeList(ids))
}
if state != StateUnspecified {
filterDict.Add("state", string(state))
}
args.Add(filterDict)
args.Add(rencode.NewList("label"))
rd, err := p.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]string{}
for k, rv := range d {
v, ok := rv.(rencode.Dictionary)
if !ok {
return nil, ErrInvalidDictionaryResponse
}
var s struct {
Label string
}
err = v.ToStruct(&s, "")
if err != nil {
return nil, err
}
result[k] = s.Label
}
return result, nil
}