mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49: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
|
||||
}
|
||||
|
|
|
@ -11,12 +11,12 @@ import (
|
|||
)
|
||||
|
||||
func (s *service) execCmd(announce domain.Announce, action domain.Action, torrentFile string) {
|
||||
log.Trace().Msgf("action EXEC: release: %v", announce.TorrentName)
|
||||
log.Debug().Msgf("action exec: %v release: %v", action.Name, announce.TorrentName)
|
||||
|
||||
// check if program exists
|
||||
cmd, err := exec.LookPath(action.ExecCmd)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("exec failed, could not find program: %v", action.ExecCmd)
|
||||
log.Error().Stack().Err(err).Msgf("exec failed, could not find program: %v", action.ExecCmd)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ func (s *service) execCmd(announce domain.Announce, action domain.Action, torren
|
|||
// parse and replace values in argument string before continuing
|
||||
parsedArgs, err := m.Parse(action.ExecArgs)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("exec failed, could not parse arguments: %v", action.ExecCmd)
|
||||
log.Error().Stack().Err(err).Msgf("exec failed, could not parse arguments: %v", action.ExecCmd)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ func (s *service) execCmd(announce domain.Announce, action domain.Action, torren
|
|||
output, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
// everything other than exit 0 is considered an error
|
||||
log.Error().Err(err).Msgf("command: %v args: %v failed, torrent: %v", cmd, parsedArgs, torrentFile)
|
||||
log.Error().Stack().Err(err).Msgf("command: %v args: %v failed, torrent: %v", cmd, parsedArgs, torrentFile)
|
||||
}
|
||||
|
||||
log.Trace().Msgf("executed command: '%v'", string(output))
|
||||
|
|
|
@ -9,13 +9,16 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const REANNOUNCE_MAX_ATTEMPTS = 30
|
||||
const REANNOUNCE_INTERVAL = 7000
|
||||
|
||||
func (s *service) qbittorrent(action domain.Action, hash string, torrentFile string) error {
|
||||
log.Trace().Msgf("action QBITTORRENT: %v", torrentFile)
|
||||
log.Debug().Msgf("action qBittorrent: %v", action.Name)
|
||||
|
||||
// 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 ID %v", action.Name, action.ClientID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -35,12 +38,10 @@ func (s *service) qbittorrent(action domain.Action, hash string, torrentFile str
|
|||
// save cookies?
|
||||
err = qbt.Login()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error logging into client: %v", action.ClientID)
|
||||
log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO check for active downloads and other rules
|
||||
|
||||
options := map[string]string{}
|
||||
|
||||
if action.Paused {
|
||||
|
@ -63,67 +64,144 @@ func (s *service) qbittorrent(action domain.Action, hash string, torrentFile str
|
|||
options["dlLimit"] = strconv.FormatInt(action.LimitDownloadSpeed, 10)
|
||||
}
|
||||
|
||||
log.Trace().Msgf("action qBittorrent options: %+v", options)
|
||||
|
||||
err = qbt.AddTorrentFromFile(torrentFile, options)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error sending to client: %v", action.ClientID)
|
||||
log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", torrentFile, client.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
if !action.Paused && hash != "" {
|
||||
err = checkTrackerStatus(*qbt, hash)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not get tracker status for torrent: %v", hash)
|
||||
log.Error().Stack().Err(err).Msgf("could not get tracker status for torrent: %v", hash)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Msgf("torrent %v successfully added to: %v", hash, client.Name)
|
||||
log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", hash, client.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool, error) {
|
||||
log.Trace().Msgf("action qBittorrent: %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", action.ClientID)
|
||||
return false, err
|
||||
}
|
||||
|
||||
if client == nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
qbtSettings := qbittorrent.Settings{
|
||||
Hostname: client.Host,
|
||||
Port: uint(client.Port),
|
||||
Username: client.Username,
|
||||
Password: client.Password,
|
||||
SSL: client.SSL,
|
||||
}
|
||||
|
||||
qbt := qbittorrent.NewClient(qbtSettings)
|
||||
// save cookies?
|
||||
err = qbt.Login()
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error logging into client: %v", client.Host)
|
||||
return false, err
|
||||
}
|
||||
|
||||
// check for active downloads and other rules
|
||||
if client.Settings.Rules.Enabled && !action.IgnoreRules {
|
||||
activeDownloads, err := qbt.GetTorrentsFilter(qbittorrent.TorrentFilterDownloading)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("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 {
|
||||
if client.Settings.Rules.IgnoreSlowTorrents {
|
||||
// check speeds of downloads
|
||||
info, err := qbt.GetTransferInfo()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("could not get transfer info")
|
||||
return false, err
|
||||
}
|
||||
|
||||
// if current transfer speed is more than threshold return out and skip
|
||||
// DlInfoSpeed is in bytes so lets convert to KB to match DownloadSpeedThreshold
|
||||
if info.DlInfoSpeed/1024 >= client.Settings.Rules.DownloadSpeedThreshold {
|
||||
log.Debug().Msg("max active downloads reached, skipping")
|
||||
return false, nil
|
||||
}
|
||||
|
||||
log.Debug().Msg("active downloads are slower than set limit, lets add it")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func checkTrackerStatus(qb qbittorrent.Client, hash string) error {
|
||||
announceOK := false
|
||||
attempts := 0
|
||||
|
||||
for attempts < REANNOUNCE_MAX_ATTEMPTS {
|
||||
log.Debug().Msgf("RE-ANNOUNCE %v attempt: %v", hash, attempts)
|
||||
// initial sleep to give tracker a head start
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// initial sleep to give tracker a head start
|
||||
time.Sleep(REANNOUNCE_INTERVAL * time.Millisecond)
|
||||
for attempts < REANNOUNCE_MAX_ATTEMPTS {
|
||||
log.Debug().Msgf("qBittorrent - run re-announce %v attempt: %v", hash, attempts)
|
||||
|
||||
trackers, err := qb.GetTorrentTrackers(hash)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not get trackers of torrent: %v", hash)
|
||||
log.Error().Err(err).Msgf("qBittorrent - could not get trackers for torrent: %v", hash)
|
||||
return err
|
||||
}
|
||||
|
||||
// check if status not working or something else
|
||||
_, working := findTrackerStatus(trackers, qbittorrent.TrackerStatusOK)
|
||||
working := findTrackerStatus(trackers, qbittorrent.TrackerStatusOK)
|
||||
|
||||
if !working {
|
||||
log.Trace().Msgf("qBittorrent - not working yet, lets re-announce %v attempt: %v", hash, attempts)
|
||||
err = qb.ReAnnounceTorrents([]string{hash})
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not get re-announce torrent: %v", hash)
|
||||
log.Error().Err(err).Msgf("qBittorrent - could not get re-announce torrent: %v", hash)
|
||||
return err
|
||||
}
|
||||
|
||||
attempts++
|
||||
|
||||
// add delay for next run
|
||||
time.Sleep(REANNOUNCE_INTERVAL * time.Millisecond)
|
||||
|
||||
continue
|
||||
} else {
|
||||
log.Debug().Msgf("RE-ANNOUNCE %v OK", hash)
|
||||
log.Debug().Msgf("qBittorrent - re-announce for %v OK", hash)
|
||||
|
||||
announceOK = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// add extra delay before delete
|
||||
time.Sleep(30 * time.Second)
|
||||
|
||||
if !announceOK {
|
||||
log.Debug().Msgf("RE-ANNOUNCE %v took too long, deleting torrent", hash)
|
||||
log.Debug().Msgf("qBittorrent - re-announce for %v took too long, deleting torrent", hash)
|
||||
|
||||
err := qb.DeleteTorrents([]string{hash}, false)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not delete torrent: %v", hash)
|
||||
log.Error().Stack().Err(err).Msgf("qBittorrent - could not delete torrent: %v", hash)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -138,11 +216,14 @@ func checkTrackerStatus(qb qbittorrent.Client, hash string) error {
|
|||
// 2 Tracker has been contacted and is working
|
||||
// 3 Tracker is updating
|
||||
// 4 Tracker has been contacted, but it is not working (or doesn't send proper replies)
|
||||
func findTrackerStatus(slice []qbittorrent.TorrentTracker, status qbittorrent.TrackerStatus) (int, bool) {
|
||||
for i, item := range slice {
|
||||
if item.Status == status {
|
||||
return i, true
|
||||
func findTrackerStatus(slice []qbittorrent.TorrentTracker, status qbittorrent.TrackerStatus) bool {
|
||||
for _, item := range slice {
|
||||
// if updating skip and give some more time
|
||||
if item.Status == qbittorrent.TrackerStatusUpdating {
|
||||
return false
|
||||
} else if item.Status == status {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return -1, false
|
||||
return false
|
||||
}
|
||||
|
|
201
internal/action/run.go
Normal file
201
internal/action/run.go
Normal file
|
@ -0,0 +1,201 @@
|
|||
package action
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/client"
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
func (s *service) RunActions(actions []domain.Action, announce domain.Announce) error {
|
||||
|
||||
var err error
|
||||
var tmpFile string
|
||||
var hash string
|
||||
|
||||
for _, action := range actions {
|
||||
if !action.Enabled {
|
||||
// only run active actions
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug().Msgf("process action: %v", action.Name)
|
||||
|
||||
switch action.Type {
|
||||
case domain.ActionTypeTest:
|
||||
s.test(action.Name)
|
||||
|
||||
case domain.ActionTypeExec:
|
||||
if tmpFile == "" {
|
||||
tmpFile, hash, err = downloadFile(announce.TorrentUrl)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
go func(announce domain.Announce, action domain.Action, tmpFile string) {
|
||||
s.execCmd(announce, action, tmpFile)
|
||||
}(announce, action, tmpFile)
|
||||
|
||||
case domain.ActionTypeWatchFolder:
|
||||
if tmpFile == "" {
|
||||
tmpFile, hash, err = downloadFile(announce.TorrentUrl)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
s.watchFolder(action.WatchFolder, tmpFile)
|
||||
|
||||
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
|
||||
canDownload, err := s.delugeCheckRulesCanDownload(action)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
|
||||
continue
|
||||
}
|
||||
if canDownload {
|
||||
if tmpFile == "" {
|
||||
tmpFile, hash, err = downloadFile(announce.TorrentUrl)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
go func(action domain.Action, tmpFile string) {
|
||||
err = s.deluge(action, tmpFile)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error sending torrent to Deluge")
|
||||
}
|
||||
}(action, tmpFile)
|
||||
}
|
||||
|
||||
case domain.ActionTypeQbittorrent:
|
||||
canDownload, err := s.qbittorrentCheckRulesCanDownload(action)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
|
||||
continue
|
||||
}
|
||||
if canDownload {
|
||||
if tmpFile == "" {
|
||||
tmpFile, hash, err = downloadFile(announce.TorrentUrl)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
go func(action domain.Action, hash string, tmpFile string) {
|
||||
err = s.qbittorrent(action, hash, tmpFile)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error sending torrent to qBittorrent")
|
||||
}
|
||||
}(action, hash, tmpFile)
|
||||
}
|
||||
|
||||
case domain.ActionTypeRadarr:
|
||||
go func(announce domain.Announce, action domain.Action) {
|
||||
err = s.radarr(announce, action)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error sending torrent to radarr")
|
||||
//continue
|
||||
}
|
||||
}(announce, action)
|
||||
|
||||
case domain.ActionTypeSonarr:
|
||||
go func(announce domain.Announce, action domain.Action) {
|
||||
err = s.sonarr(announce, action)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error sending torrent to sonarr")
|
||||
//continue
|
||||
}
|
||||
}(announce, action)
|
||||
|
||||
case domain.ActionTypeLidarr:
|
||||
go func(announce domain.Announce, action domain.Action) {
|
||||
err = s.lidarr(announce, action)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error sending torrent to lidarr")
|
||||
//continue
|
||||
}
|
||||
}(announce, action)
|
||||
|
||||
default:
|
||||
log.Warn().Msgf("unsupported action: %v type: %v", action.Name, action.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// safe to delete tmp file
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// downloadFile returns tmpFile, hash, error
|
||||
func downloadFile(url string) (string, string, error) {
|
||||
// create http client
|
||||
c := client.NewHttpClient()
|
||||
|
||||
// download torrent file
|
||||
// TODO check extra headers, cookie
|
||||
res, err := c.DownloadFile(url, nil)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not download file: %v", url)
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// match more filters like torrent size
|
||||
|
||||
// Get meta info from file to find out the hash for later use
|
||||
meta, err := metainfo.LoadFromFile(res.FileName)
|
||||
//meta, err := metainfo.Load(res.Body)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("metainfo could not open file: %v", res.FileName)
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// torrent info hash used for re-announce
|
||||
hash := meta.HashInfoBytes().String()
|
||||
|
||||
return res.FileName, hash, nil
|
||||
}
|
||||
|
||||
func (s *service) test(name string) {
|
||||
log.Info().Msgf("action TEST: %v", name)
|
||||
}
|
||||
|
||||
func (s *service) watchFolder(dir string, torrentFile string) {
|
||||
log.Trace().Msgf("action WATCH_FOLDER: %v file: %v", dir, torrentFile)
|
||||
|
||||
// Open original file
|
||||
original, err := os.Open(torrentFile)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not open temp file '%v'", torrentFile)
|
||||
return
|
||||
}
|
||||
defer original.Close()
|
||||
|
||||
_, tmpFileName := path.Split(torrentFile)
|
||||
fullFileName := path.Join(dir, tmpFileName)
|
||||
|
||||
// Create new file
|
||||
newFile, err := os.Create(fullFileName)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not create new temp file '%v'", fullFileName)
|
||||
return
|
||||
}
|
||||
defer newFile.Close()
|
||||
|
||||
// Copy file
|
||||
_, err = io.Copy(newFile, original)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not copy file %v to watch folder", fullFileName)
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().Msgf("saved file to watch folder: %v", fullFileName)
|
||||
}
|
|
@ -1,24 +1,17 @@
|
|||
package action
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/download_client"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const REANNOUNCE_MAX_ATTEMPTS = 30
|
||||
const REANNOUNCE_INTERVAL = 7000
|
||||
|
||||
type Service interface {
|
||||
RunActions(torrentFile string, hash string, filter domain.Filter, announce domain.Announce) error
|
||||
Store(action domain.Action) (*domain.Action, error)
|
||||
Fetch() ([]domain.Action, error)
|
||||
Delete(actionID int) error
|
||||
ToggleEnabled(actionID int) error
|
||||
|
||||
RunActions(actions []domain.Action, announce domain.Announce) error
|
||||
}
|
||||
|
||||
type service struct {
|
||||
|
@ -30,72 +23,6 @@ func NewService(repo domain.ActionRepo, clientSvc download_client.Service) Servi
|
|||
return &service{repo: repo, clientSvc: clientSvc}
|
||||
}
|
||||
|
||||
func (s *service) RunActions(torrentFile string, hash string, filter domain.Filter, announce domain.Announce) error {
|
||||
for _, action := range filter.Actions {
|
||||
if !action.Enabled {
|
||||
// only run active actions
|
||||
continue
|
||||
}
|
||||
|
||||
log.Trace().Msgf("process action: %v", action.Name)
|
||||
|
||||
switch action.Type {
|
||||
case domain.ActionTypeTest:
|
||||
go s.test(torrentFile)
|
||||
|
||||
case domain.ActionTypeWatchFolder:
|
||||
go s.watchFolder(action.WatchFolder, torrentFile)
|
||||
|
||||
case domain.ActionTypeQbittorrent:
|
||||
go func() {
|
||||
err := s.qbittorrent(action, hash, torrentFile)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error sending torrent to client")
|
||||
}
|
||||
}()
|
||||
|
||||
case domain.ActionTypeExec:
|
||||
go s.execCmd(announce, action, torrentFile)
|
||||
|
||||
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
|
||||
go func() {
|
||||
err := s.deluge(action, torrentFile)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error sending torrent to client")
|
||||
}
|
||||
}()
|
||||
|
||||
case domain.ActionTypeRadarr:
|
||||
go func() {
|
||||
err := s.radarr(announce, action)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error sending torrent to radarr")
|
||||
}
|
||||
}()
|
||||
|
||||
case domain.ActionTypeSonarr:
|
||||
go func() {
|
||||
err := s.sonarr(announce, action)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error sending torrent to sonarr")
|
||||
}
|
||||
}()
|
||||
case domain.ActionTypeLidarr:
|
||||
go func() {
|
||||
err := s.lidarr(announce, action)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error sending torrent to lidarr")
|
||||
}
|
||||
}()
|
||||
|
||||
default:
|
||||
log.Warn().Msgf("unsupported action: %v type: %v", action.Name, action.Type)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Store(action domain.Action) (*domain.Action, error) {
|
||||
// validate data
|
||||
|
||||
|
@ -131,36 +58,3 @@ func (s *service) ToggleEnabled(actionID int) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) test(torrentFile string) {
|
||||
log.Info().Msgf("action TEST: %v", torrentFile)
|
||||
}
|
||||
|
||||
func (s *service) watchFolder(dir string, torrentFile string) {
|
||||
log.Trace().Msgf("action WATCH_FOLDER: %v file: %v", dir, torrentFile)
|
||||
|
||||
// Open original file
|
||||
original, err := os.Open(torrentFile)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
defer original.Close()
|
||||
|
||||
_, tmpFileName := path.Split(torrentFile)
|
||||
fullFileName := path.Join(dir, tmpFileName)
|
||||
|
||||
// Create new file
|
||||
newFile, err := os.Create(fullFileName)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
defer newFile.Close()
|
||||
|
||||
// Copy file
|
||||
_, err = io.Copy(newFile, original)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
|
||||
log.Info().Msgf("saved file to watch folder: %v", fullFileName)
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ func (s *service) Parse(announceID string, msg string) error {
|
|||
|
||||
log.Trace().Msgf("announce: %+v", announce)
|
||||
|
||||
log.Info().Msgf("Matched %v (%v) for %v", announce.TorrentName, announce.Filter.Name, announce.Site)
|
||||
log.Info().Msgf("Matched '%v' (%v) for %v", announce.TorrentName, announce.Filter.Name, announce.Site)
|
||||
|
||||
// match release
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package client
|
|||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -40,8 +41,6 @@ func (c *HttpClient) DownloadFile(url string, opts map[string]string) (*Download
|
|||
hashString := hex.EncodeToString(hash[:])
|
||||
tmpFileName := fmt.Sprintf("/tmp/%v", hashString)
|
||||
|
||||
log.Debug().Msgf("tmpFileName: %v", tmpFileName)
|
||||
|
||||
// Create the file
|
||||
out, err := os.Create(tmpFileName)
|
||||
if err != nil {
|
||||
|
@ -61,8 +60,6 @@ func (c *HttpClient) DownloadFile(url string, opts map[string]string) (*Download
|
|||
|
||||
// retry logic
|
||||
|
||||
log.Trace().Msgf("downloaded file response: %v - status: %v", resp.Status, resp.StatusCode)
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
log.Error().Stack().Err(err).Msgf("error downloading file: %v - bad status: %d", tmpFileName, resp.StatusCode)
|
||||
return nil, err
|
||||
|
@ -82,7 +79,12 @@ func (c *HttpClient) DownloadFile(url string, opts map[string]string) (*Download
|
|||
FileName: tmpFileName,
|
||||
}
|
||||
|
||||
log.Trace().Msgf("successfully downloaded file: %v", tmpFileName)
|
||||
if res.FileName == "" || res.Body == nil {
|
||||
log.Error().Stack().Err(err).Msgf("tmp file error - empty body: %v", url)
|
||||
return nil, errors.New("error downloading file, no tmp file")
|
||||
}
|
||||
|
||||
log.Debug().Msgf("successfully downloaded file: %v", tmpFileName)
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
|
|
@ -91,6 +91,7 @@ func (r *DownloadClientRepo) Store(client domain.DownloadClient) (*domain.Downlo
|
|||
settings := domain.DownloadClientSettings{
|
||||
APIKey: client.Settings.APIKey,
|
||||
Basic: client.Settings.Basic,
|
||||
Rules: client.Settings.Rules,
|
||||
}
|
||||
|
||||
settingsJson, err := json.Marshal(&settings)
|
||||
|
@ -162,6 +163,8 @@ func (r *DownloadClientRepo) Store(client domain.DownloadClient) (*domain.Downlo
|
|||
|
||||
resId, _ := res.LastInsertId()
|
||||
client.ID = int(resId)
|
||||
|
||||
log.Trace().Msgf("download_client: store new record %d", client.ID)
|
||||
}
|
||||
|
||||
log.Info().Msgf("store download client: %v", client.Name)
|
||||
|
|
|
@ -22,8 +22,16 @@ type DownloadClient struct {
|
|||
}
|
||||
|
||||
type DownloadClientSettings struct {
|
||||
APIKey string `json:"apikey,omitempty"`
|
||||
Basic BasicAuth `json:"basic,omitempty"`
|
||||
APIKey string `json:"apikey,omitempty"`
|
||||
Basic BasicAuth `json:"basic,omitempty"`
|
||||
Rules DownloadClientRules `json:"rules,omitempty"`
|
||||
}
|
||||
|
||||
type DownloadClientRules struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
MaxActiveDownloads int `json:"max_active_downloads"`
|
||||
IgnoreSlowTorrents bool `json:"ignore_slow_torrents"`
|
||||
DownloadSpeedThreshold int64 `json:"download_speed_threshold"`
|
||||
}
|
||||
|
||||
type BasicAuth struct {
|
||||
|
|
|
@ -46,8 +46,6 @@ func (s *service) Store(client domain.DownloadClient) (*domain.DownloadClient, e
|
|||
// validate data
|
||||
if client.Host == "" {
|
||||
return nil, errors.New("validation error: no host")
|
||||
} else if client.Port == 0 {
|
||||
return nil, errors.New("validation error: no port")
|
||||
} else if client.Type == "" {
|
||||
return nil, errors.New("validation error: no type")
|
||||
}
|
||||
|
@ -75,8 +73,6 @@ func (s *service) Test(client domain.DownloadClient) error {
|
|||
// basic validation of client
|
||||
if client.Host == "" {
|
||||
return errors.New("validation error: no host")
|
||||
} else if client.Port == 0 {
|
||||
return errors.New("validation error: no port")
|
||||
} else if client.Type == "" {
|
||||
return errors.New("validation error: no type")
|
||||
}
|
||||
|
|
|
@ -98,8 +98,8 @@ func (s *service) FindByIndexerIdentifier(announce domain.Announce) (*domain.Fil
|
|||
// if match, return the filter
|
||||
matchedFilter := s.checkFilter(filter, announce)
|
||||
if matchedFilter {
|
||||
log.Trace().Msgf("found filter: %+v", &filter)
|
||||
log.Debug().Msgf("found filter: %+v", &filter.Name)
|
||||
log.Trace().Msgf("found matching filter: %+v", &filter)
|
||||
log.Debug().Msgf("found matching filter: %v", &filter.Name)
|
||||
|
||||
// find actions and attach
|
||||
actions, err := s.actionRepo.FindByFilterID(filter.ID)
|
||||
|
|
|
@ -1,14 +1,11 @@
|
|||
package release
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/action"
|
||||
"github.com/autobrr/autobrr/internal/client"
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
|
@ -31,54 +28,14 @@ func (s *service) Process(announce domain.Announce) error {
|
|||
return fmt.Errorf("no actions for filter: %v", announce.Filter.Name)
|
||||
}
|
||||
|
||||
// check can download
|
||||
// smart episode?
|
||||
// check against rules like active downloading torrents
|
||||
|
||||
// create http client
|
||||
c := client.NewHttpClient()
|
||||
|
||||
// download torrent file
|
||||
// TODO check extra headers, cookie
|
||||
res, err := c.DownloadFile(announce.TorrentUrl, nil)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not download file: %v", announce.TorrentName)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.FileName == "" {
|
||||
return errors.New("error downloading file, no tmp file")
|
||||
}
|
||||
|
||||
if res.Body == nil {
|
||||
log.Error().Stack().Err(err).Msgf("tmp file error - empty body: %v", announce.TorrentName)
|
||||
return errors.New("empty body")
|
||||
}
|
||||
|
||||
//log.Debug().Msgf("downloaded torrent file: %v", res.FileName)
|
||||
|
||||
// onTorrentDownloaded
|
||||
|
||||
// match more filters like torrent size
|
||||
|
||||
// Get meta info from file to find out the hash for later use
|
||||
meta, err := metainfo.LoadFromFile(res.FileName)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("metainfo could not open file: %v", res.FileName)
|
||||
return err
|
||||
}
|
||||
|
||||
// torrent info hash used for re-announce
|
||||
hash := meta.HashInfoBytes().String()
|
||||
|
||||
// take action (watchFolder, test, runProgram, qBittorrent, Deluge etc)
|
||||
err = s.actionSvc.RunActions(res.FileName, hash, *announce.Filter, announce)
|
||||
// run actions (watchFolder, test, exec, qBittorrent, Deluge etc.)
|
||||
err := s.actionSvc.RunActions(announce.Filter.Actions, announce)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error running actions for filter: %v", announce.Filter.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
// safe to delete tmp file
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue