public async Task <IActionResult> Get(string ytTrailerCode)
        {
            var cachedTrailer = _cachingService.GetCache(ytTrailerCode);

            if (cachedTrailer == null)
            {
                var trailer = await GetVideoInfoForStreamingAsync("http://www.youtube.com/watch?v=" + ytTrailerCode, YoutubeStreamingQuality.High);

                if (trailer != null && trailer.RequiresDecryption)
                {
                    await Task.Run(() => DownloadUrlResolver.DecryptDownloadUrl(trailer));

                    var response = new TrailerResponse {
                        TrailerUrl = trailer.DownloadUrl
                    };
                    _cachingService.SetCache(ytTrailerCode, JsonConvert.SerializeObject(response));
                    return(Json(response));
                }

                if (trailer != null && !trailer.RequiresDecryption)
                {
                    var response = new TrailerResponse {
                        TrailerUrl = trailer.DownloadUrl
                    };
                    _cachingService.SetCache(ytTrailerCode, JsonConvert.SerializeObject(response));
                    return(Json(response));
                }

                return(BadRequest());
            }

            return(Json(JsonConvert.DeserializeObject <TrailerResponse>(cachedTrailer)));
        }
Exemple #2
0
        /// <summary>
        /// Get the video url of the trailer by its Youtube key
        /// </summary>
        /// <param name="key">Youtube trailer key</param>
        /// <param name="ct">Cancellation token</param>
        /// <returns>Trailer url</returns>
        public async Task <string> GetVideoTrailerUrlAsync(string key, CancellationToken ct)
        {
            var watch = Stopwatch.StartNew();

            var wrapper = new TrailerResponse();

            var restClient = new RestClient(Constants.Constants.PopcornApi);
            var request    = new RestRequest("/{segment}/{key}", Method.GET);

            request.AddUrlSegment("segment", "trailer");
            request.AddUrlSegment("key", key);

            try
            {
                var response = await restClient.ExecuteGetTaskAsync <TrailerResponse>(request, ct);

                if (response.ErrorException != null)
                {
                    throw response.ErrorException;
                }

                wrapper = response.Data;
            }
            catch (Exception exception) when(exception is TaskCanceledException)
            {
                Logger.Debug(
                    "GetVideoTrailerUrlAsync cancelled.");
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"GetVideoTrailerUrlAsync: {exception.Message}");
                throw;
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"GetVideoTrailerUrlAsync ({key}) in {elapsedMs} milliseconds.");
            }

            return(wrapper?.TrailerUrl ?? string.Empty);
        }