The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UrlParsing.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Net;
  3. using System.Web;
  4. using System.Text.RegularExpressions;
  5. using Combot.IRCServices.Messaging;
  6. namespace Combot.Modules.ModuleClasses
  7. {
  8. public class UrlParsing : Module
  9. {
  10. public override void Initialize()
  11. {
  12. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  13. }
  14. public void HandleChannelMessage(object sender, ChannelMessage message)
  15. {
  16. Regex urlRegex = new Regex("(https?)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?");
  17. if (urlRegex.IsMatch(message.Message))
  18. {
  19. MatchCollection urlMatches = urlRegex.Matches(message.Message);
  20. for (int i = 0; i < urlMatches.Count; i++)
  21. {
  22. Match urlMatch = urlMatches[i];
  23. Uri url = new Uri(urlMatch.Value);
  24. WebRequest webRequest = HttpWebRequest.Create(url);
  25. webRequest.Method = "HEAD";
  26. using (WebResponse webResponse = webRequest.GetResponse())
  27. {
  28. string contentType = webResponse.ContentType.Split('/')[0];
  29. long contentLength = webResponse.ContentLength;
  30. switch (contentType)
  31. {
  32. case "text":
  33. Regex ytRegex = new Regex("(((youtube.*(v=|/v/))|(youtu\\.be/))(?<ID>[-_a-zA-Z0-9]+))");
  34. if (!ytRegex.IsMatch(message.Message) || !Bot.Modules.Exists(mod => mod.Name == "YouTube"))
  35. {
  36. WebClient x = new WebClient();
  37. string source = x.DownloadString(urlMatch.ToString());
  38. string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
  39. Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[URL] {0} ({1})", HttpUtility.UrlDecode(StripTagsCharArray(title)), url.Host));
  40. }
  41. break;
  42. case "image":
  43. Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[{0}] Size: {1}", webResponse.ContentType, ToFileSize(contentLength)));
  44. break;
  45. case "video":
  46. Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[Video] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  47. break;
  48. case "application":
  49. Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[Application] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  50. break;
  51. case "audio":
  52. Bot.IRC.SendPrivateMessage(message.Channel, string.Format("[Audio] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Remove HTML tags from string using char array.
  61. /// </summary>
  62. public static string StripTagsCharArray(string source)
  63. {
  64. char[] array = new char[source.Length];
  65. int arrayIndex = 0;
  66. bool inside = false;
  67. for (int i = 0; i < source.Length; i++)
  68. {
  69. char let = source[i];
  70. if (let == '<')
  71. {
  72. inside = true;
  73. continue;
  74. }
  75. if (let == '>')
  76. {
  77. inside = false;
  78. continue;
  79. }
  80. if (!inside)
  81. {
  82. array[arrayIndex] = let;
  83. arrayIndex++;
  84. }
  85. }
  86. return new string(array, 0, arrayIndex);
  87. }
  88. public static string ToFileSize(long source)
  89. {
  90. const int byteConversion = 1024;
  91. double bytes = Convert.ToDouble(source);
  92. if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
  93. {
  94. return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
  95. }
  96. else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
  97. {
  98. return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
  99. }
  100. else if (bytes >= byteConversion) //KB Range
  101. {
  102. return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
  103. }
  104. else //Bytes
  105. {
  106. return string.Concat(bytes, " Bytes");
  107. }
  108. }
  109. }
  110. }