Add search to the party

This commit is contained in:
Lucas BEE 2017-01-03 12:09:43 +00:00
parent 828c34830e
commit 15e2e6b546
4 changed files with 49 additions and 1 deletions

View File

@ -24,6 +24,7 @@ type Config struct {
TmdbAPIKey string `yaml:"tmdb_api_key"`
MovieDetailers []polochon.Detailer
MovieTorrenters []polochon.Torrenter
MovieSearchers []polochon.Searcher
ShowDetailers []polochon.Detailer
ShowTorrenters []polochon.Torrenter
}
@ -71,6 +72,14 @@ func Load(path string) (*Config, error) {
}
cf.MovieTorrenters = append(cf.MovieTorrenters, d)
// Default searchers
cf.MovieSearchers = []polochon.Searcher{}
s, err := yts.NewSearcher()
if err != nil {
return nil, err
}
cf.MovieSearchers = append(cf.MovieSearchers, s)
// Default detailers
cf.ShowDetailers = []polochon.Detailer{}
showDetailer, err := tvdb.NewDetailer()

View File

@ -78,7 +78,6 @@ func GetMediaIDs(env *web.Env, mediaType string, source string, category string,
IDs: ids,
}
log.Debugf("Inserting int to DB %+v", media)
err = media.Upsert(env.Database)
if err != nil {
return nil, err

View File

@ -1,6 +1,7 @@
package movies
import (
"errors"
"fmt"
"net"
"net/http"
@ -109,3 +110,41 @@ func GetDetailsHandler(env *web.Env, w http.ResponseWriter, r *http.Request) err
return env.RenderJSON(w, m)
}
// SearchMovie will search movie
func SearchMovie(env *web.Env, w http.ResponseWriter, r *http.Request) error {
key := r.FormValue("key")
if key == "" {
return env.RenderError(w, errors.New("no given key"))
}
var movies []*polochon.Movie
searchers := env.Config.MovieSearchers
for _, searcher := range searchers {
result, err := searcher.SearchMovie(key, env.Log)
if err != nil {
env.Log.Errorf("error while searching movie : %s", err)
continue
}
movies = append(movies, result...)
}
env.Log.Debugf("got %d movies doing search %q", len(movies), key)
movieList := []*Movie{}
for _, m := range movies {
movie := New(m.ImdbID)
err := movie.GetDetails(env, false)
if err != nil {
env.Log.Errorf("error while getting movie details : %s", err)
continue
}
err = movie.GetTorrents(env, false)
if err != nil {
env.Log.Errorf("error while getting movie torrents : %s", err)
continue
}
movieList = append(movieList, movie)
}
return env.RenderJSON(w, movieList)
}

View File

@ -80,6 +80,7 @@ func main() {
env.Handle("/movies/{id:tt[0-9]+}/get_details", movies.GetDetailsHandler).WithRole(users.UserRole)
env.Handle("/movies/explore", extmedias.Explore)
env.Handle("/movies/refresh", extmedias.Refresh)
env.Handle("/movies/search", movies.SearchMovie)
// env.Handle("/shows/polochon", shows.FromPolochon).WithRole(users.UserRole)
env.Handle("/shows/{id:tt[0-9]+}", shows.GetDetailsHandler)