2023-12-23 12:08:58 +01:00
|
|
|
package client
|
2023-12-21 15:01:06 +01:00
|
|
|
|
|
|
|
import (
|
2023-12-22 16:31:40 +01:00
|
|
|
"time"
|
|
|
|
|
2023-12-23 12:08:58 +01:00
|
|
|
"ewintr.nl/emdb/model"
|
2023-12-21 15:01:06 +01:00
|
|
|
tmdb "github.com/cyruzin/golang-tmdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TMDB struct {
|
|
|
|
c *tmdb.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTMDB(apikey string) (*TMDB, error) {
|
|
|
|
tmdbClient, err := tmdb.Init(apikey)
|
|
|
|
if err != nil {
|
2023-12-22 15:50:42 +01:00
|
|
|
return nil, err
|
2023-12-21 15:01:06 +01:00
|
|
|
}
|
|
|
|
tmdbClient.SetClientAutoRetry()
|
|
|
|
tmdbClient.SetAlternateBaseURL()
|
|
|
|
|
|
|
|
return &TMDB{
|
|
|
|
c: tmdbClient,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-23 12:08:58 +01:00
|
|
|
func (t TMDB) Search(query string) ([]model.Movie, error) {
|
|
|
|
return []model.Movie{
|
2023-12-22 20:20:20 +01:00
|
|
|
{Title: "movie1", Year: 2020, Summary: "summary1"},
|
|
|
|
{Title: "movie2", Year: 2020, Summary: "summary2"},
|
|
|
|
{Title: "movie3", Year: 2020, Summary: "summary3"},
|
|
|
|
}, nil
|
2023-12-22 16:31:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-23 12:08:58 +01:00
|
|
|
//func (t TMDB) Search(query string) ([]model.Movie, error) {
|
|
|
|
//
|
|
|
|
// results, err := t.c.GetSearchMovies(query, nil)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// movies := make([]model.Movie, len(results.Results))
|
|
|
|
// for i, result := range results.Results {
|
|
|
|
// movies[i], err = t.GetMovie(result.ID)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return movies, nil
|
|
|
|
//}
|
|
|
|
|
|
|
|
func (t TMDB) GetMovie(id int64) (model.Movie, error) {
|
2023-12-22 16:31:40 +01:00
|
|
|
result, err := t.c.GetMovieDetails(int(id), nil)
|
|
|
|
if err != nil {
|
2023-12-23 12:08:58 +01:00
|
|
|
return model.Movie{}, err
|
2023-12-22 16:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var year int
|
|
|
|
if release, err := time.Parse("2006-01-02", result.ReleaseDate); err == nil {
|
|
|
|
year = release.Year()
|
|
|
|
}
|
|
|
|
|
2023-12-23 12:08:58 +01:00
|
|
|
return model.Movie{
|
2023-12-22 16:31:40 +01:00
|
|
|
Title: result.Title,
|
|
|
|
TMDBID: result.ID,
|
|
|
|
Year: year,
|
|
|
|
Summary: result.Overview,
|
|
|
|
}, nil
|
|
|
|
|
2023-12-21 15:01:06 +01:00
|
|
|
}
|