159 lines
3.8 KiB
Go

package config
import (
"io/ioutil"
"os"
polochon "github.com/odwrtw/polochon/lib"
"github.com/odwrtw/polochon/modules/eztv"
"github.com/odwrtw/polochon/modules/tmdb"
"github.com/odwrtw/polochon/modules/trakttv"
"github.com/odwrtw/polochon/modules/tvdb"
"github.com/odwrtw/polochon/modules/yts"
"gopkg.in/yaml.v2"
)
// Config represents the Config of the canape app
type Config struct {
Authorizer AuthorizerConfig `yaml:"authorizer"`
PGDSN string `yaml:"pgdsn"`
Port string `yaml:"listen_port"`
TraktTVClientID string `yaml:"trakttv_client_id"`
PublicDir string `yaml:"public_dir"`
// TODO improve the detailers and torrenters configurations
TmdbAPIKey string `yaml:"tmdb_api_key"`
MovieExplorers []polochon.Explorer
MovieDetailers []polochon.Detailer
MovieTorrenters []polochon.Torrenter
MovieSearchers []polochon.Searcher
ShowExplorers []polochon.Explorer
ShowDetailers []polochon.Detailer
ShowTorrenters []polochon.Torrenter
ShowSearchers []polochon.Searcher
}
// AuthorizerConfig is the config for the authentication
type AuthorizerConfig struct {
Pepper string `yaml:"pepper"`
Cost int `yaml:"cost"`
Secret string `yaml:"secret"`
}
// Load loads a config file from a path and return a Config object
func Load(path string) (*Config, error) {
// Open the file
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
cf := &Config{}
// Read it
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
// Unmarshal its content
err = yaml.Unmarshal(b, cf)
if err != nil {
return nil, err
}
// Initialize the default values
// Default movie detailers
cf.MovieDetailers = []polochon.Detailer{}
if cf.TmdbAPIKey != "" {
d, err := tmdb.New(
&tmdb.Params{
ApiKey: cf.TmdbAPIKey,
},
)
if err != nil {
return nil, err
}
cf.MovieDetailers = append(cf.MovieDetailers, d)
}
// Default movie explorers
cf.MovieExplorers = []polochon.Explorer{}
ytsExp, err := yts.NewExplorer()
if err != nil {
return nil, err
}
cf.MovieExplorers = append(cf.MovieExplorers, ytsExp)
if cf.TraktTVClientID != "" {
traktExp, err := trakttv.NewExplorer(&trakttv.Params{
ClientID: cf.TraktTVClientID,
})
if err != nil {
return nil, err
}
cf.MovieExplorers = append(cf.MovieExplorers, traktExp)
}
// Default movie torrenters
cf.MovieTorrenters = []polochon.Torrenter{}
d, err := yts.New()
if err != nil {
return nil, err
}
cf.MovieTorrenters = append(cf.MovieTorrenters, d)
// Default movie searchers
cf.MovieSearchers = []polochon.Searcher{}
movieSearcher, err := yts.NewSearcher()
if err != nil {
return nil, err
}
cf.MovieSearchers = append(cf.MovieSearchers, movieSearcher)
// Default show searchers
cf.ShowSearchers = []polochon.Searcher{}
showSearcher, err := eztv.NewSearcher()
if err != nil {
return nil, err
}
cf.ShowSearchers = append(cf.ShowSearchers, showSearcher)
// Default show explorers
cf.ShowExplorers = []polochon.Explorer{}
eztvExp, err := eztv.NewExplorer()
if err != nil {
return nil, err
}
cf.ShowExplorers = append(cf.ShowExplorers, eztvExp)
if cf.TraktTVClientID != "" {
traktExp, err := trakttv.NewExplorer(&trakttv.Params{
ClientID: cf.TraktTVClientID,
})
if err != nil {
return nil, err
}
cf.ShowExplorers = append(cf.ShowExplorers, traktExp)
}
// Default show detailers
cf.ShowDetailers = []polochon.Detailer{}
showDetailer, err := tvdb.NewDetailer()
if err != nil {
return nil, err
}
cf.ShowDetailers = append(cf.ShowDetailers, showDetailer)
// Default show torrenters
cf.ShowTorrenters = []polochon.Torrenter{}
showTorrenter, err := eztv.New()
if err != nil {
return nil, err
}
cf.ShowTorrenters = append(cf.ShowTorrenters, showTorrenter)
return cf, nil
}