mirror of
https://github.com/simon-ding/polaris.git
synced 2026-03-05 09:00:45 +08:00
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package tmdb
|
|
|
|
import (
|
|
"polaris/log"
|
|
|
|
tmdb "github.com/cyruzin/golang-tmdb"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Client struct {
|
|
apiKey string
|
|
tmdbClient *tmdb.Client
|
|
}
|
|
|
|
func NewClient(apiKey string) (*Client, error) {
|
|
tmdbClient, err := tmdb.Init(apiKey)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "new tmdb client")
|
|
}
|
|
|
|
return &Client{
|
|
apiKey: apiKey,
|
|
tmdbClient: tmdbClient,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) GetTvDetails(id int, language string) (*tmdb.TVDetails, error) {
|
|
log.Infof("tv id %d, language %s", id, language)
|
|
language = wrapLanguage(language)
|
|
d, err := c.tmdbClient.GetTVDetails(id, withLangOption(language))
|
|
return d, err
|
|
}
|
|
|
|
func (c *Client) SearchTvShow(query string, lang string) (*tmdb.SearchTVShows, error) {
|
|
r, err := c.tmdbClient.GetSearchTVShow(query, withLangOption(lang))
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "tmdb search tv")
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func (c *Client) GetEposideDetail(id, seasonNumber, eposideNumber int, language string) (*tmdb.TVEpisodeDetails, error) {
|
|
d, err := c.tmdbClient.GetTVEpisodeDetails(id, seasonNumber, eposideNumber, withLangOption(language))
|
|
return d, err
|
|
}
|
|
|
|
func (c *Client) GetSeasonDetails(id, seasonNumber int, language string) (*tmdb.TVSeasonDetails, error) {
|
|
return c.tmdbClient.GetTVSeasonDetails(id, seasonNumber, withLangOption(language))
|
|
}
|
|
|
|
func (c *Client) GetTVAlternativeTitles(id int, language string) (*tmdb.TVAlternativeTitles, error) {
|
|
return c.tmdbClient.GetTVAlternativeTitles(id, withLangOption(language))
|
|
}
|
|
|
|
func wrapLanguage(lang string) string {
|
|
if lang == "" {
|
|
lang = "zh-CN"
|
|
}
|
|
return lang
|
|
}
|
|
|
|
func withLangOption(language string) map[string]string {
|
|
language = wrapLanguage(language)
|
|
return map[string]string{
|
|
"language": language,
|
|
}
|
|
}
|