autobrr/pkg/radarr/radarr.go
ze0s ca196f0bf1
feat(releases): support magnet links (#730)
* feat(releases): support magnet links

* feat(feeds): support magnet links

* feat(actions): log messages

* fix: component warning

* fix: check hasprefix instead of hassuffix for magnet

* feat(release): resolve magnet uri from link

* fix(actions): deluge use magnet uri

* fix(macros): add `MagnetURI` var

* fix(actions): run magnet resolving before macros

* feat(feeds): set download type on creation
2023-02-28 22:16:10 +01:00

150 lines
3.6 KiB
Go

package radarr
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/autobrr/autobrr/pkg/errors"
)
type Config struct {
Hostname string
APIKey string
// basic auth username and password
BasicAuth bool
Username string
Password string
Log *log.Logger
}
type Client interface {
Test(ctx context.Context) (*SystemStatusResponse, error)
Push(ctx context.Context, release Release) ([]string, error)
}
type client struct {
config Config
http *http.Client
Log *log.Logger
}
func New(config Config) Client {
httpClient := &http.Client{
Timeout: time.Second * 30,
}
c := &client{
config: config,
http: httpClient,
Log: config.Log,
}
if config.Log == nil {
c.Log = log.New(io.Discard, "", log.LstdFlags)
}
return c
}
type Release struct {
Title string `json:"title"`
DownloadUrl string `json:"downloadUrl,omitempty"`
MagnetUrl string `json:"magnetUrl,omitempty"`
Size int64 `json:"size"`
Indexer string `json:"indexer"`
DownloadProtocol string `json:"downloadProtocol"`
Protocol string `json:"protocol"`
PublishDate string `json:"publishDate"`
}
type PushResponse struct {
Approved bool `json:"approved"`
Rejected bool `json:"rejected"`
TempRejected bool `json:"temporarilyRejected"`
Rejections []string `json:"rejections"`
}
type SystemStatusResponse struct {
Version string `json:"version"`
}
type BadRequestResponse struct {
Severity string `json:"severity"`
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
PropertyName string `json:"propertyName"`
AttemptedValue string `json:"attemptedValue"`
}
func (r *BadRequestResponse) String() string {
return fmt.Sprintf("[%v: %v] %v: %v - got value: %v", r.Severity, r.ErrorCode, r.PropertyName, r.ErrorMessage, r.AttemptedValue)
}
func (c *client) Test(ctx context.Context) (*SystemStatusResponse, error) {
status, res, err := c.get(ctx, "system/status")
if err != nil {
return nil, errors.Wrap(err, "radarr error running test")
}
if status == http.StatusUnauthorized {
return nil, errors.New("unauthorized: bad credentials")
}
response := SystemStatusResponse{}
if err = json.Unmarshal(res, &response); err != nil {
return nil, errors.Wrap(err, "could not unmarshal data")
}
c.Log.Printf("radarr system/status status: (%v) response: %v\n", status, string(res))
return &response, nil
}
func (c *client) Push(ctx context.Context, release Release) ([]string, error) {
status, res, err := c.postBody(ctx, "release/push", release)
if err != nil {
return nil, errors.Wrap(err, "error push release")
}
c.Log.Printf("radarr release/push status: (%v) response: %v\n", status, string(res))
if status == http.StatusBadRequest {
badRequestResponses := make([]*BadRequestResponse, 0)
if err = json.Unmarshal(res, &badRequestResponses); err != nil {
return nil, errors.Wrap(err, "could not unmarshal data")
}
rejections := []string{}
for _, response := range badRequestResponses {
rejections = append(rejections, response.String())
}
return rejections, nil
}
pushResponse := make([]PushResponse, 0)
if err = json.Unmarshal(res, &pushResponse); err != nil {
return nil, errors.Wrap(err, "could not unmarshal data")
}
// log and return if rejected
if pushResponse[0].Rejected {
rejections := strings.Join(pushResponse[0].Rejections, ", ")
c.Log.Printf("radarr release/push rejected %v reasons: %q\n", release.Title, rejections)
return pushResponse[0].Rejections, nil
}
// success true
return nil, nil
}