From 522f22db4640a3230c80978d705bd1e997cabfbb Mon Sep 17 00:00:00 2001 From: ze0s <43699394+zze0s@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:02:04 +0200 Subject: [PATCH] feat(clients): Transmission set label (#1204) * feat(clients): Transmission set label * fix(clients): Transmission fmt * fix(clients): Transmission set label before reannounce --- internal/action/transmission.go | 107 +++++++++++++++++++---------- web/src/screens/filters/Action.tsx | 13 +++- 2 files changed, 80 insertions(+), 40 deletions(-) diff --git a/internal/action/transmission.go b/internal/action/transmission.go index 90f344f..a0839ba 100644 --- a/internal/action/transmission.go +++ b/internal/action/transmission.go @@ -5,6 +5,7 @@ package action import ( "context" + "fmt" "strings" "time" @@ -16,9 +17,11 @@ import ( const ( ReannounceMaxAttempts = 50 - ReannounceInterval = 7000 + ReannounceInterval = 7 // interval in seconds ) +var ErrReannounceTookTooLong = errors.New("ErrReannounceTookTooLong") + func (s *service) transmission(ctx context.Context, action *domain.Action, release domain.Release) ([]string, error) { s.log.Debug().Msgf("action Transmission: %s", action.Name) @@ -36,8 +39,9 @@ func (s *service) transmission(ctx context.Context, action *domain.Action, relea } tbt, err := transmissionrpc.New(client.Host, client.Username, client.Password, &transmissionrpc.AdvancedConfig{ - HTTPS: client.TLS, - Port: uint16(client.Port), + HTTPS: client.TLS, + Port: uint16(client.Port), + UserAgent: "autobrr", }) if err != nil { return nil, errors.Wrap(err, "error logging into client: %s", client.Host) @@ -70,42 +74,71 @@ func (s *service) transmission(ctx context.Context, action *domain.Action, relea return nil, errors.Wrap(err, "could not add torrent from magnet %s to client: %s", release.MagnetURI, client.Host) } + if action.Label != "" { + p := transmissionrpc.TorrentSetPayload{ + IDs: []int64{*torrent.ID}, + Labels: []string{action.Label}, + } + + if err := tbt.TorrentSet(ctx, p); err != nil { + return nil, errors.Wrap(err, "could not set label for hash %s to client: %s", *torrent.HashString, client.Host) + } + + s.log.Debug().Msgf("set label for torrent hash %s successful to client: '%s'", *torrent.HashString, client.Name) + } + s.log.Info().Msgf("torrent from magnet with hash %v successfully added to client: '%s'", torrent.HashString, client.Name) return nil, nil - - } else { - if release.TorrentTmpFile == "" { - if err := release.DownloadTorrentFileCtx(ctx); err != nil { - s.log.Error().Err(err).Msgf("could not download torrent file for release: %s", release.TorrentName) - return nil, err - } - } - - b64, err := transmissionrpc.File2Base64(release.TorrentTmpFile) - if err != nil { - return nil, errors.Wrap(err, "cant encode file %s into base64", release.TorrentTmpFile) - } - - payload.MetaInfo = &b64 - - // Prepare and send payload - torrent, err := tbt.TorrentAdd(ctx, payload) - if err != nil { - return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, client.Host) - } - - if !action.Paused && !action.ReAnnounceSkip { - if err := s.transmissionReannounce(ctx, action, tbt, *torrent.ID); err != nil { - return nil, errors.Wrap(err, "could not reannounce torrent: %s", *torrent.HashString) - } - - return nil, nil - } - - s.log.Info().Msgf("torrent with hash %s successfully added to client: '%s'", *torrent.HashString, client.Name) } + if release.TorrentTmpFile == "" { + if err := release.DownloadTorrentFileCtx(ctx); err != nil { + s.log.Error().Err(err).Msgf("could not download torrent file for release: %s", release.TorrentName) + return nil, err + } + } + + b64, err := transmissionrpc.File2Base64(release.TorrentTmpFile) + if err != nil { + return nil, errors.Wrap(err, "cant encode file %s into base64", release.TorrentTmpFile) + } + + payload.MetaInfo = &b64 + + // Prepare and send payload + torrent, err := tbt.TorrentAdd(ctx, payload) + if err != nil { + return nil, errors.Wrap(err, "could not add torrent %s to client: %s", release.TorrentTmpFile, client.Host) + } + + if action.Label != "" { + p := transmissionrpc.TorrentSetPayload{ + IDs: []int64{*torrent.ID}, + Labels: []string{action.Label}, + } + + if err := tbt.TorrentSet(ctx, p); err != nil { + return nil, errors.Wrap(err, "could not set label for hash %s to client: %s", *torrent.HashString, client.Host) + } + + s.log.Debug().Msgf("set label for torrent hash %s successful to client: '%s'", *torrent.HashString, client.Name) + } + + if !action.Paused && !action.ReAnnounceSkip { + if err := s.transmissionReannounce(ctx, action, tbt, *torrent.ID); err != nil { + if errors.Is(err, ErrReannounceTookTooLong) { + return []string{fmt.Sprintf("reannounce took too long for torrent: %s, deleted", *torrent.HashString)}, nil + } + + return nil, errors.Wrap(err, "could not reannounce torrent: %s", *torrent.HashString) + } + + return nil, nil + } + + s.log.Info().Msgf("torrent with hash %s successfully added to client: '%s'", *torrent.HashString, client.Name) + return rejections, nil } @@ -123,7 +156,7 @@ func (s *service) transmissionReannounce(ctx context.Context, action *domain.Act attempts := 0 for attempts <= maxAttempts { - s.log.Debug().Msgf("re-announce %v attempt: %d/%d", torrentId, attempts, maxAttempts) + s.log.Debug().Msgf("re-announce %d attempt: %d/%d", torrentId, attempts, maxAttempts) // add delay for next run time.Sleep(time.Duration(interval) * time.Second) @@ -157,7 +190,7 @@ func (s *service) transmissionReannounce(ctx context.Context, action *domain.Act } } - s.log.Debug().Msgf("transmission re-announce not working yet, lets re-announce %v again attempt: %d/%d", torrentId, attempts, maxAttempts) + s.log.Debug().Msgf("transmission re-announce not working yet, lets re-announce %d again attempt: %d/%d", torrentId, attempts, maxAttempts) if err := tbt.TorrentReannounceIDs(ctx, []int64{torrentId}); err != nil { return errors.Wrap(err, "failed to reannounce") @@ -173,7 +206,7 @@ func (s *service) transmissionReannounce(ctx context.Context, action *domain.Act return errors.Wrap(err, "could not delete torrent: %v from client after max re-announce attempts reached", torrentId) } - return errors.New("transmission re-announce took too long, deleted torrent %v", torrentId) + return errors.Wrap(ErrReannounceTookTooLong, "transmission re-announce took too long, deleted torrent %v", torrentId) } return nil diff --git a/web/src/screens/filters/Action.tsx b/web/src/screens/filters/Action.tsx index f070dd7..32b91b2 100644 --- a/web/src/screens/filters/Action.tsx +++ b/web/src/screens/filters/Action.tsx @@ -346,8 +346,8 @@ const TypeForm = ({ action, idx, clients }: TypeFormProps) => {