feat: complete qbittorrent support

This commit is contained in:
Simon Ding
2024-10-04 10:31:49 +08:00
parent 6a5c105f8c
commit c42cbb5e5d
20 changed files with 431 additions and 284 deletions

View File

@@ -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 {
}
}

View File

@@ -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) {

View File

@@ -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)