/// <summary> /// Get the link to the youtube trailer of a show /// </summary> /// <param name="show">The show</param> /// <param name="ct">Used to cancel loading trailer</param> /// <returns>Video trailer</returns> public async Task <string> GetShowTrailerAsync(ShowJson show, CancellationToken ct) { var timeoutPolicy = Policy.TimeoutAsync(Utils.Constants.DefaultRequestTimeoutInSecond, TimeoutStrategy.Pessimistic); try { return(await timeoutPolicy.ExecuteAsync(async cancellation => { var watch = Stopwatch.StartNew(); var uri = string.Empty; try { var shows = await(await _tmdbService.GetClient).SearchTvShowAsync(show.Title); if (shows.Results.Any()) { Video trailer = null; foreach (var tvShow in shows.Results) { try { var result = await(await _tmdbService.GetClient).GetTvShowExternalIdsAsync(tvShow.Id); if (result.ImdbId == show.ImdbId) { var videos = await(await _tmdbService.GetClient).GetTvShowVideosAsync(result.Id); if (videos != null && videos.Results.Any()) { trailer = videos.Results.FirstOrDefault(); } } } catch (Exception ex) { Logger.Error(ex); } } if (trailer != null) { using (var service = Client.For(YouTube.Default)) { var videos = (await service .GetAllVideosAsync("https://youtube.com/watch?v=" + trailer.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 || exception is OperationCanceledException) { Logger.Debug( "GetShowTrailerAsync cancelled."); } catch (Exception exception) { Logger.Error( $"GetShowTrailerAsync: {exception.Message}"); throw; } finally { watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Logger.Trace( $"GetShowTrailerAsync ({show.ImdbId}) in {elapsedMs} milliseconds."); } return uri; }, ct)); } catch (Exception ex) { Logger.Error(ex); throw; } }
/// <summary> /// Get the link to the youtube trailer of a show /// </summary> /// <param name="show">The show</param> /// <param name="ct">Used to cancel loading trailer</param> /// <returns>Video trailer</returns> public async Task <string> GetShowTrailerAsync(ShowJson show, CancellationToken ct) { var watch = Stopwatch.StartNew(); var uri = string.Empty; try { var shows = await TmdbClient.SearchTvShowAsync(show.Title); if (shows.Results.Any()) { Video trailer = null; await shows.Results.ParallelForEachAsync(async tvShow => { var result = await TmdbClient.GetTvShowExternalIdsAsync(tvShow.Id); if (result.ImdbId == show.ImdbId) { var videos = await TmdbClient.GetTvShowVideosAsync(result.Id); if (videos != null && videos.Results.Any()) { trailer = videos.Results.FirstOrDefault(); } } }, ct); if (trailer != null) { using (var service = Client.For(YouTube.Default)) { var videos = (await service.GetAllVideosAsync("https://youtube.com/watch?v=" + trailer.Key)) .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(); } } } } } catch (Exception exception) when(exception is TaskCanceledException || exception is OperationCanceledException) { Logger.Debug( "GetShowTrailerAsync cancelled."); } catch (Exception exception) { Logger.Error( $"GetShowTrailerAsync: {exception.Message}"); throw; } finally { watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Logger.Debug( $"GetShowTrailerAsync ({show.ImdbId}) in {elapsedMs} milliseconds."); } return(uri); }