-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMikuDBChecker.cs
186 lines (142 loc) · 4.84 KB
/
MikuDBChecker.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MikuBot.Modules;
using MikuBot.Commands;
using HtmlAgilityPack;
using System.Net;
using log4net;
using System.IO;
using MikuBot.ExtraPlugins.Helpers;
using System.Threading.Tasks;
using MikuBot.Helpers;
namespace MikuBot.ExtraPlugins
{
/*
public class MikuDBChecker : GenericModuleBase {
private class MikuDbAlbum {
public MikuDbAlbum(string name, string postLink, string dlLink) {
Name = name;
DlLink = dlLink;
PostLink = postLink;
}
public string DlLink { get; private set; }
public string Name { get; private set; }
public string PostLink { get; private set; }
}
private const string channelToPost = "#mikuchan";
private static readonly TimeSpan checkFreq = TimeSpan.FromMinutes(5);
private static readonly ILog log = LogManager.GetLogger(typeof(MikuDBChecker));
private const string urlToCheck = "http://mikudb.com/vocaloid-albums/";
private readonly HashSet<string> checkedAlbums = new HashSet<string>(new CaseInsensitiveStringComparer());
private DateTime lastCheck = DateTime.Now;
private void Check(Receiver receiver) {
var request = WebRequest.Create(urlToCheck);
request.Timeout = 5000;
WebResponse response;
try {
response = request.GetResponse();
} catch (WebException x) {
log.Error("Unable to download albums list", x);
return;
}
var enc = response.Headers[HttpResponseHeader.ContentEncoding];
MikuDbAlbum[] albums;
try {
using (var stream = response.GetResponseStream()) {
albums = GetAlbums(stream, enc);
}
} finally {
response.Close();
}
if (albums.Any()) {
var newAlbums = albums.Where(a => !checkedAlbums.Contains(a.Name)).ToArray();
foreach (var album in newAlbums) {
checkedAlbums.Add(album.Name);
if (receiver != null)
receiver.Msg(string.Format("New album on MikuDB: {0}{1}{0} - {2} - {3}", Formatting.Bold, album.Name, album.PostLink, album.DlLink));
}
}
lastCheck = DateTime.Now;
}
private MikuDbAlbum[] GetAlbums(Stream htmlStream, string encStr) {
var encoding = Helpers.ParseHelper.GetEncoding(encStr);
var doc = new HtmlDocument();
doc.Load(htmlStream, encoding);
var albums = new List<MikuDbAlbum>();
var posts = doc.DocumentNode.SelectNodes("//div[@id = 'content']/div[contains(@class, 'post')]");
if (posts == null)
return albums.ToArray();
foreach (var post in posts) {
var postTitleElem = post.SelectSingleNode("div[@class = 'posttop']/h2[@class = 'posttitle']");
if (postTitleElem == null)
continue;
var postLink = postTitleElem.Element("a");
if (postLink == null)
continue;
var postUrl = postLink.Attributes["href"].Value;
var postTitle = postTitleElem.InnerText;
var postContentElem = post.SelectSingleNode("div[2]//td[2]");
if (postContentElem == null)
continue;
var links = postContentElem.SelectNodes(".//a");
if (links == null || !links.Any())
continue;
string url = null;
var mf = links.FirstOrDefault(a => a.Attributes["href"].Value.Contains("www.mediafire.com"));
if (mf != null)
url = mf.Attributes["href"].Value;
else
url = links.First().Attributes["href"].Value;
albums.Add(new MikuDbAlbum(postTitle, postUrl, url));
}
return albums.ToArray();
}
public override string HelpText {
get { return "Checks MikuDB for new albums."; }
}
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) == "del" && msg.BotCommand.Params.HasParam(1)) {
var cmdReader = new CmdReader(msg.BotCommand.CommandString);
cmdReader.ReadNext();
var entryName = cmdReader.ReadToEnd();
var priv = new Receiver(bot.Writer, command.Sender.Nick);
var s = checkedAlbums.Remove(entryName);
if (s)
priv.Msg("Deleted " + entryName);
else
priv.Msg("Not found: " + entryName);
} 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);
}
public override BotUserLevel MinUserLevel {
get { return BotUserLevel.Manager; }
}
public override InitialModuleStatus InitialStatus {
get {
return InitialModuleStatus.NotLoaded;
}
}
public override string Name {
get { return "MikuDBChecker"; }
}
}*/
}