diff --git a/config/config.go b/config/config.go index c974639..f3e1012 100644 --- a/config/config.go +++ b/config/config.go @@ -1,14 +1,15 @@ package config import ( - "errors" "fmt" "os" "time" "projectreshoot/logging" + "projectreshoot/tmdb" "github.com/joho/godotenv" + "github.com/pkg/errors" "github.com/rs/zerolog" ) @@ -31,6 +32,7 @@ type Config struct { LogOutput string // "file", "console", or "both". Defaults to console LogDir string // Path to create log files TMDBToken string // Read access token for TMDB API + TMDBConfig *tmdb.Config // Config data for interfacing with TMDB } // Load the application configuration and get a pointer to the Config object @@ -78,6 +80,10 @@ func GetConfig(args map[string]string) (*Config, error) { if logOutput != "both" && logOutput != "console" && logOutput != "file" { logOutput = "console" } + tmdbcfg, err := tmdb.GetConfig(os.Getenv("TMDB_API_TOKEN")) + if err != nil { + return nil, errors.Wrap(err, "tmdb.GetConfig") + } config := &Config{ Host: host, @@ -98,6 +104,7 @@ func GetConfig(args map[string]string) (*Config, error) { LogOutput: logOutput, LogDir: GetEnvDefault("LOG_DIR", ""), TMDBToken: os.Getenv("TMDB_API_TOKEN"), + TMDBConfig: tmdbcfg, } if config.SecretKey == "" && args["dbver"] != "true" { diff --git a/tmdb/functions.go b/tmdb/functions.go new file mode 100644 index 0000000..52663fe --- /dev/null +++ b/tmdb/functions.go @@ -0,0 +1,23 @@ +package tmdb + +import ( + "fmt" + "net/url" + "path" +) + +func FormatRuntime(minutes int) string { + hours := minutes / 60 + mins := minutes % 60 + return fmt.Sprintf("%dh %02dm", hours, mins) +} + +func GetPoster(image *Image, size, imgpath string) string { + base, err := url.Parse(image.SecureBaseURL) + if err != nil { + return "" + } + fullPath := path.Join(base.Path, size, imgpath) + base.Path = fullPath + return base.String() +}