110 lines
2.5 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/tvdb"
"github.com/odwrtw/polochon/modules/yts"
"gopkg.in/yaml.v2"
)
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"`
MovieDetailers []polochon.Detailer
MovieTorrenters []polochon.Torrenter
MovieSearchers []polochon.Searcher
ShowDetailers []polochon.Detailer
ShowTorrenters []polochon.Torrenter
ShowSearchers []polochon.Searcher
}
type AuthorizerConfig struct {
Pepper string `yaml:"pepper"`
Cost int `yaml:"cost"`
Secret string `yaml:"secret"`
}
func Load(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
cf := &Config{}
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(b, cf)
if err != nil {
return nil, err
}
// Default movie detailers
cf.MovieDetailers = []polochon.Detailer{}
if cf.TmdbAPIKey != "" {
d, err := tmdb.New(&tmdb.Params{cf.TmdbAPIKey})
if err != nil {
return nil, err
}
cf.MovieDetailers = append(cf.MovieDetailers, d)
}
// 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 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
}