82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package tmdb
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/url"
|
|
"path"
|
|
"strconv"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Result struct {
|
|
Page int `json:"page"`
|
|
TotalPages int `json:"total_pages"`
|
|
TotalResults int `json:"total_results"`
|
|
}
|
|
|
|
type ResultMovies struct {
|
|
Result
|
|
Results []ResultMovie `json:"results"`
|
|
}
|
|
type ResultMovie struct {
|
|
Adult bool `json:"adult"`
|
|
BackdropPath string `json:"backdrop_path"`
|
|
GenreIDs []int `json:"genre_ids"`
|
|
ID int32 `json:"id"`
|
|
OriginalLanguage string `json:"original_language"`
|
|
OriginalTitle string `json:"original_title"`
|
|
Overview string `json:"overview"`
|
|
Popularity int `json:"popularity"`
|
|
PosterPath string `json:"poster_path"`
|
|
ReleaseDate string `json:"release_date"`
|
|
Title string `json:"title"`
|
|
Video bool `json:"video"`
|
|
VoteAverage int `json:"vote_average"`
|
|
VoteCount int `json:"vote_count"`
|
|
}
|
|
|
|
func (movie *ResultMovie) GetPoster(image *Image, size string) string {
|
|
base, err := url.Parse(image.SecureBaseURL)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
fullPath := path.Join(base.Path, size, movie.PosterPath)
|
|
base.Path = fullPath
|
|
return base.String()
|
|
}
|
|
|
|
func (movie *ResultMovie) ReleaseYear() string {
|
|
if movie.ReleaseDate == "" {
|
|
return ""
|
|
} else {
|
|
return "(" + movie.ReleaseDate[:4] + ")"
|
|
}
|
|
}
|
|
|
|
// TODO: genres list https://developer.themoviedb.org/reference/genre-movie-list
|
|
// func (movie *ResultMovie) FGenres() string {
|
|
// genres := ""
|
|
// for _, genre := range movie.Genres {
|
|
// genres += genre.Name + ", "
|
|
// }
|
|
// return genres[:len(genres)-2]
|
|
// }
|
|
|
|
func (api *API) SearchMovies(query string, adult bool, page int64) (*ResultMovies, error) {
|
|
path := []string{"search", "movie"}
|
|
params := map[string]string{
|
|
"query": url.QueryEscape(query),
|
|
"include_adult": strconv.FormatBool(adult),
|
|
"page": strconv.FormatInt(page, 10),
|
|
}
|
|
url := buildURL(path, params)
|
|
data, err := api.get(url)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "api.get")
|
|
}
|
|
var results ResultMovies
|
|
json.Unmarshal(data, &results)
|
|
return &results, nil
|
|
}
|