public async Task Execute() { var commentCount = 0; var postCount = 0; Console.WriteLine("Pulling comments for known posts..."); var posts = await _postDatabase.GetAll(); foreach (var post in posts) { postCount++; // Because we seed with one invalid post object if (string.IsNullOrEmpty(post.Kind)) { continue; } var postController = _redditClient.Post(post.Fullname); var comments = postController.Comments.GetComments(); foreach (var comment in comments) { commentCount++; var redditComment = comment.ToComment(); _commentDatabase.Upsert(redditComment); } Console.WriteLine($"Post {post.Id} had {comments.Count} comments. Post created: {post.CreateDate}"); } Console.WriteLine($"Pulled {commentCount} comments from {postCount} posts"); }
public void PullCommentsAndPosts(int postCount = 100, int commentCount = 500) { _logger.Information($"Pulling {postCount} posts and {commentCount} comments. Interval: {_commentAndPostPullIntervalMinutes} minutes"); var redditReader = new RedditHttpsReader(subreddit: Subreddit.Name); var recentPosts = redditReader.GetRecentPosts(numPosts: postCount); foreach (var postToUpdate in recentPosts) { var updatedPost = RedditPostDatabase.Upsert(postToUpdate); // If process old posts is enabled, re-trigger the handlers for those posts if (_processOldPosts) { if (updatedPost != null && postToUpdate.Flair != updatedPost.Flair) { PostHandlers.ForEach(c => { _logger.Information($"Reprocessing post {postToUpdate.Title} from original flair: {updatedPost.Flair} to new flair: {postToUpdate.Flair}"); var postController = RedditClient.Post(updatedPost.Fullname).Info(); c.Process(postController); }); } } } var recentComments = redditReader.GetRecentComments(numComments: commentCount); RedditCommentDatabase.Upsert(recentComments); var tries = 0; var count = 0; var oldestComment = recentComments.First(); while (count < commentCount && tries < 10) { var newComments = new List <SubredditBot.Data.Comment>(); foreach (var comment in recentComments) { if (comment.CreateDate < oldestComment.CreateDate) { oldestComment = comment; } newComments.Add(comment); count++; } RedditCommentDatabase.Upsert(newComments); recentComments = redditReader.GetRecentComments(numComments: commentCount, after: oldestComment.Fullname); tries++; } _logger.Information($"Finished pulling posts and comments."); }