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.

Url_Parsing.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Web;
  6. using Combot.IRCServices.Messaging;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. namespace Combot.Modules.Plugins
  10. {
  11. public class Url_Parsing : Module
  12. {
  13. public override void Initialize()
  14. {
  15. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  16. }
  17. public void HandleChannelMessage(object sender, ChannelMessage message)
  18. {
  19. Regex urlRegex = new Regex("(https?)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?");
  20. if (Enabled
  21. && !Bot.ServerConfig.ChannelBlacklist.Contains(message.Channel)
  22. && !Bot.ServerConfig.NickBlacklist.Contains(message.Sender.Nickname)
  23. && !ChannelBlacklist.Contains(message.Channel)
  24. && !NickBlacklist.Contains(message.Sender.Nickname)
  25. && !Bot.IsCommand(message.Message))
  26. {
  27. if (urlRegex.IsMatch(message.Message))
  28. {
  29. MatchCollection urlMatches = urlRegex.Matches(message.Message);
  30. for (int i = 0; i < urlMatches.Count; i++)
  31. {
  32. Match urlMatch = urlMatches[i];
  33. Uri url = new Uri(urlMatch.Value);
  34. HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(url);
  35. webRequest.Method = "HEAD";
  36. webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
  37. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  38. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  39. try
  40. {
  41. using (HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse())
  42. {
  43. int code = (int) webResponse.StatusCode;
  44. if (code == 200)
  45. {
  46. string contentType = webResponse.ContentType.Split('/')[0];
  47. long contentLength = webResponse.ContentLength;
  48. switch (contentType)
  49. {
  50. case "text":
  51. Regex ytRegex = new Regex("(((youtube.*(v=|/v/))|(youtu\\.be/))(?<ID>[-_a-zA-Z0-9]+))");
  52. if (ytRegex.IsMatch(urlMatch.ToString()))
  53. {
  54. Match ytMatch = ytRegex.Match(urlMatch.ToString());
  55. string youtubeMessage = GetYoutubeDescription(ytMatch.Groups["ID"].Value);
  56. Bot.IRC.Command.SendPrivateMessage(message.Channel, youtubeMessage);
  57. }
  58. else
  59. {
  60. WebClient x = new WebClient();
  61. x.Encoding = Encoding.UTF8;
  62. string source = x.DownloadString(urlMatch.ToString());
  63. string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
  64. int maxTitle = Convert.ToInt32(GetOptionValue("Max Title"));
  65. if (title.Length > (int)maxTitle)
  66. {
  67. title = string.Format("{0}...", title.Substring(0, (int)maxTitle));
  68. }
  69. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] {0} ({1})", HttpUtility.HtmlDecode(HttpUtility.UrlDecode(StripTagsCharArray(title))), url.Host));
  70. }
  71. break;
  72. case "image":
  73. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[{0}] Size: {1}", webResponse.ContentType, ToFileSize(contentLength)));
  74. break;
  75. case "video":
  76. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Video] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  77. break;
  78. case "application":
  79. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Application] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  80. break;
  81. case "audio":
  82. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Audio] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  83. break;
  84. }
  85. }
  86. else
  87. {
  88. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] Returned Status Code \u0002{0}\u0002 ({1})", code, url.Host));
  89. }
  90. }
  91. }
  92. catch (WebException ex)
  93. {
  94. if (ex.Response != null)
  95. {
  96. int code = (int) ((HttpWebResponse) ex.Response).StatusCode;
  97. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] Response Code: \u0002{0}\u0002 ({1})", code, url.Host));
  98. }
  99. }
  100. catch (OutOfMemoryException ex)
  101. {
  102. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] Site content was too large ({0})", url.Host));
  103. }
  104. }
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Remove HTML tags from string using char array.
  110. /// </summary>
  111. public static string StripTagsCharArray(string source)
  112. {
  113. char[] array = new char[source.Length];
  114. int arrayIndex = 0;
  115. bool inside = false;
  116. for (int i = 0; i < source.Length; i++)
  117. {
  118. char let = source[i];
  119. if (let == '<')
  120. {
  121. inside = true;
  122. continue;
  123. }
  124. if (let == '>')
  125. {
  126. inside = false;
  127. continue;
  128. }
  129. if (!inside)
  130. {
  131. array[arrayIndex] = let;
  132. arrayIndex++;
  133. }
  134. }
  135. return new string(array, 0, arrayIndex);
  136. }
  137. public static string ToFileSize(long source)
  138. {
  139. const int byteConversion = 1024;
  140. double bytes = Convert.ToDouble(source);
  141. if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
  142. {
  143. return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
  144. }
  145. else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
  146. {
  147. return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
  148. }
  149. else if (bytes >= byteConversion) //KB Range
  150. {
  151. return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
  152. }
  153. else //Bytes
  154. {
  155. return string.Concat(bytes, " Bytes");
  156. }
  157. }
  158. private string GetYoutubeDescription(string ID)
  159. {
  160. string description = string.Empty;
  161. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos/{0}?v=2&alt=jsonc";
  162. Uri searchUrl = new Uri(string.Format(urlTemplate, ID));
  163. WebClient web = new WebClient();
  164. web.Encoding = Encoding.UTF8;
  165. try
  166. {
  167. string page = web.DownloadString(searchUrl);
  168. JObject parsed = (JObject)JsonConvert.DeserializeObject(page);
  169. var data = parsed["data"];
  170. description = string.Format("\u0002{0}\u000F", data["title"]);
  171. if (data["duration"] == null)
  172. {
  173. return description;
  174. }
  175. TimeSpan duration = TimeSpan.FromSeconds(data["duration"].Value<double>());
  176. description += string.Format(" | Length: \u0002{0}\u000F", duration.ToString("g"));
  177. if (data["ratingCount"] != null)
  178. {
  179. int likes = data["likeCount"].Value<int>();
  180. string pluralLikes = (likes > 1) ? "s" : string.Empty;
  181. int dislikes = data["ratingCount"].Value<int>() - likes;
  182. string pluralDislikes = (dislikes > 1) ? "s" : string.Empty;
  183. double percent = 100.0 * ((double)likes / data["ratingCount"].Value<int>());
  184. description += string.Format(" | Rating: {0} Like{1}, {2} Dislike{3} (\u0002{4}\u000F%)", likes, pluralLikes, dislikes, pluralDislikes, Math.Round(percent, 1));
  185. }
  186. if (data["viewCount"] != null)
  187. {
  188. description += string.Format(" | Views: \u0002{0}\u000F", data["viewCount"].Value<int>());
  189. }
  190. DateTime uploadDate = Convert.ToDateTime(data["uploaded"].Value<string>());
  191. description += string.Format(" | Uploaded By: \u0002{0}\u000F on \u0002{1}\u000F", data["uploader"].Value<string>(), uploadDate.ToString("R"));
  192. if (data["contentRating"] != null)
  193. {
  194. description += " | \u0002NSFW\u000F";
  195. }
  196. }
  197. catch (WebException ex)
  198. {
  199. description = string.Empty;
  200. }
  201. return description;
  202. }
  203. }
  204. }