-
Notifications
You must be signed in to change notification settings - Fork 0
/
qbit.go
289 lines (239 loc) · 8.67 KB
/
qbit.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
// Package qbit provides a few methods to interact with a qbittorrent installation.
// This package is in no way complete, and was written for a specific purpose.
// If you need more features, please open a PR or GitHub Issue with the request.
package qbit
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"golang.org/x/net/publicsuffix"
)
// Package defaults.
const (
DefaultTimeout = 1 * time.Minute
)
// Custom errors returned by this package.
var (
ErrLoginFailed = fmt.Errorf("authentication failed")
)
// Config is the input data needed to return a Qbit struct.
// This is setup to allow you to easily pass this data in from a config file.
type Config struct {
URL string `json:"url" toml:"url" xml:"url" yaml:"url"`
User string `json:"user" toml:"user" xml:"user" yaml:"user"`
Pass string `json:"pass" toml:"pass" xml:"pass" yaml:"pass"`
HTTPPass string `json:"http_pass" toml:"http_pass" xml:"http_pass" yaml:"http_pass"`
HTTPUser string `json:"http_user" toml:"http_user" xml:"http_user" yaml:"http_user"`
Client *http.Client `json:"-" toml:"-" xml:"-" yaml:"-"`
}
// Qbit is what you get in return for passing in a valid Config to New().
type Qbit struct {
config *Config
auth string
client *http.Client
}
// Xfer is a transfer from the torrents/info endpoint.
type Xfer struct {
AddedOn int `json:"added_on"`
AmountLeft int `json:"amount_left"`
AutoTmm bool `json:"auto_tmm"`
Availability float64 `json:"availability"`
Category string `json:"category"`
Completed int `json:"completed"`
CompletionOn int `json:"completion_on"`
ContentPath string `json:"content_path"`
DlLimit int `json:"dl_limit"`
Dlspeed int `json:"dlspeed"`
Downloaded int `json:"downloaded"`
DownloadedSession int `json:"downloaded_session"`
Eta int `json:"eta"`
FLPiecePrio bool `json:"f_l_piece_prio"`
ForceStart bool `json:"force_start"`
Hash string `json:"hash"`
LastActivity int `json:"last_activity"`
MagnetURI string `json:"magnet_uri"`
MaxRatio float64 `json:"max_ratio"`
MaxSeedingTime int `json:"max_seeding_time"`
Name string `json:"name"`
NumComplete int `json:"num_complete"`
NumIncomplete int `json:"num_incomplete"`
NumLeechs int `json:"num_leechs"`
NumSeeds int `json:"num_seeds"`
Priority int `json:"priority"`
Progress float64 `json:"progress"`
Ratio float64 `json:"ratio"`
RatioLimit float64 `json:"ratio_limit"`
SavePath string `json:"save_path"`
RootPath string `json:"root_path"`
SeedingTime int64 `json:"seeding_time"`
SeedingTimeLimit int64 `json:"seeding_time_limit"`
SeenComplete int64 `json:"seen_complete"`
SeqDl bool `json:"seq_dl"`
Size int64 `json:"size"`
State string `json:"state"`
SuperSeeding bool `json:"super_seeding"`
Tags string `json:"tags"`
TimeActive int64 `json:"time_active"`
TotalSize int64 `json:"total_size"`
Tracker string `json:"tracker"`
TrackersCount int `json:"trackers_count"`
UpLimit int64 `json:"up_limit"`
Uploaded int64 `json:"uploaded"`
UploadedSession int64 `json:"uploaded_session"`
Upspeed int64 `json:"upspeed"`
}
// Category represents a torrent category in Qbit.
type Category struct {
Name string `json:"name"`
SavePath string `json:"savePath"`
}
func NewNoAuth(config *Config) (*Qbit, error) {
return newConfig(context.TODO(), config, false)
}
func New(ctx context.Context, config *Config) (*Qbit, error) {
return newConfig(ctx, config, true)
}
func newConfig(ctx context.Context, config *Config, login bool) (*Qbit, error) {
// The cookie jar is used to auth Qbit.
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, fmt.Errorf("cookiejar.New(publicsuffix): %w", err)
}
config.URL = strings.TrimSuffix(config.URL, "/") + "/"
// This app allows http auth, in addition to qbit web username/password.
auth := config.HTTPUser + ":" + config.HTTPPass
if auth != ":" {
auth = "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
} else {
auth = ""
}
httpClient := config.Client
if httpClient == nil {
httpClient = &http.Client{}
}
httpClient.Jar = jar
qbit := &Qbit{
config: config,
auth: auth,
client: httpClient,
}
if !login {
return qbit, nil
}
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
return qbit, qbit.login(ctx)
}
// login is called once from New().
func (q *Qbit) login(ctx context.Context) error {
params := make(url.Values)
params.Add("username", q.config.User)
params.Add("password", q.config.Pass)
post := strings.NewReader(params.Encode())
req, err := http.NewRequestWithContext(ctx, http.MethodPost, q.config.URL+"api/v2/auth/login", post)
if err != nil {
return fmt.Errorf("creating login request: %w", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := q.client.Do(req)
if err != nil {
return fmt.Errorf("login failed: %w", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK || !strings.Contains(string(body), "Ok.") {
return fmt.Errorf("%w: %s: %s: %s", ErrLoginFailed, resp.Status, req.URL, string(body))
}
return nil
}
// SetTorrentCategory updates the category for 1 or more torrents.
func (q *Qbit) SetTorrentCategory(category string, torrentHashes ...string) error {
return q.SetTorrentCategoryContext(context.Background(), category, torrentHashes...)
}
// SetTorrentCategoryContext updates the category for 1 or more torrents.
func (q *Qbit) SetTorrentCategoryContext(ctx context.Context, category string, torrentHashes ...string) error {
values := url.Values{}
values.Set("category", category)
values.Set("hashes", strings.Join(torrentHashes, "|"))
var into map[string]interface{}
if err := q.postReq(ctx, "api/v2/torrents/setCategory", values, into); err != nil {
return err
}
return nil
}
// GetCategories returns all the categories in Qbit.
func (q *Qbit) GetCategories() (map[string]*Category, error) {
return q.GetCategoriesContext(context.Background())
}
// GetCategoriesContext returns all the categories in Qbit.
func (q *Qbit) GetCategoriesContext(ctx context.Context) (map[string]*Category, error) {
cats := map[string]*Category{}
if err := q.getReq(ctx, "api/v2/torrents/categories", &cats); err != nil {
return nil, err
}
return cats, nil
}
// GetXfers returns data about all transfers/downloads in the Qbit client.
func (q *Qbit) GetXfers() ([]*Xfer, error) {
return q.GetXfersContext(context.Background())
}
// GetXfersContext returns data about all transfers/downloads in the Qbit client.
func (q *Qbit) GetXfersContext(ctx context.Context) ([]*Xfer, error) {
xfers := []*Xfer{}
if err := q.getReq(ctx, "api/v2/torrents/info", &xfers); err != nil {
return nil, err
}
return xfers, nil
}
func (q *Qbit) getReq(ctx context.Context, path string, into interface{}) error {
return q.req(ctx, http.MethodGet, q.config.URL+path, nil, into, true)
}
func (q *Qbit) postReq(ctx context.Context, path string, values url.Values, into interface{}) error {
return q.req(ctx, http.MethodPost, q.config.URL+path, values, into, true)
}
func (q *Qbit) req(ctx context.Context, method, uri string, val url.Values, into interface{}, loop bool) error {
var body io.Reader
if val == nil {
val = url.Values{}
}
if method == http.MethodPost {
body = bytes.NewBufferString(val.Encode())
}
req, err := http.NewRequestWithContext(ctx, method, uri, body)
if err != nil {
return fmt.Errorf("creating '%s' request: %w", method, err)
}
if method == http.MethodPost {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else {
val.Set("filter", "all")
req.URL.RawQuery = val.Encode()
}
req.Header.Set("Accept", "application/json")
if q.auth != "" {
req.Header.Set("Authorization", q.auth)
}
resp, err := q.client.Do(req)
if err != nil {
return fmt.Errorf("%s failed: %w", method, err)
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(into); err != nil {
if err := q.login(ctx); err != nil {
return err
}
if loop { // try again after logging in.
return q.req(ctx, method, uri, val, into, false)
}
return fmt.Errorf("%s: %w", resp.Status, err)
}
return nil
}