Implement MonitorAlbum: search, parse, filter by quality, add best to qbittorrent

This commit is contained in:
Alexander
2026-05-07 23:21:21 +02:00
parent 79f3f145de
commit 8ad2734964
16 changed files with 1479 additions and 59 deletions
+2 -1
View File
@@ -43,5 +43,6 @@ type TorrentClient interface {
Login(username string, password string) (string, error)
List() ([]TorrentInfo, error)
Find(opts FindOptions) ([]TorrentInfo, error)
Add(file TorrentFile) error
AddTorrent(file TorrentFile) error
AddMagnet(magnetURI string) error
}
+28
View File
@@ -0,0 +1,28 @@
package torrent
import (
"fmt"
"github.com/rs/zerolog/log"
"homelab.lan/music-agregator/internal/config"
)
func NewTorrentClient(cfg config.Config) (TorrentClient, error) {
var client TorrentClient
switch cfg.Torrent.ClientType {
case config.TorrentClientQbittorrent:
client = NewQbittorrentClient(cfg.Torrent.Url)
default:
return nil, fmt.Errorf("unknown torrent client type: %s", cfg.Torrent.ClientType)
}
if _, err := client.Login(cfg.Torrent.Username, cfg.Torrent.Password); err != nil {
return nil, fmt.Errorf("torrent client login failed: %w", err)
}
log.Info().Str("client", string(cfg.Torrent.ClientType)).Str("url", cfg.Torrent.Url).Msg("torrent client connected")
return client, nil
}
+28 -6
View File
@@ -173,8 +173,8 @@ func filterLocally(torrents []TorrentInfo, opts FindOptions) []TorrentInfo {
return result
}
func (c *QbittorrentClient) Add(file TorrentFile) error {
log.Trace().Str("filename", file.Filename).Int("size", len(file.Data)).Msg("qbittorrent adding torrent")
func (c *QbittorrentClient) AddTorrent(file TorrentFile) error {
log.Trace().Str("filename", file.Filename).Int("size", len(file.Data)).Msg("qbittorrent adding torrent file")
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
@@ -202,6 +202,29 @@ func (c *QbittorrentClient) Add(file TorrentFile) error {
req.Header.Set("Content-Type", writer.FormDataContentType())
req.AddCookie(&http.Cookie{Name: "SID", Value: c.sid})
return c.doAdd(req, file.Filename)
}
func (c *QbittorrentClient) AddMagnet(magnetURI string) error {
truncated := magnetURI
if len(truncated) > 80 {
truncated = truncated[:80] + "..."
}
log.Trace().Str("magnet", truncated).Msg("qbittorrent adding magnet")
data := url.Values{"urls": {magnetURI}}
req, err := http.NewRequest("POST", c.baseURL+"/api/v2/torrents/add", strings.NewReader(data.Encode()))
if err != nil {
log.Error().Err(err).Msg("qbittorrent creating magnet add request failed")
return fmt.Errorf("creating magnet add request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.AddCookie(&http.Cookie{Name: "SID", Value: c.sid})
return c.doAdd(req, truncated)
}
func (c *QbittorrentClient) doAdd(req *http.Request, label string) error {
start := time.Now()
resp, err := c.client.Do(req)
if err != nil {
@@ -219,12 +242,11 @@ func (c *QbittorrentClient) Add(file TorrentFile) error {
log.Trace().Int("status", resp.StatusCode).Dur("duration", time.Since(start)).Msg("qbittorrent add response")
if resp.StatusCode != http.StatusOK || string(body) != "Ok." {
log.Error().Int("status", resp.StatusCode).Str("body", string(body)).Msg("qbittorrent add torrent failed")
return fmt.Errorf("add torrent failed: status %d, body: %s", resp.StatusCode, string(body))
log.Error().Int("status", resp.StatusCode).Str("body", string(body)).Msg("qbittorrent add failed")
return fmt.Errorf("add failed: status %d, body: %s", resp.StatusCode, string(body))
}
log.Info().Str("filename", file.Filename).Msg("qbittorrent torrent added")
log.Info().Str("label", label).Msg("qbittorrent torrent added")
return nil
}
+1 -1
View File
@@ -69,7 +69,7 @@ func (service *TorrentService) Add(req *pb.AddRequest) (*pb.AddResponse, error)
return nil, fmt.Errorf("either torrent_data or download_url must be provided")
}
if err := service.client.Add(file); err != nil {
if err := service.client.AddTorrent(file); err != nil {
return nil, err
}