2021-02-23 16:01:29 +00:00
|
|
|
package tmdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-03-14 18:01:40 +00:00
|
|
|
"net/url"
|
2021-02-23 16:01:29 +00:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const apiKey = "9fd90530b11447f5646f8e6fb4733fb4"
|
|
|
|
const baseUrl = "https://api.themoviedb.org/3/"
|
|
|
|
const pictureBase = "https://image.tmdb.org/t/p/w500"
|
|
|
|
|
|
|
|
type VideoTMDB struct {
|
2021-09-28 08:52:18 +00:00
|
|
|
Thumbnail string
|
|
|
|
Overview string
|
|
|
|
Title string
|
|
|
|
ReleaseDate string
|
|
|
|
GenreIds []int
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 19:23:51 +00:00
|
|
|
type TVShowTMDB struct {
|
|
|
|
Thumbnail string
|
|
|
|
Overview string
|
|
|
|
GenreIds []int
|
|
|
|
}
|
|
|
|
|
2021-02-23 16:01:29 +00:00
|
|
|
type tmdbVidResult struct {
|
2021-06-06 09:49:42 +00:00
|
|
|
PosterPath *string `json:"poster_path"`
|
|
|
|
Adult bool `json:"adult"`
|
|
|
|
Overview string `json:"overview"`
|
|
|
|
ReleaseDate string `json:"release_date"`
|
|
|
|
GenreIds []int `json:"genre_ids"`
|
|
|
|
Id int `json:"id"`
|
|
|
|
OriginalTitle string `json:"original_title"`
|
|
|
|
OriginalLanguage string `json:"original_language"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
BackdropPath *string `json:"backdrop_path"`
|
|
|
|
Popularity int `json:"popularity"`
|
|
|
|
VoteCount int `json:"vote_count"`
|
|
|
|
Video bool `json:"video"`
|
|
|
|
VoteAverage int `json:"vote_average"`
|
2021-04-23 19:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type tmdbTvResult struct {
|
2021-06-06 09:49:42 +00:00
|
|
|
PosterPath *string `json:"poster_path"`
|
2021-04-23 19:37:57 +00:00
|
|
|
Popularity int `json:"popularity"`
|
|
|
|
Id int `json:"id"`
|
2021-06-06 09:49:42 +00:00
|
|
|
BackdropPath *string `json:"backdrop_path"`
|
2021-04-23 19:37:57 +00:00
|
|
|
VoteAverage int `json:"vote_average"`
|
|
|
|
Overview string `json:"overview"`
|
|
|
|
FirstAirDate string `json:"first_air_date"`
|
|
|
|
OriginCountry []string `json:"origin_country"`
|
|
|
|
GenreIds []int `json:"genre_ids"`
|
|
|
|
OriginalLanguage string `json:"original_language"`
|
|
|
|
VoteCount int `json:"vote_count"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
OriginalName string `json:"original_name"`
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TMDBGenre struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchVideo(MovieName string, year int) *VideoTMDB {
|
2021-03-14 18:01:40 +00:00
|
|
|
fmt.Printf("Searching TMDB for: Moviename: %s, year:%d \n", MovieName, year)
|
|
|
|
queryURL := fmt.Sprintf("%ssearch/movie?api_key=%s&query=%s", baseUrl, apiKey, url.QueryEscape(MovieName))
|
|
|
|
resp, err := http.Get(queryURL)
|
2021-02-23 16:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var t struct {
|
|
|
|
Results []tmdbVidResult
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &t)
|
|
|
|
|
|
|
|
fmt.Println(len(t.Results))
|
|
|
|
|
2021-03-14 16:29:33 +00:00
|
|
|
// if there was no match with tmdb return 0
|
|
|
|
if len(t.Results) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 16:01:29 +00:00
|
|
|
var tmdbVid tmdbVidResult
|
|
|
|
if year != -1 {
|
|
|
|
for _, result := range t.Results {
|
|
|
|
r, _ := regexp.Compile(fmt.Sprintf(`^%d-[0-9]{2}?-[0-9]{2}?$`, year))
|
2021-04-23 19:37:57 +00:00
|
|
|
if r.MatchString(result.ReleaseDate) {
|
2021-02-23 16:01:29 +00:00
|
|
|
tmdbVid = result
|
|
|
|
// continue parsing
|
|
|
|
goto cont
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there is no match use first one
|
|
|
|
tmdbVid = t.Results[0]
|
|
|
|
} else {
|
|
|
|
tmdbVid = t.Results[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// continue label
|
|
|
|
cont:
|
|
|
|
|
2021-06-06 09:49:42 +00:00
|
|
|
var thumbnail = ""
|
|
|
|
if tmdbVid.PosterPath != nil {
|
|
|
|
pic := fetchPoster(*tmdbVid.PosterPath)
|
|
|
|
if pic != nil {
|
|
|
|
thumbnail = *pic
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 16:01:29 +00:00
|
|
|
|
|
|
|
result := VideoTMDB{
|
2021-09-28 08:52:18 +00:00
|
|
|
Thumbnail: thumbnail,
|
|
|
|
Overview: tmdbVid.Overview,
|
|
|
|
Title: tmdbVid.Title,
|
|
|
|
ReleaseDate: tmdbVid.ReleaseDate,
|
|
|
|
GenreIds: tmdbVid.GenreIds,
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2021-04-23 19:23:51 +00:00
|
|
|
func SearchTVShow(Name string) *TVShowTMDB {
|
|
|
|
fmt.Printf("Searching TMDB for: TVShow: %s\n", Name)
|
|
|
|
queryURL := fmt.Sprintf("%ssearch/tv?api_key=%s&query=%s", baseUrl, apiKey, url.QueryEscape(Name))
|
|
|
|
resp, err := http.Get(queryURL)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var t struct {
|
|
|
|
Results []tmdbTvResult `json:"results"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &t)
|
|
|
|
|
|
|
|
fmt.Println(len(t.Results))
|
|
|
|
|
|
|
|
if len(t.Results) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := TVShowTMDB{
|
|
|
|
Thumbnail: "",
|
|
|
|
Overview: t.Results[0].Overview,
|
|
|
|
GenreIds: t.Results[0].GenreIds,
|
|
|
|
}
|
|
|
|
|
2021-06-06 09:49:42 +00:00
|
|
|
if t.Results[0].PosterPath != nil {
|
|
|
|
pic := fetchPoster(*t.Results[0].PosterPath)
|
|
|
|
if pic != nil {
|
|
|
|
res.Thumbnail = *pic
|
|
|
|
}
|
2021-04-23 19:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &res
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchPoster(posterPath string) *string {
|
2021-04-23 19:37:57 +00:00
|
|
|
posterURL := fmt.Sprintf("%s%s", pictureBase, posterPath)
|
|
|
|
resp, err := http.Get(posterURL)
|
2021-02-23 16:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
backpic64 := "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(body)
|
|
|
|
return &backpic64
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmdbGenres *[]TMDBGenre
|
|
|
|
|
|
|
|
func fetchGenres() *[]TMDBGenre {
|
2021-04-23 19:37:57 +00:00
|
|
|
posterURL := fmt.Sprintf("%sgenre/movie/list?api_key=%s", baseUrl, apiKey)
|
|
|
|
resp, err := http.Get(posterURL)
|
2021-02-23 16:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:30:18 +00:00
|
|
|
type RespType struct {
|
|
|
|
Genres []TMDBGenre
|
|
|
|
}
|
|
|
|
|
|
|
|
var t RespType
|
2021-02-23 16:01:29 +00:00
|
|
|
err = json.Unmarshal(body, &t)
|
|
|
|
|
2021-09-27 17:30:18 +00:00
|
|
|
return &t.Genres
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGenres() *[]TMDBGenre {
|
|
|
|
// if generes are nil fetch them once
|
|
|
|
if tmdbGenres == nil {
|
|
|
|
tmdbGenres = fetchGenres()
|
|
|
|
}
|
|
|
|
return tmdbGenres
|
|
|
|
}
|