WIP
This commit is contained in:
@@ -12,6 +12,8 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type QBittorrentClient struct {
|
||||
@@ -54,7 +56,7 @@ func (c *QBittorrentClient) apiURL(path string) string {
|
||||
|
||||
func (c *QBittorrentClient) mapState(state string) TorrentState {
|
||||
switch state {
|
||||
case "downloading", "forcedDL", "metaDL", "allocating":
|
||||
case "downloading", "forcedDL", "metaDL", "allocating", "stalledDL":
|
||||
return StateDownloading
|
||||
case "uploading", "forcedUP", "stalledUP":
|
||||
return StateSeeding
|
||||
@@ -209,6 +211,8 @@ func (c *QBittorrentClient) AddTorrentURL(ctx context.Context, torrentURL string
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debug().Str("url", torrentURL).Msg("[QBITTORRENT] adding torrent URL")
|
||||
|
||||
var buf bytes.Buffer
|
||||
w := multipart.NewWriter(&buf)
|
||||
w.WriteField("urls", torrentURL)
|
||||
@@ -225,15 +229,27 @@ func (c *QBittorrentClient) AddTorrentURL(ctx context.Context, torrentURL string
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("[QBITTORRENT] request failed")
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
bodyStr := strings.TrimSpace(string(body))
|
||||
|
||||
log.Debug().Int("status", resp.StatusCode).Str("body", bodyStr).Msg("[QBITTORRENT] add torrent response")
|
||||
|
||||
if !statusOK(resp.StatusCode) {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("%w: %s", ErrInvalidRequest, string(body))
|
||||
log.Error().Int("status", resp.StatusCode).Str("body", bodyStr).Msg("[QBITTORRENT] add torrent failed")
|
||||
return fmt.Errorf("%w: %s", ErrInvalidRequest, bodyStr)
|
||||
}
|
||||
|
||||
if bodyStr == "Fails." {
|
||||
log.Error().Str("url", torrentURL).Msg("[QBITTORRENT] torrent add rejected")
|
||||
return fmt.Errorf("qBittorrent rejected torrent: %s", torrentURL)
|
||||
}
|
||||
|
||||
log.Info().Msg("[QBITTORRENT] torrent added successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user