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."); }
public void RedditCommentReaderCanRetrieveMostRecentComment() { RedditHttpsReader reader = new RedditHttpsReader("chefknives"); RedditComment comment = reader.GetRecentComments(1).FirstOrDefault(); Assert.IsNotNull(comment); Assert.IsNotNull(comment.Author); Assert.IsNotNull(comment.Body); Assert.IsNotNull(comment.Id); Assert.IsNotNull(comment.PostLinkId); }
public void AllRedditCommentsShouldHaveT3InLinkId() { RedditHttpsReader reader = new RedditHttpsReader("chefknives"); var comments = reader.GetRecentComments(100); Assert.AreEqual(100, comments.Count()); foreach (RedditComment comment in comments) { Assert.IsNotNull(comment); Assert.IsNotNull(comment.PostLinkId); Assert.IsTrue(comment.PostLinkId.Contains("t3_"), $"{comment.PostLinkId} is expected to contain \"t3_\""); } }
public void RedditCommentReaderCanRetrieveMostRecent10Comments() { RedditHttpsReader reader = new RedditHttpsReader("chefknives"); var comments = reader.GetRecentComments(10); Assert.AreEqual(10, comments.Count()); foreach (RedditComment comment in comments) { Assert.IsNotNull(comment); Assert.IsNotNull(comment.Author); Assert.IsNotNull(comment.Body); Assert.IsNotNull(comment.Id); Assert.IsNotNull(comment.PostLinkId); } }