-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsearch.go
80 lines (73 loc) · 2.05 KB
/
search.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
package imdb
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
)
const searchURL = "https://v3.sg.media-imdb.com/suggestion/x/%s.json?includeVideos=0"
// SearchQueryResponse represents the JSON struct returned by a query on searchURL
type SearchQueryResponse struct {
Matches []struct {
Image struct {
Height int `json:"height"`
ImageURL string `json:"imageUrl"`
Width int `json:"width"`
} `json:"i"`
ID string `json:"id"`
Title string `json:"l"`
MainActors string `json:"s"`
Q string `json:"q,omitempty"`
Type string `json:"qid,omitempty"`
Rank int `json:"rank,omitempty"`
Year int `json:"y,omitempty"`
YearInProduction string `json:"yr,omitempty"`
} `json:"d"`
Query string `json:"q"`
V int `json:"v"`
}
// SearchTitle searches for titles matching name and returns partial Titles.
// A partial Title has only ID, URL, Name and Year set.
// A full Title can be obtained with NewTitle, at the cost of extra requests.
func SearchTitle(c *http.Client, name string) ([]Title, error) {
resp, err := c.Get(fmt.Sprintf(searchURL, name))
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusForbidden {
return nil, errors.New("forbidden (e.g. denied by AWS WAF)")
}
return nil, fmt.Errorf("imdb: status not ok: %v", resp.Status)
}
decoder := json.NewDecoder(resp.Body)
if decoder == nil {
return nil, errors.New("imdb: decoder is nil")
}
var searchResponse SearchQueryResponse
err = decoder.Decode(&searchResponse)
if err != nil {
return nil, err
}
var t []Title
for _, match := range searchResponse.Matches {
if strings.HasPrefix(match.ID, "tt") {
t = append(t, Title{
ID: match.ID,
URL: fmt.Sprintf(titleURL, match.ID),
Name: match.Title,
Year: match.Year,
Type: match.Type,
Poster: Media{
URL: match.Image.ImageURL,
},
})
}
}
return t, nil
}