Add tag support to Sonarr/Radarr requests
This commit is contained in:
@@ -148,6 +148,57 @@ func (c *Client) getExclusions() (map[int]exclusion, error) {
|
||||
return exclusions, nil
|
||||
}
|
||||
|
||||
func (c *Client) getOrCreateTag(tagName string) (int, error) {
|
||||
// get all tags
|
||||
resp, err := rek.Get(util.JoinURL(c.apiURL, "tag"), rek.Client(c.http), rek.Headers(c.apiHeaders))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("request tags: %w", err)
|
||||
}
|
||||
defer resp.Body().Close()
|
||||
|
||||
// validate response
|
||||
if resp.StatusCode() != 200 {
|
||||
return 0, fmt.Errorf("validate tags response: %s", resp.Status())
|
||||
}
|
||||
|
||||
// decode response
|
||||
tags := new([]tag)
|
||||
if err := json.NewDecoder(resp.Body()).Decode(tags); err != nil {
|
||||
return 0, fmt.Errorf("decode tags response: %w", err)
|
||||
}
|
||||
|
||||
// find tag by name
|
||||
for _, t := range *tags {
|
||||
if strings.EqualFold(t.Label, tagName) {
|
||||
return t.Id, nil
|
||||
}
|
||||
}
|
||||
|
||||
// tag not found, create it
|
||||
createReq := tag{
|
||||
Label: tagName,
|
||||
}
|
||||
createResp, err := rek.Post(util.JoinURL(c.apiURL, "tag"), rek.Client(c.http), rek.Headers(c.apiHeaders),
|
||||
rek.Json(createReq))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("request create tag: %w", err)
|
||||
}
|
||||
defer createResp.Body().Close()
|
||||
|
||||
// validate response
|
||||
if createResp.StatusCode() != 200 && createResp.StatusCode() != 201 {
|
||||
return 0, fmt.Errorf("validate create tag response: %s", createResp.Status())
|
||||
}
|
||||
|
||||
// decode created tag
|
||||
createdTag := new(tag)
|
||||
if err := json.NewDecoder(createResp.Body()).Decode(createdTag); err != nil {
|
||||
return 0, fmt.Errorf("decode create tag response: %w", err)
|
||||
}
|
||||
|
||||
return createdTag.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) AddMediaItem(item *media.Item, opts ...nabarr.PvrOption) error {
|
||||
// prepare options
|
||||
o, err := nabarr.BuildPvrOptions(opts...)
|
||||
@@ -155,6 +206,16 @@ func (c *Client) AddMediaItem(item *media.Item, opts ...nabarr.PvrOption) error
|
||||
return fmt.Errorf("build options: %v: %w", item.TmdbId, err)
|
||||
}
|
||||
|
||||
// prepare tags
|
||||
tags := []int{}
|
||||
if o.Tag != "" {
|
||||
tagId, err := c.getOrCreateTag(o.Tag)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get or create tag %q: %w", o.Tag, err)
|
||||
}
|
||||
tags = []int{tagId}
|
||||
}
|
||||
|
||||
// prepare request
|
||||
req := addRequest{
|
||||
Title: item.Title,
|
||||
@@ -162,6 +223,7 @@ func (c *Client) AddMediaItem(item *media.Item, opts ...nabarr.PvrOption) error
|
||||
Year: item.Year,
|
||||
QualityProfileId: c.qualityProfileId,
|
||||
Images: []string{},
|
||||
Tags: tags,
|
||||
Monitored: o.AddMonitored,
|
||||
RootFolderPath: c.rootFolder,
|
||||
MinimumAvailability: "released",
|
||||
|
||||
Reference in New Issue
Block a user