コード例 #1
0
        //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 : "";
        }
コード例 #2
0
        async Task AddRequesAsync(RequestViewModel model)
        {
            var comment = await _commentRepo.GetOneAsync(x => x.Url == model.RequestUrl);

            if (comment != null)
            {
                //if(comment.Fetched)
                //    if(comment.UpdatedDate?.AddHours(2).Date <= DateTime.UtcNow.Date)
                //    {
                //        comment.Fetched = false;
                //        _commentRepo.Update(comment);
                //    }
                _commentRequestRepo.Create(new CommentRequest
                {
                    dateRequested = DateTime.UtcNow,
                    emailAddress  = model.Email,
                    emailed       = false,
                    CommentId     = comment.Id,
                });
                await _commentRequestRepo.SaveAsync();
            }
            else
            {
                comment = new Data.Entity.Comment
                {
                    Url       = model.RequestUrl,
                    DateAdded = DateTime.UtcNow,
                    Fetched   = false,
                    Disabled  = false
                };

                comment.CommentRequests = new List <CommentRequest>
                {
                    new CommentRequest
                    {
                        dateRequested = DateTime.UtcNow,
                        emailAddress  = model.Email,
                        emailed       = false,
                    }
                };
                _commentRepo.Create(comment);
                await _commentRepo.SaveAsync();
            }
        }