feat(clients): add support for qBittorrent 4.4.0+ (#558)

* refactor: move client to go-qbittorrent

* refactor: move client to go-qbittorrent

* feat(downloadclient): cache qbittorrent client

* feat(downloadclient): update qbit

* feat(downloadclient): client test and remove pkg qbit

* feat(downloadclient): update pkg qbit

* fix(release): method

* feat(release): make GetCachedClient concurrent safe

* feat(release): add additional tests for buildLegacyHost

* feat(release): remove branching

* chore: update pkg autobrr/go-qbittorrent to v.1.2.0
This commit is contained in:
ze0s 2022-12-10 19:25:04 +01:00 committed by GitHub
parent 6ad4abe296
commit 29da2416ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 379 additions and 1764 deletions

View file

@ -1,404 +0,0 @@
package qbittorrent
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path"
"strings"
"time"
"golang.org/x/net/publicsuffix"
"github.com/autobrr/autobrr/pkg/errors"
)
var (
backoffSchedule = []time.Duration{
5 * time.Second,
10 * time.Second,
20 * time.Second,
}
timeout = 60 * time.Second
)
type Client struct {
Name string
settings Settings
http *http.Client
log *log.Logger
}
type Settings struct {
Name string
Hostname string
Port uint
Username string
Password string
TLS bool
TLSSkipVerify bool
protocol string
BasicAuth bool
Basic Basic
Log *log.Logger
}
type Basic struct {
Username string
Password string
}
func NewClient(settings Settings) *Client {
c := &Client{
settings: settings,
Name: settings.Name,
log: log.New(io.Discard, "", log.LstdFlags),
}
// override logger if we pass one
if settings.Log != nil {
c.log = settings.Log
}
//store cookies in jar
jarOptions := &cookiejar.Options{PublicSuffixList: publicsuffix.List}
jar, err := cookiejar.New(jarOptions)
if err != nil {
c.log.Println("new client cookie error")
}
c.http = &http.Client{
Timeout: timeout,
Jar: jar,
}
c.settings.protocol = "http"
if c.settings.TLS {
c.settings.protocol = "https"
}
if c.settings.TLSSkipVerify {
//skip TLS verification
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
c.http.Transport = tr
}
return c
}
func (c *Client) get(endpoint string, opts map[string]string) (*http.Response, error) {
var err error
var resp *http.Response
reqUrl := buildUrlOpts(c.settings, endpoint, opts)
req, err := http.NewRequest("GET", reqUrl, nil)
if err != nil {
return nil, errors.Wrap(err, "could not build request")
}
if c.settings.BasicAuth {
req.SetBasicAuth(c.settings.Basic.Username, c.settings.Basic.Password)
}
// try request and if fail run 3 retries
for i, backoff := range backoffSchedule {
resp, err = c.http.Do(req)
// request ok, lets break out of the loop
if err == nil {
break
}
c.log.Printf("qbit GET failed: retrying attempt %d - %v\n", i, reqUrl)
time.Sleep(backoff)
}
if err != nil {
return nil, errors.Wrap(err, "error making get request: %v", reqUrl)
}
return resp, nil
}
func (c *Client) post(endpoint string, opts map[string]string) (*http.Response, error) {
// add optional parameters that the user wants
form := url.Values{}
if opts != nil {
for k, v := range opts {
form.Add(k, v)
}
}
var err error
var resp *http.Response
reqUrl := buildUrl(c.settings, endpoint)
req, err := http.NewRequest("POST", reqUrl, strings.NewReader(form.Encode()))
if err != nil {
return nil, errors.Wrap(err, "could not build request")
}
if c.settings.BasicAuth {
req.SetBasicAuth(c.settings.Basic.Username, c.settings.Basic.Password)
}
// add the content-type so qbittorrent knows what to expect
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// try request and if fail run 3 retries
for i, backoff := range backoffSchedule {
resp, err = c.http.Do(req)
// request ok, lets break out of the loop
if err == nil {
break
}
c.log.Printf("qbit POST failed: retrying attempt %d - %v\n", i, reqUrl)
time.Sleep(backoff)
}
if err != nil {
return nil, errors.Wrap(err, "error making post request: %v", reqUrl)
}
return resp, nil
}
func (c *Client) postBasic(endpoint string, opts map[string]string) (*http.Response, error) {
// add optional parameters that the user wants
form := url.Values{}
if opts != nil {
for k, v := range opts {
form.Add(k, v)
}
}
var err error
var resp *http.Response
reqUrl := buildUrl(c.settings, endpoint)
req, err := http.NewRequest("POST", reqUrl, strings.NewReader(form.Encode()))
if err != nil {
return nil, errors.Wrap(err, "could not build request")
}
if c.settings.BasicAuth {
req.SetBasicAuth(c.settings.Basic.Username, c.settings.Basic.Password)
}
// add the content-type so qbittorrent knows what to expect
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err = c.http.Do(req)
if err != nil {
return nil, errors.Wrap(err, "error making post request: %v", reqUrl)
}
return resp, nil
}
func (c *Client) postFile(endpoint string, fileName string, opts map[string]string) (*http.Response, error) {
var err error
var resp *http.Response
file, err := os.Open(fileName)
if err != nil {
return nil, errors.Wrap(err, "error opening file %v", fileName)
}
// Close the file later
defer file.Close()
// Buffer to store our request body as bytes
var requestBody bytes.Buffer
// Store a multipart writer
multiPartWriter := multipart.NewWriter(&requestBody)
// Initialize file field
fileWriter, err := multiPartWriter.CreateFormFile("torrents", fileName)
if err != nil {
return nil, errors.Wrap(err, "error initializing file field %v", fileName)
}
// Copy the actual file content to the fields writer
_, err = io.Copy(fileWriter, file)
if err != nil {
return nil, errors.Wrap(err, "error copy file contents to writer %v", fileName)
}
// Populate other fields
if opts != nil {
for key, val := range opts {
fieldWriter, err := multiPartWriter.CreateFormField(key)
if err != nil {
return nil, errors.Wrap(err, "error creating form field %v with value %v", key, val)
}
_, err = fieldWriter.Write([]byte(val))
if err != nil {
return nil, errors.Wrap(err, "error writing field %v with value %v", key, val)
}
}
}
// Close multipart writer
multiPartWriter.Close()
reqUrl := buildUrl(c.settings, endpoint)
req, err := http.NewRequest("POST", reqUrl, &requestBody)
if err != nil {
return nil, errors.Wrap(err, "error creating request %v", fileName)
}
if c.settings.BasicAuth {
req.SetBasicAuth(c.settings.Basic.Username, c.settings.Basic.Password)
}
// Set correct content type
req.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
// try request and if fail run 3 retries
for i, backoff := range backoffSchedule {
resp, err = c.http.Do(req)
// request ok, lets break out of the loop
if err == nil {
break
}
c.log.Printf("qbit POST file failed: retrying attempt %d - %v\n", i, reqUrl)
time.Sleep(backoff)
}
if err != nil {
return nil, errors.Wrap(err, "error making post file request %v", fileName)
}
return resp, nil
}
func (c *Client) setCookies(cookies []*http.Cookie) {
cookieURL, _ := url.Parse(buildUrl(c.settings, ""))
c.http.Jar.SetCookies(cookieURL, cookies)
}
func buildUrl(settings Settings, endpoint string) string {
// parse url
u, _ := url.Parse(settings.Hostname)
// reset Opaque
u.Opaque = ""
// set scheme
scheme := "http"
if u.Scheme == "http" || u.Scheme == "https" {
if settings.TLS {
scheme = "https"
}
u.Scheme = scheme
} else {
if settings.TLS {
scheme = "https"
}
u.Scheme = scheme
}
// if host is empty lets use one from settings
if u.Host == "" {
u.Host = settings.Hostname
}
// reset Path
if u.Host == u.Path {
u.Path = ""
}
// handle ports
if settings.Port > 0 {
if settings.Port == 80 || settings.Port == 443 {
// skip for regular http and https
} else {
u.Host = fmt.Sprintf("%v:%v", u.Host, settings.Port)
}
}
// join path
u.Path = path.Join(u.Path, "/api/v2/", endpoint)
// make into new string and return
return u.String()
}
func buildUrlOpts(settings Settings, endpoint string, opts map[string]string) string {
// parse url
u, _ := url.Parse(settings.Hostname)
// reset Opaque
u.Opaque = ""
// set scheme
scheme := "http"
if u.Scheme == "http" || u.Scheme == "https" {
if settings.TLS {
scheme = "https"
}
u.Scheme = scheme
} else {
if settings.TLS {
scheme = "https"
}
u.Scheme = scheme
}
// if host is empty lets use one from settings
if u.Host == "" {
u.Host = settings.Hostname
}
// reset Path
if u.Host == u.Path {
u.Path = ""
}
// handle ports
if settings.Port > 0 {
if settings.Port == 80 || settings.Port == 443 {
// skip for regular http and https
} else {
u.Host = fmt.Sprintf("%v:%v", u.Host, settings.Port)
}
}
// add query params
q := u.Query()
for k, v := range opts {
q.Set(k, v)
}
u.RawQuery = q.Encode()
// join path
u.Path = path.Join(u.Path, "/api/v2/", endpoint)
// make into new string and return
return u.String()
}

