Allow movies.GetDetails to be forced

To check from the internet even if the movie is already in the database
This commit is contained in:
Grégoire Delattre 2016-11-07 16:51:02 +01:00
parent b9bfa5f625
commit 403ca081cc
2 changed files with 39 additions and 13 deletions

View File

@ -79,7 +79,7 @@ func FromPolochon(env *web.Env, w http.ResponseWriter, r *http.Request) error {
}
for _, m := range movies {
err := m.GetDetails(env)
err := m.GetDetails(env, false)
if err != nil {
env.Log.Error(err)
}
@ -119,7 +119,7 @@ func ExplorePopular(env *web.Env, w http.ResponseWriter, r *http.Request) error
for _, m := range tmovies {
movie := New(m.IDs.ImDB)
movie.Detailers = []polochon.Detailer{detailer}
err := movie.GetDetails(env)
err := movie.GetDetails(env, false)
if err != nil {
env.Log.Error(err)
continue

View File

@ -17,6 +17,11 @@ const (
VALUES (:imdbid, :title, :rating, :votes, :plot, :tmdbid, :year, :originaltitle, :runtime, :sorttitle, :tagline)
RETURNING id;`
updateMovieQuery = `
UPDATE movies
SET imdb_id=:imdbid, title=:title, rating=:rating, votes=:votes, plot=:plot, tmdb_id=:tmdbid, year=:year, original_title=:originaltitle, runtime=:runtime, sort_title=:sorttitle, tagline=:tagline
WHERE ID = :id;`
getMovieQueryByImdbID = `
SELECT
id, imdb_id AS imdbid, title, rating, votes, plot,
@ -74,25 +79,40 @@ func (m *Movie) Get(db *sqlx.DB) error {
return nil
}
// GetDetails retrieves details for the movie, first try to
// get info from db, if not exists, use polochon.Detailer
// and save informations in the database for future use
func (m *Movie) GetDetails(env *web.Env) error {
m.Detailers = env.Config.MovieDetailers
// GetDetails retrieves details for the movie, first try to get info from db,
// if not exists, use polochon.Detailer and save informations in the database
// for future use
//
// If force is used, the detailer will be used even if the movie is found in
// database
func (m *Movie) GetDetails(env *web.Env, force bool) error {
if len(m.Detailers) == 0 {
m.Detailers = env.Config.MovieDetailers
}
log := env.Log.WithFields(logrus.Fields{
"imdb_id": m.ImdbID,
"function": "movies.GetDetails",
})
// If the movie is not in db, we should add it, otherwise we should update
// it
var dbFunc func(db *sqlx.DB) error
var err error
err = m.Get(env.Database)
if err == nil {
switch err {
case nil:
log.Debug("movie found in database")
return nil
}
if err != ErrNotFound {
// Unexpected error
dbFunc = m.Update
if !force {
return nil
}
case ErrNotFound:
dbFunc = m.Add
log.Debug("movie not found in database")
default:
// Unexpected error
return err
}
@ -104,7 +124,7 @@ func (m *Movie) GetDetails(env *web.Env) error {
log.Debug("got details from detailers")
err = m.Add(env.Database)
err = dbFunc(env.Database)
if err != nil {
return err
}
@ -138,6 +158,12 @@ func (m *Movie) Add(db *sqlx.DB) error {
return nil
}
// Update a movie in the database
func (m *Movie) Update(db *sqlx.DB) error {
_, err := db.NamedQuery(updateMovieQuery, m)
return err
}
// Delete movie from database
func (m *Movie) Delete(db *sqlx.DB) error {
r, err := db.Exec(deleteMovieQuery, m.ID)