/
client.go
225 lines (190 loc) · 4.92 KB
/
client.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
package nzbget
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
//Source: https://github.com/dashotv/flame
const PriorityVeryLow = -100
const PriorityLow = -50
const PriorityNormal = 0
const PriorityHigh = 50
const PriorityVeryHigh = 100
const PriorityForce = 900
func NewOptions() *AppendOptions {
return &AppendOptions{
Category: "",
Priority: PriorityNormal,
DupeMode: "SCORE",
}
}
func NewClient(endpoint string) *Client {
client := &Client{
URL: endpoint,
rpc: NewJsonClient(endpoint),
}
return client
}
func (c *Client) List() (*GroupResponse, error) {
s, err := c.Status()
if err != nil {
return nil, err
}
r := &GroupResponse{Response: &Response{Timestamp: time.Now(), Status: s}}
err = c.request("listgroups", nil, r)
return r, err
}
func (c *Client) Groups() ([]Group, error) {
r, err := c.List()
if err != nil {
return nil, errors.Wrap(err, "could not get list")
}
return r.Result, nil
}
func (c *Client) Remove(number int) error {
// group delete
return c.EditQueue("GroupDelete", "", []int{number})
}
func (c *Client) Delete(number int) error {
return c.EditQueue("HistoryDelete", "", []int{number})
}
func (c *Client) Destroy(number int) error {
return c.EditQueue("HistoryFinalDelete", "", []int{number})
}
func (c *Client) Pause(number int) error {
return c.EditQueue("GroupPause", "", []int{number})
}
func (c *Client) Resume(number int) error {
return c.EditQueue("GroupResume", "", []int{number})
}
func (c *Client) PauseAll() error {
r, err := c.rpc.Call("pausedownload", nil)
if err != nil {
return errors.Wrap(err, "could not pause all")
}
if r.Error != nil {
return errors.Wrap(err, "could not pause all")
}
if r.Result != true {
return errors.New("response result is not true")
}
return nil
}
func (c *Client) ResumeAll() error {
r, err := c.rpc.Call("resumedownload", nil)
if err != nil {
return errors.Wrap(err, "could not pause all")
}
if r.Error != nil {
return errors.Wrap(err, "could not pause all")
}
if r.Result != true {
return errors.New("response result is not true")
}
return nil
}
func (c *Client) EditQueue(command, param string, ids []int) error {
r, err := c.rpc.Call("editqueue", command, param, ids)
if err != nil {
return errors.Wrap(err, "could not pause all")
}
if r.Error != nil {
return errors.Wrap(err, "could not pause all")
}
if r.Result != true {
return errors.New("response result is not true")
}
return nil
}
func (c *Client) Add(URL string, options *AppendOptions) (int64, error) {
path, err := downloadURL(URL)
if err != nil {
return 0, errors.Wrap(err, "could not download url")
}
defer os.Remove(path)
str, err := readFile(path)
if err != nil {
return 0, errors.Wrap(err, "could not read downloaded file")
}
name, err := nzbName(str)
if err != nil {
return 0, errors.Wrap(err, "could not get nzb name")
}
enc := base64encode(str)
if options.NiceName != "" {
name = options.NiceName
}
r, err := c.rpc.Call("append", name, enc, options.Category, options.Priority, options.AddToTop, options.AddPaused, options.DupeKey, options.DupeScore, options.DupeMode, options.Parameters)
if err != nil {
if r != nil && r.Error != nil {
return 0, errors.Wrap(err, r.Error.Error())
}
return 0, err
}
n := r.Result.(json.Number)
i, err := n.Int64()
if err != nil {
return 0, err
}
return i, nil
}
func (c *Client) History(hidden bool) ([]History, error) {
r := &HistoryResponse{}
err := c.request("history", url.Values{"": []string{fmt.Sprintf("%t", hidden)}}, r)
if err != nil {
return nil, err
}
return r.Result, nil
}
func (c *Client) Status() (*Status, error) {
r := &StatusResponse{}
err := c.request("status", url.Values{}, r)
if err != nil {
return r.Result, err
}
return r.Result, nil
}
func (c *Client) Version() (string, error) {
version := &VersionResponse{}
err := c.request("version", url.Values{}, version)
if err != nil {
return "", err
}
return version.Version, nil
}
func (c *Client) request(path string, params url.Values, target interface{}) (err error) {
var url string
var client *http.Client
var request *http.Request
var response *http.Response
var body []byte
url = fmt.Sprintf("%s/%s", c.URL, path)
if request, err = http.NewRequest("GET", url, nil); err != nil {
return errors.Wrap(err, "creating "+url+" request failed")
}
request.URL.RawQuery = params.Encode()
client = &http.Client{Timeout: 5 * time.Second}
if response, err = client.Do(request); err != nil {
//log.Fatal(err)
return errors.Wrap(err, "error making http request")
}
defer response.Body.Close()
if body, err = ioutil.ReadAll(response.Body); err != nil {
//log.Fatal(err)
return errors.Wrap(err, "reading request body")
}
logrus.Debugf("body: %s", string(body))
if target == nil {
return nil
}
if err = json.Unmarshal(body, &target); err != nil {
return errors.Wrap(err, "json unmarshal")
}
return nil
}