-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAniDbParser.cs
116 lines (91 loc) · 2.76 KB
/
AniDbParser.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
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
using log4net;
using MikuBot.Commands;
using MikuBot.Helpers;
using MikuBot.Modules;
using ParseHelper = MikuBot.ExtraPlugins.Helpers.ParseHelper;
namespace MikuBot.ExtraPlugins
{
public class AniDbParser : MsgCommandModuleBase
{
private readonly ILog log = LogManager.GetLogger(typeof(AniDbParser));
private void GetPageContent(Receiver receiver, string url)
{
string videoTitle = null;
var request = WebRequest.Create(url);
//request.Headers.Add("accept-encoding", Encoding.UTF8.HeaderName);
WebResponse response;
try
{
response = request.GetResponse();
}
catch (WebException x)
{
receiver.Msg("AniDB (error): " + x.Message);
return;
}
var enc = response.Headers[HttpResponseHeader.ContentEncoding];
try
{
using (var stream = response.GetResponseStream())
{
videoTitle = GetVideoTitle(ParseHelper.GetStream(stream, enc), enc);
}
}
finally
{
response.Close();
}
if (!string.IsNullOrEmpty(videoTitle))
{
receiver.Msg("AniDB: " + videoTitle);
}
}
private string GetVideoTitle(Stream htmlStream, string encStr)
{
var encoding = ParseHelper.GetEncoding(encStr);
var doc = new HtmlDocument();
doc.Load(htmlStream, encoding);
// Video title element (could use page title as well but this is more reliable)
var titleElem = doc.DocumentNode.SelectSingleNode("//div[@id = 'layout-main']/h1");
//log.Debug("titleElem: " + titleElem);
var titleText = (titleElem != null ? titleElem.InnerText : null);
//log.Debug("titleText:");
return (titleText != null ? HtmlEntity.DeEntitize(titleText) : null);
}
public override string HelpText
{
get { return "Parses AniDB.net links"; }
}
public override bool IsPassive
{
get { return true; }
}
public override string Name
{
get { return "AniDB"; }
}
public override void HandleCommand(MsgCommand cmd, IBotContext bot)
{
const string aniDbLink = "http://anidb.net/perl-bin/animedb.pl";
var text = cmd.Text.ToLowerInvariant();
if (!text.Contains(aniDbLink))
return;
var idRegex = new Regex(@"[a-z0-9\-\.\:\/\%\?\&\=\+]+"); // Valid URL
var linkPos = text.IndexOf(aniDbLink);
var urlMatch = idRegex.Match(cmd.Text, linkPos);
if (!urlMatch.Success)
return;
var receiver = new Receiver(bot.Writer, cmd.ChannelOrSenderNick);
var url = urlMatch.Value;
log.Info("Parsing AniDB link " + url);
//GetPageContent(receiver, url); // Synchronized version
Task.Factory.StartNew(() => GetPageContent(receiver, url)) // Async version
.ContinueWith(TaskHelper.HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
}
}
}