-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
386 lines (373 loc) · 16.2 KB
/
Program.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// The Lemmy.NET version we are using is custom as the current Lemmy.NET API is a very work in progress piece ( although its awsome and im super greatfull for it)
// but this means it is missing a decent amount of functionality we need. Since its in very active development i dont want to step on their toes and im just going to use
// a forked version for right now
using Lemmy.Net.Client.Models;
using SauceNET;
using Slko.TraceMoeNET;
internal class SauceNao
{
private HashSet<string> alreadyTaggedIds;
private string SauceNaoAPIKey = "";
private int sauceNaoBotId = 182352;
private string strWorkPath;
private string TraceMoeApiKey = "";
public static void Main(String[] args)
{
SauceNao s = new SauceNao();
s.Init();
//s.CombineHashFiles(Path.Combine(s.strWorkPath, "AlreadyTaggedIds2"), Path.Combine(s.strWorkPath, "AlreadyTaggedIds"));
//s.ResyncTaggedFile();
if (args.Length == 0)
args = new string[1] { "AllTime" };
if (args[0] == "Upkeep")
{
s.Upkeep();
}
else if (args[0] == "AllTime")
{// over time this mode should probably be removed as im not sure it serves much point to label past posts.
s.RunForAllTime();
s.Upkeep();
}
}
private void CombineHashFiles(string file1, string file2)
{
alreadyTaggedIds = new HashSet<string>(File.ReadAllLines(file1));
var alreadyTaggedIds2 = new HashSet<string>(File.ReadAllLines(file2));
foreach (var item in alreadyTaggedIds2)
{
if (!alreadyTaggedIds.Contains(item))
{
alreadyTaggedIds.Add(item);
}
}
File.WriteAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds"), alreadyTaggedIds);
}
private void IdentifyAndTagPost(Post post)
{
//Get the sauce and comment it
try
{
string image = post.Url;
if (image == null)
{
// no post url, maybe its in the body?
string bodyContent = post.Body;
if (bodyContent.Contains("http"))
{// pretty sure theres a image in there lets grab the first one.
int startindex = bodyContent.IndexOf("http");
string bodycontent2 = bodyContent.Substring(startindex);
string hopefullLink = bodycontent2.Substring(0, bodycontent2.IndexOf(')'));
image = hopefullLink;
}
}
string sauceNaoMessage = "";
try
{
sauceNaoMessage = SauceNaoImage(image);
if (sauceNaoMessage.Contains("hard time finding your image") && image != null)
{
Console.WriteLine("Unsupported File Type like a video:" + image);
alreadyTaggedIds.Add(post.Id.ToString());
File.WriteAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds"), alreadyTaggedIds);
sauceNaoMessage = "";
}
}
catch (Exception ex)
{
Console.WriteLine("Exception hit trying to read post ? unsure why");
alreadyTaggedIds.Add(post.Id.ToString());
File.WriteAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds"), alreadyTaggedIds);
}
string overallMessage = "";
if (sauceNaoMessage != "")
{
string traceMoeImage = TraceMoeImage(image);
overallMessage = sauceNaoMessage + "\r\n\r\n" + traceMoeImage;
LemmySauceNao.Models.LemmyService.CommentOnPost(post.Id, overallMessage);
alreadyTaggedIds.Add(post.Id.ToString());
File.WriteAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds"), alreadyTaggedIds);
}
}
catch (Exception ex)
{
if (ex.ToString().ToLower().Contains("deleted"))
{
alreadyTaggedIds.Add(post.Id.ToString());
File.WriteAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds"), alreadyTaggedIds);
}
else if (ex.Message.Contains("Length cannot be less than zero"))
{
//cant find any sort of image. just move on
alreadyTaggedIds.Add(post.Id.ToString());
File.WriteAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds"), alreadyTaggedIds);
}
}
}
private void Init()
{
string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
strWorkPath = System.IO.Path.GetDirectoryName(strExeFilePath);
SauceNaoAPIKey = File.ReadAllText(Path.Combine(strWorkPath, "SauceNaoAPIKey.txt"));
TraceMoeApiKey = File.ReadAllText(Path.Combine(strWorkPath, "TraceMoeApiKey.txt"));
LemmySauceNao.Models.LemmyService.Init();
//ResyncTaggedFile();
}
private void ResyncTaggedFile()
{
// this sucks. i cant find any way to get comments by user... so we are just getting all comments on subs it moderates, then sorting it by user here... only call if the tagged file gets out of sync somehow.
alreadyTaggedIds = new HashSet<string>();
List<Community> communities = LemmySauceNao.Models.LemmyService.GetCommunitiesModdedBy(sauceNaoBotId);
foreach (var community in communities)
{
var comments = LemmySauceNao.Models.LemmyService.GetCommentsBySub(community.Name);
foreach (var comment in comments)
{
if (comment.CreatorId == sauceNaoBotId && !comment.Deleted)
alreadyTaggedIds.Add(comment.PostId.ToString());
}
}
File.WriteAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds"), alreadyTaggedIds);
}
private void RunForAllTime()
{
alreadyTaggedIds = new HashSet<string>(File.ReadAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds")));
List<Community> communities = LemmySauceNao.Models.LemmyService.GetCommunitiesModdedBy(sauceNaoBotId);
var messages = LemmySauceNao.Models.LemmyService.GetMessages();
List<string> removedAlready = new List<string>();
foreach (var message in messages)
{
try
{
int indexOfSeperator = message.content.IndexOf(":");
string sub = message.content.Substring(0, indexOfSeperator);
bool enable = message.content.Substring(indexOfSeperator + 1).ToLower() == "enable";
bool disable = message.content.Substring(indexOfSeperator + 1).ToLower() == "disable";
if (enable)
{
bool found = false;
foreach (var comunity in communities)
{
if (comunity.Name.ToLower() == sub.ToLower())
{
found = true;
break;
}
}
if (!found && !removedAlready.Contains(sub))
communities.Add(new Community() { Name = sub });
else if (!found)
removedAlready.Remove(sub);// if we found it after removing it, remove it from the removed already list incase we get another enable.
}
else if (disable)
{
removedAlready.Add(sub);
}
}
catch (Exception ex)
{
}
}
foreach (var community in communities)
{
var posts = LemmySauceNao.Models.LemmyService.GetAllPostsForCommunityName(community.Name);
if (posts[0].community_id != 6)
{
foreach (var post in posts)
{
if (!alreadyTaggedIds.Contains(post.Id.ToString()) && !post.Deleted)
{
Console.WriteLine($"Checking a post in {community.Name} on post {post.Name}");
IdentifyAndTagPost(post);
}
}
}
else
{
}
}
}
private string SauceNaoImage(string image)
{
var client = new SauceNETClient(SauceNaoAPIKey);
Task<SauceNET.Model.Sauce> sauceTask = client.GetSauceAsync(image);
sauceTask.Wait();
SauceNET.Model.Sauce sauce = sauceTask.Result;
string message = "";
if (sauce.Message.Contains("Too many failed search"))
{
Console.WriteLine("Too many failed searches, waiting a while");
Thread.Sleep(30000);
}
else if (sauce.Message.Contains("Specified file does not seem to be an image"))
{
Console.WriteLine("Specified File was not an image (hit gallery?)");
message = "Im sorry, this post doesnt seem to be an image (It may be something like a reddit gallery?) I can not currently check these :/";
}
else if (sauce.Message.Contains("Search Rate Too High"))
{
Console.WriteLine("Searching too fast, waiting the 30 seconds");
Thread.Sleep(30000);
}
else if (sauce.Message.Contains("Problem with remote server"))
{
message = "Im sorry, but i cant seem to access that link? due to a problem on the remote server?";
}
else if (sauce.Message.Contains("You need an Image"))
{
message = "Im sorry ive let ya down, Im having a hard time finding your image. Please send this post to u/paddedperson if you think this is an error so he can fix his code!";
}
else if (sauce.Message.Contains("Specified file no longer exists on the remote server"))
{
message = "Hey i got a 404 trying to access that image, are you sure it still exists?";
}
else if (sauce.Message.ToLower().Contains("dimensions too small"))
{
message = "Sauce Nao does not support images with dimensions this small, sorry.";
}
else if (sauce.Message != "")
{
Console.WriteLine("Some unknown error has occured with saucenao:" + sauce.Message);
}
else if (sauce.Results.Count > 0)
{
List<Tuple<string, string>> properties = new List<Tuple<string, string>>();
properties.Add(new Tuple<string, string>("SauceNao SourceURL", sauce.Results[0].SourceURL));
foreach (var prop in sauce.Results[0].Properties)
{
properties.Add(new Tuple<string, string>(prop.Name, prop.Value));
}
properties.Add(new Tuple<string, string>("Similarity", sauce.Results[0].Similarity));
if (double.Parse(sauce.Results[0].Similarity) > 80)
{
Console.WriteLine("Found a solid match! \r\n \r\n");
message = $"Im pretty sure I found the sauce! \r\n \r\n";
foreach (var prop in properties)
{
message += $"- {prop.Item1}:{prop.Item2}\r\n";
}
}
else if (double.Parse(sauce.Results[0].Similarity) > 60)
{
Console.WriteLine("Found a match! \r\n \r\n");
message = $"I think I found the sauce! \r\n \r\n";
foreach (var prop in properties)
{
message += $"- {prop.Item1}:{prop.Item2}\r\n";
}
}
else
{
Console.WriteLine("Found a possible match! \r\n \r\n");
message = $"I might have found the sauce, but im unsure. \r\n \r\n";
foreach (var prop in properties)
{
message += $"- {prop.Item1}:{prop.Item2}\r\n";
}
}
}
else
{
Console.WriteLine("didnt find a match :/");
message = "Im sorry, I could not find a sauce for this post using SauceNao. I'll try to do better next time ;-;";
}
return message;
}
private string TraceMoeImage(string? image)
{
string message = "";
try
{
using TraceMoeClient moeApi = new TraceMoeClient(TraceMoeApiKey);
var task = moeApi.SearchByURLAsync(image);
task.Wait();
var result = task.Result;
if (result.error == "")
{
if (result.result.Count > 0)
{
message += $"Found On Trace.Moe with a similarity of {Math.Round(result.result[0].similarity, 2)}: ";
message += $"- Title English:{result.result[0].anilist.title.english} \r\n";
message += $"- Title Romaji:{result.result[0].anilist.title.romaji} \r\n";
message += $"- Title Native:{result.result[0].anilist.title.english} \r\n";
message += $"- anilist Id:{result.result[0].anilist.id} \r\n";
}
else
{
message = "Unable to find this on Trace.moe, sorry about that";
}
}
else
{
message = "Ran into an unhandled exception when running this through Trace.Moe. sorry";
}
}
catch (Exception ex)
{
message = "Ran into an unhandled exception when running this through Trace.Moe. sorry";
}
return message;
}
private void Upkeep()
{
Console.WriteLine("Moving Into Upkeep Mode");
while (true)
{
Console.WriteLine("Beginging a scan");
alreadyTaggedIds = new HashSet<string>(File.ReadAllLines(Path.Combine(strWorkPath, "AlreadyTaggedIds")));
List<Community> communities = LemmySauceNao.Models.LemmyService.GetCommunitiesModdedBy(sauceNaoBotId); var messages = LemmySauceNao.Models.LemmyService.GetMessages();
List<string> removedAlready = new List<string>();
foreach (var message in messages)
{
try
{
int indexOfSeperator = message.content.IndexOf(":");
string sub = message.content.Substring(0, indexOfSeperator).Trim();
bool enable = message.content.Substring(indexOfSeperator + 1).Trim().ToLower() == "enable";
bool disable = message.content.Substring(indexOfSeperator + 1).Trim().ToLower() == "disable";
if (enable)
{
bool found = false;
foreach (var comunity in communities)
{
if (comunity.Name.ToLower() == sub.ToLower())
{
found = true;
break;
}
}
if (!found && !removedAlready.Contains(sub))
communities.Add(new Community() { Name = sub });
else if (!found)
removedAlready.Remove(sub);// if we found it after removing it, remove it from the removed already list incase we get another enable.
}
else if (disable)
{
removedAlready.Add(sub);
}
}
catch (Exception ex)
{
}
}
foreach (var community in communities)
{
var posts = LemmySauceNao.Models.LemmyService.GetNewest50PostsByCommunityName(community.Name);
if (posts[0].community_id != 6)
{
foreach (Post post in posts)
{
if (!alreadyTaggedIds.Contains(post.Id.ToString()) && !post.Deleted)
{
Console.WriteLine($"Checking a post in {community.Name} on post {post.Name}");
IdentifyAndTagPost(post);
}
}
Thread.Sleep(10000);
}
else
{
}
}
}
}
}