Added tmdb config getter and made tmdbGet function

This commit is contained in:
2025-02-23 16:20:36 +11:00
parent 838d6264c9
commit a3e9ffb012
4 changed files with 89 additions and 45 deletions

32
tmdb/config.go Normal file
View File

@@ -0,0 +1,32 @@
package tmdb
import (
"encoding/json"
"github.com/pkg/errors"
)
type Config struct {
Image Image `json:"images"`
}
type Image struct {
BaseURL string `json:"base_url"`
SecureBaseURL string `json:"secure_base_url"`
BackdropSizes []string `json:"backdrop_sizes"`
LogoSizes []string `json:"logo_sizes"`
PosterSizes []string `json:"poster_sizes"`
ProfileSizes []string `json:"profile_sizes"`
StillSizes []string `json:"still_sizes"`
}
func GetConfig(token string) (*Config, error) {
url := "https://api.themoviedb.org/3/configuration"
data, err := tmdbGet(url, token)
if err != nil {
return nil, errors.Wrap(err, "tmdbGet")
}
config := Config{}
json.Unmarshal(data, &config)
return &config, nil
}

View File

@@ -3,33 +3,43 @@ package tmdb
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
)
type Movie struct {
Adult bool `json:"adult"`
Backdrop string `json:"backdrop_path"`
Collection string `json:"belongs_to_collection"`
Budget int `json:"budget"`
Genres []Genre `json:"genres"`
Homepage string `json:"homepage"`
ID int32 `json:"id"`
IMDbID string `json:"imdb_id"`
OriginalLanguage string `json:"original_language"`
OriginalTitle string `json:"original_title"`
Overview string `json:"overview"`
Popularity float32 `json:"popularity"`
Poster string `json:"poster_path"`
ProductionCompanies []ProductionCompany `json:"production_companies"`
ProductionCountries []ProductionCountry `json:"production_countries"`
ReleaseDate string `json:"release_date"`
Revenue int `json:"revenue"`
Runtime int `json:"runtime"`
SpokenLanguages []SpokenLanguage `json:"spoken_languages"`
Status string `json:"status"`
Tagline string `json:"tagline"`
Title string `json:"title"`
Video bool `json:"video"`
}
func GetMovie(id int32, token string) (*Movie, error) {
url := fmt.Sprintf("https://api.themoviedb.org/3/movie/%v?language=en-US", id)
req, err := http.NewRequest("GET", url, nil)
data, err := tmdbGet(url, token)
if err != nil {
return nil, errors.Wrap(err, "http.NewRequest")
}
req.Header.Add("accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "http.DefaultClient.Do")
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "io.ReadAll")
return nil, errors.Wrap(err, "tmdbGet")
}
movie := Movie{}
json.Unmarshal(body, &movie)
json.Unmarshal(data, &movie)
return &movie, nil
}

28
tmdb/request.go Normal file
View File

@@ -0,0 +1,28 @@
package tmdb
import (
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
)
func tmdbGet(url string, token string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrap(err, "http.NewRequest")
}
req.Header.Add("accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "http.DefaultClient.Do")
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "io.ReadAll")
}
return body, nil
}

View File

@@ -4,32 +4,6 @@ import (
// "encoding/json"
)
type Movie struct {
Adult bool `json:"adult"`
Backdrop string `json:"backdrop_path"`
Collection string `json:"belongs_to_collection"`
Budget int `json:"budget"`
Genres []Genre `json:"genres"`
Homepage string `json:"homepage"`
ID int32 `json:"id"`
IMDbID string `json:"imdb_id"`
OriginalLanguage string `json:"original_language"`
OriginalTitle string `json:"original_title"`
Overview string `json:"overview"`
Popularity float32 `json:"popularity"`
Poster string `json:"poster_path"`
ProductionCompanies []ProductionCompany `json:"production_companies"`
ProductionCountries []ProductionCountry `json:"production_countries"`
ReleaseDate string `json:"release_date"`
Revenue int `json:"revenue"`
Runtime int `json:"runtime"`
SpokenLanguages []SpokenLanguage `json:"spoken_languages"`
Status string `json:"status"`
Tagline string `json:"tagline"`
Title string `json:"title"`
Video bool `json:"video"`
}
type Genre struct {
ID int `json:"id"`
Name string `json:"name"`