2021-02-14 16:18:26 +00:00
|
|
|
package sonarr
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2021-02-20 20:01:04 +00:00
|
|
|
"github.com/l3uddz/nabarr"
|
2021-02-14 16:18:26 +00:00
|
|
|
"github.com/l3uddz/nabarr/media"
|
|
|
|
|
"github.com/l3uddz/nabarr/util"
|
|
|
|
|
"github.com/lucperkins/rek"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
ErrItemNotFound = errors.New("not found")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (c *Client) getSystemStatus() (*systemStatus, error) {
|
|
|
|
|
// send request
|
2021-02-21 14:01:21 +00:00
|
|
|
resp, err := rek.Get(util.JoinURL(c.apiURL, "system", "status"), rek.Client(c.http), rek.Headers(c.apiHeaders))
|
2021-02-14 16:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("request system status: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body().Close()
|
|
|
|
|
|
|
|
|
|
// validate response
|
|
|
|
|
if resp.StatusCode() != 200 {
|
|
|
|
|
return nil, fmt.Errorf("validate system status response: %s", resp.Status())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// decode response
|
|
|
|
|
b := new(systemStatus)
|
|
|
|
|
if err := json.NewDecoder(resp.Body()).Decode(b); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("decode system status response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) getQualityProfileId(profileName string) (int, error) {
|
|
|
|
|
// send request
|
2021-02-21 14:01:21 +00:00
|
|
|
resp, err := rek.Get(util.JoinURL(c.apiURL, "profile"), rek.Client(c.http), rek.Headers(c.apiHeaders))
|
2021-02-14 16:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("request quality profiles: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body().Close()
|
|
|
|
|
|
|
|
|
|
// validate response
|
|
|
|
|
if resp.StatusCode() != 200 {
|
|
|
|
|
return 0, fmt.Errorf("validate quality profiles response: %s", resp.Status())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// decode response
|
|
|
|
|
b := new([]qualityProfile)
|
|
|
|
|
if err := json.NewDecoder(resp.Body()).Decode(b); err != nil {
|
|
|
|
|
return 0, fmt.Errorf("decode quality profiles response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find quality profile
|
|
|
|
|
for _, profile := range *b {
|
|
|
|
|
if strings.EqualFold(profile.Name, profileName) {
|
|
|
|
|
return profile.Id, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0, errors.New("quality profile not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) lookupMediaItem(item *media.Item) (*lookupRequest, error) {
|
2021-02-21 22:30:56 +00:00
|
|
|
// retrieve and validate media provider data
|
|
|
|
|
mdp, mdi := item.GetProviderData()
|
|
|
|
|
if mdp == "" || mdi == "" {
|
|
|
|
|
return nil, fmt.Errorf("no media provider details found")
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:18:26 +00:00
|
|
|
// prepare request
|
2021-02-19 19:15:38 +00:00
|
|
|
reqUrl, err := util.URLWithQuery(util.JoinURL(c.apiURL, "series", "lookup"),
|
2021-02-21 22:30:56 +00:00
|
|
|
url.Values{"term": []string{fmt.Sprintf("%s:%s", mdp, mdi)}})
|
2021-02-14 16:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("generate series lookup request url: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send request
|
2021-02-21 14:01:21 +00:00
|
|
|
resp, err := rek.Get(reqUrl, rek.Client(c.http), rek.Headers(c.apiHeaders))
|
2021-02-14 16:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("request series lookup: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body().Close()
|
|
|
|
|
|
|
|
|
|
// validate response
|
|
|
|
|
if resp.StatusCode() != 200 {
|
|
|
|
|
return nil, fmt.Errorf("validate series lookup response: %s", resp.Status())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// decode response
|
|
|
|
|
b := new([]lookupRequest)
|
|
|
|
|
if err := json.NewDecoder(resp.Body()).Decode(b); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("decode series lookup response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find series
|
|
|
|
|
for _, s := range *b {
|
2021-02-21 22:30:56 +00:00
|
|
|
if strconv.Itoa(s.TvdbId) == mdi {
|
2021-02-14 16:18:26 +00:00
|
|
|
return &s, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 22:30:56 +00:00
|
|
|
return nil, fmt.Errorf("series lookup %sId: %v: %w", mdp, mdi, ErrItemNotFound)
|
2021-02-14 16:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-20 20:01:04 +00:00
|
|
|
func (c *Client) AddMediaItem(item *media.Item, opts ...nabarr.PvrOption) error {
|
|
|
|
|
// prepare options
|
|
|
|
|
o, err := nabarr.BuildPvrOptions(opts...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("build options: %v: %w", item.TvdbId, err)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:18:26 +00:00
|
|
|
// prepare request
|
|
|
|
|
tvdbId, err := strconv.Atoi(item.TvdbId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("converting tvdb id to int: %q", item.TvdbId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := addRequest{
|
|
|
|
|
Title: item.Title,
|
|
|
|
|
TitleSlug: item.Slug,
|
|
|
|
|
Year: item.Year,
|
|
|
|
|
QualityProfileId: c.qualityProfileId,
|
|
|
|
|
Images: []string{},
|
|
|
|
|
Tags: []string{},
|
2021-02-20 20:01:04 +00:00
|
|
|
Monitored: o.AddMonitored,
|
2021-02-14 16:18:26 +00:00
|
|
|
RootFolderPath: c.rootFolder,
|
|
|
|
|
AddOptions: addOptions{
|
2021-02-20 20:01:04 +00:00
|
|
|
SearchForMissingEpisodes: o.SearchMissing,
|
2021-02-14 16:18:26 +00:00
|
|
|
IgnoreEpisodesWithFiles: false,
|
|
|
|
|
IgnoreEpisodesWithoutFiles: false,
|
|
|
|
|
},
|
|
|
|
|
Seasons: []string{},
|
2021-02-21 14:01:21 +00:00
|
|
|
SeriesType: util.StringOrDefault(o.SeriesType, "standard"),
|
2021-02-14 16:18:26 +00:00
|
|
|
SeasonFolder: true,
|
|
|
|
|
TvdbId: tvdbId,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send request
|
2021-02-21 14:01:21 +00:00
|
|
|
resp, err := rek.Post(util.JoinURL(c.apiURL, "series"), rek.Client(c.http), rek.Headers(c.apiHeaders),
|
|
|
|
|
rek.Json(req))
|
2021-02-14 16:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("request add series: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body().Close()
|
|
|
|
|
|
|
|
|
|
// validate response
|
|
|
|
|
if resp.StatusCode() != 200 && resp.StatusCode() != 201 {
|
|
|
|
|
return fmt.Errorf("validate add series response: %s", resp.Status())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|