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 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.HtmlDecode(HttpUtility.UrlDecode(StripTagsCharArray(result.Value<string>("titleNoFormatting"))));
  44. string content = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(StripTagsCharArray(result.Value<string>("content"))));
  45. string resultMessage = string.Format("[{0}] \u0002{1}\u000F: {2}.", url, title, content);
  46. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, resultMessage);
  47. }
  48. else
  49. {
  50. string noResults = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  51. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noResults);
  52. }
  53. }
  54. else
  55. {
  56. string errorCode = string.Format("Unable to search for \u0002{0}\u000F. Google returned status code \u0002{1}\u000F.", command.Arguments["Query"], responseCode);
  57. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorCode);
  58. }
  59. }
  60. /// <summary>
  61. /// Remove HTML tags from string using char array.
  62. /// </summary>
  63. public static string StripTagsCharArray(string source)
  64. {
  65. char[] array = new char[source.Length];
  66. int arrayIndex = 0;
  67. bool inside = false;
  68. for (int i = 0; i < source.Length; i++)
  69. {
  70. char let = source[i];
  71. if (let == '<')
  72. {
  73. inside = true;
  74. continue;
  75. }
  76. if (let == '>')
  77. {
  78. inside = false;
  79. continue;
  80. }
  81. if (!inside)
  82. {
  83. array[arrayIndex] = let;
  84. arrayIndex++;
  85. }
  86. }
  87. return new string(array, 0, arrayIndex);
  88. }
  89. }
  90. }