-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDanbooruParser.cs
130 lines (107 loc) · 3.29 KB
/
DanbooruParser.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using MikuBot.Commands;
using MikuBot.ExtraPlugins.Helpers;
using MikuBot.Helpers;
using MikuBot.LinkParsing;
using MikuBot.Modules;
namespace MikuBot.ExtraPlugins
{
public class DanbooruParser : MsgCommandModuleBase
{
private string passhash;
private string username;
private readonly Dictionary<char, string> ratingNames = new Dictionary<char, string> {
{ 's', "Safe" },
{ 'q', "Questionable" },
{ 'e', "Explicit" }
};
private readonly RegexLinkMatcher[] linkMatchers = new[] {
new RegexLinkMatcher("danbooru.donmai.us/posts/{0}", @"danbooru.donmai.us/posts/(\d+)"),
};
private void HandlePage(Receiver receiver, int id)
{
var apiUrl = string.Format("http://danbooru.donmai.us/post/index.xml?login={0}&password_hash={1}&tags=id%3A{2}", username, passhash, id);
//var apiUrl = string.Format("http://danbooru.donmai.us/post/index.xml?tags=id%3A{0}", id);
var request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.UserAgent = "MikuBot";
XDocument doc;
try
{
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
try
{
doc = XDocument.Load(stream);
}
catch (XmlException)
{
receiver.Msg("Danbooru (error): Invalid response");
return;
}
}
}
catch (WebException x)
{
receiver.Msg("Danbooru (error): " + x.Message);
return;
}
var res = doc.Element("posts");
if (res == null || res.Element("post") == null)
{
receiver.Msg("Danbooru (error): Invalid response");
return;
}
var post = res.Element("post");
var rating = post.Attribute("rating").Value;
string ratingName;
ratingNames.TryGetValue(rating[0], out ratingName);
var width = post.Attribute("width").Value;
var height = post.Attribute("height").Value;
var createdAt = post.Attribute("created_at").Value;
receiver.Msg(string.Format("Danbooru: rating '{0}', {1}x{2} pixels, uploaded at {3}", ratingName, width, height, createdAt));
}
public override string HelpText
{
get { return "Parses Danbooru links"; }
}
public override InitialModuleStatus InitialStatus
{
get { return InitialModuleStatus.Enabled; }
}
public override bool IsPassive
{
get { return true; }
}
public override string Name
{
get { return "Danbooru"; }
}
public override void HandleCommand(MsgCommand cmd, IBotContext bot)
{
if (cmd.BotCommand.Is("NoLink"))
return;
var receiver = new Receiver(bot.Writer, cmd.ChannelOrSenderNick);
var possibleUrl = cmd.Text;
var matcher = linkMatchers.FirstOrDefault(m => m.IsMatch(possibleUrl));
if (matcher == null)
return;
var url = PluginHelper.MakeLink(matcher.MakeLink(possibleUrl));
var id = int.Parse(matcher.GetId(url));
//GetPageContent(receiver, url); // Synchronized version
Task.Factory.StartNew(() => HandlePage(receiver, id)) // Async version
.ContinueWith(TaskHelper.HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
}
public override void OnLoaded(IBotContext bot, IModuleFile moduleFile)
{
username = bot.Config.DanbooruUserName;
passhash = bot.Config.DanbooruPassHash;
}
}
}