/// <summary> /// Translate movie informations (title, description, ...) /// </summary> /// <param name="movieToTranslate">Movie to translate</param> /// <returns>Task</returns> public async Task TranslateMovieAsync(MovieJson movieToTranslate) { if (!MustRefreshLanguage) { return; } var watch = Stopwatch.StartNew(); try { var movie = await TmdbClient.GetMovieAsync(movieToTranslate.ImdbCode, MovieMethods.Credits); movieToTranslate.Title = movie?.Title; movieToTranslate.Genres = movie?.Genres?.Select(a => a.Name).ToList(); movieToTranslate.DescriptionFull = movie?.Overview; } catch (Exception exception) when(exception is TaskCanceledException) { Logger.Debug( "TranslateMovieAsync cancelled."); } catch (Exception exception) { Logger.Error( $"TranslateMovieAsync: {exception.Message}"); } finally { watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Logger.Debug( $"TranslateMovieAsync ({movieToTranslate.ImdbCode}) in {elapsedMs} milliseconds."); } }
/// <summary> /// Get the link to the youtube trailer of a movie /// </summary> /// <param name="movie">The movie</param> /// <param name="ct">Used to cancel loading trailer</param> /// <returns>Video trailer</returns> public async Task <string> GetMovieTrailerAsync(MovieJson movie, CancellationToken ct) { var watch = Stopwatch.StartNew(); var uri = string.Empty; try { var tmdbMovie = await TmdbClient.GetMovieAsync(movie.ImdbCode, MovieMethods.Videos) .ConfigureAwait(false); var trailers = tmdbMovie?.Videos; if (trailers != null && trailers.Results.Any()) { using (var service = Client.For(YouTube.Default)) { var videos = (await service.GetAllVideosAsync("https://youtube.com/watch?v=" + trailers.Results .FirstOrDefault() .Key).ConfigureAwait(false)) .ToList(); if (videos.Any()) { var settings = SimpleIoc.Default.GetInstance <ApplicationSettingsViewModel>(); var maxRes = settings.DefaultHdQuality ? 1080 : 720; uri = await videos.Where(a => !a.Is3D && a.Resolution <= maxRes && a.Format == VideoFormat.Mp4 && a.AudioBitrate > 0) .Aggregate((i1, i2) => i1.Resolution > i2.Resolution ? i1 : i2).GetUriAsync(); } } } else { throw new PopcornException("No trailer found."); } } catch (Exception exception) when(exception is TaskCanceledException) { Logger.Debug( "GetMovieTrailerAsync cancelled."); } catch (Exception exception) { Logger.Error( $"GetMovieTrailerAsync: {exception.Message}"); throw; } finally { watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Logger.Debug( $"GetMovieTrailerAsync ({movie.ImdbCode}) in {elapsedMs} milliseconds."); } return(uri); }
/// <summary> /// Translate movie informations (title, description, ...) /// </summary> /// <param name="movieToTranslate">Movie to translate</param> /// <returns>Task</returns> public async Task TranslateMovieAsync(IMovie movieToTranslate) { if (!MustRefreshLanguage) { return; } var watch = Stopwatch.StartNew(); try { var movie = await TmdbClient.GetMovieAsync(movieToTranslate.ImdbCode, MovieMethods.Credits).ConfigureAwait(false); var refMovie = movieToTranslate as MovieJson; if (refMovie != null) { refMovie.Title = movie?.Title; refMovie.Genres = movie?.Genres?.Select(a => a.Name).ToList(); refMovie.DescriptionFull = movie?.Overview; return; } var refMovieLight = movieToTranslate as MovieLightJson; if (refMovieLight != null) { refMovieLight.Title = movie?.Title; refMovieLight.Genres = movie?.Genres != null ? string.Join(", ", movie.Genres?.Select(a => a.Name)) : string.Empty; } } catch (Exception exception) when(exception is TaskCanceledException) { Logger.Debug( "TranslateMovieAsync cancelled."); } catch (Exception exception) { Logger.Error( $"TranslateMovieAsync: {exception.Message}"); } finally { watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Logger.Debug( $"TranslateMovieAsync ({movieToTranslate.ImdbCode}) in {elapsedMs} milliseconds."); } }
/// <summary> /// Get the link to the youtube trailer of a movie /// </summary> /// <param name="movie">The movie</param> /// <param name="ct">Used to cancel loading trailer</param> /// <returns>Video trailer</returns> public async Task <string> GetMovieTrailerAsync(MovieJson movie, CancellationToken ct) { var watch = Stopwatch.StartNew(); var uri = string.Empty; try { var tmdbMovie = await TmdbClient.GetMovieAsync(movie.ImdbCode, MovieMethods.Videos); var trailers = tmdbMovie?.Videos; if (trailers != null && trailers.Results.Any()) { var restClient = new RestClient(Utils.Constants.PopcornApi); var request = new RestRequest("/{segment}/{key}", Method.GET); request.AddUrlSegment("segment", "trailer"); request.AddUrlSegment("key", trailers.Results.FirstOrDefault().Key); var response = await restClient.ExecuteTaskAsync <TrailerResponse>(request, ct); if (response.ErrorException != null) { throw response.ErrorException; } uri = response.Data?.TrailerUrl ?? string.Empty; } } catch (Exception exception) when(exception is TaskCanceledException) { Logger.Debug( "GetMovieTrailerAsync cancelled."); } catch (Exception exception) { Logger.Error( $"GetMovieTrailerAsync: {exception.Message}"); throw; } finally { watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Logger.Debug( $"GetMovieTrailerAsync ({movie.ImdbCode}) in {elapsedMs} milliseconds."); } return(uri); }
/// <summary> /// Get recommendations by page /// </summary> /// <param name="page"></param> /// <returns></returns> public async Task <(IEnumerable <MovieLightJson>, int nbMovies)> Discover(int page) { var discover = TmdbClient.DiscoverMoviesAsync(); var result = await discover.Query(page); var movies = new ConcurrentBag <MovieLightJson>(); await result.Results.ParallelForEachAsync(async movie => { var imdbMovie = await TmdbClient.GetMovieAsync(movie.Id); if (imdbMovie?.ImdbId == null) { return; } var fetch = await GetMovieLightAsync(imdbMovie.ImdbId); if (fetch != null) { movies.Add(fetch); } }); return(movies, result.TotalResults); }