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)
{
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);
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 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;
}

@ -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> CustomizationSettings { get; }
private static Logger Log = LogManager.GetCurrentClassLogger();
private IEnumerable<PlexContent> _plexMovies = null;
private Dictionary<int, RequestedModel> _dbMovies = null;
private async Task<Negotiator> RequestLoad()
{
@ -216,6 +219,29 @@ namespace Ombi.UI.Modules
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)
{
var result = TvApi.ShowLookupByTheTvDbId(theTvDbId);
@ -297,22 +323,25 @@ namespace Ombi.UI.Modules
return await TransformMovieResultsToResponse(apiMovies);
}
private async Task<Response> TransformMovieResultsToResponse(List<MovieResult> movies)
private async Task<Dictionary<int, RequestedModel>> 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<Response> TransformMovieResultsToResponse(List<MovieResult> movies)
{
await Task.Yield();
var viewMovies = new List<SearchMovieViewModel>();
var counter = 0;
Dictionary<int, RequestedModel> 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,

Loading…
Cancel
Save