-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkFlow.cs
139 lines (121 loc) · 4.15 KB
/
WorkFlow.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Reddit;
using Reddit.Controllers;
using System;
using System.Collections.Generic;
namespace RedditBot
{
/// <summary>
/// https://github.com/sirkris/Reddit.NET#running-the-tests
/// </summary>
public class WorkFlow
{
private RedditClient Reddit;
private int NumComments = 0;
private HashSet<string> CommentIds;
public WorkFlow(string[] args)
{
string appId = args[0];
string refreshToken = args[1];
string accessToken = (args.Length > 2 ? args[2] : null);
Reddit = new RedditClient(appId: appId, refreshToken: refreshToken, accessToken: accessToken);
CommentIds = new HashSet<string>();
}
/// <summary>
/// Gets post object from url
/// </summary>
/// <param name="url"></param>
/// <returns>Returns null if url is incorrect, or user is unauthorized</returns>
public Post ParsePostFromUrl(string url)
{
try
{
string subreddit = url.Split("r/")[1];
subreddit = subreddit.Split("/")[0];
string postId = url.Split("comments/")[1];
postId = postId.Split("/")[0];
postId = "t3_" + postId;
return Reddit.Subreddit(subreddit).Post(postId).About();
}
catch
{
return null;
}
}
public string MassReply(Post post, string[] keywords, string replyMsg)
{
return "Replied to " + IterateCommentsAndReply(post.Comments.GetNew(limit: 1500), keywords, replyMsg) + " Comments.";
}
private void ShowComment(Comment comment, int depth = 0)
{
if (comment == null || string.IsNullOrWhiteSpace(comment.Author))
{
return;
}
NumComments++;
if (!CommentIds.Contains(comment.Id))
{
CommentIds.Add(comment.Id);
}
if (depth.Equals(0))
{
Console.WriteLine("---------------------");
}
else
{
for (int i = 1; i <= depth; i++)
{
Console.Write("> ");
}
}
Console.WriteLine("[" + comment.Author + "] " + comment.Body);
}
private int IterateCommentsAndReply(IList<Comment> comments, string[] keywords, string replyMsg, int maxReplies = 3,int depth = 0)
{
int numTimesReplied = 0;
foreach (Comment comment in comments)
{
if (numTimesReplied > maxReplies)
return numTimesReplied;
if(comment.Author.ToLower() != Program.BotUsername && DoesCommentContain(comment, keywords))
{
comment.Reply(replyMsg);
Console.WriteLine("Replied to comment: " + comment.Body);
numTimesReplied++;
}
//ShowComment(comment, depth);
IterateCommentsAndReply(comment.Replies,keywords, replyMsg, (depth + 1));
IterateCommentsAndReply(GetMoreChildren(comment), keywords, replyMsg, depth);
}
return numTimesReplied;
}
private bool DoesCommentContain(Comment comment, string[] keywords)
{
foreach(string str in keywords)
{
if (comment.Body.ToLower().IndexOf(str) != -1)
{
return true;
}
}
return false;
}
private IList<Comment> GetMoreChildren(Comment comment)
{
List<Comment> res = new List<Comment>();
if (comment.More == null)
{
return res;
}
foreach (Reddit.Things.More more in comment.More)
{
foreach (string id in more.Children)
{
if (!CommentIds.Contains(id))
{
}
}
}
return res;
}
}
}