9 muutettua tiedostoa jossa 275 lisäystä ja 59 poistoa
@ -0,0 +1,117 @@
@@ -0,0 +1,117 @@
|
||||
using System; |
||||
using System.Net; |
||||
using System.Web; |
||||
using System.Text.RegularExpressions; |
||||
using Combot.IRCServices.Messaging; |
||||
|
||||
namespace Combot.Modules.ModuleClasses |
||||
{ |
||||
public class UrlParsing : Module |
||||
{ |
||||
public override void Initialize() |
||||
{ |
||||
Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage; |
||||
} |
||||
|
||||
public void HandleChannelMessage(object sender, ChannelMessage message) |
||||
{ |
||||
Regex urlRegex = new Regex("(https?)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?"); |
||||
|
||||
if (urlRegex.IsMatch(message.Message)) |
||||
{ |
||||
MatchCollection urlMatches = urlRegex.Matches(message.Message); |
||||
for (int i = 0; i < urlMatches.Count; i++) |
||||
{ |
||||
Match urlMatch = urlMatches[i]; |
||||
Uri url = new Uri(urlMatch.Value); |
||||
WebRequest webRequest = HttpWebRequest.Create(url); |
||||
webRequest.Method = "HEAD"; |
||||
using (WebResponse webResponse = webRequest.GetResponse()) |
||||
{ |
||||
string contentType = webResponse.ContentType.Split('/')[0]; |
||||
long contentLength = webResponse.ContentLength; |
||||
switch (contentType) |
||||
{ |
||||
case "text": |
||||
Regex ytRegex = new Regex("(((youtube.*(v=|/v/))|(youtu\\.be/))(?<ID>[-_a-zA-Z0-9]+))"); |
||||
if (!ytRegex.IsMatch(message.Message) || !Bot.Modules.Exists(mod => mod.Name == "YouTube")) |
||||
{ |
||||
WebClient x = new WebClient(); |
||||
string source = x.DownloadString(urlMatch.ToString()); |
||||
string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value; |
||||
Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[URL] {0} ({1})", HttpUtility.UrlDecode(StripTagsCharArray(title)), url.Host)); |
||||
} |
||||
break; |
||||
case "image": |
||||
Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[{0}] Size: {1}", webResponse.ContentType, ToFileSize(contentLength))); |
||||
break; |
||||
case "video": |
||||
Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[Video] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength))); |
||||
break; |
||||
case "application": |
||||
Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[Application] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength))); |
||||
break; |
||||
case "audio": |
||||
Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[Audio] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength))); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Remove HTML tags from string using char array.
|
||||
/// </summary>
|
||||
public static string StripTagsCharArray(string source) |
||||
{ |
||||
char[] array = new char[source.Length]; |
||||
int arrayIndex = 0; |
||||
bool inside = false; |
||||
|
||||
for (int i = 0; i < source.Length; i++) |
||||
{ |
||||
char let = source[i]; |
||||
if (let == '<') |
||||
{ |
||||
inside = true; |
||||
continue; |
||||
} |
||||
if (let == '>') |
||||
{ |
||||
inside = false; |
||||
continue; |
||||
} |
||||
if (!inside) |
||||
{ |
||||
array[arrayIndex] = let; |
||||
arrayIndex++; |
||||
} |
||||
} |
||||
return new string(array, 0, arrayIndex); |
||||
} |
||||
|
||||
public static string ToFileSize(long source) |
||||
{ |
||||
const int byteConversion = 1024; |
||||
double bytes = Convert.ToDouble(source); |
||||
|
||||
if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
|
||||
{ |
||||
return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB"); |
||||
} |
||||
else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
|
||||
{ |
||||
return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB"); |
||||
} |
||||
else if (bytes >= byteConversion) //KB Range
|
||||
{ |
||||
return string.Concat(Math.Round(bytes / byteConversion, 2), " KB"); |
||||
} |
||||
else //Bytes
|
||||
{ |
||||
return string.Concat(bytes, " Bytes"); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,92 @@
@@ -0,0 +1,92 @@
|
||||
using System.Net; |
||||
using System.Text; |
||||
using System.Xml; |
||||
|
||||
namespace Combot.Modules.ModuleClasses |
||||
{ |
||||
public class WolframAlpha : Module |
||||
{ |
||||
public override void Initialize() |
||||
{ |
||||
Bot.CommandReceivedEvent += HandleCommandEvent; |
||||
} |
||||
|
||||
public override void ParseCommand(CommandMessage command) |
||||
{ |
||||
Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command)); |
||||
switch (foundCommand.Name) |
||||
{ |
||||
case "Wolfram Alpha Search": |
||||
GetResults(command); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
private void GetResults(CommandMessage command) |
||||
{ |
||||
string URL = "http://api.wolframalpha.com/v2/query?input=" + System.Web.HttpUtility.UrlEncode(command.Arguments["Query"]) + "&appid=" + GetOptionValue("API") + "&format=plaintext"; |
||||
XmlNodeList xnList = null; |
||||
try |
||||
{ |
||||
WebClient web = new WebClient(); |
||||
web.Encoding = Encoding.UTF8; |
||||
string results = web.DownloadString(URL); |
||||
XmlDocument xmlDoc = new XmlDocument(); |
||||
xmlDoc.LoadXml(results); |
||||
xnList = xmlDoc.SelectNodes("/queryresult/pod"); |
||||
} |
||||
catch |
||||
{ |
||||
string errorMessage = string.Format("Unable to fetch results for \u0002{0}\u000F.", command.Arguments["Query"]); |
||||
switch (command.MessageType) |
||||
{ |
||||
case MessageType.Channel: |
||||
Bot.IRC.SendPrivateMessage(command.Location, errorMessage); |
||||
break; |
||||
case MessageType.Query: |
||||
Bot.IRC.SendPrivateMessage(command.Nick.Nickname, errorMessage); |
||||
break; |
||||
case MessageType.Notice: |
||||
Bot.IRC.SendNotice(command.Nick.Nickname, errorMessage); |
||||
break; |
||||
} |
||||
} |
||||
if (xnList.Count > 1) |
||||
{ |
||||
string queryMessage = string.Format("Result for: {0}", xnList[0]["subpod"]["plaintext"].InnerText); |
||||
string resultMessage = xnList[1]["subpod"]["plaintext"].InnerText; |
||||
switch (command.MessageType) |
||||
{ |
||||
case MessageType.Channel: |
||||
Bot.IRC.SendPrivateMessage(command.Location, queryMessage); |
||||
Bot.IRC.SendPrivateMessage(command.Location, resultMessage); |
||||
break; |
||||
case MessageType.Query: |
||||
Bot.IRC.SendPrivateMessage(command.Nick.Nickname, queryMessage); |
||||
Bot.IRC.SendPrivateMessage(command.Nick.Nickname, resultMessage); |
||||
break; |
||||
case MessageType.Notice: |
||||
Bot.IRC.SendPrivateMessage(command.Nick.Nickname, queryMessage); |
||||
Bot.IRC.SendPrivateMessage(command.Nick.Nickname, resultMessage); |
||||
break; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
string errorMessage = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]); |
||||
switch (command.MessageType) |
||||
{ |
||||
case MessageType.Channel: |
||||
Bot.IRC.SendPrivateMessage(command.Location, errorMessage); |
||||
break; |
||||
case MessageType.Query: |
||||
Bot.IRC.SendPrivateMessage(command.Nick.Nickname, errorMessage); |
||||
break; |
||||
case MessageType.Notice: |
||||
Bot.IRC.SendNotice(command.Nick.Nickname, errorMessage); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,7 +1,4 @@
@@ -1,7 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<packages> |
||||
<package id="Google.GData.Client" version="2.2.0.0" targetFramework="net451" /> |
||||
<package id="Google.GData.Extensions" version="2.2.0.0" targetFramework="net451" /> |
||||
<package id="Google.GData.YouTube" version="2.2.0.0" targetFramework="net451" /> |
||||
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" /> |
||||
</packages> |
Ladataan…
Reference in new issue