-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
159 lines (150 loc) · 4.86 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
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
// Copyright 2016 The tmdb Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package tmdb
import (
"encoding/json"
"errors"
"fmt"
"net/url"
)
// SearchMovieResult type represents The Movie Database Movies search results
type SearchMovieResult struct {
Page int `json:"page"`
Results []struct {
Adult bool `json:"adult"`
BackdropPath string `json:"backdrop_path"`
GenreIds []int `json:"genre_ids"`
ID int `json:"id"`
OriginalLanguage string `json:"original_language"`
OriginalTitle string `json:"original_title"`
Overview string `json:"overview"`
Popularity float64 `json:"popularity"`
PosterPath string `json:"poster_path"`
ReleaseDate string `json:"release_date"`
Title string `json:"title"`
Video bool `json:"video"`
VoteAverage float64 `json:"vote_average"`
VoteCount int `json:"vote_count"`
} `json:"results"`
TotalPages int `json:"total_pages"`
TotalResults int `json:"total_results"`
}
// SearchMultiResult type represents The Movie Database multiple media
// search result.
//
// * Search on Movies
// * Search on TV Shows
// * Search on Persons
type SearchMultiResult struct {
Page int `json:"page"`
Results []struct {
BackdropPath string `json:"backdrop_path"`
ProfilePath string `json:"profile_path"`
FirstAirDate string `json:"first_air_date"`
GenreIds []int `json:"genre_ids"`
ID int `json:"id"`
MediaType string `json:"media_type"`
Name string `json:"name"`
Title string `json:"title"`
OriginCountry []string `json:"origin_country"`
OriginalLanguage string `json:"original_language"`
OriginalName string `json:"original_name"`
Overview string `json:"overview"`
Popularity float64 `json:"popularity"`
PosterPath string `json:"poster_path"`
VoteAverage float64 `json:"vote_average"`
VoteCount int `json:"vote_count"`
} `json:"results"`
TotalPages int `json:"total_pages"`
TotalResults int `json:"total_results"`
}
// SearchTVResult type represents The Movie Database TV Shows search results
type SearchTVResult struct {
Page int `json:"page"`
Results []struct {
BackdropPath string `json:"backdrop_path"`
FirstAirDate string `json:"first_air_date"`
GenreIds []int `json:"genre_ids"`
ID int `json:"id"`
Name string `json:"name"`
OriginCountry []string `json:"origin_country"`
OriginalLanguage string `json:"original_language"`
OriginalName string `json:"original_name"`
Overview string `json:"overview"`
Popularity float64 `json:"popularity"`
PosterPath string `json:"poster_path"`
VoteAverage float64 `json:"vote_average"`
VoteCount int `json:"vote_count"`
} `json:"results"`
TotalPages int `json:"total_pages"`
TotalResults int `json:"total_results"`
}
// PopularMovie ...
func (tmdb *TMDB) PopularMovie() (result SearchMovieResult, err error) {
s := fmt.Sprintf("%smovie/popular?api_key=%s", tmdb.BaseURL, tmdb.APIKey)
u, err := url.Parse(s)
if err != nil {
return result, err
}
body, resp, err := tmdb.FetchContent(u)
if err != nil {
return result, err
}
if resp.StatusCode != 200 {
return result, errors.New(resp.Status)
}
err = json.Unmarshal(body, &result)
return result, err
}
// PopularTV ...
func (tmdb *TMDB) PopularTV() (result SearchTVResult, err error) {
s := fmt.Sprintf("%stv/popular?api_key=%s", tmdb.BaseURL, tmdb.APIKey)
u, err := url.Parse(s)
if err != nil {
return result, err
}
body, resp, err := tmdb.FetchContent(u)
if err != nil {
return result, err
}
if resp.StatusCode != 200 {
return result, errors.New(resp.Status)
}
err = json.Unmarshal(body, &result)
return result, err
}
// SearchMulti ...
func (tmdb *TMDB) SearchMulti(query string) (result SearchMultiResult, err error) {
s := fmt.Sprintf("%ssearch/multi?api_key=%s&query=%s", tmdb.BaseURL, tmdb.APIKey, query)
u, err := url.Parse(s)
if err != nil {
return result, err
}
body, resp, err := tmdb.FetchContent(u)
if err != nil {
return result, err
}
if resp.StatusCode != 200 {
return result, errors.New(resp.Status)
}
err = json.Unmarshal(body, &result)
return result, err
}
// SearchMovie ...
func (tmdb *TMDB) SearchMovie(query string) (result SearchMovieResult, err error) {
s := fmt.Sprintf("%ssearch/movie?api_key=%s&query=%s", tmdb.BaseURL, tmdb.APIKey, query)
u, err := url.Parse(s)
if err != nil {
return result, err
}
body, resp, err := tmdb.FetchContent(u)
if err != nil {
return result, err
}
if resp.StatusCode != 200 {
return result, errors.New(resp.Status)
}
err = json.Unmarshal(body, &result)
return result, err
}