Files
Watcharr/server/media/tmdb/tmdb.go
T
IRHM 7b65c8af0f tmdb: Strip TMDB prefix from all struct names
This prefix is a remnant from when the structs where in the "main" package (from before the server refactor).
2026-08-03 01:14:02 +00:00

97 lines
2.1 KiB
Go

package tmdb
import (
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"net/url"
"time"
gocache "github.com/robfig/go-cache"
"github.com/sbondCo/Watcharr/database/entity"
)
// TODO The *WithWatched structs likely need to go in the watched package (or with go 1.25 can we
// fix needing so many extra structs for the *WithWatched types and functions)
var ContentStore = gocache.New(time.Hour*24, time.Minute)
type ContentProvider interface {
CacheContentShow(content ShowDetails, onlyUpdate bool) (entity.Content, error)
CacheContentMovie(content MovieDetails, onlyUpdate bool) (entity.Content, error)
}
type TMDB struct {
Key string
contentProvider ContentProvider
}
func NewTMDB(key string) *TMDB {
return &TMDB{
Key: key,
}
}
func (t *TMDB) AddContentProvider(contentProvider ContentProvider) {
t.contentProvider = contentProvider
}
func (t *TMDB) GetKey() string {
if t.Key != "" {
return t.Key //Config.TMDB_KEY
}
return "d047fa61d926371f277e7a83c9c4ff2c"
}
func (t *TMDB) apiRequest(ep string, p map[string]string) ([]byte, error) {
slog.Debug("tmdbAPIRequest", "endpoint", ep, "params", p)
base, err := url.Parse("https://api.themoviedb.org/3")
if err != nil {
return nil, errors.New("failed to parse api uri")
}
// Path params
base.Path += ep
// Query params
params := url.Values{}
params.Add("api_key", t.GetKey())
params.Add("language", "en-US")
for k, v := range p {
params.Add(k, v)
}
// Add params to url
base.RawQuery = params.Encode()
// Run get request
res, err := http.Get(base.String())
if err != nil {
return nil, err
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
slog.Error("TMDB non 200 status code:", "status_code", res.StatusCode)
return nil, errors.New(string(body))
}
return body, nil
}
func (t *TMDB) req(ep string, p map[string]string, resp interface{}) error {
body, err := t.apiRequest(ep, p)
if err != nil {
return err
}
err = json.Unmarshal([]byte(body), &resp)
if err != nil {
return err
}
return nil
}