diff --git a/config/config.go b/config/config.go index 0b9bc90..c974639 100644 --- a/config/config.go +++ b/config/config.go @@ -30,6 +30,7 @@ type Config struct { LogLevel zerolog.Level // Log level for global logging. Defaults to info LogOutput string // "file", "console", or "both". Defaults to console LogDir string // Path to create log files + TMDBToken string // Read access token for TMDB API } // Load the application configuration and get a pointer to the Config object @@ -96,11 +97,15 @@ func GetConfig(args map[string]string) (*Config, error) { LogLevel: logLevel, LogOutput: logOutput, LogDir: GetEnvDefault("LOG_DIR", ""), + TMDBToken: os.Getenv("TMDB_API_TOKEN"), } if config.SecretKey == "" && args["dbver"] != "true" { return nil, errors.New("Envar not set: SECRET_KEY") } + if config.TMDBToken == "" && args["dbver"] != "true" { + return nil, errors.New("Envar not set: TMDB_API_TOKEN") + } return config, nil } diff --git a/tmdb/movie.go b/tmdb/movie.go new file mode 100644 index 0000000..8e081f5 --- /dev/null +++ b/tmdb/movie.go @@ -0,0 +1,35 @@ +package tmdb + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/pkg/errors" +) + +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) + 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") + } + movie := Movie{} + json.Unmarshal(body, &movie) + return &movie, nil +} diff --git a/tmdb/structs.go b/tmdb/structs.go new file mode 100644 index 0000000..fd7c04a --- /dev/null +++ b/tmdb/structs.go @@ -0,0 +1,54 @@ +package tmdb + +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"` +} + +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"` +}