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