249 lines
6.8 KiB
Go
249 lines
6.8 KiB
Go
package extmedias
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"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"
|
|
)
|
|
|
|
// NewExplorer returns a polochon.Explorer from the list of Explorers in the config
|
|
func NewExplorer(env *web.Env, mediaType, name string) (polochon.Explorer, error) {
|
|
var explorers []polochon.Explorer
|
|
if mediaType == "movie" {
|
|
explorers = env.Config.Movie.Explorers
|
|
} else {
|
|
explorers = env.Config.Show.Explorers
|
|
}
|
|
|
|
for _, e := range explorers {
|
|
// Check the name of the explorer
|
|
if e.Name() == name {
|
|
return e, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("no such explorer %s", name)
|
|
}
|
|
|
|
// GetMediaIDs will get some media IDs
|
|
func GetMediaIDs(env *web.Env, mediaType string, source string, category string) ([]string, error) {
|
|
log := env.Log.WithFields(logrus.Fields{
|
|
"mediaType": mediaType,
|
|
"source": source,
|
|
"category": category,
|
|
"function": "extmedias.GetMediaIds",
|
|
})
|
|
|
|
// Get the explorer that we need
|
|
explorer, err := NewExplorer(env, mediaType, source)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var ids []string
|
|
if mediaType == "movie" {
|
|
// Explore new movies
|
|
medias, err := explorer.GetMovieList(category, log)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Add them in the list of ids
|
|
for _, media := range medias {
|
|
ids = append(ids, media.ImdbID)
|
|
}
|
|
} else {
|
|
// Explore new shows
|
|
medias, err := explorer.GetShowList(category, log)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Add them in the list of ids
|
|
for _, media := range medias {
|
|
ids = append(ids, media.ImdbID)
|
|
}
|
|
}
|
|
|
|
log.Debugf("got %d medias from %s", len(ids), source)
|
|
|
|
// Upserts the new Media entry
|
|
media := &models.Media{
|
|
Type: mediaType,
|
|
Source: source,
|
|
Category: category,
|
|
IDs: ids,
|
|
}
|
|
|
|
err = media.Upsert(env.Database)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Debug("medias updated in database")
|
|
|
|
return ids, nil
|
|
}
|
|
|
|
// RefreshShows refresh explored shows
|
|
// Call all explorers and Refresh
|
|
// Retrieve a list of all the ids to be refreshed
|
|
// Retrieve a list of all the wishlisted shows
|
|
// Refresh all the shows
|
|
// Refresh Torrents for the last season of all the wishlisted shows
|
|
func RefreshShows(env *web.Env) {
|
|
showMap := make(map[string]*shows.Show)
|
|
// Iterate over all show explorers
|
|
for _, showExplorer := range env.Config.Show.Explorers {
|
|
availableOptions := showExplorer.AvailableShowOptions()
|
|
// Iterate over all show explorer options
|
|
for _, option := range availableOptions {
|
|
env.Log.Infof("refreshing shows %s %s", showExplorer.Name(), option)
|
|
|
|
// Get the new explored shows
|
|
showIds, err := GetMediaIDs(env, "show", showExplorer.Name(), option)
|
|
if err != nil {
|
|
env.Log.Warnf("error while refreshing %s/%s : %s", showExplorer.Name(), option, err)
|
|
continue
|
|
}
|
|
|
|
// Add them in a map ( to get every shows only once )
|
|
for _, id := range showIds {
|
|
showMap[id] = &shows.Show{}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Iterate over all the wishlisted shows
|
|
wishlistedList, err := models.GetAllWishlistedShows(env.Database)
|
|
if err != nil {
|
|
env.Log.Warnf("error while getting list of wishlisted shows: %s", err)
|
|
return
|
|
}
|
|
|
|
// Add them in the map
|
|
for _, id := range wishlistedList {
|
|
showMap[id] = &shows.Show{}
|
|
}
|
|
|
|
// Iterate over the map of shows to refresh them
|
|
for id := range showMap {
|
|
show := shows.New(id, env.Config.PublicDir, env.Config.ImgURLPrefix)
|
|
// Refresh the shows
|
|
err := show.Refresh(env, env.Config.Show.Detailers)
|
|
if err != nil {
|
|
env.Log.Warnf("error while refreshing show %s : %s", id, err)
|
|
}
|
|
showMap[id] = show
|
|
}
|
|
|
|
// Iterate over the wishlisted shows in order to get torrents for their last season
|
|
for _, id := range wishlistedList {
|
|
show := showMap[id]
|
|
// We don't look for torrents of all those shows as it generates way
|
|
// too many requests on our torrenters
|
|
for _, e := range show.LastSeasonEpisodes() {
|
|
// Refresh the torrents
|
|
err := shows.RefreshTorrents(env, e, env.Config.Show.Torrenters)
|
|
if err != nil {
|
|
env.Log.Error(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// RefreshMovies refresh explored Movies
|
|
// Call all explorers and Refresh
|
|
// Retrieve a list of all the ids to be refreshed
|
|
// Retrieve a list of all the wishlisted movies
|
|
// Refresh all the movies + Torrents
|
|
func RefreshMovies(env *web.Env) {
|
|
movieMap := make(map[string]struct{})
|
|
// Iterate over all movie explorers
|
|
for _, movieExplorer := range env.Config.Movie.Explorers {
|
|
availableOptions := movieExplorer.AvailableMovieOptions()
|
|
// Iterate over all movie explorer options
|
|
for _, option := range availableOptions {
|
|
env.Log.Infof("refreshing movies %s %s", movieExplorer.Name(), option)
|
|
|
|
// Get the new explored movies
|
|
movieIds, err := GetMediaIDs(env, "movie", movieExplorer.Name(), option)
|
|
if err != nil {
|
|
env.Log.Warnf("error while refreshing %s/%s : %s", movieExplorer.Name(), option, err)
|
|
continue
|
|
}
|
|
|
|
// Add them in a map ( to get every movies only once )
|
|
for _, id := range movieIds {
|
|
movieMap[id] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Iterate over all the wishlisted movies
|
|
wishlistedList, err := models.GetAllWishlistedMovies(env.Database)
|
|
if err != nil {
|
|
env.Log.Warnf("error while getting list of wishlisted movies %q", err)
|
|
return
|
|
}
|
|
|
|
// Add them in the map
|
|
for _, id := range wishlistedList {
|
|
movieMap[id] = struct{}{}
|
|
}
|
|
|
|
// Iterate over the map of movies to refresh them
|
|
for id := range movieMap {
|
|
movie := movies.New(env, id, nil, nil, false)
|
|
// Refresh the movie
|
|
err := movie.Refresh(env, env.Config.Movie.Detailers)
|
|
if err != nil {
|
|
env.Log.Warnf("error while refreshing movie %s : %s", id, err)
|
|
}
|
|
// Refresh its torrents
|
|
err = movie.RefreshTorrents(env, env.Config.Movie.Torrenters)
|
|
if err != nil {
|
|
env.Log.Warnf("error while refreshing movie torrents %s : %s", id, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Refresh refreshes explore new shows and refresh them
|
|
func Refresh(env *web.Env) {
|
|
env.Log.Debugf("refreshing shows")
|
|
RefreshShows(env)
|
|
env.Log.Debugf("done refreshing shows")
|
|
|
|
env.Log.Debugf("refreshing movies")
|
|
RefreshMovies(env)
|
|
env.Log.Debugf("done refreshing movies")
|
|
}
|
|
|
|
// GetShowOptions will get show explorer options available
|
|
func GetShowOptions(env *web.Env) (map[string][]string, error) {
|
|
log := env.Log.WithFields(logrus.Fields{
|
|
"function": "extmedias.GetShowOptions",
|
|
})
|
|
log.Debugf("getting explorer show options")
|
|
e, err := models.GetMediaOptions(env.Database, "show")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return e, nil
|
|
}
|
|
|
|
// GetMovieOptions will get movie explorer options available
|
|
func GetMovieOptions(env *web.Env) (map[string][]string, error) {
|
|
log := env.Log.WithFields(logrus.Fields{
|
|
"function": "extmedias.GetMovieOptions",
|
|
})
|
|
log.Debugf("getting explorer movie options")
|
|
e, err := models.GetMediaOptions(env.Database, "movie")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return e, nil
|
|
}
|