mirror of
https://github.com/simon-ding/polaris.git
synced 2026-04-21 03:07:30 +08:00
feat: complete qbittorrent support
This commit is contained in:
@@ -9,9 +9,13 @@ type Torrent interface {
|
||||
Save() string
|
||||
Exists() bool
|
||||
SeedRatio() (float64, error)
|
||||
GetHash() string
|
||||
}
|
||||
|
||||
type Downloader interface {
|
||||
GetAll() ([]Torrent, error)
|
||||
Download(link, dir string) (Torrent, error)
|
||||
}
|
||||
|
||||
type Storage interface {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,49 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
URL string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
c *qbt.Client
|
||||
Info
|
||||
}
|
||||
|
||||
func NewClient(url, user, pass string) (*Client, error) {
|
||||
// connect to qbittorrent client
|
||||
qb := qbt.NewClient(url)
|
||||
|
||||
// login to the client
|
||||
loginOpts := qbt.LoginOptions{
|
||||
Username: user,
|
||||
Password: pass,
|
||||
}
|
||||
err := qb.Login(loginOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{c: qb, Info: Info{URL: url, User: user, Password: pass}}, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetAll() ([]pkg.Torrent, error) {
|
||||
tt, err :=c.c.Torrents(qbt.TorrentsOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get torrents")
|
||||
}
|
||||
var res []pkg.Torrent
|
||||
for _, t := range tt {
|
||||
t1 := &Torrent{
|
||||
c: c.c,
|
||||
Hash: t.Hash,
|
||||
Info: c.Info,
|
||||
}
|
||||
res = append(res, t1)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Client) Download(link, dir string) (pkg.Torrent, error) {
|
||||
@@ -48,16 +89,18 @@ loop:
|
||||
if newHash == "" {
|
||||
return nil, fmt.Errorf("download torrent fail: timeout")
|
||||
}
|
||||
return &Torrent{Hash: newHash, c: c.c}, nil
|
||||
return &Torrent{Hash: newHash, c: c.c, Info: c.Info}, nil
|
||||
|
||||
}
|
||||
|
||||
type Torrent struct {
|
||||
c *qbt.Client
|
||||
Hash string
|
||||
URL string
|
||||
User string
|
||||
Password string
|
||||
c *qbt.Client
|
||||
Hash string
|
||||
Info
|
||||
}
|
||||
|
||||
func (t *Torrent) GetHash() string {
|
||||
return t.Hash
|
||||
}
|
||||
|
||||
func (t *Torrent) getTorrent() (*qbt.TorrentInfo, error) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"polaris/log"
|
||||
"polaris/pkg"
|
||||
"strings"
|
||||
|
||||
"github.com/hekmon/transmissionrpc/v3"
|
||||
@@ -45,12 +46,12 @@ type Client struct {
|
||||
cfg Config
|
||||
}
|
||||
|
||||
func (c *Client) GetAll() ([]*Torrent, error) {
|
||||
func (c *Client) GetAll() ([]pkg.Torrent, error) {
|
||||
all, err := c.c.TorrentGetAll(context.TODO())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get all")
|
||||
}
|
||||
var torrents []*Torrent
|
||||
var torrents []pkg.Torrent
|
||||
for _, t := range all {
|
||||
torrents = append(torrents, &Torrent{
|
||||
Hash: *t.HashString,
|
||||
@@ -61,7 +62,7 @@ func (c *Client) GetAll() ([]*Torrent, error) {
|
||||
return torrents, nil
|
||||
}
|
||||
|
||||
func (c *Client) Download(link, dir string) (*Torrent, error) {
|
||||
func (c *Client) Download(link, dir string) (pkg.Torrent, error) {
|
||||
if strings.HasPrefix(link, "http") {
|
||||
client := &http.Client{
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
@@ -207,6 +208,11 @@ func (t *Torrent) Save() string {
|
||||
return string(d)
|
||||
}
|
||||
|
||||
func (t *Torrent) GetHash() string {
|
||||
return t.Hash
|
||||
}
|
||||
|
||||
|
||||
func ReloadTorrent(s string) (*Torrent, error) {
|
||||
var torrent = Torrent{}
|
||||
err := json.Unmarshal([]byte(s), &torrent)
|
||||
|
||||
Reference in New Issue
Block a user