Java IRC bot
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.

GoogleCommand.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.md87.charliebravo.commands;
  23. import com.md87.charliebravo.Command;
  24. import com.md87.charliebravo.Followup;
  25. import com.md87.charliebravo.InputHandler;
  26. import com.md87.charliebravo.Response;
  27. import java.io.BufferedReader;
  28. import java.io.IOException;
  29. import java.io.InputStreamReader;
  30. import java.net.MalformedURLException;
  31. import java.net.URL;
  32. import java.net.URLConnection;
  33. import java.net.URLEncoder;
  34. import java.nio.charset.Charset;
  35. import org.json.JSONArray;
  36. import org.json.JSONException;
  37. import org.json.JSONObject;
  38. /**
  39. *
  40. * @author chris
  41. */
  42. public class GoogleCommand implements Command {
  43. public void execute(final InputHandler handler, Response response, String line) throws MalformedURLException, IOException, JSONException {
  44. URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" +
  45. URLEncoder.encode(line, Charset.defaultCharset().name()));
  46. URLConnection connection = url.openConnection();
  47. connection.addRequestProperty("Referer", "http://chris.smith.name/");
  48. String input;
  49. StringBuilder builder = new StringBuilder();
  50. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  51. while((input = reader.readLine()) != null) {
  52. builder.append(input);
  53. }
  54. JSONObject json = new JSONObject(builder.toString());
  55. if (json.getInt("responseStatus") != 200) {
  56. throw new IOException(json.getString("responseDetails"));
  57. }
  58. if (json.getJSONObject("responseData").getJSONArray("results").length() == 0) {
  59. response.sendMessage("There were no results for '" + line + "'", true);
  60. } else {
  61. final JSONObject obj = json.getJSONObject("responseData")
  62. .getJSONArray("results").getJSONObject(0);
  63. response.sendMessage("the first result for '" + line + "' is "
  64. + obj.getString("unescapedUrl") + ", titled '"
  65. + obj.getString("titleNoFormatting") + "'");
  66. response.addFollowup(new DescriptionFollowup(obj));
  67. response.addFollowup(new CacheFollowup(obj));
  68. response.addFollowup(new NextFollowup(line,
  69. json.getJSONObject("responseData").getJSONArray("results"), 1));
  70. }
  71. }
  72. protected static class DescriptionFollowup implements Followup {
  73. private final JSONObject result;
  74. public DescriptionFollowup(JSONObject result) {
  75. this.result = result;
  76. }
  77. public boolean matches(String line) {
  78. return line.equals("description");
  79. }
  80. public void execute(final InputHandler handler, Response response, String line) throws Exception {
  81. response.setInheritFollows(true);
  82. response.sendMessage("the description of '" + result.getString("unescapedUrl")
  83. + "' is: " + result.getString("content"));
  84. }
  85. }
  86. protected static class CacheFollowup implements Followup {
  87. private final JSONObject result;
  88. public CacheFollowup(JSONObject result) {
  89. this.result = result;
  90. }
  91. public boolean matches(String line) {
  92. return line.equals("cache");
  93. }
  94. public void execute(final InputHandler handler, Response response, String line) throws Exception {
  95. response.setInheritFollows(true);
  96. if (result.has("cacheUrl")) {
  97. response.sendMessage("the cached version of '" + result.getString("url")
  98. + "' is: " + result.getString("cacheUrl"));
  99. } else {
  100. response.sendMessage("There doesn't seem to be a cached version of '"
  101. + result.getString("unescapedUrl") + "'", true);
  102. }
  103. }
  104. }
  105. protected static class NextFollowup implements Followup {
  106. private final String query;
  107. private final JSONArray result;
  108. private final int offset;
  109. public NextFollowup(String query, JSONArray results, final int offset) {
  110. this.query = query;
  111. this.result = results;
  112. this.offset = offset;
  113. }
  114. public boolean matches(String line) {
  115. return line.equals("next") || line.equals("more");
  116. }
  117. public void execute(final InputHandler handler, Response response, String line) throws Exception {
  118. if (result.length() <= offset) {
  119. response.setInheritFollows(true);
  120. response.sendMessage("There are no more results", true);
  121. } else {
  122. final JSONObject obj = result.getJSONObject(offset);
  123. response.sendMessage("the next result for '" + query + "' is "
  124. + obj.getString("unescapedUrl") + ", titled '"
  125. + obj.getString("titleNoFormatting") + "'");
  126. response.addFollowup(new DescriptionFollowup(obj));
  127. response.addFollowup(new CacheFollowup(obj));
  128. response.addFollowup(new NextFollowup(query, result, offset + 1));
  129. }
  130. }
  131. }
  132. }