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.

Search.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. string page = web.DownloadString(searchUrl);
  35. JObject parsed = (JObject)JsonConvert.DeserializeObject(page);
  36. int responseCode = parsed.Value<int>("responseStatus");
  37. if (responseCode < 300 && responseCode >= 200)
  38. {
  39. if (parsed["responseData"]["results"].Any())
  40. {
  41. var result = parsed["responseData"]["results"][0];
  42. string url = result.Value<string>("unescapedUrl");
  43. string title = HttpUtility.UrlDecode(StripTagsCharArray(result.Value<string>("titleNoFormatting")));
  44. string content = HttpUtility.UrlDecode(StripTagsCharArray(result.Value<string>("content")));
  45. string resultMessage = string.Format("[{0}] \u0002{1}\u000F: {2}.", url, title, content);
  46. switch (command.MessageType)
  47. {
  48. case MessageType.Channel:
  49. Bot.IRC.SendPrivateMessage(command.Location, resultMessage);
  50. break;
  51. case MessageType.Query:
  52. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, resultMessage);
  53. break;
  54. case MessageType.Notice:
  55. Bot.IRC.SendNotice(command.Nick.Nickname, resultMessage);
  56. break;
  57. }
  58. }
  59. else
  60. {
  61. string noResults = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  62. switch (command.MessageType)
  63. {
  64. case MessageType.Channel:
  65. Bot.IRC.SendPrivateMessage(command.Location, noResults);
  66. break;
  67. case MessageType.Query:
  68. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noResults);
  69. break;
  70. case MessageType.Notice:
  71. Bot.IRC.SendNotice(command.Nick.Nickname, noResults);
  72. break;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. string errorCode = string.Format("Unable to search for \u0002{0}\u000F. Google returned status code \u0002{1}\u000F.", command.Arguments["Query"], responseCode);
  79. switch (command.MessageType)
  80. {
  81. case MessageType.Channel:
  82. Bot.IRC.SendPrivateMessage(command.Location, errorCode);
  83. break;
  84. case MessageType.Query:
  85. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, errorCode);
  86. break;
  87. case MessageType.Notice:
  88. Bot.IRC.SendNotice(command.Nick.Nickname, errorCode);
  89. break;
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// Remove HTML tags from string using char array.
  95. /// </summary>
  96. public static string StripTagsCharArray(string source)
  97. {
  98. char[] array = new char[source.Length];
  99. int arrayIndex = 0;
  100. bool inside = false;
  101. for (int i = 0; i < source.Length; i++)
  102. {
  103. char let = source[i];
  104. if (let == '<')
  105. {
  106. inside = true;
  107. continue;
  108. }
  109. if (let == '>')
  110. {
  111. inside = false;
  112. continue;
  113. }
  114. if (!inside)
  115. {
  116. array[arrayIndex] = let;
  117. arrayIndex++;
  118. }
  119. }
  120. return new string(array, 0, arrayIndex);
  121. }
  122. }
  123. }