79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
|
|
package tmdb
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"math/rand/v2"
|
||
|
|
"os"
|
||
|
|
"strconv"
|
||
|
|
|
||
|
|
sdk "github.com/cyruzin/golang-tmdb"
|
||
|
|
)
|
||
|
|
|
||
|
|
var ErrMissingKey = errors.New("missing TMDB key")
|
||
|
|
|
||
|
|
func FetchMovieDetails(inf Info) (*sdk.MovieDetails, error) {
|
||
|
|
if inf.Type != Movie {
|
||
|
|
return nil, fmt.Errorf("%s is not a movie", inf.Type)
|
||
|
|
}
|
||
|
|
if inf.ID < 1 {
|
||
|
|
return nil, fmt.Errorf("%d is not a valid TMDB id", inf.ID)
|
||
|
|
}
|
||
|
|
|
||
|
|
sdk, err := GetClient()
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return sdk.GetMovieDetails(inf.ID, map[string]string{
|
||
|
|
"cache": strconv.Itoa(rand.Int()),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func FetchEpisodeDetails(inf Info) (TVDetails, error) {
|
||
|
|
var det TVDetails
|
||
|
|
if inf.Type != Series {
|
||
|
|
return det, fmt.Errorf("%s is not an episode", inf.Type)
|
||
|
|
}
|
||
|
|
if inf.ID < 1 {
|
||
|
|
return det, fmt.Errorf("%d is not a valid TMDB id", inf.ID)
|
||
|
|
}
|
||
|
|
if inf.Season < 1 {
|
||
|
|
return det, fmt.Errorf("%d is not a valid season", inf.Season)
|
||
|
|
}
|
||
|
|
if inf.Episode < 1 {
|
||
|
|
return det, fmt.Errorf("%d is not a valid episode", inf.Episode)
|
||
|
|
}
|
||
|
|
|
||
|
|
sdk, err := GetClient()
|
||
|
|
if err != nil {
|
||
|
|
return det, err
|
||
|
|
}
|
||
|
|
|
||
|
|
epDet, err := sdk.GetTVEpisodeDetails(inf.ID, inf.Season, inf.Episode, map[string]string{
|
||
|
|
"cache": strconv.Itoa(rand.Int()),
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return det, err
|
||
|
|
}
|
||
|
|
|
||
|
|
showDet, err := sdk.GetTVDetails(inf.ID, map[string]string{
|
||
|
|
"cache": strconv.Itoa(rand.Int()),
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return det, err
|
||
|
|
}
|
||
|
|
|
||
|
|
det.Episode = epDet
|
||
|
|
det.Series = showDet
|
||
|
|
|
||
|
|
return det, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetClient() (*sdk.Client, error) {
|
||
|
|
key := os.Getenv("TMDB_KEY")
|
||
|
|
if key == "" {
|
||
|
|
return nil, ErrMissingKey
|
||
|
|
}
|
||
|
|
return sdk.Init(key)
|
||
|
|
}
|