Files
nabarr/sonarr/sonarr.go

141 lines
3.1 KiB
Go
Raw Permalink Normal View History

package sonarr
import (
"fmt"
2022-04-01 20:42:05 +01:00
"net/http"
"strings"
"time"
2022-04-14 16:18:06 +01:00
"github.com/rs/zerolog"
"github.com/l3uddz/nabarr"
"github.com/l3uddz/nabarr/cache"
"github.com/l3uddz/nabarr/logger"
"github.com/l3uddz/nabarr/media"
"github.com/l3uddz/nabarr/util"
)
type Client struct {
pvrType string
name string
testMode bool
testModeAdd bool
rootFolder string
qualityProfileId int
languageProfileId int
// options
searchMissing bool
addMonitored bool
skipAnime bool
tag string
apiURL string
apiHeaders map[string]string
cache *cache.Client
cacheTempDuration time.Duration
cacheFiltersHash string
queue chan *media.FeedItem
m *media.Client
http *http.Client
log zerolog.Logger
ignoresExpr []*nabarr.ExprProgram
}
func New(c nabarr.PvrConfig, mode string, m *media.Client, cc *cache.Client) (*Client, error) {
2022-04-14 16:18:06 +01:00
l := logger.Child(logger.WithLevel(c.Verbosity)).With().
Str("pvr_name", c.Name).
Str("pvr_type", c.Type).
Logger()
// set config defaults (if not set)
if c.CacheDuration == 0 {
c.CacheDuration = 24 * time.Hour
}
// set api url
apiURL := ""
if strings.Contains(strings.ToLower(c.URL), "/api") {
apiURL = c.URL
} else {
apiURL = util.JoinURL(c.URL, "api", "v3")
}
// set api headers
apiHeaders := map[string]string{
"X-Api-Key": c.ApiKey,
}
// create client
cl := &Client{
pvrType: "sonarr",
name: strings.ToLower(c.Name),
testMode: util.StringSliceContains([]string{"test", "test-add"}, mode),
testModeAdd: strings.EqualFold(mode, "test-add"),
rootFolder: c.RootFolder,
searchMissing: util.BoolOrDefault(c.Options.SearchMissing, true),
addMonitored: util.BoolOrDefault(c.Options.AddMonitored, true),
skipAnime: util.BoolOrDefault(c.Options.SkipAnime, true),
tag: c.Tag,
cache: cc,
cacheTempDuration: c.CacheDuration,
cacheFiltersHash: util.AsSHA256(c.Filters),
queue: make(chan *media.FeedItem, 1024),
apiURL: apiURL,
apiHeaders: apiHeaders,
m: m,
http: util.NewRetryableHttpClient(60*time.Second, nil, &l),
log: l,
}
// compile expressions
if err := cl.compileExpressions(c.Filters); err != nil {
return nil, fmt.Errorf("compile expressions: %w", err)
}
// validate api access
ss, err := cl.getSystemStatus()
if err != nil {
return nil, fmt.Errorf("validate api: %w", err)
}
// get quality profile
if qid, err := cl.getQualityProfileId(c.QualityProfile); err != nil {
return nil, fmt.Errorf("get quality profile: %v: %w", c.QualityProfile, err)
} else {
cl.qualityProfileId = qid
}
// get language profile
2022-11-02 11:05:50 +00:00
if !strings.HasPrefix(ss.Version, "4.") {
if lid, err := cl.getLanguageProfileId(c.LanguageProfile); err != nil {
return nil, fmt.Errorf("get language profile: %v: %w", c.LanguageProfile, err)
} else {
cl.languageProfileId = lid
}
}
cl.log.Info().
Str("pvr_version", ss.Version).
Msg("Initialised")
return cl, nil
}
func (c *Client) Type() string {
return c.pvrType
}
func (c *Client) GetFiltersHash() string {
return c.cacheFiltersHash
}