From 0d3ff966d64038ac27501096072b6e70d49adc0c Mon Sep 17 00:00:00 2001 From: smcpeck Date: Thu, 23 Feb 2017 17:15:58 -0600 Subject: [PATCH] Added API endpoint for /actor/new/ to support searching for movies not already available/requested --- Ombi.Api/TheMovieDbApi.cs | 23 +++++++-- Ombi.UI/Modules/SearchModule.cs | 86 ++++++++++++++++++++++----------- 2 files changed, 77 insertions(+), 32 deletions(-) diff --git a/Ombi.Api/TheMovieDbApi.cs b/Ombi.Api/TheMovieDbApi.cs index 7a072caa4..1dee00d62 100644 --- a/Ombi.Api/TheMovieDbApi.cs +++ b/Ombi.Api/TheMovieDbApi.cs @@ -107,9 +107,16 @@ namespace Ombi.Api } public async Task> SearchActor(string searchTerm) + { + return await SearchActor(searchTerm, null); + } + + public async Task> SearchActor(string searchTerm, Func> alreadyAvailable) { SearchContainer result = await Client.SearchPerson(searchTerm); - var person = result?.Results[0] ?? null; + + var people = result?.Results ?? new List(); + var person = (people.Count != 0 ? people[0] : null); var movies = new List(); var counter = 0; try @@ -121,15 +128,23 @@ namespace Ombi.Api foreach (var credit in credits.Cast) { if (counter == 10) break; - movies.Add(await GetMovie(credit.Id)); - counter++; + if (alreadyAvailable == null || !(await alreadyAvailable(credit.Id, credit.Title, credit.ReleaseDate.Value.Year.ToString()))) + { + movies.Add(await GetMovie(credit.Id)); + counter++; + } + else + { + //if we didn't add the movie, delay longer since we know we'll be making additional API calls + await Task.Delay(75); + } await Task.Delay(50); } } } catch(Exception e) { - Log.LogException(LogLevel.Error, $"Aggregating movies for {searchTerm} failed.", e); + Log.Log(LogLevel.Error, e); } return movies; } diff --git a/Ombi.UI/Modules/SearchModule.cs b/Ombi.UI/Modules/SearchModule.cs index 601ba1914..77aea51fc 100644 --- a/Ombi.UI/Modules/SearchModule.cs +++ b/Ombi.UI/Modules/SearchModule.cs @@ -116,6 +116,7 @@ namespace Ombi.UI.Modules Get["SearchIndex", "/", true] = async (x, ct) => await RequestLoad(); Get["actor/{searchTerm}", true] = async (x, ct) => await SearchActor((string)x.searchTerm); + Get["actor/new/{searchTerm}", true] = async (x, ct) => await SearchActor((string)x.searchTerm, true); Get["movie/{searchTerm}", true] = async (x, ct) => await SearchMovie((string)x.searchTerm); Get["tv/{searchTerm}", true] = async (x, ct) => await SearchTvShow((string)x.searchTerm); Get["music/{searchTerm}", true] = async (x, ct) => await SearchAlbum((string)x.searchTerm); @@ -173,6 +174,8 @@ namespace Ombi.UI.Modules private IRadarrCacher RadarrCacher { get; } private ISettingsService CustomizationSettings { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); + private IEnumerable _plexMovies = null; + private Dictionary _dbMovies = null; private async Task RequestLoad() { @@ -216,6 +219,29 @@ namespace Ombi.UI.Modules return await TransformMovieResultsToResponse(movies); } + private async Task SearchActor(string searchTerm, bool filterExisting) + { + var movies = TransformMovieListToMovieResultList(await MovieApi.SearchActor(searchTerm, AlreadyAvailable).ConfigureAwait(false)); + return await TransformMovieResultsToResponse(movies); + } + + private async Task AlreadyAvailable(int id, string title, string year) + { + await Task.Yield(); + return IsMovieInCache(id, String.Empty) || Checker.IsMovieAvailable(plexMovies(), title, year); + } + + private IEnumerable plexMovies() + { + if(_plexMovies == null) + { + var content = PlexContentRepository.GetAll(); + _plexMovies = Checker.GetPlexMovies(content); + } + + return _plexMovies; + } + private Response GetTvPoster(int theTvDbId) { var result = TvApi.ShowLookupByTheTvDbId(theTvDbId); @@ -297,22 +323,25 @@ namespace Ombi.UI.Modules return await TransformMovieResultsToResponse(apiMovies); } - private async Task TransformMovieResultsToResponse(List movies) + private async Task> requestedMovies() { - var allResults = await RequestService.GetAllAsync(); - allResults = allResults.Where(x => x.Type == RequestType.Movie); - - var distinctResults = allResults.DistinctBy(x => x.ProviderId); - var dbMovies = distinctResults.ToDictionary(x => x.ProviderId); + if (_dbMovies == null) + { + var allResults = await RequestService.GetAllAsync(); + allResults = allResults.Where(x => x.Type == RequestType.Movie); + var distinctResults = allResults.DistinctBy(x => x.ProviderId); + _dbMovies = distinctResults.ToDictionary(x => x.ProviderId); + } + return _dbMovies; + } - var cpCached = CpCacher.QueuedIds(); - var watcherCached = WatcherCacher.QueuedIds(); - var radarrCached = RadarrCacher.QueuedIds(); - var content = PlexContentRepository.GetAll(); - var plexMovies = Checker.GetPlexMovies(content); + private async Task TransformMovieResultsToResponse(List movies) + { + await Task.Yield(); var viewMovies = new List(); var counter = 0; + Dictionary dbMovies = await requestedMovies(); foreach (var movie in movies) { var viewMovie = new SearchMovieViewModel @@ -353,10 +382,8 @@ namespace Ombi.UI.Modules counter++; } - - var canSee = CanUserSeeThisRequest(viewMovie.Id, Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests), dbMovies); - var plexMovie = Checker.GetMovie(plexMovies.ToArray(), movie.Title, movie.ReleaseDate?.Year.ToString(), - imdbId); + var canSee = CanUserSeeThisRequest(viewMovie.Id, Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests), await requestedMovies()); + var plexMovie = Checker.GetMovie(plexMovies(), movie.Title, movie.ReleaseDate?.Year.ToString(), imdbId); if (plexMovie != null) { viewMovie.Available = true; @@ -370,26 +397,29 @@ namespace Ombi.UI.Modules viewMovie.Approved = dbm.Approved; viewMovie.Available = dbm.Available; } - else if (cpCached.Contains(movie.Id) && canSee) // compare to the couchpotato db - { - viewMovie.Approved = true; - viewMovie.Requested = true; - } - else if (watcherCached.Contains(imdbId) && canSee) // compare to the watcher db + else if (canSee) { - viewMovie.Approved = true; - viewMovie.Requested = true; - } - else if (radarrCached.Contains(movie.Id) && canSee) - { - viewMovie.Approved = true; - viewMovie.Requested = true; + bool exists = IsMovieInCache(movie, imdbId); + viewMovie.Approved = exists; + viewMovie.Requested = exists; } viewMovies.Add(viewMovie); } return Response.AsJson(viewMovies); + } + + private bool IsMovieInCache(MovieResult movie, string imdbId) + { int id = movie.Id; + return IsMovieInCache(id, imdbId); + } + + private bool IsMovieInCache(int id, string imdbId) + { var cpCached = CpCacher.QueuedIds(); + var watcherCached = WatcherCacher.QueuedIds(); + var radarrCached = RadarrCacher.QueuedIds(); + return cpCached.Contains(id) || watcherCached.Contains(imdbId) || radarrCached.Contains(id); } private bool CanUserSeeThisRequest(int movieId, bool usersCanViewOnlyOwnRequests,