Compare commits

...

1 Commits

Author SHA1 Message Date
c4574e32c7 imported tmdb module 2026-01-01 20:42:50 +11:00
10 changed files with 352 additions and 0 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
}

54
tmdb/credits.go Normal file
View File

@@ -0,0 +1,54 @@
package tmdb
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
type Credits struct {
ID int32 `json:"id"`
Cast []Cast `json:"cast"`
Crew []Crew `json:"crew"`
}
type Cast struct {
Adult bool `json:"adult"`
Gender int `json:"gender"`
ID int32 `json:"id"`
KnownFor string `json:"known_for_department"`
Name string `json:"name"`
OriginalName string `json:"original_name"`
Popularity int `json:"popularity"`
Profile string `json:"profile_path"`
CastID int32 `json:"cast_id"`
Character string `json:"character"`
CreditID string `json:"credit_id"`
Order int `json:"order"`
}
type Crew struct {
Adult bool `json:"adult"`
Gender int `json:"gender"`
ID int32 `json:"id"`
KnownFor string `json:"known_for_department"`
Name string `json:"name"`
OriginalName string `json:"original_name"`
Popularity int `json:"popularity"`
Profile string `json:"profile_path"`
CreditID string `json:"credit_id"`
Department string `json:"department"`
Job string `json:"job"`
}
func GetCredits(movieid int32, token string) (*Credits, error) {
url := fmt.Sprintf("https://api.themoviedb.org/3/movie/%v/credits?language=en-US", movieid)
data, err := tmdbGet(url, token)
if err != nil {
return nil, errors.Wrap(err, "tmdbGet")
}
credits := Credits{}
json.Unmarshal(data, &credits)
return &credits, nil
}

41
tmdb/crew_functions.go Normal file
View File

@@ -0,0 +1,41 @@
package tmdb
import "sort"
type BilledCrew struct {
Name string
Roles []string
}
func (credits *Credits) BilledCrew() []BilledCrew {
crewmap := make(map[string][]string)
billedcrew := []BilledCrew{}
for _, crew := range credits.Crew {
if crew.Job == "Director" ||
crew.Job == "Screenplay" ||
crew.Job == "Writer" ||
crew.Job == "Novel" ||
crew.Job == "Story" {
crewmap[crew.Name] = append(crewmap[crew.Name], crew.Job)
}
}
for name, jobs := range crewmap {
billedcrew = append(billedcrew, BilledCrew{Name: name, Roles: jobs})
}
for i := range billedcrew {
sort.Strings(billedcrew[i].Roles)
}
sort.Slice(billedcrew, func(i, j int) bool {
return billedcrew[i].Roles[0] < billedcrew[j].Roles[0]
})
return billedcrew
}
func (billedcrew *BilledCrew) FRoles() string {
jobs := ""
for _, job := range billedcrew.Roles {
jobs += job + ", "
}
return jobs[:len(jobs)-2]
}

5
tmdb/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module git.haelnorr.com/h/golib/tmdb
go 1.25.5
require github.com/pkg/errors v0.9.1

2
tmdb/go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

45
tmdb/movie.go Normal file
View File

@@ -0,0 +1,45 @@
package tmdb
import (
"encoding/json"
"fmt"
"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)
data, err := tmdbGet(url, token)
if err != nil {
return nil, errors.Wrap(err, "tmdbGet")
}
movie := Movie{}
json.Unmarshal(data, &movie)
return &movie, nil
}

42
tmdb/movie_functions.go Normal file
View File

@@ -0,0 +1,42 @@
package tmdb
import (
"fmt"
"net/url"
"path"
)
func (movie *Movie) FRuntime() string {
hours := movie.Runtime / 60
mins := movie.Runtime % 60
return fmt.Sprintf("%dh %02dm", hours, mins)
}
func (movie *Movie) GetPoster(image *Image, size string) string {
base, err := url.Parse(image.SecureBaseURL)
if err != nil {
return ""
}
fullPath := path.Join(base.Path, size, movie.Poster)
base.Path = fullPath
return base.String()
}
func (movie *Movie) ReleaseYear() string {
if movie.ReleaseDate == "" {
return ""
} else {
return "(" + movie.ReleaseDate[:4] + ")"
}
}
func (movie *Movie) FGenres() string {
genres := ""
for _, genre := range movie.Genres {
genres += genre.Name + ", "
}
if len(genres) > 2 {
return genres[:len(genres)-2]
}
return genres
}

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
}

79
tmdb/search.go Normal file
View File

@@ -0,0 +1,79 @@
package tmdb
import (
"encoding/json"
"fmt"
"net/url"
"path"
"github.com/pkg/errors"
)
type Result struct {
Page int `json:"page"`
TotalPages int `json:"total_pages"`
TotalResults int `json:"total_results"`
}
type ResultMovies struct {
Result
Results []ResultMovie `json:"results"`
}
type ResultMovie struct {
Adult bool `json:"adult"`
BackdropPath string `json:"backdrop_path"`
GenreIDs []int `json:"genre_ids"`
ID int32 `json:"id"`
OriginalLanguage string `json:"original_language"`
OriginalTitle string `json:"original_title"`
Overview string `json:"overview"`
Popularity int `json:"popularity"`
PosterPath string `json:"poster_path"`
ReleaseDate string `json:"release_date"`
Title string `json:"title"`
Video bool `json:"video"`
VoteAverage int `json:"vote_average"`
VoteCount int `json:"vote_count"`
}
func (movie *ResultMovie) GetPoster(image *Image, size string) string {
base, err := url.Parse(image.SecureBaseURL)
if err != nil {
return ""
}
fullPath := path.Join(base.Path, size, movie.PosterPath)
base.Path = fullPath
return base.String()
}
func (movie *ResultMovie) ReleaseYear() string {
if movie.ReleaseDate == "" {
return ""
} else {
return "(" + movie.ReleaseDate[:4] + ")"
}
}
// TODO: genres list https://developer.themoviedb.org/reference/genre-movie-list
// func (movie *ResultMovie) FGenres() string {
// genres := ""
// for _, genre := range movie.Genres {
// genres += genre.Name + ", "
// }
// return genres[:len(genres)-2]
// }
func SearchMovies(token string, query string, adult bool, page int) (*ResultMovies, error) {
url := "https://api.themoviedb.org/3/search/movie" +
fmt.Sprintf("?query=%s", url.QueryEscape(query)) +
fmt.Sprintf("&include_adult=%t", adult) +
fmt.Sprintf("&page=%v", page) +
"&language=en-US"
response, err := tmdbGet(url, token)
if err != nil {
return nil, errors.Wrap(err, "tmdbGet")
}
var results ResultMovies
json.Unmarshal(response, &results)
return &results, nil
}

24
tmdb/structs.go Normal file
View File

@@ -0,0 +1,24 @@
package tmdb
type Genre struct {
ID int `json:"id"`
Name string `json:"name"`
}
type ProductionCompany struct {
ID int `json:"id"`
Logo string `json:"logo_path"`
Name string `json:"name"`
OriginCountry string `json:"origin_country"`
}
type ProductionCountry struct {
ISO_3166_1 string `json:"iso_3166_1"`
Name string `json:"name"`
}
type SpokenLanguage struct {
EnglishName string `json:"english_name"`
ISO_639_1 string `json:"iso_639_1"`
Name string `json:"name"`
}