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.

DefineCommand.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.charliebravo.commands;
  6. import com.md87.charliebravo.Command;
  7. import com.md87.charliebravo.Followup;
  8. import com.md87.charliebravo.InputHandler;
  9. import com.md87.charliebravo.Response;
  10. import java.io.BufferedReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.net.URLConnection;
  16. import java.net.URLEncoder;
  17. import java.nio.charset.Charset;
  18. import org.json.JSONArray;
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21. /**
  22. *
  23. * @author chris
  24. */
  25. public class DefineCommand implements Command {
  26. public void execute(final InputHandler handler, Response response, String line) throws MalformedURLException, IOException, JSONException {
  27. URL url = new URL("http://apps.md87.co.uk/services/wiktionary/?query=" +
  28. URLEncoder.encode(line, Charset.defaultCharset().name()));
  29. URLConnection connection = url.openConnection();
  30. connection.addRequestProperty("Referer", "http://chris.smith.name/");
  31. String input;
  32. StringBuilder builder = new StringBuilder();
  33. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  34. while((input = reader.readLine()) != null) {
  35. builder.append(input);
  36. }
  37. JSONObject json = new JSONObject(builder.toString());
  38. if (json.getInt("responseStatus") != 200) {
  39. throw new IOException(json.getString("responseDetails"));
  40. }
  41. if (json.getJSONArray("responseData").length() == 0) {
  42. response.sendMessage("There were no results for '" + line + "'", true);
  43. } else {
  44. final StringBuilder res = new StringBuilder();
  45. res.append("there ");
  46. if (json.getJSONArray("responseData").length() == 1) {
  47. res.append("was 1 match");
  48. } else {
  49. res.append("were ");
  50. res.append(json.getJSONArray("responseData").length());
  51. res.append(" matches");
  52. }
  53. res.append(" for '");
  54. res.append(line);
  55. res.append("'");
  56. if (json.getJSONArray("responseData").length() == 1) {
  57. res.append(". It is ");
  58. } else {
  59. res.append(". Result 1 is ");
  60. }
  61. final String name = json.getJSONArray("responseData").getJSONObject(0).getString("title");
  62. res.append('\'');
  63. res.append(name);
  64. res.append("', which has ");
  65. final int defs = json.getJSONArray("responseData").getJSONObject(0)
  66. .getJSONArray("definitions").length();
  67. res.append(defs);
  68. res.append(" definition");
  69. if (defs != 1) {
  70. res.append("s, the first of which is");
  71. }
  72. res.append(": ");
  73. res.append(json.getJSONArray("responseData").getJSONObject(0)
  74. .getJSONArray("definitions").get(0));
  75. response.sendMessage(res.toString());
  76. response.addFollowup(new NextWordFollowup(json.getJSONArray("responseData"), 1));
  77. response.addFollowup(new NextDefinitionFollowup(json.getJSONArray("responseData")
  78. .getJSONObject(0).getJSONArray("definitions"), 1,
  79. new NextWordFollowup(json.getJSONArray("responseData"), 1)));
  80. }
  81. }
  82. protected static class NextWordFollowup implements Followup {
  83. private final JSONArray words;
  84. private final int next;
  85. public NextWordFollowup(JSONArray words, int next) {
  86. this.words = words;
  87. this.next = next;
  88. }
  89. public boolean matches(String line) {
  90. return next < words.length() && line.startsWith("next word");
  91. }
  92. public void execute(InputHandler handler, Response response, String line) throws Exception {
  93. final StringBuilder res = new StringBuilder();
  94. res.append("result " + (next + 1) + " is ");
  95. final String name = words.getJSONObject(next).getString("title");
  96. res.append('\'');
  97. res.append(name);
  98. res.append("', which has ");
  99. final int defs = words.getJSONObject(next)
  100. .getJSONArray("definitions").length();
  101. res.append(defs);
  102. res.append(" definition");
  103. if (defs != 1) {
  104. res.append("s, the first of which is");
  105. }
  106. res.append(": ");
  107. res.append(words.getJSONObject(next)
  108. .getJSONArray("definitions").get(0));
  109. response.sendMessage(res.toString());
  110. response.addFollowup(new NextWordFollowup(words, next + 1));
  111. response.addFollowup(new NextDefinitionFollowup(words.getJSONObject(next)
  112. .getJSONArray("definitions"), 1, new NextWordFollowup(words, next + 1)));
  113. }
  114. }
  115. protected static class NextDefinitionFollowup implements Followup {
  116. private final JSONArray defs;
  117. private final int next;
  118. private final NextWordFollowup nextword;
  119. public NextDefinitionFollowup(JSONArray defs, int next, NextWordFollowup nextword) {
  120. this.defs = defs;
  121. this.next = next;
  122. this.nextword = nextword;
  123. }
  124. public boolean matches(String line) {
  125. return next < defs.length() && line.startsWith("next definition");
  126. }
  127. public void execute(InputHandler handler, Response response, String line) throws Exception {
  128. final StringBuilder res = new StringBuilder();
  129. res.append("definition " + (next + 1) + " is: ");
  130. res.append(defs.get(next));
  131. response.sendMessage(res.toString());
  132. response.addFollowup(nextword);
  133. response.addFollowup(new NextDefinitionFollowup(defs, next + 1, nextword));
  134. }
  135. }
  136. }