Return the movie URL from the API
This commit is contained in:
parent
fa48dcac5d
commit
884af0f8b8
@ -2,6 +2,7 @@ package movies
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
@ -50,7 +51,8 @@ var (
|
|||||||
type Movie struct {
|
type Movie struct {
|
||||||
sqly.BaseModel
|
sqly.BaseModel
|
||||||
polochon.Movie
|
polochon.Movie
|
||||||
PolochonURL string
|
PolochonURL string `json:"polochon_url"`
|
||||||
|
PosterURL string `json:"poster_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(imdbID string) *Movie {
|
func New(imdbID string) *Movie {
|
||||||
@ -62,12 +64,12 @@ func New(imdbID string) *Movie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get returns show details in database from id or imdbid or an error
|
// Get returns show details in database from id or imdbid or an error
|
||||||
func (m *Movie) Get(db *sqlx.DB) error {
|
func (m *Movie) Get(env *web.Env) error {
|
||||||
var err error
|
var err error
|
||||||
if m.ID != "" {
|
if m.ID != "" {
|
||||||
err = db.QueryRowx(getMovieQueryByID, m.ID).StructScan(m)
|
err = env.Database.QueryRowx(getMovieQueryByID, m.ID).StructScan(m)
|
||||||
} else if m.ImdbID != "" {
|
} else if m.ImdbID != "" {
|
||||||
err = db.QueryRowx(getMovieQueryByImdbID, m.ImdbID).StructScan(m)
|
err = env.Database.QueryRowx(getMovieQueryByImdbID, m.ImdbID).StructScan(m)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("Can't get movie details, you have to specify an ID or ImdbID")
|
err = fmt.Errorf("Can't get movie details, you have to specify an ID or ImdbID")
|
||||||
}
|
}
|
||||||
@ -77,6 +79,10 @@ func (m *Movie) Get(db *sqlx.DB) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the poster url
|
||||||
|
m.PosterURL = m.GetPosterURL(env)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +107,7 @@ func (m *Movie) GetDetails(env *web.Env, force bool) error {
|
|||||||
var dbFunc func(db *sqlx.DB) error
|
var dbFunc func(db *sqlx.DB) error
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
err = m.Get(env.Database)
|
err = m.Get(env)
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
log.Debug("movie found in database")
|
log.Debug("movie found in database")
|
||||||
@ -133,14 +139,16 @@ func (m *Movie) GetDetails(env *web.Env, force bool) error {
|
|||||||
log.Debug("movie added in database")
|
log.Debug("movie added in database")
|
||||||
|
|
||||||
// Download poster
|
// Download poster
|
||||||
imgPath := filepath.Join(env.Config.PublicDir, "img", "movies", m.ImdbID+".jpg")
|
err = web.Download(m.Thumb, m.imgFile(env))
|
||||||
err = web.Download(m.Thumb, imgPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("poster downloaded")
|
log.Debug("poster downloaded")
|
||||||
|
|
||||||
|
// Set the poster url
|
||||||
|
m.PosterURL = m.GetPosterURL(env)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,3 +185,23 @@ func (m *Movie) Delete(db *sqlx.DB) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// imgURL returns the default image url
|
||||||
|
func (m *Movie) imgURL(env *web.Env) string {
|
||||||
|
return fmt.Sprintf("img/movies/%s.jpg", m.ImdbID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// imgFile returns the image location on disk
|
||||||
|
func (m *Movie) imgFile(env *web.Env) string {
|
||||||
|
return filepath.Join(env.Config.PublicDir, m.imgURL(env))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPosterURL returns the image URL or the default image if the poster is not yet downloaded
|
||||||
|
func (m *Movie) GetPosterURL(env *web.Env) string {
|
||||||
|
// Check if the movie image exists
|
||||||
|
if _, err := os.Stat(m.imgFile(env)); os.IsNotExist(err) {
|
||||||
|
// TODO image in the config ?
|
||||||
|
return "img/noimage.png"
|
||||||
|
}
|
||||||
|
return m.imgURL(env)
|
||||||
|
}
|
||||||
|
@ -32,32 +32,19 @@ function MoviePosters(props) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class MoviePoster extends React.Component {
|
function MoviePoster(props) {
|
||||||
constructor(props) {
|
const selected = props.selected ? ' thumbnail-selected' : '';
|
||||||
super(props);
|
const imgClass = 'thumbnail' + selected;
|
||||||
this.state = {
|
return (
|
||||||
src: `/img/movies/${this.props.data.imdb_id}.jpg`,
|
<div className="col-xs-12 col-md-3">
|
||||||
}
|
<a className={imgClass}>
|
||||||
this.handleError = this.handleError.bind(this);
|
<img
|
||||||
}
|
src={props.data.poster_url}
|
||||||
handleError() {
|
onClick={props.onClick}
|
||||||
this.setState({ src: '/img/noimage.png' });
|
/>
|
||||||
}
|
</a>
|
||||||
render() {
|
</div>
|
||||||
const selected = this.props.selected ? ' thumbnail-selected' : '';
|
);
|
||||||
const imgClass = 'thumbnail' + selected;
|
|
||||||
return (
|
|
||||||
<div className="col-xs-12 col-md-3">
|
|
||||||
<a className={imgClass}>
|
|
||||||
<img
|
|
||||||
src={this.state.src}
|
|
||||||
onClick={this.props.onClick}
|
|
||||||
onError={this.handleError}
|
|
||||||
/>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function MovieDetails(props) {
|
function MovieDetails(props) {
|
||||||
|
@ -33,11 +33,10 @@ export default function movieStore(state = defaultState, action) {
|
|||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedMovie = Object.assign({}, state.selectedMovie, {
|
|
||||||
index: action.index,
|
|
||||||
})
|
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
selectedMovie: selectedMovie,
|
selectedMovie: Object.assign({}, state.selectedMovie, {
|
||||||
|
index: action.index,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
return state
|
return state
|
||||||
|
Loading…
x
Reference in New Issue
Block a user