-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchpotato.go
312 lines (288 loc) · 7.32 KB
/
couchpotato.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package couchpotato
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strconv"
"time"
)
type client struct {
hostname string
port int
apiKey string
urlBase string
ssl bool
username string
password string
auth bool
httpClient *http.Client
// httpTimeout time.Duration
// debug bool
}
type apikey struct {
ApiKey string `json:"api_key"`
Success bool `json:"success"`
}
type MovieResults struct {
Movies []Movie `json:"movies,omitempty"`
}
type Movie struct {
Tmdb int `json:"tmdb_id,omitempty"`
Imdb string `json:"imdb,omitempty"`
Year int `json:"year,omitempty"`
// Titles []string `json:"titles,omitempty"`
Title string `json:"original_title,omitempty"`
Images Poster `json:"images,omitempty"`
Runtime int `json:"runtime,omitempty"`
Rating ImdbRating `json:"rating,omitempty"`
// Library InLibrary `json:"in_library,string"`
}
type InLibrary struct {
IsSet bool `json:"-"`
InLibraryStatus
}
type InLibraryStatus struct {
Status string `json:"status,omitempty"`
}
type ImdbRating struct {
Score []float64 `json:"imdb,omitempty"`
// Score float64 `json:"score,omitempty"`
// Votes int `json:"votes,omitempty"`
}
type Poster struct {
Original []string `json:"poster_original,omitempty"`
More []string `json:"poster,omitempty"`
}
// NewClient returns a new CP HTTP client
func NewClient(hostname string, port int, apiKey, urlBase string, ssl bool, username, password string) (c *client) {
auth := true
return &client{hostname, port, apiKey, urlBase, ssl, username, password, auth, &http.Client{Timeout: 10 * time.Second}}
}
/*
func (w *InLibrary) UnmarshalJSON(data []byte) error {
if id, err := strconv.Atoi(string(data)); err == nil {
w.IsSet = id
w.IsSet = true
return nil
}
return json.Unmarshal(data, &w.InLibrary)
}
*/
func (c *client) GetApiKey(serverUrl string) (string, error) {
var errorMsg string
// Username MD5 hash
h := md5.New()
io.WriteString(h, c.username)
u := fmt.Sprintf("%x", h.Sum(nil))
// Password MD5 hash
h = md5.New()
io.WriteString(h, c.password)
p := fmt.Sprintf("%x", h.Sum(nil))
// Make URL
var Url *url.URL
Url, err := url.Parse(serverUrl)
if err != nil {
log.Println("Bad serverUrl")
return "Bad serverUrl", err
}
// Set URL path
Url.Path += "/getkey/"
// Set URL parameters (username and password hashes)
parameters := url.Values{}
parameters.Add("p", p)
parameters.Add("u", u)
// Prepare URL
Url.RawQuery = parameters.Encode()
// GET URL
log.Printf("Will try to GET: %q\n", Url.String())
resp, err := c.httpClient.Get(Url.String())
if err != nil {
log.Println(err)
return "Can't GET " + Url.String(), err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
apikey := apikey{}
jerr := json.Unmarshal(body, &apikey)
if err != nil {
log.Println(jerr)
return "Failed unmarshalling JSON", jerr
}
if apikey.Success {
return apikey.ApiKey, err
} else {
errorMsg = "Failed getting API key"
log.Println(errorMsg)
log.Println(string(body))
return "Failed getting API key", errors.New(errorMsg)
}
}
func (c *client) GetAddr() string {
// var errorMsg string
var err error
log.Println("Start building full address")
serverUrl := "http"
if c.ssl == true {
serverUrl += "s"
}
serverUrl += "://" + c.hostname + ":" + strconv.Itoa(c.port)
if c.urlBase != "" {
if string(c.urlBase[0]) == "/" {
serverUrl += fmt.Sprintf("%s", c.urlBase)
} else {
serverUrl += fmt.Sprintf("/%s", c.urlBase)
}
}
if c.apiKey == "" {
log.Println("No API key specified")
// Get API key
if c.username == "" || c.password == "" {
log.Panic("Username and/or password not specified, API key cannot be computed")
} else {
log.Println("Compute API key with c.GetApiKey()")
c.apiKey, err = c.GetApiKey(serverUrl)
if err != nil {
log.Println(err)
}
}
}
if len(c.apiKey) != 32 {
log.Panic("API key length not 32")
}
match, _ := regexp.MatchString("[a-z0-9]{32}", c.apiKey)
if !match {
log.Panic("API key contains invalid characters")
}
// Complete API URL
serverApi := serverUrl + "/api/" + c.apiKey + "/"
log.Println("Return full API addr")
return serverApi
}
func (c *client) AddMovie(id, title string) (string, error) {
var errorMsg, bodyString string
//var bodyBytes []byte
var Url *url.URL
Url, err := url.Parse(c.GetAddr())
if err != nil {
panic("Arghh bad serverUrl")
}
// Set API method
Url.Path += "movie.add"
// Set method parameters
// IMDB identifier, original title, and whether to re-add existing title
parameters := url.Values{}
parameters.Add("identifier", id)
parameters.Add("title", title)
parameters.Add("force_readd", "false")
// Prepare URL
Url.RawQuery = parameters.Encode()
// GET URL
log.Printf("Will try to GET: %q\n", Url.String())
resp, err := c.httpClient.Get(Url.String())
if err != nil {
log.Println(err)
return "Can't GET " + Url.String(), err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
// Return JSON result
bodyString = string(bodyBytes)
}
return bodyString, err2
}
return bodyString, errors.New(errorMsg)
}
// movie.search
func (c *client) SearchMovie(q string) (*MovieResults, error) {
var errorMsg string
var Url *url.URL
Url, err := url.Parse(c.GetAddr())
if err != nil {
panic("Bad serverUrl")
}
// Set API method
Url.Path += "movie.search"
// Set method parameters
// q - Search query
parameters := url.Values{}
parameters.Add("q", q)
// Prepare URL
Url.RawQuery = parameters.Encode()
// Tally tries
tries := 0
// GET URL
for tries < 3 {
tries += 1
log.Printf("Will try to GET: %q\n", Url.String())
resp, err := c.httpClient.Get(Url.String())
if err != nil {
log.Println("Can't GET " + Url.String())
return &MovieResults{}, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
// Decode JSON result
decoder := json.NewDecoder(resp.Body)
val := &MovieResults{}
err := decoder.Decode(val)
if err != nil {
log.Fatal(err)
}
// Tally results
resultCount := 0
for _, _ = range val.Movies {
// log.Printf("%s (%d) [%s]\n", a.Titles[0], a.Year, a.Imdb)
// log.Printf("Original poster: %s", a.Images.Original[0])
// log.Printf("Small poster: %s", a.Images.More[0])
resultCount += 1
}
if resultCount == 0 {
errorMsg := "No results found, either try again or change search terms"
log.Println(errorMsg)
// tries += 1
} else {
return val, nil
}
} else {
log.Println(resp.StatusCode)
// tries += 1
}
}
return &MovieResults{}, errors.New(errorMsg)
}
// movie.searcher.full_search
func (c *client) FullSearch() (string, error) {
var errorMsg, bodyString string
var Url *url.URL
Url, err := url.Parse(c.GetAddr())
if err != nil {
panic("Bad serverUrl")
}
// Set API method
Url.Path += "movie.searcher.full_search"
// GET URL
log.Printf("Will try to GET: %q\n", Url.String())
resp, err := c.httpClient.Get(Url.String())
if err != nil {
log.Println(err)
return "Can't GET " + Url.String(), err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
bodyString = string(bodyBytes)
}
return bodyString, err2
}
return bodyString, errors.New(errorMsg)
}