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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Text;
  5. using System.Web;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. namespace Combot.Modules.Plugins
  9. {
  10. public class Search : Module
  11. {
  12. public override void Initialize()
  13. {
  14. Bot.CommandReceivedEvent += HandleCommandEvent;
  15. }
  16. public override void ParseCommand(CommandMessage command)
  17. {
  18. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  19. switch (foundCommand.Name)
  20. {
  21. case "Google":
  22. GoogleSearch(command);
  23. break;
  24. case "Bing":
  25. break;
  26. }
  27. }
  28. private void GoogleSearch(CommandMessage command)
  29. {
  30. string urlTemplate = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=off&q={0}";
  31. Uri searchUrl = new Uri(string.Format(urlTemplate, command.Arguments["Query"]));
  32. WebClient web = new WebClient();
  33. web.Encoding = Encoding.UTF8;
  34. try
  35. {
  36. string page = web.DownloadString(searchUrl);
  37. JObject parsed = (JObject) JsonConvert.DeserializeObject(page);
  38. int responseCode = parsed.Value<int>("responseStatus");
  39. if (responseCode < 300 && responseCode >= 200)
  40. {
  41. if (parsed["responseData"]["results"].Any())
  42. {
  43. var result = parsed["responseData"]["results"][0];
  44. string url = result.Value<string>("unescapedUrl");
  45. string title = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(StripTagsCharArray(result.Value<string>("titleNoFormatting"))));
  46. string content = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(StripTagsCharArray(result.Value<string>("content"))));
  47. string resultMessage = string.Format("[{0}] \u0002{1}\u000F: {2}.", url, title, content);
  48. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, resultMessage);
  49. }
  50. else
  51. {
  52. string noResults = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  53. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noResults);
  54. }
  55. }
  56. else
  57. {
  58. string errorCode = string.Format("Unable to search for \u0002{0}\u000F. Google returned status code \u0002{1}\u000F.", command.Arguments["Query"], responseCode);
  59. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorCode);
  60. }
  61. }
  62. catch (WebException ex)
  63. {
  64. if (ex.Response != null)
  65. {
  66. int code = (int)((HttpWebResponse)ex.Response).StatusCode;
  67. string errorCode = string.Format("Unable to search for \u0002{0}\u000F. Google returned status code \u0002{1}\u000F.", command.Arguments["Query"], code);
  68. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorCode);
  69. }
  70. else
  71. {
  72. string errorCode = string.Format("Unable to search for \u0002{0}\u000F.", command.Arguments["Query"]);
  73. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorCode);
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// Remove HTML tags from string using char array.
  79. /// </summary>
  80. public static string StripTagsCharArray(string source)
  81. {
  82. char[] array = new char[source.Length];
  83. int arrayIndex = 0;
  84. bool inside = false;
  85. for (int i = 0; i < source.Length; i++)
  86. {
  87. char let = source[i];
  88. if (let == '<')
  89. {
  90. inside = true;
  91. continue;
  92. }
  93. if (let == '>')
  94. {
  95. inside = false;
  96. continue;
  97. }
  98. if (!inside)
  99. {
  100. array[arrayIndex] = let;
  101. arrayIndex++;
  102. }
  103. }
  104. return new string(array, 0, arrayIndex);
  105. }
  106. }
  107. }