Add tmdb in the config file and use it as detailer

Until the detailers are available in the configuration file, let's use
tmdb as the default detailer for the movies
This commit is contained in:
Grégoire Delattre 2016-11-06 23:38:14 +01:00
parent b4be290888
commit b9bfa5f625
4 changed files with 40 additions and 18 deletions

View File

@ -5,6 +5,7 @@ authorizer:
cost: 10 cost: 10
pgdsn: postgres://test:test@127.0.0.1:5432/dev?sslmode=disable pgdsn: postgres://test:test@127.0.0.1:5432/dev?sslmode=disable
trakttv_client_id: my_trakttv_client_id trakttv_client_id: my_trakttv_client_id
tmdb_api_key: my_tmdb_key
listen_port: 3000 listen_port: 3000
templates_dir: build/templates templates_dir: build/templates
public_dir: build/public public_dir: build/public

View File

@ -4,6 +4,9 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
polochon "github.com/odwrtw/polochon/lib"
"github.com/odwrtw/polochon/modules/tmdb"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -14,6 +17,10 @@ type Config struct {
TraktTVClientID string `yaml:"trakttv_client_id"` TraktTVClientID string `yaml:"trakttv_client_id"`
TemplatesDir string `yaml:"templates_dir"` TemplatesDir string `yaml:"templates_dir"`
PublicDir string `yaml:"public_dir"` PublicDir string `yaml:"public_dir"`
// TODO improve the detailers configurations
TmdbAPIKey string `yaml:"tmdb_api_key"`
MovieDetailers []polochon.Detailer
} }
type AuthorizerConfig struct { type AuthorizerConfig struct {
@ -42,5 +49,14 @@ func Load(path string) (*Config, error) {
return nil, err return nil, err
} }
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)
}
return cf, nil return cf, nil
} }

View File

@ -5,11 +5,10 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"path/filepath"
"github.com/gorilla/mux"
"github.com/odwrtw/papi" "github.com/odwrtw/papi"
"github.com/odwrtw/polochon/lib" "github.com/odwrtw/polochon/lib"
"github.com/odwrtw/polochon/modules/mock"
traktdetailer "github.com/odwrtw/polochon/modules/trakttv" traktdetailer "github.com/odwrtw/polochon/modules/trakttv"
"github.com/odwrtw/trakttv" "github.com/odwrtw/trakttv"
@ -79,16 +78,8 @@ func FromPolochon(env *web.Env, w http.ResponseWriter, r *http.Request) error {
return err return err
} }
//TODO use configurable detailer
// detailer, err := tmdb.New(&tmdb.Params{"57be344f84917b3f32c68a678f1482eb"})
detailer, _ := mock.NewDetailer(nil)
if err != nil {
return err
}
for _, m := range movies { for _, m := range movies {
m.Detailers = []polochon.Detailer{detailer} err := m.GetDetails(env)
err := m.GetDetails(env.Database, filepath.Join(env.Config.PublicDir, "img", "movies"), env.Log)
if err != nil { if err != nil {
env.Log.Error(err) env.Log.Error(err)
} }
@ -128,7 +119,7 @@ func ExplorePopular(env *web.Env, w http.ResponseWriter, r *http.Request) error
for _, m := range tmovies { for _, m := range tmovies {
movie := New(m.IDs.ImDB) movie := New(m.IDs.ImDB)
movie.Detailers = []polochon.Detailer{detailer} movie.Detailers = []polochon.Detailer{detailer}
err := movie.GetDetails(env.Database, filepath.Join(env.Config.PublicDir, "img", "movies"), env.Log) err := movie.GetDetails(env)
if err != nil { if err != nil {
env.Log.Error(err) env.Log.Error(err)
continue continue

View File

@ -77,35 +77,49 @@ func (m *Movie) Get(db *sqlx.DB) error {
// GetDetails retrieves details for the movie, first try to // GetDetails retrieves details for the movie, first try to
// get info from db, if not exists, use polochon.Detailer // get info from db, if not exists, use polochon.Detailer
// and save informations in the database for future use // and save informations in the database for future use
func (m *Movie) GetDetails(db *sqlx.DB, imgPath string, log *logrus.Entry) error { func (m *Movie) GetDetails(env *web.Env) error {
m.Detailers = env.Config.MovieDetailers
log := env.Log.WithFields(logrus.Fields{
"imdb_id": m.ImdbID,
"function": "movies.GetDetails",
})
var err error var err error
err = m.Get(db) err = m.Get(env.Database)
if err == nil { if err == nil {
// found ok log.Debug("movie found in database")
return nil return nil
} }
if err != ErrNotFound { if err != ErrNotFound {
// Unexpected error // Unexpected error
log.Debug("movie not found in database")
return err return err
} }
// so we got ErrNotFound so GetDetails from a detailer // so we got ErrNotFound so GetDetails from a detailer
err = m.Movie.GetDetails(log) err = m.Movie.GetDetails(env.Log)
if err != nil { if err != nil {
return err return err
} }
err = m.Add(db) log.Debug("got details from detailers")
err = m.Add(env.Database)
if err != nil { if err != nil {
return err return err
} }
log.Debug("movie added in database")
// Download poster // Download poster
err = web.Download(m.Thumb, filepath.Join(imgPath, m.ImdbID+".jpg")) imgPath := filepath.Join(env.Config.PublicDir, "img", "movies", m.ImdbID+".jpg")
err = web.Download(m.Thumb, imgPath)
if err != nil { if err != nil {
return err return err
} }
log.Debug("poster downloaded")
return nil return nil
} }