mirror of
https://github.com/idanoo/autobrr
synced 2025-07-24 17:29:12 +00:00
feat: add backend
This commit is contained in:
parent
bc418ff248
commit
a838d994a6
68 changed files with 9561 additions and 0 deletions
176
pkg/qbittorrent/client.go
Normal file
176
pkg/qbittorrent/client.go
Normal file
|
@ -0,0 +1,176 @@
|
|||
package qbittorrent
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
settings Settings
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
type Settings struct {
|
||||
Hostname string
|
||||
Port uint
|
||||
Username string
|
||||
Password string
|
||||
SSL bool
|
||||
protocol string
|
||||
}
|
||||
|
||||
func NewClient(s Settings) *Client {
|
||||
jarOptions := &cookiejar.Options{PublicSuffixList: publicsuffix.List}
|
||||
//store cookies in jar
|
||||
jar, err := cookiejar.New(jarOptions)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("new client cookie error")
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 10,
|
||||
Jar: jar,
|
||||
}
|
||||
|
||||
c := &Client{
|
||||
settings: s,
|
||||
http: httpClient,
|
||||
}
|
||||
|
||||
c.settings.protocol = "http"
|
||||
if c.settings.SSL {
|
||||
c.settings.protocol = "https"
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) get(endpoint string, opts map[string]string) (*http.Response, error) {
|
||||
reqUrl := fmt.Sprintf("%v://%v:%v/api/v2/%v", c.settings.protocol, c.settings.Hostname, c.settings.Port, endpoint)
|
||||
|
||||
req, err := http.NewRequest("GET", reqUrl, nil)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("GET: error %v", reqUrl)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("GET: do %v", reqUrl)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
reqUrl := fmt.Sprintf("%v://%v:%v/api/v2/%v", c.settings.protocol, c.settings.Hostname, c.settings.Port, endpoint)
|
||||
req, err := http.NewRequest("POST", reqUrl, strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("POST: req %v", reqUrl)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 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 {
|
||||
log.Error().Err(err).Msgf("POST: do %v", reqUrl)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) postFile(endpoint string, fileName string, opts map[string]string) (*http.Response, error) {
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("POST file: opening file %v", fileName)
|
||||
return nil, err
|
||||
}
|
||||
// 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 {
|
||||
log.Error().Err(err).Msgf("POST file: initializing file field %v", fileName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Copy the actual file content to the fields writer
|
||||
_, err = io.Copy(fileWriter, file)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("POST file: could not copy file to writer %v", fileName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Populate other fields
|
||||
if opts != nil {
|
||||
for key, val := range opts {
|
||||
fieldWriter, err := multiPartWriter.CreateFormField(key)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("POST file: could not add other fields %v", fileName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = fieldWriter.Write([]byte(val))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("POST file: could not write field %v", fileName)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close multipart writer
|
||||
multiPartWriter.Close()
|
||||
|
||||
reqUrl := fmt.Sprintf("%v://%v:%v/api/v2/%v", c.settings.protocol, c.settings.Hostname, c.settings.Port, endpoint)
|
||||
req, err := http.NewRequest("POST", reqUrl, &requestBody)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("POST file: could not create request object %v", fileName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set correct content type
|
||||
req.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
|
||||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("POST file: could not perform request %v", fileName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Client) setCookies(cookies []*http.Cookie) {
|
||||
cookieURL, _ := url.Parse(fmt.Sprintf("%v://%v:%v", c.settings.protocol, c.settings.Hostname, c.settings.Port))
|
||||
c.http.Jar.SetCookies(cookieURL, cookies)
|
||||
}
|
179
pkg/qbittorrent/domain.go
Normal file
179
pkg/qbittorrent/domain.go
Normal file
|
@ -0,0 +1,179 @@
|
|||
package qbittorrent
|
||||
|
||||
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 uint `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 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
|
||||
TorrentStateForceDl TorrentState = "forceDL"
|
||||
|
||||
// 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"
|
||||
)
|
||||
|
||||
// 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
|
||||
)
|
222
pkg/qbittorrent/methods.go
Normal file
222
pkg/qbittorrent/methods.go
Normal file
|
@ -0,0 +1,222 @@
|
|||
package qbittorrent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// Login https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#authentication
|
||||
func (c *Client) Login() error {
|
||||
credentials := make(map[string]string)
|
||||
credentials["username"] = c.settings.Username
|
||||
credentials["password"] = c.settings.Password
|
||||
|
||||
resp, err := c.post("auth/login", credentials)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("login error")
|
||||
return err
|
||||
} else if resp.StatusCode == http.StatusForbidden {
|
||||
log.Error().Err(err).Msg("User's IP is banned for too many failed login attempts")
|
||||
return err
|
||||
|
||||
} else if resp.StatusCode != http.StatusOK { // check for correct status code
|
||||
log.Error().Err(err).Msg("login bad status error")
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
bodyBytes, err := ioutil.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")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) GetTorrents() ([]Torrent, error) {
|
||||
var torrents []Torrent
|
||||
|
||||
resp, err := c.get("torrents/info", nil)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("get torrents error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, readErr := ioutil.ReadAll(resp.Body)
|
||||
if readErr != nil {
|
||||
log.Error().Err(err).Msg("get torrents read error")
|
||||
return nil, readErr
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &torrents)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("get torrents unmarshal error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return torrents, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
|
||||
var torrents []Torrent
|
||||
|
||||
v := url.Values{}
|
||||
v.Add("filter", string(filter))
|
||||
params := v.Encode()
|
||||
|
||||
resp, err := c.get("torrents/info?"+params, nil)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("get filtered torrents error: %v", filter)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, readErr := ioutil.ReadAll(resp.Body)
|
||||
if readErr != nil {
|
||||
log.Error().Err(err).Msgf("get filtered torrents read error: %v", filter)
|
||||
return nil, readErr
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &torrents)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("get filtered torrents unmarshal error: %v", filter)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return torrents, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetTorrentsRaw() (string, error) {
|
||||
resp, err := c.get("torrents/info", nil)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("get torrent trackers raw error")
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
|
||||
var trackers []TorrentTracker
|
||||
|
||||
params := url.Values{}
|
||||
params.Add("hash", hash)
|
||||
|
||||
p := params.Encode()
|
||||
|
||||
resp, err := c.get("torrents/trackers?"+p, nil)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("get torrent trackers error: %v", hash)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, readErr := ioutil.ReadAll(resp.Body)
|
||||
if readErr != nil {
|
||||
log.Error().Err(err).Msgf("get torrent trackers read error: %v", hash)
|
||||
return nil, readErr
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &trackers)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("get torrent trackers: %v", hash)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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 {
|
||||
log.Error().Err(err).Msgf("add torrents error: %v", file)
|
||||
return err
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Msgf("add torrents bad status: %v", file)
|
||||
return err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) DeleteTorrents(hashes []string, deleteFiles bool) error {
|
||||
v := url.Values{}
|
||||
|
||||
// Add hashes together with | separator
|
||||
hv := strings.Join(hashes, "|")
|
||||
v.Add("hashes", hv)
|
||||
v.Add("deleteFiles", strconv.FormatBool(deleteFiles))
|
||||
|
||||
encodedHashes := v.Encode()
|
||||
|
||||
resp, err := c.get("torrents/delete?"+encodedHashes, nil)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("delete torrents error: %v", hashes)
|
||||
return err
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Msgf("delete torrents bad code: %v", hashes)
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) ReAnnounceTorrents(hashes []string) error {
|
||||
v := url.Values{}
|
||||
|
||||
// Add hashes together with | separator
|
||||
hv := strings.Join(hashes, "|")
|
||||
v.Add("hashes", hv)
|
||||
|
||||
encodedHashes := v.Encode()
|
||||
|
||||
resp, err := c.get("torrents/reannounce?"+encodedHashes, nil)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("re-announce error: %v", hashes)
|
||||
return err
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Msgf("re-announce error bad status: %v", hashes)
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue