-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvmaze.go
273 lines (238 loc) · 5.47 KB
/
tvmaze.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
// Package tvmaze is an HTTP Client for the tvmaze API.
package tvmaze
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
cache "github.com/robfig/go-cache"
)
// Client is a tvmaze client.
type Client struct {
Debug bool
BaseURI string
Region string
Cache *cache.Cache
CacheFile string
UseCache bool
*http.Client
// UserAgent may be set to identify your application.
UserAgent string
}
// NewClient returns a ready to use Client.
func NewClient(cachefile string) (*Client, error) {
c := cache.New(time.Minute*60*24*7, time.Minute*60)
if _, err := os.Stat(cachefile); err == nil {
err := c.LoadFile(cachefile)
if err != nil {
return nil, err
}
}
timeout := time.Duration(180 * time.Second)
client := &http.Client{
Timeout: timeout,
}
return &Client{
Cache: c,
CacheFile: cachefile,
BaseURI: "http://api.tvmaze.com",
Client: client,
UserAgent: "github.com/rickyninja/tvmaze",
}, nil
}
// WriteCache writes cache contents to disk.
func (c *Client) WriteCache() error {
err := c.Cache.SaveFile(c.CacheFile)
if err != nil {
return err
}
return nil
}
// FindShow searches tvmaze for showname, and returns it as a Show if a match is found.
func (c *Client) FindShow(showname string) (Show, error) {
candidates, err := c.GetShow(showname)
if err != nil {
return Show{}, err
}
// Lost Girl is listed as CA country in tvrage, and so my configured region of US
// will not match. Lost Girl has only aired in CA though, so you can get a string
// equality match on the 2nd retry (the old behavior).
for retry := 0; retry <= 1; retry++ {
for _, cand := range candidates {
show := cand.Show
if c.Region != "" && retry < 1 {
if c.Region == show.Network.Country.Code && strings.HasPrefix(show.Name, showname) {
return show, nil
}
} else {
if strings.ToLower(show.Name) == strings.ToLower(showname) {
return show, nil
} else if retry > 0 && strings.HasPrefix(show.Name, showname) {
return show, nil
}
}
}
}
return Show{}, errors.New("Failed to match show in tvmaze!")
}
// GetShow queries tvmaze for show, and returns Candidates that may be a match.
func (c *Client) GetShow(show string) ([]Candidate, error) {
route := "/search/shows"
uri, err := url.Parse(c.BaseURI + route)
if err != nil {
log.Fatal(err)
}
query := url.Values{}
query.Add("q", show)
uri.RawQuery = query.Encode()
var candidates []Candidate
jsondata, err := c.Go(uri)
if err != nil {
return nil, err
}
err = json.Unmarshal(jsondata, &candidates)
if err != nil {
return nil, err
}
return candidates, nil
}
// GetEpisodes queries tvmaze, and returns a list of Episodes.
func (c *Client) GetEpisodes(showID int64) ([]Episode, error) {
route := fmt.Sprintf("/shows/%d/episodes", showID)
uri, err := url.Parse(c.BaseURI + route)
if err != nil {
return nil, err
}
jsondata, err := c.Go(uri)
if err != nil {
return nil, err
}
var episodes []Episode
err = json.Unmarshal(jsondata, &episodes)
if err != nil {
return nil, err
}
return episodes, nil
}
// Go does an HTTP GET to tvmaze with the provided uri, and returns the response body.
// It will cache response if UseCache is true.
func (c *Client) Go(uri *url.URL) ([]byte, error) {
data, found := c.Cache.Get(uri.String())
if !found || !c.UseCache {
if c.Debug {
log.Print("cache miss: " + uri.String() + "\n")
}
request, err := http.NewRequest("GET", uri.String(), nil)
if err != nil {
return nil, err
}
if c.UserAgent != "" {
request.Header.Set("User-Agent", c.UserAgent)
}
resp, err := c.Do(request)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("Request failed: %s", http.StatusText(resp.StatusCode)))
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
c.Cache.Set(uri.String(), data, 0)
} else {
if c.Debug {
fmt.Printf("cache hit: %s\n", uri.String())
}
}
return data.([]byte), nil
}
// Episode represents a tv episode.
type Episode struct {
ID int64
URL string
Name string
Season int
Number int
AirDate string
AirTime string
AirStamp string
Runtime int
//Image
Summary string
Links Links `json:"_links"`
}
// Candidate represents a search candidate.
type Candidate struct {
Score float64
Show Show
}
// Show represents a tv show.
type Show struct {
ID int64
URL string
Name string
Type string
Language string
Genres []string
Status string
Runtime int
Premiered string
Schedule Schedule
Rating Rating
Weight int
Network Network
//WebChannel
Externals External
Image Image
Summary string
Updated int64
Links Links
}
// Links represents Episode links.
type Links struct {
Self Link
PreviousEpisode Link
}
// Link represents a uri link.
type Link struct {
Href string
}
// Image represents an image.
type Image struct {
Medium string
Original string
}
// External represents a 3rd party tv api.
type External struct {
TVRage int64
TheTVDB int64
}
// Schedule represents a Show schedule.
type Schedule struct {
Time string
Days []string
}
// Rating represents a tv show rating.
type Rating struct {
Average float64
}
// Network represents the tv network airing the show.
type Network struct {
ID int
Name string
Country Country
}
// Country represents the country the show aired in.
type Country struct {
Name string
Code string
TimeZone string
}