-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
radarr.go
175 lines (144 loc) · 4.92 KB
/
radarr.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package dnclient
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"sync"
"github.com/gorilla/mux"
"golift.io/starr"
"golift.io/starr/radarr"
)
func (c *Client) radarrMethods(r *mux.Router) {
for _, r := range c.Config.Radarr {
r.Radarr = radarr.New(r.Config)
}
r.Handle("/api/radarr/add/{id:[0-9]+}",
c.checkAPIKey(c.responseWrapper(c.radarrAddMovie))).Methods("POST")
r.Handle("/api/radarr/check/{id:[0-9]+}/{tmdbid:[0-9]+}",
c.checkAPIKey(c.responseWrapper(c.radarrCheckMovie))).Methods("GET")
r.Handle("/api/radarr/qualityProfiles/{id:[0-9]+}",
c.checkAPIKey(c.responseWrapper(c.radarrProfiles))).Methods("GET")
r.Handle("/api/radarr/rootFolder/{id:[0-9]+}",
c.checkAPIKey(c.responseWrapper(c.radarrRootFolders))).Methods("GET")
}
func (c *Config) fixRadarrConfig() {
for i := range c.Radarr {
if c.Radarr[i].Timeout.Duration == 0 {
c.Radarr[i].Timeout.Duration = c.Timeout.Duration
}
}
}
// RadarrConfig represents the input data for a Radarr server.
type RadarrConfig struct {
*starr.Config
*radarr.Radarr
sync.RWMutex `json:"-" toml:"-" xml:"-" yaml:"-"`
}
func (c *Client) logRadarr() {
if count := len(c.Radarr); count == 1 {
c.Printf(" => Radarr Config: 1 server: %s, apikey:%v, timeout:%v, verify ssl:%v",
c.Radarr[0].URL, c.Radarr[0].APIKey != "", c.Radarr[0].Timeout, c.Radarr[0].ValidSSL)
} else {
c.Print(" => Radarr Config:", count, "servers")
for _, f := range c.Radarr {
c.Printf(" => Server: %s, apikey:%v, timeout:%v, verify ssl:%v",
f.URL, f.APIKey != "", f.Timeout, f.ValidSSL)
}
}
}
// getRadarr finds a radar based on the passed-in ID.
// Every Radarr handler calls this.
func (c *Client) getRadarr(id string) *RadarrConfig {
j, _ := strconv.Atoi(id)
for i, radar := range c.Radarr {
if i != j-1 { // discordnotifier wants 1-indexes
continue
}
return radar
}
return nil
}
func (c *Client) radarrRootFolders(r *http.Request) (int, interface{}) {
// Make sure the provided radarr id exists.
radar := c.getRadarr(mux.Vars(r)["id"])
if radar == nil {
return http.StatusUnprocessableEntity, fmt.Errorf("%v: %w", mux.Vars(r)["id"], ErrNoRadarr)
}
// Get folder list from Radarr.
folders, err := radar.GetRootFolders()
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("getting folders: %w", err)
}
// Format folder list into a nice path=>freesSpace map.
p := make(map[string]int64)
for i := range folders {
p[folders[i].Path] = folders[i].FreeSpace
}
return http.StatusOK, p
}
func (c *Client) radarrProfiles(r *http.Request) (int, interface{}) {
// Make sure the provided radarr id exists.
radar := c.getRadarr(mux.Vars(r)["id"])
if radar == nil {
return http.StatusUnprocessableEntity, fmt.Errorf("%v: %w", mux.Vars(r)["id"], ErrNoRadarr)
}
// Get the profiles from radarr.
profiles, err := radar.GetQualityProfiles()
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("getting profiles: %w", err)
}
// Format profile ID=>Name into a nice map.
p := make(map[int]string)
for i := range profiles {
p[profiles[i].ID] = profiles[i].Name
}
return http.StatusOK, p
}
func (c *Client) radarrCheckMovie(r *http.Request) (int, interface{}) {
radar := c.getRadarr(mux.Vars(r)["id"])
if radar == nil {
return http.StatusUnprocessableEntity, fmt.Errorf("%v: %w", mux.Vars(r)["id"], ErrNoRadarr)
}
tmdbID, _ := strconv.Atoi(mux.Vars(r)["tmdbid"])
// Check for existing movie.
if m, err := radar.GetMovie(tmdbID); err != nil {
return http.StatusServiceUnavailable, fmt.Errorf("checking movie: %w", err)
} else if len(m) > 0 {
return http.StatusConflict, fmt.Errorf("%d: %w", tmdbID, ErrExists)
}
return http.StatusOK, http.StatusText(http.StatusNotFound)
}
func (c *Client) radarrAddMovie(r *http.Request) (int, interface{}) {
// Make sure the provided radarr id exists.
radar := c.getRadarr(mux.Vars(r)["id"])
if radar == nil {
return http.StatusUnprocessableEntity, fmt.Errorf("%v: %w", mux.Vars(r)["id"], ErrNoRadarr)
}
// Extract payload and check for TMDB ID.
payload := &radarr.AddMovieInput{}
if err := json.NewDecoder(r.Body).Decode(payload); err != nil {
return http.StatusBadRequest, fmt.Errorf("decoding payload: %w", err)
} else if payload.TmdbID == 0 {
return http.StatusUnprocessableEntity, fmt.Errorf("0: %w", ErrNoTMDB)
}
// Check for existing movie.
if m, err := radar.GetMovie(payload.TmdbID); err != nil {
return http.StatusServiceUnavailable, fmt.Errorf("checking movie: %w", err)
} else if len(m) > 0 {
return http.StatusConflict, fmt.Errorf("%d: %w", payload.TmdbID, ErrExists)
}
if payload.Title == "" {
// Title must exist, even if it's wrong.
payload.Title = strconv.Itoa(payload.TmdbID)
}
if payload.MinimumAvailability == "" {
payload.MinimumAvailability = "released"
}
// Add movie using fixed payload.
movie, err := radar.AddMovie(payload)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("adding movie: %w", err)
}
return http.StatusCreated, movie
}