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

@@ -123,7 +123,7 @@ type TorrentInfo struct {
}
func (s *Server) GetAllTorrents(c *gin.Context) (interface{}, error) {
trc, _, err := s.getDownloadClient()
trc, _, err := s.core.GetDownloadClient()
if err != nil {
return nil, errors.Wrap(err, "connect transmission")
}
@@ -140,7 +140,7 @@ func (s *Server) GetAllTorrents(c *gin.Context) (interface{}, error) {
p, _ := t.Progress()
infos = append(infos, TorrentInfo{
Name: name,
ID: t.Hash,
ID: t.GetHash(),
Progress: p,
})
}

View File

@@ -3,7 +3,10 @@ package core
import (
"polaris/db"
"polaris/ent"
"polaris/ent/downloadclients"
"polaris/log"
"polaris/pkg"
"polaris/pkg/qbittorrent"
"polaris/pkg/tmdb"
"polaris/pkg/transmission"
"polaris/pkg/utils"
@@ -61,17 +64,34 @@ func (c *Client) reloadTasks() {
}
}
func (c *Client) getDownloadClient() (*transmission.Client, *ent.DownloadClients, error) {
tr := c.db.GetTransmission()
trc, err := transmission.NewClient(transmission.Config{
URL: tr.URL,
User: tr.User,
Password: tr.Password,
})
if err != nil {
return nil, nil, errors.Wrap(err, "connect transmission")
func (c *Client) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, error) {
downloaders := c.db.GetAllDonloadClients()
for _, d := range downloaders {
if !d.Enable {
continue
}
if d.Implementation == downloadclients.ImplementationTransmission {
trc, err := transmission.NewClient(transmission.Config{
URL: d.URL,
User: d.User,
Password: d.Password,
})
if err != nil {
log.Warnf("connect to download client error: %v", d.URL)
continue
}
return trc, d, nil
} else if d.Implementation == downloadclients.ImplementationQbittorrent {
qbt, err := qbittorrent.NewClient(d.URL, d.User, d.Password)
if err != nil {
log.Warnf("connect to download client error: %v", d.URL)
continue
}
return qbt, d, nil
}
}
return trc, tr, nil
return nil, nil, errors.Errorf("no available download client")
}
func (c *Client) TMDB() (*tmdb.Client, error) {

View File

@@ -14,7 +14,7 @@ import (
)
func (c *Client) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum, episodeNum int) (*string, error) {
trc, dlc, err := c.getDownloadClient()
trc, dlc, err := c.GetDownloadClient()
if err != nil {
return nil, errors.Wrap(err, "connect transmission")
}
@@ -102,7 +102,7 @@ func (c *Client) SearchAndDownload(seriesId, seasonNum, episodeNum int) (*string
}
func (c *Client) DownloadMovie(m *ent.Media, link, name string, size int, indexerID int) (*string, error) {
trc, dlc, err := c.getDownloadClient()
trc, dlc, err := c.GetDownloadClient()
if err != nil {
return nil, errors.Wrap(err, "connect transmission")
}

View File

@@ -341,7 +341,7 @@ func (c *Client) DownloadMovieByID(id int) (string, error) {
}
func (c *Client) downloadMovieSingleEpisode(ep *ent.Episode, targetDir string) (string, error) {
trc, dlc, err := c.getDownloadClient()
trc, dlc, err := c.GetDownloadClient()
if err != nil {
return "", errors.Wrap(err, "connect transmission")
}

View File

@@ -6,7 +6,9 @@ import (
"html/template"
"polaris/db"
"polaris/ent"
"polaris/ent/downloadclients"
"polaris/log"
"polaris/pkg/qbittorrent"
"polaris/pkg/transmission"
"polaris/pkg/utils"
"strconv"
@@ -189,20 +191,6 @@ func (s *Server) GetAllIndexers(c *gin.Context) (interface{}, error) {
}
return indexers, nil
}
func (s *Server) getDownloadClient() (*transmission.Client, *ent.DownloadClients, error) {
tr := s.db.GetTransmission()
trc, err := transmission.NewClient(transmission.Config{
URL: tr.URL,
User: tr.User,
Password: tr.Password,
})
if err != nil {
return nil, nil, errors.Wrap(err, "connect transmission")
}
return trc, tr, nil
}
type downloadClientIn struct {
Name string `json:"name" binding:"required"`
URL string `json:"url" binding:"required"`
@@ -218,16 +206,30 @@ func (s *Server) AddDownloadClient(c *gin.Context) (interface{}, error) {
}
utils.TrimFields(&in)
//test connection
_, err := transmission.NewClient(transmission.Config{
URL: in.URL,
User: in.User,
Password: in.Password,
})
if err != nil {
return nil, errors.Wrap(err, "tranmission setting")
if in.Implementation == downloadclients.ImplementationTransmission.String() {
_, err := transmission.NewClient(transmission.Config{
URL: in.URL,
User: in.User,
Password: in.Password,
})
if err != nil {
return nil, errors.Wrap(err, "tranmission setting")
}
} else if in.Implementation == downloadclients.ImplementationQbittorrent.String() {
_, err := qbittorrent.NewClient(in.URL, in.User, in.Password)
if err != nil {
return nil, errors.Wrap(err, "qbittorrent")
}
}
if err := s.db.SaveTransmission(in.Name, in.URL, in.User, in.Password); err != nil {
return nil, errors.Wrap(err, "save transmission")
if err := s.db.SaveDownloader(&ent.DownloadClients{
Name: in.Name,
Implementation: downloadclients.Implementation(in.Implementation),
URL: in.URL,
User: in.User,
Password: in.Password,
}); err != nil {
return nil, errors.Wrap(err, "save downloader")
}
return nil, nil
}