summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-30 11:48:43 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-30 11:48:43 +0200
commit7e10625e9cdb4cc8650773e2a674796f0ab8c478 (patch)
treeb2e862afb6ab9a1237aa170695a5846508bdd2af
parentf044cf1c9782b773d1d0b2d7b651ad03341382b4 (diff)
downloadTwitterDataAnalytics-7e10625e9cdb4cc8650773e2a674796f0ab8c478.tar.gz
main: simple cmd to search for tweets
-rw-r--r--src/main/Main.java22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/main/Main.java b/src/main/Main.java
index 81c06dc..7615c43 100644
--- a/src/main/Main.java
+++ b/src/main/Main.java
@@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import mining.TwitterApi;
+import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -85,6 +86,7 @@ public class Main {
user,
tweet,
+ searchtweets,
hack,
help;
@@ -106,6 +108,18 @@ public class Main {
"Available commands:"
};
+ private void searchTweets(String q) throws IOException, JSONException {
+ TwitterApi.Builder req = getApi().build("search/tweets");
+ req.param("q", q);
+ req.param("count", "100"); // max number of tweets, cannot be higher
+ JSONObject resp = req.request();
+ JSONArray statuses = resp.getJSONArray("statuses");
+ for (int i = 0; i < statuses.length(); i++) {
+ JSONObject tweet = statuses.getJSONObject(i);
+ System.out.println(tweet);
+ }
+ }
+
public void execute() throws IOException {
TwitterApi.Builder req = null;
/* build a request for commands */
@@ -118,6 +132,14 @@ public class Main {
req = getApi().build("statuses/show");
req.param("id", getParam(0, "numerical ID of tweet"));
break;
+ case searchtweets:
+ try {
+ searchTweets(getParam(0, "search query"));
+ } catch (JSONException ex) {
+ throw new IOException(ex);
+ }
+ /* no req, will be handled by search */
+ break;
case hack:
String name = getParam(0, "resource name");
req = getApi().build(name);