Added API endpoint for /actor/new/ to support searching for movies not already available/requested

pull/1178/head
smcpeck 8 years ago
parent b51f790493
commit 0d3ff966d6

@ -107,9 +107,16 @@ namespace Ombi.Api
} }
public async Task<List<Movie>> SearchActor(string searchTerm) public async Task<List<Movie>> SearchActor(string searchTerm)
{
return await SearchActor(searchTerm, null);
}
public async Task<List<Movie>> SearchActor(string searchTerm, Func<int, string, string, Task<bool>> alreadyAvailable)
{ {
SearchContainer<SearchPerson> result = await Client.SearchPerson(searchTerm); SearchContainer<SearchPerson> result = await Client.SearchPerson(searchTerm);
var person = result?.Results[0] ?? null;
var people = result?.Results ?? new List<SearchPerson>();
var person = (people.Count != 0 ? people[0] : null);
var movies = new List<Movie>(); var movies = new List<Movie>();
var counter = 0; var counter = 0;
try try
@ -121,15 +128,23 @@ namespace Ombi.Api
foreach (var credit in credits.Cast) foreach (var credit in credits.Cast)
{ if (counter == 10) { if (counter == 10)
break; break;
if (alreadyAvailable == null || !(await alreadyAvailable(credit.Id, credit.Title, credit.ReleaseDate.Value.Year.ToString())))
{
movies.Add(await GetMovie(credit.Id)); movies.Add(await GetMovie(credit.Id));
counter++; 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); await Task.Delay(50);
} }
} }
} }
catch(Exception e) catch(Exception e)
{ {
Log.LogException(LogLevel.Error, $"Aggregating movies for {searchTerm} failed.", e); Log.Log(LogLevel.Error, e);
} }
return movies; return movies;
} }

@ -116,6 +116,7 @@ namespace Ombi.UI.Modules
Get["SearchIndex", "/", true] = async (x, ct) => await RequestLoad(); Get["SearchIndex", "/", true] = async (x, ct) => await RequestLoad();
Get["actor/{searchTerm}", true] = async (x, ct) => await SearchActor((string)x.searchTerm); 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["movie/{searchTerm}", true] = async (x, ct) => await SearchMovie((string)x.searchTerm);
Get["tv/{searchTerm}", true] = async (x, ct) => await SearchTvShow((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); 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 IRadarrCacher RadarrCacher { get; }
private ISettingsService<CustomizationSettings> CustomizationSettings { get; } private ISettingsService<CustomizationSettings> CustomizationSettings { get; }
private static Logger Log = LogManager.GetCurrentClassLogger(); private static Logger Log = LogManager.GetCurrentClassLogger();
private IEnumerable<PlexContent> _plexMovies = null;
private Dictionary<int, RequestedModel> _dbMovies = null;
private async Task<Negotiator> RequestLoad() private async Task<Negotiator> RequestLoad()
{ {
@ -216,6 +219,29 @@ namespace Ombi.UI.Modules
return await TransformMovieResultsToResponse(movies); return await TransformMovieResultsToResponse(movies);
} }
private async Task<Response> SearchActor(string searchTerm, bool filterExisting)
{
var movies = TransformMovieListToMovieResultList(await MovieApi.SearchActor(searchTerm, AlreadyAvailable).ConfigureAwait(false));
return await TransformMovieResultsToResponse(movies);
}
private async Task<bool> AlreadyAvailable(int id, string title, string year)
{
await Task.Yield();
return IsMovieInCache(id, String.Empty) || Checker.IsMovieAvailable(plexMovies(), title, year);
}
private IEnumerable<PlexContent> plexMovies()
{
if(_plexMovies == null)
{
var content = PlexContentRepository.GetAll();
_plexMovies = Checker.GetPlexMovies(content);
}
return _plexMovies;
}
private Response GetTvPoster(int theTvDbId) private Response GetTvPoster(int theTvDbId)
{ {
var result = TvApi.ShowLookupByTheTvDbId(theTvDbId); var result = TvApi.ShowLookupByTheTvDbId(theTvDbId);
@ -297,22 +323,25 @@ namespace Ombi.UI.Modules
return await TransformMovieResultsToResponse(apiMovies); return await TransformMovieResultsToResponse(apiMovies);
} }
private async Task<Response> TransformMovieResultsToResponse(List<MovieResult> movies) private async Task<Dictionary<int, RequestedModel>> requestedMovies()
{
if (_dbMovies == null)
{ {
var allResults = await RequestService.GetAllAsync(); var allResults = await RequestService.GetAllAsync();
allResults = allResults.Where(x => x.Type == RequestType.Movie); allResults = allResults.Where(x => x.Type == RequestType.Movie);
var distinctResults = allResults.DistinctBy(x => x.ProviderId); var distinctResults = allResults.DistinctBy(x => x.ProviderId);
var dbMovies = distinctResults.ToDictionary(x => x.ProviderId); _dbMovies = distinctResults.ToDictionary(x => x.ProviderId);
}
return _dbMovies;
}
var cpCached = CpCacher.QueuedIds(); private async Task<Response> TransformMovieResultsToResponse(List<MovieResult> movies)
var watcherCached = WatcherCacher.QueuedIds(); {
var radarrCached = RadarrCacher.QueuedIds(); await Task.Yield();
var content = PlexContentRepository.GetAll();
var plexMovies = Checker.GetPlexMovies(content);
var viewMovies = new List<SearchMovieViewModel>(); var viewMovies = new List<SearchMovieViewModel>();
var counter = 0; var counter = 0;
Dictionary<int, RequestedModel> dbMovies = await requestedMovies();
foreach (var movie in movies) foreach (var movie in movies)
{ {
var viewMovie = new SearchMovieViewModel var viewMovie = new SearchMovieViewModel
@ -353,10 +382,8 @@ namespace Ombi.UI.Modules
counter++; counter++;
} }
var canSee = CanUserSeeThisRequest(viewMovie.Id, Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests), await requestedMovies());
var canSee = CanUserSeeThisRequest(viewMovie.Id, Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests), dbMovies); var plexMovie = Checker.GetMovie(plexMovies(), movie.Title, movie.ReleaseDate?.Year.ToString(), imdbId);
var plexMovie = Checker.GetMovie(plexMovies.ToArray(), movie.Title, movie.ReleaseDate?.Year.ToString(),
imdbId);
if (plexMovie != null) if (plexMovie != null)
{ {
viewMovie.Available = true; viewMovie.Available = true;
@ -370,26 +397,29 @@ namespace Ombi.UI.Modules
viewMovie.Approved = dbm.Approved; viewMovie.Approved = dbm.Approved;
viewMovie.Available = dbm.Available; viewMovie.Available = dbm.Available;
} }
else if (cpCached.Contains(movie.Id) && canSee) // compare to the couchpotato db else if (canSee)
{ {
viewMovie.Approved = true; bool exists = IsMovieInCache(movie, imdbId);
viewMovie.Requested = true; viewMovie.Approved = exists;
} viewMovie.Requested = exists;
else if (watcherCached.Contains(imdbId) && canSee) // compare to the watcher db
{
viewMovie.Approved = true;
viewMovie.Requested = true;
}
else if (radarrCached.Contains(movie.Id) && canSee)
{
viewMovie.Approved = true;
viewMovie.Requested = true;
} }
viewMovies.Add(viewMovie); viewMovies.Add(viewMovie);
} }
return Response.AsJson(viewMovies); 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, private bool CanUserSeeThisRequest(int movieId, bool usersCanViewOnlyOwnRequests,

Loading…
Cancel
Save