The func now takes a full polochon.Show instead of just an ImdbID, so that it we have more than an ImdbID (a TvdbID for example), we can transmit it to the show.Show
241 lines
6.3 KiB
Go
241 lines
6.3 KiB
Go
package extmedias
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.quimbo.fr/odwrtw/canape/backend/auth"
|
|
"git.quimbo.fr/odwrtw/canape/backend/models"
|
|
"git.quimbo.fr/odwrtw/canape/backend/movies"
|
|
"git.quimbo.fr/odwrtw/canape/backend/shows"
|
|
"git.quimbo.fr/odwrtw/canape/backend/web"
|
|
polochon "github.com/odwrtw/polochon/lib"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// RefreshHandler refresh the explored movies
|
|
func RefreshHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
|
log := env.Log.WithFields(logrus.Fields{
|
|
"function": "extmedias.RefreshHandler",
|
|
})
|
|
log.Debugf("refreshing shows and movies")
|
|
Refresh(env)
|
|
log.Debugf("done refreshing shows and movies")
|
|
return nil
|
|
}
|
|
|
|
// RefreshMoviesHandler refresh the explored movies
|
|
func RefreshMoviesHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
|
log := env.Log.WithFields(logrus.Fields{
|
|
"function": "extmedias.RefreshMoviesHandler",
|
|
})
|
|
log.Debugf("refreshing movies")
|
|
RefreshMovies(env)
|
|
log.Debugf("done refreshing movies")
|
|
return nil
|
|
}
|
|
|
|
// RefreshShowsHandler refresh the explored shows
|
|
func RefreshShowsHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
|
log := env.Log.WithFields(logrus.Fields{
|
|
"function": "extmedias.RefreshShowsHandler",
|
|
})
|
|
log.Debugf("refreshing shows")
|
|
RefreshShows(env)
|
|
log.Debugf("done refreshing shows")
|
|
return nil
|
|
}
|
|
|
|
// GetMovies get some movies
|
|
func GetMovies(env *web.Env, user *models.User, source string, category string) ([]*movies.Movie, error) {
|
|
log := env.Log.WithFields(logrus.Fields{
|
|
"source": source,
|
|
"category": category,
|
|
"function": "extmedias.GetMovies",
|
|
})
|
|
log.Debugf("getting movies")
|
|
media, err := models.Explore(env.Database, "movie", source, category)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Create a papi client
|
|
client, err := user.NewPapiClient(env.Database)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Get the user's polochon movies
|
|
pMovies, err := client.GetMovies()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Get the user's wishlisted movies
|
|
moviesWishlist, err := models.GetMovieWishlist(env.Database, user.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
movieList := []*movies.Movie{}
|
|
// Fill all the movies infos from the list of IDs
|
|
for _, id := range media.IDs {
|
|
pMovie, _ := pMovies.Has(id)
|
|
movie := movies.New(id, client, pMovie, moviesWishlist.IsMovieInWishlist(id), env.Config.PublicDir, env.Config.ImgURLPrefix)
|
|
// First check in the DB
|
|
before := []polochon.Detailer{env.Backend.Detailer}
|
|
// Then with the default detailers
|
|
after := env.Config.Movie.Detailers
|
|
err := movie.GetAndFetch(env, before, after)
|
|
if err != nil {
|
|
env.Log.Errorf("error while getting movie details : %s", err)
|
|
continue
|
|
}
|
|
|
|
// Look only for torrents in db
|
|
torrenters := []polochon.Torrenter{env.Backend.Torrenter}
|
|
err = movie.GetTorrents(env, torrenters)
|
|
if err != nil {
|
|
env.Log.Errorf("error while getting movie torrents : %s", err)
|
|
continue
|
|
}
|
|
|
|
movieList = append(movieList, movie)
|
|
}
|
|
return movieList, nil
|
|
}
|
|
|
|
// GetShows get some shows
|
|
func GetShows(env *web.Env, user *models.User, source string, category string, force bool) ([]*shows.Show, error) {
|
|
log := env.Log.WithFields(logrus.Fields{
|
|
"source": source,
|
|
"category": category,
|
|
"function": "extmedias.GetShows",
|
|
})
|
|
log.Debugf("getting shows")
|
|
// Get the user's polochon config
|
|
media, err := models.Explore(env.Database, "show", source, category)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Create a papi client
|
|
client, err := user.NewPapiClient(env.Database)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Get the polochon's shows
|
|
pShows, err := client.GetShows()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Get the user's wishlisted shows
|
|
wShows, err := models.GetShowWishlist(env.Database, user.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
showList := []*shows.Show{}
|
|
// Fill all the shows infos from the list of IDs
|
|
for _, id := range media.IDs {
|
|
pShow, _ := pShows.Has(id)
|
|
wShow, _ := wShows.IsShowInWishlist(id)
|
|
show := shows.NewWithClient(&polochon.Show{ImdbID: id}, client, pShow, wShow, env.Config.PublicDir, env.Config.ImgURLPrefix)
|
|
|
|
// First check in the DB
|
|
before := []polochon.Detailer{env.Backend.Detailer}
|
|
// Then with the default detailers
|
|
after := env.Config.Show.Detailers
|
|
err := show.GetAndFetch(env, before, after)
|
|
if err != nil {
|
|
env.Log.Errorf("error while getting show details : %s", err)
|
|
continue
|
|
}
|
|
showList = append(showList, show)
|
|
}
|
|
return showList, nil
|
|
}
|
|
|
|
// ExploreMovies will explore some movies from the db
|
|
func ExploreMovies(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
source := r.FormValue("source")
|
|
// Default source
|
|
if source == "" {
|
|
source = "trakttv"
|
|
}
|
|
|
|
category := r.FormValue("category")
|
|
// Default category
|
|
if category == "" {
|
|
category = "popular"
|
|
}
|
|
|
|
user := auth.GetCurrentUser(r, env.Log)
|
|
|
|
// Get the medias without trying to refresh them
|
|
movies, err := GetMovies(env, user, source, category)
|
|
if err != nil {
|
|
return env.RenderError(w, err)
|
|
}
|
|
|
|
return env.RenderJSON(w, movies)
|
|
}
|
|
|
|
// ExploreShows will explore some shows
|
|
func ExploreShows(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
source := r.FormValue("source")
|
|
// Default source
|
|
if source == "" {
|
|
source = "eztv"
|
|
}
|
|
|
|
category := r.FormValue("category")
|
|
// Default category
|
|
if category == "" {
|
|
category = "rating"
|
|
}
|
|
|
|
user := auth.GetCurrentUser(r, env.Log)
|
|
|
|
// Get the medias without trying to refresh them
|
|
shows, err := GetShows(env, user, source, category, false)
|
|
if err != nil {
|
|
return env.RenderError(w, err)
|
|
}
|
|
|
|
return env.RenderJSON(w, shows)
|
|
}
|
|
|
|
// MovieExplorerOptions will return all the explore movie options available
|
|
func MovieExplorerOptions(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
|
// Get the list of movie explorer options available
|
|
movieOptions, err := GetMovieOptions(env)
|
|
if err != nil {
|
|
return env.RenderError(w, err)
|
|
}
|
|
|
|
return env.RenderJSON(w, movieOptions)
|
|
}
|
|
|
|
// ShowExplorerOptions will return all the explore show options available
|
|
func ShowExplorerOptions(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
|
// Get the list of show explorer options available
|
|
showOptions, err := GetShowOptions(env)
|
|
if err != nil {
|
|
return env.RenderError(w, err)
|
|
}
|
|
|
|
return env.RenderJSON(w, showOptions)
|
|
}
|