Implement MonitorAlbum: search, parse, filter by quality, add best to qbittorrent
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user