feat(download-clients): porla implement rules (#711)

* feat(downloadclients): Porla implement rules

* feat(downloadclients): Porla add basic auth support

* feat(porla): use new token for auth

* feat(porla): update check can download rules
This commit is contained in:
ze0s 2023-02-24 19:17:02 +01:00 committed by GitHub
parent 209e23de4f
commit d100703784
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 248 additions and 59 deletions

View file

@ -4,54 +4,66 @@ import (
"bufio"
"context"
"encoding/base64"
"io/ioutil"
"io"
"os"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/porla"
"github.com/dcarbone/zadapters/zstdlog"
"github.com/rs/zerolog"
)
func (s *service) porla(action domain.Action, release domain.Release) ([]string, error) {
s.log.Debug().Msgf("action Porla: %v", action.Name)
func (s *service) porla(ctx context.Context, action *domain.Action, release domain.Release) ([]string, error) {
s.log.Debug().Msgf("action Porla: %s", action.Name)
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
client, err := s.clientSvc.FindByID(ctx, action.ClientID)
if err != nil {
return nil, errors.Wrap(err, "error finding client: %v", action.ClientID)
return nil, errors.Wrap(err, "error finding client: %d", action.ClientID)
}
if client == nil {
return nil, errors.New("could not find client by id: %v", action.ClientID)
return nil, errors.New("could not find client by id: %d", action.ClientID)
}
porlaSettings := porla.Settings{
Hostname: client.Host,
AuthToken: client.Settings.APIKey,
porlaSettings := porla.Config{
Hostname: client.Host,
AuthToken: client.Settings.APIKey,
TLSSkipVerify: client.TLSSkipVerify,
BasicUser: client.Settings.Basic.Username,
BasicPass: client.Settings.Basic.Password,
}
porlaSettings.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "Porla").Str("client", client.Name).Logger(), zerolog.TraceLevel)
prl := porla.NewClient(porlaSettings)
rejections, err := s.porlaCheckRulesCanDownload(ctx, action, client, prl)
if err != nil {
return nil, errors.Wrap(err, "error checking Porla client rules: %s", action.Name)
}
if len(rejections) > 0 {
return rejections, nil
}
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
return nil, errors.Wrap(err, "error downloading torrent file for release: %v", release.TorrentName)
return nil, errors.Wrap(err, "error downloading torrent file for release: %s", release.TorrentName)
}
}
file, err := os.Open(release.TorrentTmpFile)
if err != nil {
return nil, errors.Wrap(err, "error opening file %v", release.TorrentTmpFile)
return nil, errors.Wrap(err, "error opening file %s", release.TorrentTmpFile)
}
defer file.Close()
reader := bufio.NewReader(file)
content, err := ioutil.ReadAll(reader)
content, err := io.ReadAll(reader)
if err != nil {
return nil, errors.Wrap(err, "failed to read file: %v", release.TorrentTmpFile)
return nil, errors.Wrap(err, "failed to read file: %s", release.TorrentTmpFile)
}
opts := &porla.TorrentsAddReq{
@ -69,7 +81,7 @@ func (s *service) porla(action domain.Action, release domain.Release) ([]string,
opts.UploadLimit = action.LimitUploadSpeed * 1000
}
if err = prl.TorrentsAdd(opts); err != nil {
if err = prl.TorrentsAdd(ctx, opts); err != nil {
return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
}
@ -77,3 +89,27 @@ func (s *service) porla(action domain.Action, release domain.Release) ([]string,
return nil, nil
}
func (s *service) porlaCheckRulesCanDownload(ctx context.Context, action *domain.Action, client *domain.DownloadClient, prla *porla.Client) ([]string, error) {
s.log.Trace().Msgf("action Porla: %s check rules", action.Name)
// check for active downloads and other rules
if client.Settings.Rules.Enabled && !action.IgnoreRules {
torrents, err := prla.TorrentsList(ctx, &porla.TorrentsListFilters{Query: "is:downloading and not is:paused"})
if err != nil {
return nil, errors.Wrap(err, "could not fetch active downloads")
}
if client.Settings.Rules.MaxActiveDownloads > 0 {
if len(torrents.Torrents) >= client.Settings.Rules.MaxActiveDownloads {
rejection := "max active downloads reached, skipping"
s.log.Debug().Msg(rejection)
return []string{rejection}, nil
}
}
}
return nil, nil
}

View file

@ -61,7 +61,7 @@ func (s *service) RunAction(ctx context.Context, action *domain.Action, release
rejections, err = s.transmission(ctx, action, release)
case domain.ActionTypePorla:
rejections, err = s.porla(*action, release)
rejections, err = s.porla(ctx, action, release)
case domain.ActionTypeRadarr:
rejections, err = s.radarr(ctx, action, release)