Beispiel #1
0
        public async Task <IActionResult> Detail(int id)
        {
            ViewBag.username = User.Identity.Name;
            User user = await db.Users.FirstOrDefaultAsync(u => u.Email == User.Identity.Name);

            var userMovie = db.UsersMovies.Where(
                um => um.user == user && um.movie_id == id).ToList().FirstOrDefault();

            if (userMovie != null)
            {
                ViewBag.buttonCaption = "REMOVE FROM FAVOURITES";
            }
            else
            {
                ViewBag.buttonCaption = "ADD TO FAVOURITES";
            }
            //get movie detail
            var response = client.
                           GetStringAsync("https://api.themoviedb.org/3/movie/"
                                          + id
                                          + "?api_key=30c4ec1f7ead936d610a56b54bc4bbd4");
            var data = response.Result;

            ViewBag.str = data;
            MovieDetail movie = JsonConvert.DeserializeObject <MovieDetail>(data);

            //get video key
            response = client.
                       GetStringAsync(
                "https://api.themoviedb.org/3/movie/" + id + "/videos?api_key=30c4ec1f7ead936d610a56b54bc4bbd4&language=en-US");

            data = response.Result;
            VideoRoot vr = JsonConvert.DeserializeObject <VideoRoot>(data);

            if (vr.results.Count != 0)
            {
                ViewBag.videoKey = vr.results.FirstOrDefault().key;
            }
            else
            {
                ViewBag.videoKey = "";
            }
            ViewBag.bgImage = "https://image.tmdb.org/t/p/w500/" + movie.poster_path;
            return(View(movie));
        }
        private async Task <List <LivestreamModel> > GetLivestreamModels(ChannelIdentifier channelIdentifier, List <string> videoIds, CancellationToken cancellationToken)
        {
            var livestreamModels = new List <LivestreamModel>();

            foreach (var videoId in videoIds)
            {
                LiveStreamingDetails livestreamDetails = null;
                VideoRoot            videoRoot         = null;

                int retryCount = 0;
                while (retryCount < 3 && livestreamDetails == null)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(livestreamModels);
                    }

                    try
                    {
                        videoRoot = await youtubeClient.GetLivestreamDetails(videoId, cancellationToken);

                        livestreamDetails = videoRoot.Items?.FirstOrDefault()?.LiveStreamingDetails;
                    }
                    catch (HttpRequestWithStatusException ex) when(ex.StatusCode == HttpStatusCode.ServiceUnavailable)
                    {
                        await Task.Delay(2000, cancellationToken);
                    }
                    catch (HttpRequestWithStatusException)
                    {
                        // can happen in the case of the video being removed
                        // the youtube api will report the videoid as live but looking up the videoid will fail with BadRequest
                        break;
                    }
                    retryCount++;
                }

                if (livestreamDetails == null)
                {
                    continue;
                }

                var snippet = videoRoot.Items?.FirstOrDefault()?.Snippet;
                if (snippet == null)
                {
                    continue;
                }

                var livestreamModel = new LivestreamModel(videoId, channelIdentifier)
                {
                    Live = snippet.LiveBroadcastContent != "none"
                };
                if (!livestreamModel.Live)
                {
                    continue;
                }

                livestreamModel.DisplayName   = snippet.ChannelTitle;
                livestreamModel.Description   = snippet.Title?.Trim();
                livestreamModel.ThumbnailUrls = new ThumbnailUrls()
                {
                    Small  = snippet.Thumbnails?.Standard?.Url,
                    Large  = snippet.Thumbnails?.High?.Url,
                    Medium = snippet.Thumbnails?.Medium?.Url
                };

                livestreamModel.Viewers = livestreamDetails.ConcurrentViewers;

                if (livestreamDetails.ActualStartTime.HasValue)
                {
                    livestreamModel.StartTime = livestreamDetails.ActualStartTime.Value;
                    livestreamModels.Add(livestreamModel);
                }
            }

            return(livestreamModels);
        }