-
Notifications
You must be signed in to change notification settings - Fork 77
/
Track Subreddit Daily Comments.cs
60 lines (54 loc) · 2.63 KB
/
Track Subreddit Daily Comments.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Reddit;
using Reddit.Controllers;
using Reddit.Exceptions;
using Reddit.Inputs;
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var reddit = new RedditClient("YourRedditAppID", "YourBotUserRefreshToken");
// reddit.Subreddit("MySubreddit") loads an empty named subreddit instance, then About() queries Reddit and returns the data. --Kris
var subreddit = reddit.Subreddit("MySubreddit").About();
/*
* Gather all of yesterday's posts and add the total comments. Note the use of "after" for pagination, as the API is limited to a maximum of 100 results per query.
* The API sometimes returns posts slightly out of order (don't ask me why), so this will keep going until it gets 3 or more consecutive posts outside the date range. It's an arbitrary number but should be sufficient.
* Loop will timeout after 5 minutes, just to be safe.
*
* --Kris
*/
int totalComments = 0;
int outdatedPosts = 0;
string after = "";
DateTime start = DateTime.Now;
do
{
foreach (Post post in subreddit.Posts.GetNew(new CategorizedSrListingInput(after: after, limit: 100)))
{
// Keep going until we hit 3 posts in a row from the day before yesterday. Today's posts are completely ignored. --Kris
if (post.Created >= DateTime.Today.AddDays(-1) && post.Created < DateTime.Today)
{
outdatedPosts = 0;
totalComments += post.Listing.NumComments;
}
else if (post.Created < DateTime.Today.AddDays(-1))
{
outdatedPosts++;
}
after = post.Fullname;
}
} while (outdatedPosts < 3 && start.AddMinutes(5) > DateTime.Now);
// Create a new self-post to report the result. --Kris
var newPost = subreddit.SelfPost("Total Comments for " + DateTime.Today.AddDays(-1).ToString("D"), totalComments.ToString()).Submit();
// Update the sidebar to reflect yesterday's total. --Kris
subreddit.Sidebar = "**Yesterday's Comments Total:** " + totalComments.ToString();
try
{
subreddit.Update(); // Sends the subreddit data with the updated sidebar text back to the Reddit API to apply the change. --Kris
}
catch (RedditControllerException) { }
}
}
}