using AutoMapper; using Microsoft.Extensions.Logging; using Ombi.Api.TheMovieDb; using Ombi.Api.TheMovieDb.Models; using Ombi.Core.Models.Requests; using Ombi.Core.Models.Search; using Ombi.Core.Settings; using Ombi.Core.Settings.Models.External; using Ombi.Store.Repository; using System.Collections.Generic; using System.Linq; using System.Security.Principal; using System.Threading.Tasks; using Ombi.Core.Rule.Interfaces; using StackExchange.Profiling; namespace Ombi.Core.Engine { public class MovieSearchEngine : BaseMediaEngine, IMovieEngine { public MovieSearchEngine(IPrincipal identity, IRequestServiceMain service, IMovieDbApi movApi, IMapper mapper, ILogger logger, IRuleEvaluator r) : base(identity, service, r) { MovieApi = movApi; Mapper = mapper; Logger = logger; } private IMovieDbApi MovieApi { get; } private IMapper Mapper { get; } private ILogger Logger { get; } /// /// Lookups the imdb information. /// /// The movie database identifier. /// public async Task LookupImdbInformation(int theMovieDbId) { var movieInfo = await MovieApi.GetMovieInformationWithVideo(theMovieDbId); var viewMovie = Mapper.Map(movieInfo); return await ProcessSingleMovie(viewMovie, true); } /// /// Searches the specified movie. /// /// The search. /// public async Task> Search(string search) { using (MiniProfiler.Current.Step("Starting Movie Search Engine")) using (MiniProfiler.Current.Step("Searching Movie")) { var result = await MovieApi.SearchMovie(search); using (MiniProfiler.Current.Step("Fin API, Transforming")) { if (result != null) { Logger.LogDebug("Search Result: {result}", result); return await TransformMovieResultsToResponse(result); } } return null; } } /// /// Gets popular movies. /// /// public async Task> PopularMovies() { var result = await MovieApi.PopularMovies(); if (result != null) { Logger.LogDebug("Search Result: {result}", result); return await TransformMovieResultsToResponse(result); } return null; } /// /// Gets top rated movies. /// /// public async Task> TopRatedMovies() { var result = await MovieApi.TopRated(); if (result != null) { Logger.LogDebug("Search Result: {result}", result); return await TransformMovieResultsToResponse(result); } return null; } /// /// Gets upcoming movies. /// /// public async Task> UpcomingMovies() { var result = await MovieApi.Upcoming(); if (result != null) { Logger.LogDebug("Search Result: {result}", result); return await TransformMovieResultsToResponse(result); } return null; } /// /// Gets now playing movies. /// /// public async Task> NowPlayingMovies() { var result = await MovieApi.NowPlaying(); if (result != null) { Logger.LogDebug("Search Result: {result}", result); return await TransformMovieResultsToResponse(result); } return null; } private async Task> TransformMovieResultsToResponse( IEnumerable movies) { var viewMovies = new List(); foreach (var movie in movies) { viewMovies.Add(await ProcessSingleMovie(movie)); } return viewMovies; } private async Task ProcessSingleMovie(SearchMovieViewModel viewMovie, bool lookupExtraInfo = false) { if (lookupExtraInfo) { var showInfo = await MovieApi.GetMovieInformation(viewMovie.Id); viewMovie.Id = showInfo.Id; // TheMovieDbId } await RunSearchRules(viewMovie); return viewMovie; } private async Task ProcessSingleMovie(MovieSearchResult movie) { var viewMovie = Mapper.Map(movie); return await ProcessSingleMovie(viewMovie); } } }