//public override async Task<string> FetchComments(Data.Entity.Comment comment)
        public override async Task <CommentDetails> FetchComments(Data.Entity.Comment comment)
        {
            _logger.LogInformation($"Youtube Started: {comment.Url}");
            #region initialize
            var videoId  = GetVideoId(comment.Url);
            var query    = BuildQuery(videoId);
            var response = await _httpClient.GetAsync(query);

            #endregion

            #region Failed Fetch
            if (response.StatusCode == HttpStatusCode.Forbidden)
            {
                _logger.LogInformation($"Youtube: {response.StatusCode.ToString()}");
                return(default(CommentDetails));
            }
            #endregion

            #region needed params
            var    fileName        = Guid.NewGuid().ToString().Replace("-", "").ToUpper() + ".csv";
            var    currentResponse = new YouTubeCommentSet();
            bool   hasMore         = false;
            string nextPageToken   = "";
            var    refinedComments = new List <RefinedComment>();
            #endregion

            do
            {
                if (hasMore)
                {
                    var newQuery = AddNextPageToken(query, nextPageToken);
                    response = await _httpClient.GetAsync(newQuery);
                }
                currentResponse = await response.Content.ReadAsAsync <YouTubeCommentSet>();

                var snippets = currentResponse.Items.Select(e => GetRefinedComment(e, videoId));
                refinedComments.AddRange(snippets);
                nextPageToken = currentResponse.NextPageToken;
                hasMore       = !string.IsNullOrEmpty(nextPageToken);
            } while (hasMore);

            var fileSaved = SaveToFile(fileName, refinedComments);
            _logger.LogInformation($"fetching successfull for {comment.Url}, {response.StatusCode}");
            var details = new CommentDetails
            {
                Filename = fileSaved ? fileName : "",
                Name     = await VideoName(videoId),
                NOC      = refinedComments.Count
            };
            return(details);
            //return fileSaved ? fileName : "";
        }
        private async Task GetYouTubeCommentAsync(string videoId)
        {
            var query    = string.Format(_endpoint, videoId);
            var response = await _httpClient.GetAsync(query);

            if (response.StatusCode == HttpStatusCode.Forbidden)
            {
                return;
            }

            var currentResponse = new YouTubeCommentSet();
            var refinedComments = new List <RefinedComment>();

            currentResponse = await response.Content.ReadAsAsync <YouTubeCommentSet>();

            var snippets = currentResponse.Items.Select(e => GetRefinedComment(e, videoId));

            refinedComments.AddRange(snippets);

            ViewBag.refinedComments = refinedComments;
        }
Ejemplo n.º 3
0
        public override async Task <string> FetchComments(CommentsRequest request)
        {
            #region initialize
            var videoId  = GetVideoId(request.RequestUrl);
            var query    = BuildQuery(videoId);
            var response = await _httpClient.GetAsync(query);

            #endregion

            #region needed params
            var    fileName        = request.Id + ".csv";
            var    currentResponse = new YouTubeCommentSet();
            bool   hasMore         = false;
            string nextPageToken   = "";
            var    refinedComments = new List <RefinedComment> ();
            #endregion

            do
            {
                if (hasMore)
                {
                    var newQuery = AddNextPageToken(query, nextPageToken);
                    response = await _httpClient.GetAsync(newQuery);
                }
                currentResponse = await response.Content.ReadAsAsync <YouTubeCommentSet> ();

                var snippets = currentResponse.Items.Select(e => GetRefinedComment(e, videoId));
                refinedComments.AddRange(snippets);
                nextPageToken = currentResponse.NextPageToken;
                hasMore       = !string.IsNullOrEmpty(nextPageToken);
            } while (hasMore);

            var fileSaved = SaveToFile(fileName, refinedComments);
            _logger.LogInformation($"fetching successfull for {request.RequestUrl}, {response.StatusCode}");
            return(fileSaved ? fileName : "");
        }