-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroulette.go
162 lines (142 loc) · 3.24 KB
/
roulette.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
package goroulette
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const baseURL = "http://netflixroulette.net/api/api.php"
type Movie struct {
Unit int `json:unit`
ShowID int `json:show_id`
ShowTitle string `json:"show_title"`
ReleaseYear string `json:"release_year"`
Rating string `json:"rating"`
Category string `json:"category"`
ShowCast string `json:"show_cast"`
Director string `json:"director"`
Summary string `json:"summary"`
Poster string `json:"poster"`
MediaType int `json:mediatype`
Runtime string `json:"runtime"`
}
func GetURL(params map[string]string) (string, error) {
var URL *url.URL
URL, err := url.Parse(baseURL)
if err != nil {
return "", err
}
parameters := url.Values{}
for param, value := range params {
parameters.Add(param, value)
URL.RawQuery = parameters.Encode()
}
return URL.String(), nil
}
func CreateRouletteCall(params map[string]string) ([]byte, error) {
url, err := GetURL(params)
if err != nil {
return []byte{}, err
}
res, err := http.Get(url)
if err != nil {
return []byte{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
e := errors.New(fmt.Sprintf("Unexpected API status code %s", res.Status))
return []byte{}, e
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return []byte{}, err
}
return body, nil
}
func GetMoviesByTitle(title string) (Movie, error) {
m := map[string]string{"title": title}
data, err := CreateRouletteCall(m)
if err != nil {
return Movie{}, err
}
var movie Movie
err = json.Unmarshal(data, &movie)
if err != nil {
return Movie{}, err
}
return movie, nil
}
func GetMoviesByTitleAndYear(title string, year string) (Movie, error) {
m := map[string]string{"title": title, "year": year}
data, err := CreateRouletteCall(m)
var movie Movie
if err != nil {
return movie, err
}
err = json.Unmarshal(data, &movie)
if err != nil {
return movie, err
}
return movie, nil
}
func GetMoviesByDirector(director string) ([]Movie, error) {
m := map[string]string{"director": director}
data, err := CreateRouletteCall(m)
if err != nil {
return nil, err
}
var movie []Movie
err = json.Unmarshal(data, &movie)
if err != nil {
return nil, err
}
return movie, nil
}
func GetMoviesByActor(actor string) ([]Movie, error) {
m := map[string]string{"actor": actor}
data, err := CreateRouletteCall(m)
if err != nil {
return nil, err
}
var movie []Movie
err = json.Unmarshal(data, &movie)
if err != nil {
return nil, err
}
return movie, nil
}
func (movie Movie) GetNetflixID() int {
return movie.ShowID
}
func (movie Movie) GetTitle() string {
return movie.ShowTitle
}
func (movie Movie) GetReleaseYear() string {
return movie.ReleaseYear
}
func (movie Movie) GetRating() string {
return movie.Rating
}
func (movie Movie) GetCategory() string {
return movie.Category
}
func (movie Movie) GetShowCast() string {
return movie.ShowCast
}
func (movie Movie) GetDirector() string {
return movie.Director
}
func (movie Movie) GetSummary() string {
return movie.Summary
}
func (movie Movie) GetPoster() string {
return movie.Poster
}
func (movie Movie) GetMediaType() int {
return movie.MediaType
}
func (movie Movie) GetRuntime() string {
return movie.Runtime
}