-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRedditChecker.cs
136 lines (110 loc) · 3.11 KB
/
RedditChecker.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using MikuBot.Commands;
using MikuBot.Helpers;
using MikuBot.Modules;
using Rss;
using log4net;
namespace MikuBot.ExtraPlugins
{
public class RedditChecker : GenericModuleBase
{
private const string channelToPost = "#Vocaloid";
private static readonly TimeSpan checkFreq = TimeSpan.FromMinutes(5);
private static readonly ILog log = LogManager.GetLogger(typeof(RedditChecker));
private const string urlToCheck = "http://reddit.com/r/Vocaloid/.rss";
private readonly HashSet<string> checkedItems = new HashSet<string>(new CaseInsensitiveStringComparer());
private DateTime lastCheck = DateTime.Now;
public override string Name
{
get { return "RedditRSS"; }
}
public override InitialModuleStatus InitialStatus
{
get { return InitialModuleStatus.Disabled; }
}
public override string HelpText
{
get { return "Checks Reddit RSS for new posts."; }
}
private void Check(Receiver receiver)
{
var feed = RssFeed.Read(urlToCheck);
if (feed.Exceptions.LastException != null)
{
log.Warn("Unable to read feed", feed.Exceptions.LastException);
return;
}
if (feed.Channels.Count == 0)
{
log.Warn("Feed contains no channels");
return;
}
var channel = feed.Channels[0];
var items = channel.Items.Cast<RssItem>().Take(3);
var newItems = items.Where(i => !checkedItems.Contains(i.Link.ToString())).ToArray();
foreach (var item in newItems)
{
checkedItems.Add(item.Link.ToString());
if (receiver != null)
{
var shortLink = GetShortLink(item.Link.ToString());
receiver.Msg(string.Format("New r/Vocaloid post: {0}{1}{0} - {2}",
Formatting.Bold, item.Title, shortLink));
}
}
lastCheck = DateTime.Now;
}
private string GetShortLink(string link)
{
return link;
/*var url = string.Format("http://api.waa.ai/?url={0}", link);
var request = WebRequest.Create(url);
try {
using (var response = request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream())) {
return reader.ReadLine();
}
} catch (WebException x) {
log.Warn("Unable to get shortened URL", x);
return link;
}*/
}
public override void HandleCommand(IrcCommand command, IBotContext bot)
{
bool check = false;
if (command is MsgCommand)
{
var msg = (MsgCommand)command;
if (msg.BotCommand.Is(Name) && command.Sender.UserLevel >= BotUserLevel.Manager)
{
if (msg.BotCommand.Params.ParamOrEmpty(0) == "clear")
{
checkedItems.Clear();
}
else
{
check = true;
}
}
}
if (DateTime.Now - lastCheck > checkFreq)
check = true;
if (check)
{
var receiver = new Receiver(bot.Writer, new IrcName(channelToPost));
Task.Factory.StartNew(() => Check(receiver)) // Async version
.ContinueWith(TaskHelper.HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
}
}
public override void OnLoaded(IBotContext bot, IModuleFile moduleFile)
{
Check(null);
}
}
}