-
Notifications
You must be signed in to change notification settings - Fork 1
/
torrents.go
878 lines (762 loc) · 28.2 KB
/
torrents.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
package qbittorrent_api
import (
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"github.com/pkg/errors"
)
type torrentsApi struct {
client *Client
}
type Torrent struct {
AddedOn int `json:"added_on"`
AmountLeft int64 `json:"amount_left"`
AutoTmm bool `json:"auto_tmm"`
Availability float64 `json:"availability"`
Category string `json:"category"`
Completed int64 `json:"completed"`
CompletionOn int `json:"completion_on"`
ContentPath string `json:"content_path"`
DlLimit int `json:"dl_limit"`
DlSpeed int `json:"dlspeed"`
DownloadPath string `json:"download_path"`
Downloaded int64 `json:"downloaded"`
DownloadedSession int `json:"downloaded_session"`
Eta int `json:"eta"`
FirstLastPiecePriority bool `json:"f_l_piece_prio"`
ForceStart bool `json:"force_start"`
Hash string `json:"hash"`
InfoHashV1 string `json:"infohash_v1"`
InfoHashV2 string `json:"infohash_v2"`
LastActivity int `json:"last_activity"`
MagnetUri string `json:"magnet_uri"`
MaxRatio int `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 int `json:"ratio_limit"`
SavePath string `json:"save_path"`
SeedingTime int `json:"seeding_time"`
SeedingTimeLimit int `json:"seeding_time_limit"`
SeenComplete int `json:"seen_complete"`
SequentialDownload bool `json:"seq_dl"`
Size int64 `json:"size"`
State TorrentState `json:"state"`
SuperSeeding bool `json:"super_seeding"`
Tags string `json:"tags"`
TimeActive int `json:"time_active"`
TotalSize int64 `json:"total_size"`
Tracker string `json:"tracker"`
TrackersCount int `json:"trackers_count"`
UpLimit int `json:"up_limit"`
Uploaded int64 `json:"uploaded"`
UploadedSession int `json:"uploaded_session"`
UpSpeed int `json:"upspeed"`
}
type TorrentState = string
const (
StateError TorrentState = "error"
StateMissingFiles TorrentState = "missingFiles"
StateUploading TorrentState = "uploading"
StatePausedUpload TorrentState = "pausedUP"
StateQueuedUpload TorrentState = "queuedUP"
StateStalledUpload TorrentState = "stalledUP"
StateCheckingUpload TorrentState = "checkingUP"
StateForcedUpload TorrentState = "forcedUP"
StateAllocating TorrentState = "allocating"
StateDownloading TorrentState = "downloading"
StateMetadataDownload TorrentState = "metaDL"
StateForcedMetadataDownload TorrentState = "forcedMetaDL"
StatePausedDownload TorrentState = "pausedDL"
StateQueuedDownload TorrentState = "queuedDL"
StateForcedDownload TorrentState = "forcedDL"
StateStalledDownload TorrentState = "stalledDL"
StateCheckingDownload TorrentState = "checkingDL"
StateCheckingResumeData TorrentState = "checkingResumeData"
StateMoving TorrentState = "moving"
StateUnknown TorrentState = "unknown"
)
func (t Torrent) IsDownloading() bool {
switch t.State {
case StateDownloading,
StateMetadataDownload,
StateForcedMetadataDownload,
StateStalledDownload,
StateCheckingDownload,
StatePausedDownload,
StateQueuedDownload,
StateForcedDownload:
return true
}
return false
}
func (t Torrent) IsUploading() bool {
switch t.State {
case StateUploading,
StateStalledUpload,
StateCheckingUpload,
StateQueuedUpload,
StateForcedUpload:
return true
}
return false
}
func (t Torrent) IsComplete() bool {
switch t.State {
case StateUploading,
StateStalledUpload,
StateCheckingUpload,
StatePausedUpload,
StateQueuedUpload,
StateForcedUpload:
return true
}
return false
}
func (t Torrent) IsChecking() bool {
return t.State == StateCheckingUpload || t.State == StateCheckingDownload || t.State == StateCheckingResumeData
}
func (t Torrent) IsErrored() bool {
return t.State == StateMissingFiles || t.State == StateError
}
func (t Torrent) IsPaused() bool {
return t.State == StatePausedUpload || t.State == StatePausedDownload
}
// Filter
// param status_filter: Filter list by all, downloading, completed, paused, active, inactive, resumed
//
// stalled, stalled_uploading and stalled_downloading added in Web API 2.4.1
//
// param category: Filter list by category
// param sort: Sort list by any property returned
// param reverse: Reverse sorting
// param limit: Limit length of list
// param offset: Start of list (if < 0, offset from end of list)
// param torrent_hashes: Filter list by hash (separate multiple hashes with a '|')
// param tag: Filter list by tag (empty string means "untagged"; no "tag" param means "any tag"; added in Web API 2.8.3)
type Filter struct {
StatusFilter TorrentState `json:"filter"`
Category string `json:"category"`
Sort FilterSort `json:"sort"`
Reverse bool `json:"reverse"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Hashes []string `json:"torrent_hashes"`
Tag string `json:"tag"`
}
type FilterSort = string
const (
FilterSortPriority = "priority"
FilterSortName = "name"
FilterSortProgress = "progress"
)
func (f *Filter) toMap() map[string]string {
var data = map[string]string{
"filter": f.StatusFilter,
//"category": f.Category,
"sort": f.Sort,
"reverse": "false",
//"limit": limit,
//"offset": offset,
"hashes": strings.Join(f.Hashes, "|"),
//"tag": f.Tag,
}
if f.Reverse && f.Sort != "" {
data["reverse"] = "true"
}
if f.Sort == "" {
data["sort"] = FilterSortPriority
}
if f.Limit > 0 {
data["limit"] = strconv.Itoa(f.Limit)
}
if f.Offset != 0 {
data["offset"] = strconv.Itoa(f.Offset)
}
if f.Category != "" {
data["category"] = f.Category
}
if f.Tag != "" {
data["tag"] = f.Tag
}
return data
}
// Torrents
// Retrieves list of info for torrents.
// Note: “hashes“ introduced in Web API 2.0.1
//
// @param status_filter: Filter list by all, downloading, completed, paused, active, inactive, resumed
//
// stalled, stalled_uploading and stalled_downloading added in Web API 2.4.1
//
// @param category: Filter list by category
// @param sort: Sort list by any property returned
// @param reverse: Reverse sorting
// @param limit: Limit length of list
// @param offset: Start of list (if < 0, offset from end of list)
// @param torrent_hashes: Filter list by hash (separate multiple hashes with a '|')
// @param tag: Filter list by tag (empty string means "untagged"; no "tag" param means "any tag"; added in Web API 2.8.3)
// @return: :class:`TorrentInfoList` - `<https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#get-torrent-list>`_
// noqa: E501
func (t *torrentsApi) Torrents(params *Filter) ([]*Torrent, error) {
_, body, err := t.postForTorrent("info", params.toMap())
if err != nil {
//logrus.Error(err)
return nil, err
}
var torrents []*Torrent
if err = json.Unmarshal(body, &torrents); err != nil {
//logrus.Error(err)
return nil, err
}
return torrents, nil
}
type Category struct {
Name string `json:"name"`
SavePath string `json:"savePath"`
DownloadPath string `json:"download_path"`
}
// GetAllCategories Retrieve all category definitions.
func (t *torrentsApi) GetAllCategories() ([]*Category, error) {
_, body, err := t.getForTorrent(actionGetAllCategories, nil)
if err != nil {
return nil, err
}
var categories = map[string]*Category{}
if err = json.Unmarshal(body, &categories); err != nil {
//logrus.Error(err)
return nil, err
}
var res []*Category
for _, category := range categories {
res = append(res, category)
}
return res, nil
}
// GetAllTags Retrieve all category definitions.
func (t *torrentsApi) GetAllTags() ([]string, error) {
_, body, err := t.getForTorrent(actionGetAllTags, nil)
if err != nil {
return nil, err
}
var tags []string
if err = json.Unmarshal(body, &tags); err != nil {
//logrus.Error(err)
return nil, err
}
return tags, nil
}
// DownloadBaseConfig
// :param save_path: location to save the torrent data
// :param cookie: cookie to retrieve torrents by URL
// :param category: category to assign to torrent(s)
// :param is_skip_checking: skip hash checking
// :param is_paused: True to start torrent(s) paused
// :param is_root_folder: True or False to create root folder (superseded by content_layout with v4.3.2)
// :param rename: new name for torrent(s)
// :param upload_limit: upload limit in bytes/second
// :param download_limit: download limit in bytes/second
// :param use_auto_torrent_management: True or False to use automatic torrent management
// :param is_sequential_download: True or False for sequential download
// :param is_first_last_piece_priority: True or False for first and last piece download priority
// :param tags: tag(s) to assign to torrent(s) (added in Web API 2.6.2)
// :param content_layout: Original, Subfolder, or NoSubfolder to control filesystem structure for content (added in Web API 2.7)
// :param ratio_limit: share limit as ratio of upload amt over download amt; e.g. 0.5 or 2.0 (added in Web API 2.8.1)
// :param seeding_time_limit: number of minutes to seed torrent (added in Web API 2.8.1)
// :param download_path: location to download torrent content before moving to save_path (added in Web API 2.8.4)
// :param use_download_path: whether the download_path should be used...defaults to True if download_path is specified (added in Web API 2.8.4)
type DownloadBaseConfig struct {
SavePath string `json:"savepath"` // 保存文件到:
Category string `json:"category"` // 分类:
Tags []string `json:"tags"` // 标签
SkipChecking bool `json:"skip_checking"` // 跳过哈希校验
Paused bool `json:"paused"` // 开始 Torrent
ContentLayout ContentLayout `json:"contentLayout"` // 内容布局:
Rename string `json:"rename"` // 重命名 torrent
UpLimit int `json:"upLimit"` // 限制上传速率 bytes/second
DlLimit int `json:"dlLimit"` // 限制下载速率 bytes/second
AutoTMM bool `json:"autoTMM"` // 自动 Torrent 管理
SequentialDownload bool `json:"sequentialDownload"` // 按顺序下载
FirstLastPiecePrio bool `json:"firstLastPiecePrio"` // 先下载首尾文件块
//RootFolder bool `json:"root_folder"`
//RatioLimit float64 `json:"ratioLimit"`
//SeedingTimeLimit string `json:"seedingTimeLimit"`
//DownloadPath string `json:"downloadPath"`
//UseDownloadPath bool `json:"useDownloadPath"`
}
func (cfg DownloadBaseConfig) toMap() map[string]string {
data := map[string]string{
"autoTMM": "false",
//"savepath": cfg.SavePath,
"rename": cfg.Rename,
"category": cfg.Category,
"tags": strings.Join(cfg.Tags, ","),
"paused": "false",
//"sequentialDownload": "false",
//"firstLastPiecePrio": "false",
"contentLayout": cfg.ContentLayout,
//"dlLimit": "NaN",
//"upLimit": "NaN",
}
if cfg.AutoTMM {
data["autoTMM"] = "true"
} else {
data["savepath"] = cfg.SavePath
}
if cfg.Paused {
data["paused"] = "true"
}
if cfg.ContentLayout == "" {
data["contentLayout"] = ContentLayoutOriginal
}
if cfg.DlLimit > 0 {
data["dlLimit"] = strconv.Itoa(cfg.DlLimit)
}
if cfg.UpLimit > 0 {
data["upLimit"] = strconv.Itoa(cfg.UpLimit)
}
if cfg.SkipChecking {
data["skip_checking"] = "true"
}
if cfg.SequentialDownload {
data["sequentialDownload"] = "true"
}
if cfg.FirstLastPiecePrio {
data["firstLastPiecePrio"] = "true"
}
return data
}
type ContentLayout = string
const (
ContentLayoutOriginal = "Original" // 原始
ContentLayoutSubFolder = "Subfolder" // 创建子文件夹
ContentLayoutNoSubFolder = "NoSubfolder" // 不创建子文件夹
)
// MagnetDLConfig
// :param urls: single instance or an iterable of URLs (http://, https://, magnet: and bc://bt/)
type MagnetDLConfig struct {
Urls []string `json:"urls"`
DownloadBaseConfig
}
func (cfg MagnetDLConfig) toMap() map[string]string {
data := cfg.DownloadBaseConfig.toMap()
data["urls"] = strings.Join(cfg.Urls, "\n")
return data
}
// DownloadFromLink
// Add one or more torrents by URLs and/or torrent files.
//
// :raises UnsupportedMediaType415Error: if file is not a valid torrent file
// :raises TorrentFileNotFoundError: if a torrent file doesn't exist
// :raises TorrentFilePermissionError: if read permission is denied to torrent file
//
// :return: “Ok.“ for success and “Fails.“ for failure
func (t *torrentsApi) DownloadFromLink(cfg MagnetDLConfig) error {
resp, err := t.client.postMultipartData(apiNameTorrents, "add", cfg.toMap())
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
//fmt.Println(string(body))
//fmt.Println(resp.StatusCode)
if resp.StatusCode == http.StatusOK {
if string(body) == "Ok." {
return nil
} else {
return errors.New("DownloadFromLink Fails.")
}
}
return handleResponsesErr(resp.StatusCode)
}
// TorrentDLConfig
// :param torrent_files: several options are available to send torrent files to qBittorrent:
// - single instance of bytes: useful if torrent file already read from disk or downloaded from internet.
// - single instance of file handle to torrent file: use open(<filepath>, 'rb') to open the torrent file.
// - single instance of a filepath to torrent file: e.g. '/home/user/torrent_filename.torrent'
// - an iterable of the single instances above to send more than one torrent file
// - dictionary with key/value pairs of torrent name and single instance of above object
// Note: The torrent name in a dictionary is useful to identify which torrent file
// errored. qBittorrent provides back that name in the error text. If a torrent
// name is not provided, then the name of the file will be used. And in the case of
// bytes (or if filename cannot be determined), the value 'torrent__n' will be used
type TorrentDLConfig struct {
File string `json:"file"`
DownloadBaseConfig
}
func (t *torrentsApi) DownloadFromFile(cfg TorrentDLConfig) error {
resp, err := t.client.postMultipartFile(apiNameTorrents, "add", cfg.File, cfg.toMap())
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
//fmt.Println(string(body))
//fmt.Println(resp.StatusCode)
if resp.StatusCode == http.StatusOK {
if string(body) == "Ok." {
return nil
} else {
return errors.New("DownloadFromLink Fails.")
}
}
return handleResponsesErr(resp.StatusCode)
}
func (t *torrentsApi) getForTorrent(path string, data map[string]string) (int, []byte, error) {
resp, err := t.client.request.get(apiNameTorrents, path, data)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
//fmt.Println(string(body))
//fmt.Println(resp.StatusCode)
if err := handleResponsesErr(resp.StatusCode); err != nil {
return 0, nil, err
}
return resp.StatusCode, body, nil
}
func (t *torrentsApi) postForTorrent(path string, data map[string]string) (int, []byte, error) {
resp, err := t.client.request.post(apiNameTorrents, path, data)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
//fmt.Println(string(body))
//fmt.Println(resp.StatusCode)
return resp.StatusCode, body, nil
}
type torrentsAction = string
const (
actionPause torrentsAction = "pause"
actionResume torrentsAction = "resume"
actionDelete torrentsAction = "delete"
actionRecheck torrentsAction = "recheck"
actionReAnnounce torrentsAction = "reannounce"
actionIncreasePriority torrentsAction = "increasePrio"
actionDecreasePriority torrentsAction = "decreasePrio"
actionTopPriority torrentsAction = "topPrio"
actionBottomPriority torrentsAction = "bottomPrio"
actionGetAllCategories torrentsAction = "categories"
actionSetCategory torrentsAction = "setCategory"
actionCreateCategory torrentsAction = "createCategory"
actionEditCategory torrentsAction = "editCategory"
actionRemoveCategories torrentsAction = "removeCategories"
actionGetAllTags torrentsAction = "tags"
actionAddTags torrentsAction = "addTags"
actionRemoveTags torrentsAction = "removeTags"
actionCreateTags torrentsAction = "createTags"
actionDeleteTags torrentsAction = "deleteTags"
)
const (
actionToggleSequentialDownload torrentsAction = "toggleSequentialDownload"
actionToggleFirstLastPiecePriority torrentsAction = "toggleFirstLastPiecePrio"
)
var actionForAll = []string{"all"}
type action struct {
param map[string]string
method torrentsAction
}
func newAction(method torrentsAction) *action {
return &action{param: map[string]string{}, method: method}
}
func (a *action) withHashes(ts []*Torrent) *action {
var hashes []string
for _, torrent := range ts {
hashes = append(hashes, torrent.Hash)
}
a.param["hashes"] = strings.Join(hashes, "|")
return a
}
func (a *action) withTags(tags []string) *action {
return a.setParam("tags", strings.Join(tags, ","))
}
func (a *action) setParam(key, val string) *action {
a.param[key] = val
return a
}
func (a *action) setBoolParam(key string, val bool) *action {
a.param[key] = "false"
if val {
a.setParam(key, "true")
}
return a
}
func (a *action) setIntParam(key string, val int) *action {
a.param[key] = strconv.Itoa(val)
return a
}
func (t *torrentsApi) actionByHashes(hashes []string, act *action) error {
return t.actionByParams(act.setParam("hashes", strings.Join(hashes, "|")))
}
func (t *torrentsApi) actionByParams(act *action) error {
statusCode, _, err := t.client.postForTorrent(act.method, act.param)
if err != nil {
return err
}
if statusCode == http.StatusOK {
return nil
}
return handleResponsesErr(statusCode)
}
// Pause pause one or more torrents in qBittorrent.
func (t *torrentsApi) Pause(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionPause).withHashes(ts))
}
// PauseByHashes pause torrents in qBittorrent by hashes.
func (t *torrentsApi) PauseByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionPause))
}
// PauseAll pause all torrents in qBittorrent.
func (t *torrentsApi) PauseAll() error {
return t.PauseByHashes(actionForAll)
}
// Resume resume one or more torrents in qBittorrent.
func (t *torrentsApi) Resume(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionResume).withHashes(ts))
}
// ResumeByHashes resume torrents in qBittorrent by hashes.
func (t *torrentsApi) ResumeByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionResume))
}
// ResumeAll resume all torrents in qBittorrent.
func (t *torrentsApi) ResumeAll() error {
return t.ResumeByHashes(actionForAll)
}
// Delete
// Remove a torrent from qBittorrent and optionally delete its files.
// param delete_files: True to delete the torrent's files
// param torrent_hashes: single torrent hash or list of torrent hashes. Or “all“ for all torrents.
// return: None
func (t *torrentsApi) Delete(deleteFiles bool, ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionDelete).withHashes(ts).setBoolParam("deleteFiles", deleteFiles))
}
func (t *torrentsApi) DeleteByHashes(deleteFiles bool, hashes []string) error {
return t.actionByHashes(hashes, newAction(actionDelete).setBoolParam("deleteFiles", deleteFiles))
}
func (t *torrentsApi) DeleteAll(deleteFiles bool) error {
return t.DeleteByHashes(deleteFiles, actionForAll)
}
// Recheck a torrent in qBittorrent.
func (t *torrentsApi) Recheck(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionRecheck).withHashes(ts))
}
func (t *torrentsApi) RecheckByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionRecheck))
}
func (t *torrentsApi) RecheckAll() error {
return t.RecheckByHashes(actionForAll)
}
// ReAnnounce a torrent in qBittorrent.
func (t *torrentsApi) ReAnnounce(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionReAnnounce).withHashes(ts))
}
func (t *torrentsApi) ReAnnounceByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionReAnnounce))
}
func (t *torrentsApi) ReAnnounceAll() error {
return t.ReAnnounceByHashes(actionForAll)
}
// IncreasePriority
// Increase the priority of a torrent. Torrent Queuing must be enabled.
// 向上移动队列
func (t *torrentsApi) IncreasePriority(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionIncreasePriority).withHashes(ts))
}
func (t *torrentsApi) IncreasePriorityByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionIncreasePriority))
}
//func (t *torrentsApi) IncreasePriorityAll() error {
// return t.IncreasePriorityByHashes(actionForAll)
//}
// DecreasePriority
// Decrease the priority of a torrent. Torrent Queuing must be enabled.
// 向下移动队列
func (t *torrentsApi) DecreasePriority(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionDecreasePriority).withHashes(ts))
}
func (t *torrentsApi) DecreasePriorityByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionDecreasePriority))
}
//func (t *torrentsApi) DecreasePriorityAll() error {
// return t.DecreasePriorityByHashes(actionForAll)
//}
// TopPriority
// Set torrent as highest priority. Torrent Queuing must be enabled.
// 移动到队列顶部
func (t *torrentsApi) TopPriority(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionTopPriority).withHashes(ts))
}
func (t *torrentsApi) TopPriorityByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionTopPriority))
}
//func (t *torrentsApi) TopPriorityAll() error {
// return t.TopPriorityByHashes(actionForAll)
//}
// BottomPriority
// Set torrent as highest priority. Torrent Queuing must be enabled.
// 移动到队列底部
func (t *torrentsApi) BottomPriority(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionBottomPriority).withHashes(ts))
}
func (t *torrentsApi) BottomPriorityByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionBottomPriority))
}
//func (t *torrentsApi) BottomPriorityAll() error {
// return t.BottomPriorityByHashes(actionForAll)
//}
// SetCategory Set a category for one or more torrents.
func (t *torrentsApi) SetCategory(category string, ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionSetCategory).withHashes(ts).setParam("category", category))
}
func (t *torrentsApi) SetCategoryByHashes(category string, hashes []string) error {
return t.actionByHashes(hashes, newAction(actionSetCategory).setParam("category", category))
}
func (t *torrentsApi) SetCategoryForAll(category string) error {
return t.SetCategoryByHashes(category, actionForAll)
}
// CreateCategory Create a new torrent category.
// param name: name for new category
// param save_path: location to save torrents for this category (added in Web API 2.1.0)
// param download_path: download location for torrents with this category
// param enable_download_path: True or False to enable or disable download path
func (t *torrentsApi) CreateCategory(category Category) error {
act := newAction(actionCreateCategory).
setParam("category", category.Name).
setParam("savePath", category.SavePath).
setParam("downloadPath", category.DownloadPath).
setBoolParam("downloadPathEnabled", category.DownloadPath != "")
return t.actionByParams(act)
}
// EditCategory Edit an existing category.
func (t *torrentsApi) EditCategory(category Category) error {
act := newAction(actionEditCategory).
setParam("category", category.Name).
setParam("savePath", category.SavePath).
setParam("downloadPath", category.DownloadPath).
setBoolParam("downloadPathEnabled", category.DownloadPath != "")
return t.actionByParams(act)
}
// RemoveCategories Delete one or more categories.
func (t *torrentsApi) RemoveCategories(categories []string) error {
return t.actionByParams(newAction(actionRemoveCategories).setParam("categories", strings.Join(categories, "\n")))
}
// CreateTags Create one or more tags.
func (t *torrentsApi) CreateTags(tags ...string) error {
if len(tags) == 0 {
return nil
}
return t.actionByParams(newAction(actionCreateTags).withTags(tags))
}
// DeleteTags Delete one or more tags.
func (t *torrentsApi) DeleteTags(tags ...string) error {
if len(tags) == 0 {
return nil
}
return t.actionByParams(newAction(actionDeleteTags).withTags(tags))
}
// AddTags Add one or more tags to one or more torrents.
// Note: Tags that do not exist will be created on-the-fly.
//
// param tags: tag name or list of tags
// param torrent_hashes: single torrent hash or list of torrent hashes. Or “all“ for all torrents.
func (t *torrentsApi) AddTags(tags []string, ts ...*Torrent) error {
return t.actionByParams(newAction(actionAddTags).withHashes(ts).withTags(tags))
}
func (t *torrentsApi) AddTagsByHashes(tags, hashes []string) error {
return t.actionByHashes(hashes, newAction(actionAddTags).withTags(tags))
}
func (t *torrentsApi) AddTagsForAll(tags []string) error {
return t.AddTagsByHashes(tags, actionForAll)
}
// RemoveTags Remove one or more tags to one or more torrents.
func (t *torrentsApi) RemoveTags(tags []string) error {
return t.actionByParams(newAction(actionRemoveTags).withTags(tags))
}
func (t *torrentsApi) RemoveTagsByHashes(tags, hashes []string) error {
return t.actionByHashes(hashes, newAction(actionRemoveTags).withTags(tags))
}
func (t *torrentsApi) RemoveTagsForAll(tags []string) error {
return t.RemoveTagsByHashes(tags, actionForAll)
}
// ToggleFirstLastPiecePriority
// Increase the priority of a torrent. Torrent Queuing must be enabled.
// 选中/取消 先下载首尾文件块
func (t *torrentsApi) ToggleFirstLastPiecePriority(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionToggleFirstLastPiecePriority).withHashes(ts))
}
func (t *torrentsApi) ToggleFirstLastPiecePriorityByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionToggleFirstLastPiecePriority))
}
func (t *torrentsApi) ToggleFirstLastPiecePriorityForAll() error {
return t.ToggleFirstLastPiecePriorityByHashes(actionForAll)
}
// ToggleSequentialDownload
// Increase the priority of a torrent. Torrent Queuing must be enabled.
// 选中/取消 按顺序下载
func (t *torrentsApi) ToggleSequentialDownload(ts ...*Torrent) error {
if len(ts) == 0 {
return nil
}
return t.actionByParams(newAction(actionToggleSequentialDownload).withHashes(ts))
}
func (t *torrentsApi) ToggleSequentialDownloadByHashes(hashes []string) error {
return t.actionByHashes(hashes, newAction(actionToggleSequentialDownload))
}
func (t *torrentsApi) ToggleSequentialDownloadForAll() error {
return t.ToggleSequentialDownloadByHashes(actionForAll)
}