diff options
Diffstat (limited to 'src/main/TweetShell.java')
-rw-r--r-- | src/main/TweetShell.java | 64 |
1 files changed, 51 insertions, 13 deletions
diff --git a/src/main/TweetShell.java b/src/main/TweetShell.java index 956f060..583b3a4 100644 --- a/src/main/TweetShell.java +++ b/src/main/TweetShell.java @@ -39,6 +39,8 @@ public class TweetShell implements TwitterApi.PinSupplier { private Stream stream_cached; private final CompositeResultListener resultListeners; + private Search search_cached; + /** * Whether to convert uncompressed tweet files (e.g. "tweets.txt") to the * compressed files ("tweets.txt.gz"). @@ -80,6 +82,15 @@ public class TweetShell implements TwitterApi.PinSupplier { return stream_cached; } + private Search getSearch() throws IOException { + if (search_cached == null) { + search_cached = new SearchImpl(getApi()); + search_cached.setExceptionListener(new StreamExceptionHandler()); + search_cached.setResultListener(resultListeners); + } + return search_cached; + } + private class StreamExceptionHandler implements ExceptionListener { @Override @@ -159,7 +170,7 @@ public class TweetShell implements TwitterApi.PinSupplier { } public boolean execute(String cmd) { - String[] args = cmd.trim().split("\\s+", 2); + String[] args = cmd.trim().split("\\s+", 3); if (!args[0].isEmpty()) { // non-empty command, let's see whether it makes sense? return execute(args); @@ -191,6 +202,7 @@ public class TweetShell implements TwitterApi.PinSupplier { } // another satisfied customer, next! return true; + } enum Command { @@ -199,6 +211,7 @@ public class TweetShell implements TwitterApi.PinSupplier { del("Deletes a keyword from search", 1), keywords("Display currently active keywords"), commit("Activate the stream or apply the stream keyword changes"), + search("Searches for the tweets of some user", 2), status("Show some status and statistics"), flush("Writes any pending buffers"), close("Close the stream"), @@ -297,23 +310,37 @@ public class TweetShell implements TwitterApi.PinSupplier { getStream().commit(); } break; + case search: + Search search = getSearch(); + search.search(params[0], Integer.parseInt(params[1])); + break; case status: TweetCounter tc; - tc = (TweetCounter) resultListeners.findListener(TweetCounter.class); - if (stream_cached != null && getStream().isValid()) { + tc + = (TweetCounter) resultListeners.findListener(TweetCounter.class + ); + if (stream_cached + != null && getStream() + .isValid()) { System.out.println("Streaming is active."); } else { System.out.println("Streaming is inactive."); } DateFormat df = SimpleDateFormat.getDateTimeInstance(); String start_date = df.format(tc.getStartDate()); - System.out.println("Started at " + start_date); - System.out.println("Elapsed: " + tc.getActiveTime()); - System.out.println("Received tweets in session: " + tc.getTweetCount()); - System.out.println("Unique users: " + tc.getUsers().size()); + + System.out.println( + "Started at " + start_date); + System.out.println( + "Elapsed: " + tc.getActiveTime()); + System.out.println( + "Received tweets in session: " + tc.getTweetCount()); + System.out.println( + "Unique users: " + tc.getUsers().size()); break; case flush: - if (stream_cached != null) { + if (stream_cached + != null) { resultListeners.flush(); } break; @@ -324,7 +351,8 @@ public class TweetShell implements TwitterApi.PinSupplier { for (String line : HELP) { System.out.println(line); } - for (Command cmd : Command.values()) { + for (Command cmd + : Command.values()) { System.out.printf(" %-10s", cmd.name()); if (!cmd.getDescription().isEmpty()) { System.out.print(" " + cmd.getDescription()); @@ -339,9 +367,11 @@ public class TweetShell implements TwitterApi.PinSupplier { break; case exit: safeClose(); + throw new NoSuchElementException(); case target: - if (params.length > 0) { + if (params.length + > 0) { // due to limitations of shell (does not handle escapes), // do a manualy split as we cannot have spaces in our args. if (params.length == 1) { @@ -384,12 +414,20 @@ public class TweetShell implements TwitterApi.PinSupplier { */ private ClassEnabledTracker<ResultListener> getPossibleTargets() { Map<String, Class<? extends ResultListener>> targets = new TreeMap<>(); - targets.put("file", DataWriter.class); - targets.put("cfile", CompressableDataWriter.class); - targets.put("shell", StreamHandler.class); + targets + .put("file", DataWriter.class + ); + targets.put( + "cfile", CompressableDataWriter.class + ); + targets.put( + "shell", StreamHandler.class + ); ClassEnabledTracker<ResultListener> targetFoo = new ClassEnabledTracker<>(targets); + targetFoo.disableAll(); + targetFoo.enableClasses(resultListeners.getRegistered()); return targetFoo; } |