View file

@ -1,135 +0,0 @@
package qbittorrent
import "testing"
func Test_buildUrl(t *testing.T) {
type args struct {
settings Settings
endpoint string
}
tests := []struct {
name string
args args
want string
}{
{
name: "build_url_1",
args: args{
settings: Settings{
Hostname: "https://qbit.domain.ltd",
Port: 0,
Username: "",
Password: "",
TLS: true,
TLSSkipVerify: false,
protocol: "",
},
endpoint: "auth/login",
},
want: "https://qbit.domain.ltd/api/v2/auth/login",
},
{
name: "build_url_2",
args: args{
settings: Settings{
Hostname: "http://qbit.domain.ltd",
Port: 0,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
protocol: "",
},
endpoint: "/auth/login",
},
want: "http://qbit.domain.ltd/api/v2/auth/login",
},
{
name: "build_url_3",
args: args{
settings: Settings{
Hostname: "https://qbit.domain.ltd:8080",
Port: 0,
Username: "",
Password: "",
TLS: true,
TLSSkipVerify: false,
protocol: "",
},
endpoint: "/auth/login",
},
want: "https://qbit.domain.ltd:8080/api/v2/auth/login",
},
{
name: "build_url_4",
args: args{
settings: Settings{
Hostname: "qbit.domain.ltd:8080",
Port: 0,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
protocol: "",
},
endpoint: "/auth/login",
},
want: "http://qbit.domain.ltd:8080/api/v2/auth/login",
},
{
name: "build_url_5",
args: args{
settings: Settings{
Hostname: "qbit.domain.ltd",
Port: 8080,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
protocol: "",
},
endpoint: "/auth/login",
},
want: "http://qbit.domain.ltd:8080/api/v2/auth/login",
},
{
name: "build_url_6",
args: args{
settings: Settings{
Hostname: "qbit.domain.ltd",
Port: 443,
Username: "",
Password: "",
TLS: true,
TLSSkipVerify: false,
protocol: "",
},
endpoint: "/auth/login",
},
want: "https://qbit.domain.ltd/api/v2/auth/login",
},
{
name: "build_url_6",
args: args{
settings: Settings{
Hostname: "qbit.domain.ltd",
Port: 10200,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
protocol: "",
},
endpoint: "/auth/login",
},
want: "http://qbit.domain.ltd:10200/api/v2/auth/login",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildUrl(tt.args.settings, tt.args.endpoint); got != tt.want {
t.Errorf("buildUrl() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -1,316 +0,0 @@
package qbittorrent
import (
"strconv"
)
type Torrent struct {
AddedOn int `json:"added_on"`
AmountLeft int `json:"amount_left"`
AutoManaged bool `json:"auto_tmm"`
Availability float32 `json:"availability"`
Category string `json:"category"`
Completed int `json:"completed"`
CompletionOn int `json:"completion_on"`
DlLimit int `json:"dl_limit"`
DlSpeed int `json:"dl_speed"`
Downloaded int `json:"downloaded"`
DownloadedSession int `json:"downloaded_session"`
ETA int `json:"eta"`
FirstLastPiecePrio 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 float32 `json:"max_ratio"`
MaxSeedingTime int `json:"max_seeding_time"`
Name string `json:"name"`
NumComplete int `json:"num_complete"`
NumIncomplete int `json:"num_incomplete"`
NumSeeds int `json:"num_seeds"`
Priority int `json:"priority"`
Progress float32 `json:"progress"`
Ratio float32 `json:"ratio"`
RatioLimit float32 `json:"ratio_limit"`
SavePath string `json:"save_path"`
SeedingTimeLimit int `json:"seeding_time_limit"`
SeenComplete int `json:"seen_complete"`
SequentialDownload bool `json:"seq_dl"`
Size int `json:"size"`
State TorrentState `json:"state"`
SuperSeeding bool `json:"super_seeding"`
Tags string `json:"tags"`
TimeActive int `json:"time_active"`
TotalSize int `json:"total_size"`
Tracker *string `json:"tracker"`
UpLimit int `json:"up_limit"`
Uploaded int `json:"uploaded"`
UploadedSession int `json:"uploaded_session"`
UpSpeed int `json:"upspeed"`
}
type TorrentTrackersResponse struct {
Trackers []TorrentTracker `json:"trackers"`
}
type TorrentTracker struct {
//Tier int `json:"tier"` // can be both empty "" and int
Url string `json:"url"`
Status TrackerStatus `json:"status"`
NumPeers int `json:"num_peers"`
NumSeeds int `json:"num_seeds"`
NumLeechers int `json:"num_leechers"`
NumDownloaded int `json:"num_downloaded"`
Message string `json:"msg"`
}
type TorrentFiles []struct {
Availability int `json:"availability"`
Index int `json:"index"`
IsSeed bool `json:"is_seed,omitempty"`
Name string `json:"name"`
PieceRange []int `json:"piece_range"`
Priority int `json:"priority"`
Progress int `json:"progress"`
Size int `json:"size"`
}
type Category struct {
Name string `json:"name"`
SavePath string `json:"savePath"`
}
type TorrentState string
const (
// Some error occurred, applies to paused torrents
TorrentStateError TorrentState = "error"
// Torrent data files is missing
TorrentStateMissingFiles TorrentState = "missingFiles"
// Torrent is being seeded and data is being transferred
TorrentStateUploading TorrentState = "uploading"
// Torrent is paused and has finished downloading
TorrentStatePausedUp TorrentState = "pausedUP"
// Queuing is enabled and torrent is queued for upload
TorrentStateQueuedUp TorrentState = "queuedUP"
// Torrent is being seeded, but no connection were made
TorrentStateStalledUp TorrentState = "stalledUP"
// Torrent has finished downloading and is being checked
TorrentStateCheckingUp TorrentState = "checkingUP"
// Torrent is forced to uploading and ignore queue limit
TorrentStateForcedUp TorrentState = "forcedUP"
// Torrent is allocating disk space for download
TorrentStateAllocating TorrentState = "allocating"
// Torrent is being downloaded and data is being transferred
TorrentStateDownloading TorrentState = "downloading"
// Torrent has just started downloading and is fetching metadata
TorrentStateMetaDl TorrentState = "metaDL"
// Torrent is paused and has NOT finished downloading
TorrentStatePausedDl TorrentState = "pausedDL"
// Queuing is enabled and torrent is queued for download
TorrentStateQueuedDl TorrentState = "queuedDL"
// Torrent is being downloaded, but no connection were made
TorrentStateStalledDl TorrentState = "stalledDL"
// Same as checkingUP, but torrent has NOT finished downloading
TorrentStateCheckingDl TorrentState = "checkingDL"
// Torrent is forced to downloading to ignore queue limit
TorrentStateForcedDl TorrentState = "forcedDL"
// Checking resume data on qBt startup
TorrentStateCheckingResumeData TorrentState = "checkingResumeData"
// Torrent is moving to another location
TorrentStateMoving TorrentState = "moving"
// Unknown status
TorrentStateUnknown TorrentState = "unknown"
)
type TorrentFilter string
const (
// Torrent is paused
TorrentFilterAll TorrentFilter = "all"
// Torrent is active
TorrentFilterActive TorrentFilter = "active"
// Torrent is inactive
TorrentFilterInactive TorrentFilter = "inactive"
// Torrent is completed
TorrentFilterCompleted TorrentFilter = "completed"
// Torrent is resumed
TorrentFilterResumed TorrentFilter = "resumed"
// Torrent is paused
TorrentFilterPaused TorrentFilter = "paused"
// Torrent is stalled
TorrentFilterStalled TorrentFilter = "stalled"
// Torrent is being seeded and data is being transferred
TorrentFilterUploading TorrentFilter = "uploading"
// Torrent is being seeded, but no connection were made
TorrentFilterStalledUploading TorrentFilter = "stalled_uploading"
// Torrent is being downloaded and data is being transferred
TorrentFilterDownloading TorrentFilter = "downloading"
// Torrent is being downloaded, but no connection were made
TorrentFilterStalledDownloading TorrentFilter = "stalled_downloading"
// Torrent is errored
TorrentFilterError TorrentFilter = "errored"
)
// TrackerStatus https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#get-torrent-trackers
type TrackerStatus int
const (
// 0 Tracker is disabled (used for DHT, PeX, and LSD)
TrackerStatusDisabled TrackerStatus = 0
// 1 Tracker has not been contacted yet
TrackerStatusNotContacted TrackerStatus = 1
// 2 Tracker has been contacted and is working
TrackerStatusOK TrackerStatus = 2
// 3 Tracker is updating
TrackerStatusUpdating TrackerStatus = 3
// 4 Tracker has been contacted, but it is not working (or doesn't send proper replies)
TrackerStatusNotWorking TrackerStatus = 4
)
type ConnectionStatus string
const (
ConnectionStatusConnected = "connected"
ConnectionStatusFirewalled = "firewalled"
ConnectionStatusDisconnected = "disconnected"
)
// TransferInfo
//
// https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#get-global-transfer-info
//
// dl_info_speed integer Global download rate (bytes/s)
//
// dl_info_data integer Data downloaded this session (bytes)
//
// up_info_speed integer Global upload rate (bytes/s)
//
// up_info_data integer Data uploaded this session (bytes)
//
// dl_rate_limit integer Download rate limit (bytes/s)
//
// up_rate_limit integer Upload rate limit (bytes/s)
//
// dht_nodes integer DHT nodes connected to
//
// connection_status string Connection status. See possible values here below
//
type TransferInfo struct {
ConnectionStatus ConnectionStatus `json:"connection_status"`
DHTNodes int64 `json:"dht_nodes"`
DlInfoData int64 `json:"dl_info_data"`
DlInfoSpeed int64 `json:"dl_info_speed"`
DlRateLimit int64 `json:"dl_rate_limit"`
UpInfoData int64 `json:"up_info_data"`
UpInfoSpeed int64 `json:"up_info_speed"`
UpRateLimit int64 `json:"up_rate_limit"`
}
type ContentLayout string
// https://www.youtube.com/watch?v=4N1iwQxiHrs
const (
ContentLayoutOriginal ContentLayout = "Original"
ContentLayoutSubfolderNone ContentLayout = "NoSubfolder"
ContentLayoutSubfolderCreate ContentLayout = "Subfolder"
)
type TorrentAddOptions struct {
Paused *bool
SkipHashCheck *bool
ContentLayout *ContentLayout
SavePath *string
AutoTMM *bool
Category *string
Tags *string
LimitUploadSpeed *int64
LimitDownloadSpeed *int64
LimitRatio *float64
LimitSeedTime *int64
}
func (o *TorrentAddOptions) Prepare() map[string]string {
options := map[string]string{}
if o.Paused != nil {
options["paused"] = "true"
}
if o.SkipHashCheck != nil {
options["skip_checking"] = "true"
}
if o.ContentLayout != nil {
if *o.ContentLayout == ContentLayoutSubfolderCreate {
// pre qBittorrent version 4.3.2
options["root_folder"] = "true"
// post version 4.3.2
options["contentLayout"] = string(ContentLayoutSubfolderCreate)
} else if *o.ContentLayout == ContentLayoutSubfolderNone {
// pre qBittorrent version 4.3.2
options["root_folder"] = "false"
// post version 4.3.2
options["contentLayout"] = string(ContentLayoutSubfolderNone)
}
// if ORIGINAL then leave empty
}
if o.SavePath != nil && *o.SavePath != "" {
options["savepath"] = *o.SavePath
options["autoTMM"] = "false"
}
if o.Category != nil && *o.Category != "" {
options["category"] = *o.Category
}
if o.Tags != nil && *o.Tags != "" {
options["tags"] = *o.Tags
}
if o.LimitUploadSpeed != nil && *o.LimitUploadSpeed > 0 {
options["upLimit"] = strconv.FormatInt(*o.LimitUploadSpeed*1000, 10)
}
if o.LimitDownloadSpeed != nil && *o.LimitDownloadSpeed > 0 {
options["dlLimit"] = strconv.FormatInt(*o.LimitDownloadSpeed*1000, 10)
}
if o.LimitRatio != nil && *o.LimitRatio > 0 {
options["ratioLimit"] = strconv.FormatFloat(*o.LimitRatio, 'f', 2, 64)
}
if o.LimitSeedTime != nil && *o.LimitSeedTime > 0 {
options["seedingTimeLimit"] = strconv.FormatInt(*o.LimitSeedTime, 10)
}
return options
}

View file

@ -1,179 +0,0 @@
package qbittorrent
import (
"testing"
"github.com/stretchr/testify/assert"
)
func PtrBool(b bool) *bool {
return &b
}
func PtrStr(s string) *string {
return &s
}
func PtrInt64(i int64) *int64 {
return &i
}
func PtrFloat64(f float64) *float64 {
return &f
}
func TestTorrentAddOptions_Prepare(t *testing.T) {
layoutNone := ContentLayoutSubfolderNone
layoutCreate := ContentLayoutSubfolderCreate
layoutOriginal := ContentLayoutOriginal
type fields struct {
Paused *bool
SkipHashCheck *bool
ContentLayout *ContentLayout
SavePath *string
AutoTMM *bool
Category *string
Tags *string
LimitUploadSpeed *int64
LimitDownloadSpeed *int64
LimitRatio *float64
LimitSeedTime *int64
}
tests := []struct {
name string
fields fields
want map[string]string
}{
{
name: "test_01",
fields: fields{
Paused: nil,
SkipHashCheck: PtrBool(true),
ContentLayout: nil,
SavePath: PtrStr("/home/test/torrents"),
AutoTMM: nil,
Category: PtrStr("test"),
Tags: PtrStr("limited,slow"),
LimitUploadSpeed: PtrInt64(100000),
LimitDownloadSpeed: PtrInt64(100000),
LimitRatio: PtrFloat64(2.0),
LimitSeedTime: PtrInt64(100),
},
want: map[string]string{
"skip_checking": "true",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "100000000",
"dlLimit": "100000000",
},
},
{
name: "test_02",
fields: fields{
Paused: nil,
SkipHashCheck: PtrBool(true),
ContentLayout: &layoutCreate,
SavePath: PtrStr("/home/test/torrents"),
AutoTMM: nil,
Category: PtrStr("test"),
Tags: PtrStr("limited,slow"),
LimitUploadSpeed: PtrInt64(100000),
LimitDownloadSpeed: PtrInt64(100000),
LimitRatio: PtrFloat64(2.0),
LimitSeedTime: PtrInt64(100),
},
want: map[string]string{
"skip_checking": "true",
"root_folder": "true",
"contentLayout": "Subfolder",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "100000000",
"dlLimit": "100000000",
},
},
{
name: "test_03",
fields: fields{
Paused: nil,
SkipHashCheck: PtrBool(true),
ContentLayout: &layoutNone,
SavePath: PtrStr("/home/test/torrents"),
AutoTMM: nil,
Category: PtrStr("test"),
Tags: PtrStr("limited,slow"),
LimitUploadSpeed: PtrInt64(100000),
LimitDownloadSpeed: PtrInt64(100000),
LimitRatio: PtrFloat64(2.0),
LimitSeedTime: PtrInt64(100),
},
want: map[string]string{
"skip_checking": "true",
"root_folder": "false",
"contentLayout": "NoSubfolder",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "100000000",
"dlLimit": "100000000",
},
},
{
name: "test_04",
fields: fields{
Paused: nil,
SkipHashCheck: PtrBool(true),
ContentLayout: &layoutOriginal,
SavePath: PtrStr("/home/test/torrents"),
AutoTMM: nil,
Category: PtrStr("test"),
Tags: PtrStr("limited,slow"),
LimitUploadSpeed: PtrInt64(100000),
LimitDownloadSpeed: PtrInt64(100000),
LimitRatio: PtrFloat64(2.0),
LimitSeedTime: PtrInt64(100),
},
want: map[string]string{
"skip_checking": "true",
"autoTMM": "false",
"ratioLimit": "2.00",
"savepath": "/home/test/torrents",
"seedingTimeLimit": "100",
"category": "test",
"tags": "limited,slow",
"upLimit": "100000000",
"dlLimit": "100000000",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &TorrentAddOptions{
Paused: tt.fields.Paused,
SkipHashCheck: tt.fields.SkipHashCheck,
ContentLayout: tt.fields.ContentLayout,
SavePath: tt.fields.SavePath,
AutoTMM: tt.fields.AutoTMM,
Category: tt.fields.Category,
Tags: tt.fields.Tags,
LimitUploadSpeed: tt.fields.LimitUploadSpeed,
LimitDownloadSpeed: tt.fields.LimitDownloadSpeed,
LimitRatio: tt.fields.LimitRatio,
LimitSeedTime: tt.fields.LimitSeedTime,
}
got := o.Prepare()
assert.Equal(t, tt.want, got)
})
}
}

View file

@ -1,482 +0,0 @@
package qbittorrent
import (
"encoding/json"
"io"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"github.com/autobrr/autobrr/pkg/errors"
)
// Login https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#authentication
func (c *Client) Login() error {
opts := map[string]string{
"username": c.settings.Username,
"password": c.settings.Password,
}
resp, err := c.postBasic("auth/login", opts)
if err != nil {
return errors.Wrap(err, "login error")
} else if resp.StatusCode == http.StatusForbidden {
return errors.New("User's IP is banned for too many failed login attempts")
} else if resp.StatusCode != http.StatusOK { // check for correct status code
return errors.New("qbittorrent login bad status %v", resp.StatusCode)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
bodyString := string(bodyBytes)
// read output
if bodyString == "Fails." {
return errors.New("bad credentials")
}
// good response == "Ok."
// place cookies in jar for future requests
if cookies := resp.Cookies(); len(cookies) > 0 {
c.setCookies(cookies)
} else {
return errors.New("bad credentials")
}
c.log.Printf("logged into client: %v", c.Name)
return nil
}
func (c *Client) GetTorrents() ([]Torrent, error) {
resp, err := c.get("torrents/info", nil)
if err != nil {
return nil, errors.Wrap(err, "get torrents error")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read body")
}
var torrents []Torrent
if err := json.Unmarshal(body, &torrents); err != nil {
return nil, errors.Wrap(err, "could not unmarshal body")
}
return torrents, nil
}
func (c *Client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
opts := map[string]string{
"filter": string(filter),
}
resp, err := c.get("torrents/info", opts)
if err != nil {
return nil, errors.Wrap(err, "could not get filtered torrents with filter: %v", filter)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read body")
}
var torrents []Torrent
if err := json.Unmarshal(body, &torrents); err != nil {
return nil, errors.Wrap(err, "could not unmarshal body")
}
return torrents, nil
}
func (c *Client) GetTorrentsActiveDownloads() ([]Torrent, error) {
var filter = TorrentFilterDownloading
opts := map[string]string{
"filter": string(filter),
}
resp, err := c.get("torrents/info", opts)
if err != nil {
return nil, errors.Wrap(err, "could not get active torrents")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read body")
}
var torrents []Torrent
if err := json.Unmarshal(body, &torrents); err != nil {
return nil, errors.Wrap(err, "could not unmarshal body")
}
res := make([]Torrent, 0)
for _, torrent := range torrents {
// qbit counts paused torrents as downloading as well by default
// so only add torrents with state downloading, and not pausedDl, stalledDl etc
if torrent.State == TorrentStateDownloading || torrent.State == TorrentStateStalledDl {
res = append(res, torrent)
}
}
return res, nil
}
func (c *Client) GetTorrentsRaw() (string, error) {
resp, err := c.get("torrents/info", nil)
if err != nil {
return "", errors.Wrap(err, "could not get torrents raw")
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "could not get read body torrents raw")
}
return string(data), nil
}
func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
opts := map[string]string{
"hash": hash,
}
resp, err := c.get("torrents/trackers", opts)
if err != nil {
return nil, errors.Wrap(err, "could not get torrent trackers for hash: %v", hash)
}
defer resp.Body.Close()
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
//c.log.Printf("get torrent trackers error dump response: %v\n", string(dump))
return nil, errors.Wrap(err, "could not dump response for hash: %v", hash)
}
c.log.Printf("get torrent trackers response dump: %q", dump)
if resp.StatusCode == http.StatusNotFound {
return nil, nil
} else if resp.StatusCode == http.StatusForbidden {
return nil, nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read body")
}
c.log.Printf("get torrent trackers body: %v\n", string(body))
var trackers []TorrentTracker
if err := json.Unmarshal(body, &trackers); err != nil {
return nil, errors.Wrap(err, "could not unmarshal body")
}
return trackers, nil
}
// AddTorrentFromFile add new torrent from torrent file
func (c *Client) AddTorrentFromFile(file string, options map[string]string) error {
res, err := c.postFile("torrents/add", file, options)
if err != nil {
return errors.Wrap(err, "could not add torrent %v", file)
} else if res.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not add torrent %v unexpected status: %v", file, res.StatusCode)
}
defer res.Body.Close()
return nil
}
func (c *Client) DeleteTorrents(hashes []string, deleteFiles bool) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
"hashes": hv,
"deleteFiles": strconv.FormatBool(deleteFiles),
}
resp, err := c.get("torrents/delete", opts)
if err != nil {
return errors.Wrap(err, "could not delete torrents: %+v", hashes)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not delete torrents %v unexpected status: %v", hashes, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) ReAnnounceTorrents(hashes []string) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
"hashes": hv,
}
resp, err := c.get("torrents/reannounce", opts)
if err != nil {
return errors.Wrap(err, "could not re-announce torrents: %v", hashes)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not re-announce torrents: %v unexpected status: %v", hashes, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) GetTransferInfo() (*TransferInfo, error) {
resp, err := c.get("transfer/info", nil)
if err != nil {
return nil, errors.Wrap(err, "could not get transfer info")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read body")
}
var info TransferInfo
if err := json.Unmarshal(body, &info); err != nil {
return nil, errors.Wrap(err, "could not unmarshal body")
}
return &info, nil
}
func (c *Client) Resume(hashes []string) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
"hashes": hv,
}
resp, err := c.get("torrents/resume", opts)
if err != nil {
return errors.Wrap(err, "could not resume torrents: %v", hashes)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not resume torrents: %v unexpected status: %v", hashes, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) SetForceStart(hashes []string, value bool) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
"hashes": hv,
"value": strconv.FormatBool(value),
}
resp, err := c.get("torrents/setForceStart", opts)
if err != nil {
return errors.Wrap(err, "could not setForceStart torrents: %v", hashes)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not setForceStart torrents: %v unexpected status: %v", hashes, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) Recheck(hashes []string) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
"hashes": hv,
}
resp, err := c.get("torrents/recheck", opts)
if err != nil {
return errors.Wrap(err, "could not recheck torrents: %v", hashes)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not recheck torrents: %v unexpected status: %v", hashes, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) Pause(hashes []string) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
"hashes": hv,
}
resp, err := c.get("torrents/pause", opts)
if err != nil {
return errors.Wrap(err, "could not pause torrents: %v", hashes)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not pause torrents: %v unexpected status: %v", hashes, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) SetAutoManagement(hashes []string, enable bool) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
"hashes": hv,
"enable": strconv.FormatBool(enable),
}
resp, err := c.get("torrents/setAutoManagement", opts)
if err != nil {
return errors.Wrap(err, "could not setAutoManagement torrents: %v", hashes)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not setAutoManagement torrents: %v unexpected status: %v", hashes, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) CreateCategory(category string, path string) error {
opts := map[string]string{
"category": category,
"savePath": path,
}
resp, err := c.get("torrents/createCategory", opts)
if err != nil {
return errors.Wrap(err, "could not createCategory torrents: %v", category)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not createCategory torrents: %v unexpected status: %v", category, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) EditCategory(category string, path string) error {
opts := map[string]string{
"category": category,
"savePath": path,
}
resp, err := c.get("torrents/editCategory", opts)
if err != nil {
return errors.Wrap(err, "could not editCategory torrents: %v", category)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not editCategory torrents: %v unexpected status: %v", category, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) RemoveCategories(categories []string) error {
opts := map[string]string{
"categories": strings.Join(categories, "\n"),
}
resp, err := c.get("torrents/removeCategories", opts)
if err != nil {
return errors.Wrap(err, "could not removeCategories torrents: %v", opts["categories"])
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not removeCategories torrents: %v unexpected status: %v", opts["categories"], resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) SetCategory(hashes []string, category string) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
"hashes": hv,
"category": category,
}
resp, err := c.get("torrents/setCategory", opts)
if err != nil {
return errors.Wrap(err, "could not setCategory torrents: %v", hashes)
} else if resp.StatusCode != http.StatusOK {
return errors.Wrap(err, "could not setCategory torrents: %v unexpected status: %v", hashes, resp.StatusCode)
}
defer resp.Body.Close()
return nil
}
func (c *Client) GetFilesInformation(hash string) (*TorrentFiles, error) {
opts := map[string]string{
"hash": hash,
}
resp, err := c.get("torrents/files", opts)
if err != nil {
return nil, errors.Wrap(err, "could not get files info")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read body")
}
var info TorrentFiles
if err := json.Unmarshal(body, &info); err != nil {
return nil, errors.Wrap(err, "could not unmarshal body")
}
return &info, nil
}
func (c *Client) GetCategories() (map[string]Category, error) {
resp, err := c.get("torrents/categories", nil)
if err != nil {
return nil, errors.Wrap(err, "could not get files info")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read body")
}
m := make(map[string]Category)
if err := json.Unmarshal(body, &m); err != nil {
return nil, errors.Wrap(err, "could not unmarshal body")
}
return m, nil
}