feat(download-clients): add transmission (#350)

This commit is contained in:
Ludvig Lundgren 2022-07-10 18:01:58 +02:00 committed by GitHub
parent b03edbfc87
commit 7eefeb54c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 200 additions and 21 deletions

View file

@ -48,6 +48,9 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
case domain.ActionTypeQbittorrent:
rejections, err = s.qbittorrent(*action, release)
case domain.ActionTypeTransmission:
rejections, err = s.transmission(*action, release)
case domain.ActionTypeRadarr:
rejections, err = s.radarr(*action, release)

View file

@ -0,0 +1,70 @@
package action
import (
"context"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/hekmon/transmissionrpc/v2"
)
func (s *service) transmission(action domain.Action, release domain.Release) ([]string, error) {
s.log.Debug().Msgf("action Transmission: %v", action.Name)
var err error
// get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("error finding client: %v", action.ClientID)
return nil, err
}
if client == nil {
return nil, errors.New("could not find client by id: %v", action.ClientID)
}
var rejections []string
if release.TorrentTmpFile == "" {
err = release.DownloadTorrentFile()
if err != nil {
s.log.Error().Err(err).Msgf("could not download torrent file for release: %v", release.TorrentName)
return nil, err
}
}
tbt, err := transmissionrpc.New(client.Host, client.Username, client.Password, &transmissionrpc.AdvancedConfig{
HTTPS: client.TLS,
Port: uint16(client.Port),
})
if err != nil {
return nil, errors.Wrap(err, "error logging into client: %v", client.Host)
}
b64, err := transmissionrpc.File2Base64(release.TorrentTmpFile)
if err != nil {
return nil, errors.Wrap(err, "cant encode file %v into base64", release.TorrentTmpFile)
}
payload := transmissionrpc.TorrentAddPayload{
MetaInfo: &b64,
}
if action.SavePath != "" {
payload.DownloadDir = &action.SavePath
}
if action.Paused {
payload.Paused = &action.Paused
}
// Prepare and send payload
torrent, err := tbt.TorrentAdd(context.TODO(), payload)
if err != nil {
return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, client.Host)
}
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrent.HashString, client.Name)
return rejections, nil
}

View file

@ -47,15 +47,16 @@ type Action struct {
type ActionType string
const (
ActionTypeTest ActionType = "TEST"
ActionTypeExec ActionType = "EXEC"
ActionTypeQbittorrent ActionType = "QBITTORRENT"
ActionTypeDelugeV1 ActionType = "DELUGE_V1"
ActionTypeDelugeV2 ActionType = "DELUGE_V2"
ActionTypeWatchFolder ActionType = "WATCH_FOLDER"
ActionTypeWebhook ActionType = "WEBHOOK"
ActionTypeRadarr ActionType = "RADARR"
ActionTypeSonarr ActionType = "SONARR"
ActionTypeLidarr ActionType = "LIDARR"
ActionTypeWhisparr ActionType = "WHISPARR"
ActionTypeTest ActionType = "TEST"
ActionTypeExec ActionType = "EXEC"
ActionTypeQbittorrent ActionType = "QBITTORRENT"
ActionTypeDelugeV1 ActionType = "DELUGE_V1"
ActionTypeDelugeV2 ActionType = "DELUGE_V2"
ActionTypeTransmission ActionType = "TRANSMISSION"
ActionTypeWatchFolder ActionType = "WATCH_FOLDER"
ActionTypeWebhook ActionType = "WEBHOOK"
ActionTypeRadarr ActionType = "RADARR"
ActionTypeSonarr ActionType = "SONARR"
ActionTypeLidarr ActionType = "LIDARR"
ActionTypeWhisparr ActionType = "WHISPARR"
)

View file

@ -46,11 +46,12 @@ type BasicAuth struct {
type DownloadClientType string
const (
DownloadClientTypeQbittorrent DownloadClientType = "QBITTORRENT"
DownloadClientTypeDelugeV1 DownloadClientType = "DELUGE_V1"
DownloadClientTypeDelugeV2 DownloadClientType = "DELUGE_V2"
DownloadClientTypeRadarr DownloadClientType = "RADARR"
DownloadClientTypeSonarr DownloadClientType = "SONARR"
DownloadClientTypeLidarr DownloadClientType = "LIDARR"
DownloadClientTypeWhisparr DownloadClientType = "WHISPARR"
DownloadClientTypeQbittorrent DownloadClientType = "QBITTORRENT"
DownloadClientTypeDelugeV1 DownloadClientType = "DELUGE_V1"
DownloadClientTypeDelugeV2 DownloadClientType = "DELUGE_V2"
DownloadClientTypeTransmission DownloadClientType = "TRANSMISSION"
DownloadClientTypeRadarr DownloadClientType = "RADARR"
DownloadClientTypeSonarr DownloadClientType = "SONARR"
DownloadClientTypeLidarr DownloadClientType = "LIDARR"
DownloadClientTypeWhisparr DownloadClientType = "WHISPARR"
)

View file

@ -1,6 +1,7 @@
package download_client
import (
"context"
"time"
"github.com/autobrr/autobrr/internal/domain"
@ -12,6 +13,7 @@ import (
"github.com/autobrr/autobrr/pkg/whisparr"
delugeClient "github.com/gdm85/go-libdeluge"
"github.com/hekmon/transmissionrpc/v2"
)
func (s *service) testConnection(client domain.DownloadClient) error {
@ -22,6 +24,9 @@ func (s *service) testConnection(client domain.DownloadClient) error {
case domain.DownloadClientTypeDelugeV1, domain.DownloadClientTypeDelugeV2:
return s.testDelugeConnection(client)
case domain.DownloadClientTypeTransmission:
return s.testTransmissionConnection(client)
case domain.DownloadClientTypeRadarr:
return s.testRadarrConnection(client)
@ -114,6 +119,31 @@ func (s *service) testDelugeConnection(client domain.DownloadClient) error {
return nil
}
func (s *service) testTransmissionConnection(client domain.DownloadClient) error {
tbt, err := transmissionrpc.New(client.Host, client.Username, client.Password, &transmissionrpc.AdvancedConfig{
HTTPS: client.TLS,
Port: uint16(client.Port),
})
if err != nil {
return errors.Wrap(err, "error logging into client: %v", client.Host)
}
ok, version, _, err := tbt.RPCVersion(context.TODO())
if err != nil {
return errors.Wrap(err, "error getting rpc info: %v", client.Host)
}
if !ok {
return errors.Wrap(err, "error getting rpc info: %v", client.Host)
}
s.log.Debug().Msgf("test client connection for Transmission: got version: %v", version)
s.log.Debug().Msgf("test client connection for Transmission: success")
return nil
}
func (s *service) testRadarrConnection(client domain.DownloadClient) error {
r := radarr.New(radarr.Config{
Hostname: client.Host,