-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlookup.go
127 lines (104 loc) · 2.64 KB
/
lookup.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
package radarr
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
)
// LookupService contains Radarr movie lookup operations
type LookupService struct {
s *Service
}
func newLookupService(s *Service) *LookupService {
return &LookupService{s}
}
// Plain simply search movies matching given term
// https://github.com/Radarr/Radarr/wiki/API:Movie-Lookup#search-by-term
func (l *LookupService) Plain(term string) (Movies, error) {
if term == "" {
return nil, errors.New("Please specify a valid term")
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api%s", l.s.url, lookupURI), nil)
if err != nil {
return nil, err
}
// Append query
params := req.URL.Query()
params.Add("term", term)
req.URL.RawQuery = params.Encode()
resp, err := l.s.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}
var movies Movies
err = json.NewDecoder(resp.Body).Decode(&movies)
if err != nil {
return nil, err
}
return movies, nil
}
// Tmdb Search by The Movie Database ID
// https://github.com/Radarr/Radarr/wiki/API:Movie-Lookup#search-by-the-movie-database-id
func (l *LookupService) Tmdb(TMDBID int) (*Movie, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api%s", l.s.url, lookupTMDBURI), nil)
if err != nil {
return nil, err
}
// Append query
params := req.URL.Query()
params.Add("tmdbId", strconv.Itoa(TMDBID))
req.URL.RawQuery = params.Encode()
resp, err := l.s.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}
var movie Movie
err = json.NewDecoder(resp.Body).Decode(&movie)
if err != nil {
return nil, err
}
return &movie, nil
}
// Imdb Search using IMDB id
// https://github.com/Radarr/Radarr/wiki/API:Movie-Lookup#search-using-imdb-id
func (l *LookupService) Imdb(IMDBID string) (*Movie, error) {
var re = regexp.MustCompile(`(?i)^tt\d*$`)
if !re.MatchString(IMDBID) {
return nil, fmt.Errorf("Please specify a valid IMDB id. Pattern %s", re.String())
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api%s", l.s.url, lookupIMDBURI), nil)
if err != nil {
return nil, err
}
// Append query
parms := req.URL.Query()
parms.Add("imdbId", IMDBID)
req.URL.RawQuery = parms.Encode()
resp, err := l.s.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}
var movie Movie
err = json.NewDecoder(resp.Body).Decode(&movie)
if err != nil {
return nil, err
}
return &movie, nil
}