This repository has been archived by the owner on Dec 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathservice.go
157 lines (124 loc) · 5.34 KB
/
service.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package processor
import (
"context"
"fmt"
"net/http"
"time"
"github.com/autobrr/omegabrr/internal/domain"
"github.com/autobrr/omegabrr/pkg/autobrr"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
type Service struct {
cfg *domain.Config
httpClient *http.Client
autobrrClient *autobrr.Client
}
func NewService(cfg *domain.Config) *Service {
s := &Service{
cfg: cfg,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
if cfg != nil {
s.autobrrClient = s.newAutobrrClient()
}
return s
}
func (s Service) newAutobrrClient() *autobrr.Client {
if s.cfg.Clients.Autobrr == nil {
log.Fatal().Msg("must supply omegabrr configuration!")
return nil
}
a := autobrr.NewClient(s.cfg.Clients.Autobrr.Host, s.cfg.Clients.Autobrr.Apikey)
if s.cfg.Clients.Autobrr.BasicAuth != nil {
a.SetBasicAuth(s.cfg.Clients.Autobrr.BasicAuth.User, s.cfg.Clients.Autobrr.BasicAuth.Pass)
}
return a
}
func (s Service) ProcessArrs(ctx context.Context, dryRun bool) []string {
var processingErrors []string
a := s.autobrrClient
if s.cfg.Clients.Arr != nil {
for _, arrClient := range s.cfg.Clients.Arr {
arrClient := arrClient
switch arrClient.Type {
case domain.ArrTypeRadarr:
if err := s.radarr(ctx, arrClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "radarr").Str("client", arrClient.Name).Msg("error while processing Radarr, continuing with other clients")
processingErrors = append(processingErrors, fmt.Sprintf("Radarr - %s: %v", arrClient.Name, err))
}
case domain.ArrTypeWhisparr:
if err := s.sonarr(ctx, arrClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "whisparr").Str("client", arrClient.Name).Msg("error while processing Whisparr, continuing with other clients")
processingErrors = append(processingErrors, fmt.Sprintf("Whisparr - %s: %v", arrClient.Name, err))
}
case domain.ArrTypeSonarr:
if err := s.sonarr(ctx, arrClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "sonarr").Str("client", arrClient.Name).Msg("error while processing Sonarr, continuing with other clients")
processingErrors = append(processingErrors, fmt.Sprintf("Sonarr - %s: %v", arrClient.Name, err))
}
case domain.ArrTypeReadarr:
if err := s.readarr(ctx, arrClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "readarr").Str("client", arrClient.Name).Msg("error while processing Readarr, continuing with other clients")
processingErrors = append(processingErrors, fmt.Sprintf("Readarr - %s: %v", arrClient.Name, err))
}
case domain.ArrTypeLidarr:
if err := s.lidarr(ctx, arrClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "lidarr").Str("client", arrClient.Name).Msg("error while processing Lidarr, continuing with other clients")
processingErrors = append(processingErrors, fmt.Sprintf("Lidarr - %s: %v", arrClient.Name, err))
}
}
}
}
return processingErrors
}
func (s Service) ProcessLists(ctx context.Context, dryRun bool) []string {
var processingErrors []string
a := s.autobrrClient
if s.cfg.Lists != nil {
for _, listsClient := range s.cfg.Lists {
listsClient := listsClient
switch listsClient.Type {
case domain.ListTypeTrakt:
if err := s.trakt(ctx, listsClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "trakt").Str("client", listsClient.Name).Msg("error while processing Trakt list, continuing with other lists")
processingErrors = append(processingErrors, fmt.Sprintf("Trakt - %s: %v", listsClient.Name, err))
}
case domain.ListTypeMdblist:
if err := s.mdblist(ctx, listsClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "mdblist").Str("client", listsClient.Name).Msg("error while processing Mdblist, continuing with other lists")
processingErrors = append(processingErrors, fmt.Sprintf("Mdblist - %s: %v", listsClient.Name, err))
}
case domain.ListTypeMetacritic:
if err := s.metacritic(ctx, listsClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "metacritic").Str("client", listsClient.Name).Msg("error while processing Metacritic, continuing with other lists")
processingErrors = append(processingErrors, fmt.Sprintf("Metacritic - %s: %v", listsClient.Name, err))
}
case domain.ListTypePlaintext:
if err := s.plaintext(ctx, listsClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "plaintext").Str("client", listsClient.Name).Msg("error while processing Plaintext list, continuing with other lists")
processingErrors = append(processingErrors, fmt.Sprintf("Plaintext - %s: %v", listsClient.Name, err))
}
case domain.ListTypeSteam:
if err := s.steam(ctx, listsClient, dryRun, a); err != nil {
log.Error().Err(err).Str("type", "steam").Str("client", listsClient.Name).Msg("error while processing Steam wishlist, continuing with other lists")
processingErrors = append(processingErrors, fmt.Sprintf("Steam - %s: %v", listsClient.Name, err))
}
}
}
}
return processingErrors
}
func (s Service) GetFilters(ctx context.Context) ([]autobrr.Filter, error) {
if s.autobrrClient == nil {
log.Fatal().Msg("must supply omegabrr configuration!")
return nil, errors.New("must supply omegabrr configuration")
}
filters, err := s.autobrrClient.GetFilters(ctx)
if err != nil {
return nil, err
}
return filters, nil
}