/
whisparr.go
85 lines (67 loc) · 2.39 KB
/
whisparr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package action
import (
"context"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/whisparr"
)
func (s *service) whisparr(ctx context.Context, action *domain.Action, release domain.Release) ([]string, error) {
s.log.Trace().Msg("action WHISPARR")
// TODO validate data
// get client for action
client, err := s.clientSvc.FindByID(ctx, action.ClientID)
if err != nil {
return nil, errors.Wrap(err, "sonarr could not find client: %v", action.ClientID)
}
// return early if no client found
if client == nil {
return nil, errors.New("could not find client by id: %v", action.ClientID)
}
// initial config
cfg := whisparr.Config{
Hostname: client.Host,
APIKey: client.Settings.APIKey,
Log: s.subLogger,
}
// only set basic auth if enabled
if client.Settings.Basic.Auth {
cfg.BasicAuth = client.Settings.Basic.Auth
cfg.Username = client.Settings.Basic.Username
cfg.Password = client.Settings.Basic.Password
}
externalClientId := client.Settings.ExternalDownloadClientId
if action.ExternalDownloadClientID > 0 {
externalClientId = int(action.ExternalDownloadClientID)
}
externalClient := client.Settings.ExternalDownloadClient
if action.ExternalDownloadClient != "" {
externalClient = action.ExternalDownloadClient
}
r := whisparr.Release{
Title: release.TorrentName,
InfoUrl: release.InfoURL,
DownloadUrl: release.DownloadURL,
MagnetUrl: release.MagnetURI,
Size: int64(release.Size),
Indexer: release.Indexer.Identifier,
DownloadClientId: externalClientId,
DownloadClient: externalClient,
DownloadProtocol: string(release.Protocol),
Protocol: string(release.Protocol),
PublishDate: time.Now().Format(time.RFC3339),
}
arr := whisparr.New(cfg)
rejections, err := arr.Push(ctx, r)
if err != nil {
return nil, errors.Wrap(err, "whisparr: failed to push release: %v", r)
}
if rejections != nil {
s.log.Debug().Msgf("whisparr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
return rejections, nil
}
s.log.Debug().Msgf("whisparr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
return nil, nil
}