Add torrent search in the backend
This commit is contained in:
parent
538c5224f8
commit
0dda37de50
@ -20,6 +20,11 @@ func (b *Backend) GetTorrents(media interface{}, log *logrus.Entry) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SearchTorrents implements the polochon Torrenter interface
|
||||||
|
func (b *Backend) SearchTorrents(s string) ([]*polochon.Torrent, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetMovieTorrents fetch Torrents for movies
|
// GetMovieTorrents fetch Torrents for movies
|
||||||
func (b *Backend) GetMovieTorrents(pmovie *polochon.Movie, log *logrus.Entry) error {
|
func (b *Backend) GetMovieTorrents(pmovie *polochon.Movie, log *logrus.Entry) error {
|
||||||
movieTorrents, err := GetMovieTorrents(b.Database, pmovie.ImdbID)
|
movieTorrents, err := GetMovieTorrents(b.Database, pmovie.ImdbID)
|
||||||
|
@ -4,9 +4,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/odwrtw/polochon/lib"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/auth"
|
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/auth"
|
||||||
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/users"
|
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/users"
|
||||||
@ -94,3 +97,55 @@ func RemoveHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
|||||||
|
|
||||||
return env.RenderOK(w, "Torrent removed")
|
return env.RenderOK(w, "Torrent removed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SearchHandler search for torrents
|
||||||
|
func SearchHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
||||||
|
log := env.Log.WithFields(logrus.Fields{
|
||||||
|
"function": "torrents.SearchHandler",
|
||||||
|
})
|
||||||
|
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
searchType := vars["type"]
|
||||||
|
searchStr := vars["search"]
|
||||||
|
|
||||||
|
// Get the appropriate torrenters
|
||||||
|
var torrenters []polochon.Torrenter
|
||||||
|
switch searchType {
|
||||||
|
case "movies":
|
||||||
|
torrenters = env.Config.MovieTorrenters
|
||||||
|
case "shows":
|
||||||
|
torrenters = env.Config.ShowTorrenters
|
||||||
|
default:
|
||||||
|
return env.RenderError(w, errors.New("invalid search type"))
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("searching for %s torrents for the query %q", searchType, searchStr)
|
||||||
|
|
||||||
|
// Search for torrents
|
||||||
|
results := []*polochon.Torrent{}
|
||||||
|
for _, torrenter := range torrenters {
|
||||||
|
torrents, err := torrenter.SearchTorrents(searchStr)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if torrents == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
results = append(results, torrents...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by seeds
|
||||||
|
sort.Sort(BySeed(results))
|
||||||
|
|
||||||
|
return env.RenderJSON(w, results)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BySeed is an helper to sort torrents by seeders
|
||||||
|
type BySeed []*polochon.Torrent
|
||||||
|
|
||||||
|
func (t BySeed) Len() int { return len(t) }
|
||||||
|
func (t BySeed) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
|
||||||
|
func (t BySeed) Less(i, j int) bool { return t[i].Seeders > t[j].Seeders }
|
||||||
|
@ -54,6 +54,7 @@ func setupRoutes(env *web.Env) {
|
|||||||
env.Handle("/torrents", torrents.DownloadHandler).WithRole(users.UserRole).Methods("POST")
|
env.Handle("/torrents", torrents.DownloadHandler).WithRole(users.UserRole).Methods("POST")
|
||||||
env.Handle("/torrents", torrents.ListHandler).WithRole(users.UserRole).Methods("GET")
|
env.Handle("/torrents", torrents.ListHandler).WithRole(users.UserRole).Methods("GET")
|
||||||
env.Handle("/torrents/{id:[0-9]+}", torrents.RemoveHandler).WithRole(users.UserRole).Methods("DELETE")
|
env.Handle("/torrents/{id:[0-9]+}", torrents.RemoveHandler).WithRole(users.UserRole).Methods("DELETE")
|
||||||
|
env.Handle("/torrents/search/{type}/{search}", torrents.SearchHandler).WithRole(users.UserRole).Methods("GET")
|
||||||
|
|
||||||
// Route to refresh all movies and shows
|
// Route to refresh all movies and shows
|
||||||
env.Handle("/refresh", extmedias.RefreshHandler).WithRole(users.AdminRole).Methods("POST")
|
env.Handle("/refresh", extmedias.RefreshHandler).WithRole(users.AdminRole).Methods("POST")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user