Try to be smart on refresh

We now refresh wishlisted shows and movies too
And we only look for torrents for episodes of the last season of the
wishlisted shows
This commit is contained in:
Lucas BEE 2017-06-14 13:19:49 +02:00
parent a14bccf75c
commit e382cd0dd9
4 changed files with 83 additions and 6 deletions

View File

@ -31,6 +31,8 @@ const (
ON movies.imdb_id=movies_tracked.imdb_id AND movies_tracked.user_id=$1;`
deleteMovieWishlistedQueryByID = `DELETE FROM movies_tracked WHERE imdb_id=$1 AND user_id=$2;`
getAllWishlistedMovies = `SELECT DISTINCT(imdb_id) FROM movies_tracked;`
)
// movieWishlist represents the list of movies wishlisted by a user
@ -126,3 +128,15 @@ func DeleteMovieFromWishlist(db *sqlx.DB, userID, imdbID string) error {
}
return nil
}
// GetAllWishlistedMovies returns the list of all wishlisted movies
func GetAllWishlistedMovies(db *sqlx.DB) ([]string, error) {
var movies = []string{}
// Get the list of movies
err := db.Select(&movies, getAllWishlistedMovies)
if err != nil {
return nil, err
}
return movies, nil
}

View File

@ -27,6 +27,8 @@ const (
ON s.imdb_id = t.imdb_id AND t.user_id=$1;`
deleteShowWishlistedQueryByID = `DELETE FROM shows_tracked WHERE imdb_id=$1 AND user_id=$2;`
getAllWishlistedShows = `SELECT DISTINCT(imdb_id) FROM shows_tracked;`
)
// showWishlist represents the show wishlist of a user
@ -137,3 +139,15 @@ func DeleteShowFromWishlist(db *sqlx.DB, userID, imdbID string) error {
}
return nil
}
// GetAllWishlistedShows returns the list of all wishlisted shows
func GetAllWishlistedShows(db *sqlx.DB) ([]string, error) {
var shows = []string{}
// Get the list of movies
err := db.Select(&shows, getAllWishlistedShows)
if err != nil {
return nil, err
}
return shows, nil
}

View File

@ -91,9 +91,11 @@ func GetMediaIDs(env *web.Env, mediaType string, source string, category string)
// RefreshShows refresh explored shows
// Call all explorers and Refresh
// Retrieve a list of all the ids to be refreshed
// Refresh all the shows + Torrents for all episodes
// 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]struct{})
showMap := make(map[string]*shows.Show)
// Iterate over all show explorers
for _, showExplorer := range env.Config.ShowExplorers {
availableOptions := showExplorer.AvailableShowOptions()
@ -110,10 +112,23 @@ func RefreshShows(env *web.Env) {
// Add them in a map ( to get every shows only once )
for _, id := range showIds {
showMap[id] = struct{}{}
showMap[id] = &shows.Show{}
}
}
}
// Iterate over all the wishlisted shows
wishlistedList, err := backend.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)
@ -122,8 +137,15 @@ func RefreshShows(env *web.Env) {
if err != nil {
env.Log.Warnf("error while refreshing show %s : %s", id, err)
}
// Iterate over all episodes to refresh their torrents
for _, e := range show.Episodes {
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.ShowTorrenters)
if err != nil {
@ -136,6 +158,7 @@ func RefreshShows(env *web.Env) {
// 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{})
@ -159,6 +182,19 @@ func RefreshMovies(env *web.Env) {
}
}
}
// Iterate over all the wishlisted movies
wishlistedList, err := backend.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(id, nil, nil, false, env.Config.PublicDir, env.Config.ImgURLPrefix)

View File

@ -105,7 +105,7 @@ func (s *Show) GetDetails(env *web.Env, detailers []polochon.Detailer) error {
return err
}
log.Debugf("got details from detailers ")
log.Debugf("got details from detailers")
return nil
}
@ -216,3 +216,16 @@ func getPolochonShows(env *web.Env, user *users.User) ([]*Show, error) {
}
return shows, nil
}
// LastSeasonEpisodes will return the episodes of the last season of the show
func (s *Show) LastSeasonEpisodes() []*polochon.ShowEpisode {
episodes := make(map[int][]*polochon.ShowEpisode)
lastSeason := 0
for _, e := range s.Episodes {
if lastSeason < e.Season {
lastSeason = e.Season
}
episodes[e.Season] = append(episodes[e.Season], e)
}
return episodes[lastSeason]
}