diff options
author | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-05-01 17:09:31 +0200 |
---|---|---|
committer | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-05-01 17:09:31 +0200 |
commit | 355bb332328a16ae967cf276bfdbe4cda0776c47 (patch) | |
tree | d6ba7b3cdcda3ae8da8ce6eff167f5c1395fcf58 | |
parent | 24fc22336273e919517cde2e8a81b5e4222f9e91 (diff) | |
download | TwitterDataAnalytics-355bb332328a16ae967cf276bfdbe4cda0776c47.tar.gz |
Added the target command with options: file, shell.
* Implemented target file, which uses DataWriter as
RequestListener which writes the tweets and profiles.
* Added profiles-filename, tweets-filename to the config.
-rw-r--r-- | src/io/DataWriter.java | 19 | ||||
-rw-r--r-- | src/io/StreamImpl.java | 4 | ||||
-rw-r--r-- | src/main/TweetShell.java | 46 | ||||
-rw-r--r-- | src/utils/Configuration.java | 16 |
4 files changed, 73 insertions, 12 deletions
diff --git a/src/io/DataWriter.java b/src/io/DataWriter.java index bbf0918..c26dbf3 100644 --- a/src/io/DataWriter.java +++ b/src/io/DataWriter.java @@ -39,14 +39,16 @@ public class DataWriter implements ResultListener { * the buffer of profile ids that already exist. */ private final Set<Long> m_profileIdSet; - - private static final String profilesName = "profiles.txt"; - private static final String tweetsName = "tweets.txt"; + + public final static String CFG_PROFILE_FILENAME = "profiles-filename"; + public final static String CFG_TWEETS_FILENAME = "tweets-filename"; /** * Opens a stream to every single file that data will be streamed to. + * @param profilesName The file to write the profiles to. + * @param tweetsName The file to write the tweets to. */ - public DataWriter() { + public DataWriter(final String profilesName, final String tweetsName) { try { m_profileIdSet = readIds(profilesName); m_profileWriter = new FileWriter(profilesName, true); @@ -58,6 +60,15 @@ public class DataWriter implements ResultListener { throw new RuntimeException(ex.getMessage()); } } + + public void close() { + try { + m_tweetWriter.close(); + m_profileWriter.close(); + } catch (IOException ex) { + getLogger().log(Level.SEVERE, null, ex); + } + } @Override public void profileGenerated(JSONObject obj) { diff --git a/src/io/StreamImpl.java b/src/io/StreamImpl.java index d72f926..16d0f2b 100644 --- a/src/io/StreamImpl.java +++ b/src/io/StreamImpl.java @@ -70,6 +70,10 @@ public class StreamImpl implements Stream { this.resultListener = resultListener; } + public ResultListener getResultListener() { + return resultListener; + } + public void setExceptionListener(ExceptionListener exceptionListener) { synchronized (listenerSync) { this.exceptionListener = exceptionListener; diff --git a/src/main/TweetShell.java b/src/main/TweetShell.java index b318544..25d785a 100644 --- a/src/main/TweetShell.java +++ b/src/main/TweetShell.java @@ -1,5 +1,6 @@ package main; +import io.DataWriter; import io.OAuthRequester; import io.StreamImpl; import java.io.IOException; @@ -11,6 +12,7 @@ import mining.TwitterApi; import org.json.JSONObject; import provider.ExceptionListener; import provider.ResultListener; +import utils.Configuration; /** * Provides an interactive shell where requests can be made and displayed. @@ -19,9 +21,6 @@ public class TweetShell implements TwitterApi.PinSupplier { private final Scanner scanner = new Scanner(System.in); - public TweetShell() { - - } private TwitterApi api_cached; private Stream stream_cached; @@ -39,9 +38,7 @@ public class TweetShell implements TwitterApi.PinSupplier { stream_cached = new StreamImpl(requester); StreamImpl streamObserver = (StreamImpl) stream_cached; streamObserver.setExceptionListener(handler); - // TODO: wire up DataWriter and add a command ("target"?) to set a - // point to save results - streamObserver.setResultListener(handler); + streamObserver.setResultListener(new StreamHandler()); } return stream_cached; } @@ -78,6 +75,7 @@ public class TweetShell implements TwitterApi.PinSupplier { System.out.print("€ "); } } + /** * Processes commands from stdin until the exit command is received or EOF. */ @@ -135,7 +133,8 @@ public class TweetShell implements TwitterApi.PinSupplier { close("Close the stream"), test(""), exit("Returns to shell"), - help("Get help"); + help("Get help"), + target("Set output target: {file, shell}."); private final String description; private final int paramCount; @@ -215,8 +214,41 @@ public class TweetShell implements TwitterApi.PinSupplier { break; case exit: throw new NoSuchElementException(); + case target: + StreamImpl stream = (StreamImpl) getStream(); + + ResultListener oldListener = stream.getResultListener(); + if (oldListener instanceof DataWriter) { + ((DataWriter) oldListener).close(); + } + + switch (params[0]) { + case "file": + if (!(oldListener instanceof DataWriter)) { + Configuration config = Configuration.getConfig(); + + String profilesFilename = config.getProperty(DataWriter.CFG_PROFILE_FILENAME, "profiles.txt"); + String tweetsFilename = config.getProperty(DataWriter.CFG_TWEETS_FILENAME, "tweets.txt"); + + stream.setResultListener(new DataWriter(profilesFilename, tweetsFilename)); + + // save the changes to the config. + config.setProperty(DataWriter.CFG_PROFILE_FILENAME, profilesFilename); + config.setProperty(DataWriter.CFG_TWEETS_FILENAME, tweetsFilename); + config.save(); + } + break; + case "shell": + if (!(oldListener instanceof StreamHandler)) { + stream.setResultListener(new StreamHandler()); + } + break; + } + break; default: throw new AssertionError(command.name()); } + } +; } diff --git a/src/utils/Configuration.java b/src/utils/Configuration.java index abb4df0..78bd9e2 100644 --- a/src/utils/Configuration.java +++ b/src/utils/Configuration.java @@ -22,6 +22,9 @@ public class Configuration { public static final String REQUEST_TOKEN_URL = "https://twitter.com/oauth/request_token"; public static final String AUTHORIZE_URL = "https://twitter.com/oauth/authorize"; public static final String ACCESS_TOKEN_URL = "https://twitter.com/oauth/access_token"; + + public static final String DEFAULT_TWEETS_FILENAME = "tweets.txt"; + public static final String DEFAULT_PROFILE_FILENAME = "profiles.txt"; private final Properties properties; private final File storeFile; @@ -77,6 +80,17 @@ public class Configuration { * @return The value for the key or null if there is none. */ public String getProperty(String key) { - return properties.getProperty(key); + return getProperty(key, null); + } + + /** + * Gets a setting with the given key. + * + * @param key Name of the configuration setting. + * @param defaultVal The default value for the key. + * @return The value for the key or null if there is none. + */ + public String getProperty(String key, String defaultVal) { + return properties.getProperty(key, defaultVal); } } |