mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
Feature: Download client rules (#18)
* feat(web): add and update download client rules * feat: add and update download client rules * feat: add active downloads check * chore: update pkg * feat: deluge max active downloads * feat: use basic rules for deluge * feat: add as paused * refactor: download file if needed * feat: better errors qbit
This commit is contained in:
parent
09eb0b1716
commit
c02f16b64d
25 changed files with 628 additions and 228 deletions
|
@ -13,14 +13,14 @@ import (
|
|||
)
|
||||
|
||||
func (s *service) deluge(action domain.Action, torrentFile string) error {
|
||||
log.Trace().Msgf("action DELUGE: %v", torrentFile)
|
||||
log.Debug().Msgf("action Deluge: %v", action.Name)
|
||||
|
||||
var err error
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error finding client: %v", action.ClientID)
|
||||
log.Error().Stack().Err(err).Msgf("error finding client: %v", action.ClientID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -39,23 +39,109 @@ func (s *service) deluge(action domain.Action, torrentFile string) error {
|
|||
|
||||
switch client.Type {
|
||||
case "DELUGE_V1":
|
||||
err = delugeV1(settings, action, torrentFile)
|
||||
if err = delugeV1(client, settings, action, torrentFile); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case "DELUGE_V2":
|
||||
err = delugeV2(settings, action, torrentFile)
|
||||
if err = delugeV2(client, settings, action, torrentFile); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func delugeV1(settings delugeClient.Settings, action domain.Action, torrentFile string) error {
|
||||
func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error) {
|
||||
log.Trace().Msgf("action Deluge: %v check rules", action.Name)
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error finding client: %v ID %v", action.Name, action.ClientID)
|
||||
return false, err
|
||||
}
|
||||
|
||||
if client == nil {
|
||||
return false, errors.New("no client found")
|
||||
}
|
||||
|
||||
settings := delugeClient.Settings{
|
||||
Hostname: client.Host,
|
||||
Port: uint(client.Port),
|
||||
Login: client.Username,
|
||||
Password: client.Password,
|
||||
DebugServerResponses: true,
|
||||
ReadWriteTimeout: time.Second * 20,
|
||||
}
|
||||
var deluge delugeClient.DelugeClient
|
||||
|
||||
switch client.Type {
|
||||
case "DELUGE_V1":
|
||||
deluge = delugeClient.NewV1(settings)
|
||||
|
||||
case "DELUGE_V2":
|
||||
deluge = delugeClient.NewV2(settings)
|
||||
}
|
||||
|
||||
// perform connection to Deluge server
|
||||
err = deluge.Connect()
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
|
||||
return false, err
|
||||
}
|
||||
|
||||
defer deluge.Close()
|
||||
|
||||
// check for active downloads and other rules
|
||||
if client.Settings.Rules.Enabled && !action.IgnoreRules {
|
||||
activeDownloads, err := deluge.TorrentsStatus(delugeClient.StateDownloading, nil)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("Deluge - could not fetch downloading torrents")
|
||||
return false, err
|
||||
}
|
||||
|
||||
// make sure it's not set to 0 by default
|
||||
if client.Settings.Rules.MaxActiveDownloads > 0 {
|
||||
|
||||
// if max active downloads reached, check speed and if lower than threshold add anyways
|
||||
if len(activeDownloads) >= client.Settings.Rules.MaxActiveDownloads {
|
||||
log.Debug().Msg("max active downloads reached, skipping")
|
||||
return false, nil
|
||||
|
||||
// // TODO handle ignore slow torrents
|
||||
//if client.Settings.Rules.IgnoreSlowTorrents {
|
||||
//
|
||||
// // get session state
|
||||
// // gives type conversion errors
|
||||
// state, err := deluge.GetSessionStatus()
|
||||
// if err != nil {
|
||||
// log.Error().Err(err).Msg("could not get session state")
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// if int64(state.DownloadRate)*1024 >= client.Settings.Rules.DownloadSpeedThreshold {
|
||||
// log.Trace().Msg("max active downloads reached, skip adding")
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// log.Trace().Msg("active downloads are slower than set limit, lets add it")
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func delugeV1(client *domain.DownloadClient, settings delugeClient.Settings, action domain.Action, torrentFile string) error {
|
||||
|
||||
deluge := delugeClient.NewV1(settings)
|
||||
|
||||
// perform connection to Deluge server
|
||||
err := deluge.Connect()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error logging into client: %v", settings.Hostname)
|
||||
log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -63,14 +149,14 @@ func delugeV1(settings delugeClient.Settings, action domain.Action, torrentFile
|
|||
|
||||
t, err := ioutil.ReadFile(torrentFile)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not read torrent file: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not read torrent file: %v", torrentFile)
|
||||
return err
|
||||
}
|
||||
|
||||
// encode file to base64 before sending to deluge
|
||||
encodedFile := base64.StdEncoding.EncodeToString(t)
|
||||
if encodedFile == "" {
|
||||
log.Error().Err(err).Msgf("could not encode torrent file: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not encode torrent file: %v", torrentFile)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -92,17 +178,18 @@ func delugeV1(settings delugeClient.Settings, action domain.Action, torrentFile
|
|||
options.MaxUploadSpeed = &maxUL
|
||||
}
|
||||
|
||||
log.Trace().Msgf("action Deluge options: %+v", options)
|
||||
|
||||
torrentHash, err := deluge.AddTorrentFile(torrentFile, encodedFile, &options)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not add torrent to client: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", torrentFile, client.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
if action.Label != "" {
|
||||
|
||||
p, err := deluge.LabelPlugin()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not load label plugin: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not load label plugin: %v", client.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -110,25 +197,25 @@ func delugeV1(settings delugeClient.Settings, action domain.Action, torrentFile
|
|||
// TODO first check if label exists, if not, add it, otherwise set
|
||||
err = p.SetTorrentLabel(torrentHash, action.Label)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not set label: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not set label: %v on client: %v", action.Label, client.Name)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Trace().Msgf("deluge: torrent successfully added! hash: %v", torrentHash)
|
||||
log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrentHash, client.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func delugeV2(settings delugeClient.Settings, action domain.Action, torrentFile string) error {
|
||||
func delugeV2(client *domain.DownloadClient, settings delugeClient.Settings, action domain.Action, torrentFile string) error {
|
||||
|
||||
deluge := delugeClient.NewV2(settings)
|
||||
|
||||
// perform connection to Deluge server
|
||||
err := deluge.Connect()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error logging into client: %v", settings.Hostname)
|
||||
log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -136,14 +223,14 @@ func delugeV2(settings delugeClient.Settings, action domain.Action, torrentFile
|
|||
|
||||
t, err := ioutil.ReadFile(torrentFile)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not read torrent file: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not read torrent file: %v", torrentFile)
|
||||
return err
|
||||
}
|
||||
|
||||
// encode file to base64 before sending to deluge
|
||||
encodedFile := base64.StdEncoding.EncodeToString(t)
|
||||
if encodedFile == "" {
|
||||
log.Error().Err(err).Msgf("could not encode torrent file: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not encode torrent file: %v", torrentFile)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -165,17 +252,18 @@ func delugeV2(settings delugeClient.Settings, action domain.Action, torrentFile
|
|||
options.MaxUploadSpeed = &maxUL
|
||||
}
|
||||
|
||||
log.Trace().Msgf("action Deluge options: %+v", options)
|
||||
|
||||
torrentHash, err := deluge.AddTorrentFile(torrentFile, encodedFile, &options)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not add torrent to client: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", torrentFile, client.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
if action.Label != "" {
|
||||
|
||||
p, err := deluge.LabelPlugin()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not load label plugin: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not load label plugin: %v", client.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -183,13 +271,13 @@ func delugeV2(settings delugeClient.Settings, action domain.Action, torrentFile
|
|||
// TODO first check if label exists, if not, add it, otherwise set
|
||||
err = p.SetTorrentLabel(torrentHash, action.Label)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not set label: %v", torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("could not set label: %v on client: %v", action.Label, client.Name)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Trace().Msgf("deluge: torrent successfully added! hash: %v", torrentHash)
|
||||
log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrentHash, client.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue