summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurice Laveaux <m.laveaux@student.tue.nl>2014-04-28 08:31:58 +0200
committerMaurice Laveaux <m.laveaux@student.tue.nl>2014-04-28 08:31:58 +0200
commit7b963dba6a0c300f9fab68cfaf15b8de41a009f6 (patch)
treed0b211c9ddeccad29e6fb435c5f2c1288687e1f1
parent43f0dc66420ca2cd53f387d8cacabb84d4e9965a (diff)
downloadTwitterDataAnalytics-7b963dba6a0c300f9fab68cfaf15b8de41a009f6.tar.gz
First commit for all classes that require implementation for streaming.
* TODO: implement OAuthStream and StreamCommand.
-rw-r--r--src/main/StreamCommand.java43
-rw-r--r--src/mining/OAuthStream.java26
-rw-r--r--src/mining/Stream.java27
-rw-r--r--src/mining/StreamListener.java18
4 files changed, 106 insertions, 8 deletions
diff --git a/src/main/StreamCommand.java b/src/main/StreamCommand.java
index 058cbd6..f0f38fe 100644
--- a/src/main/StreamCommand.java
+++ b/src/main/StreamCommand.java
@@ -1,10 +1,45 @@
-
package main;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import mining.OAuthStream;
+import mining.Stream;
+import mining.StreamListener;
+import mining.TwitterApi;
+import org.json.JSONObject;
+
/**
+ * A command that reads a stream from a stream listener and writes the data to a
+ * file.
*
- * @author maurice
+ * @author Maurice Laveaux
*/
-public class StreamCommand {
-
+public class StreamCommand implements StreamListener {
+
+ /**
+ * A private reference for the stream.
+ */
+ private final Stream m_stream;
+
+ public StreamCommand(final TwitterApi api) {
+
+ m_stream = new OAuthStream();
+ }
+
+ @Override
+ public void notify(JSONObject data) {
+ try {
+ // TODO: add the results to the main database.
+ // For now write to a file
+ File file = new File("database.txt");
+
+ FileWriter writer = new FileWriter(file, true);
+
+ writer.write("\n\r" + data.toString());
+ writer.flush();
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
}
diff --git a/src/mining/OAuthStream.java b/src/mining/OAuthStream.java
new file mode 100644
index 0000000..3cddb57
--- /dev/null
+++ b/src/mining/OAuthStream.java
@@ -0,0 +1,26 @@
+
+package mining;
+
+/**
+ * An implementation of a Stream using the OAuthentication method.
+ * @author Maurice Laveaux
+ */
+public class OAuthStream implements Stream {
+
+ @Override
+ public void open() {
+
+ }
+
+ @Override
+ public void setListener(StreamListener listener) {
+
+ }
+
+ @Override
+ public boolean isValid() {
+
+ return false;
+ }
+
+}
diff --git a/src/mining/Stream.java b/src/mining/Stream.java
index c9c08f0..283b4ea 100644
--- a/src/mining/Stream.java
+++ b/src/mining/Stream.java
@@ -1,9 +1,28 @@
-
package mining;
/**
- * This miner obtains data by receiving a stream from twitter.
+ * interface for all streaming classes.
+ *
+ * @author Maurice Laveaux
*/
-public class Stream {
-
+public interface Stream {
+
+ /**
+ * Open the connection to the server.
+ */
+ public void open();
+
+ /**
+ * Set a single listener for this stream.
+ *
+ * @param listener The object that is listening.
+ */
+ public void setListener(StreamListener listener);
+
+ /**
+ * Test whether the stream is ready for streaming
+ *
+ * @return true if connection can be made, false otherwise.
+ */
+ public boolean isValid();
}
diff --git a/src/mining/StreamListener.java b/src/mining/StreamListener.java
new file mode 100644
index 0000000..3b163eb
--- /dev/null
+++ b/src/mining/StreamListener.java
@@ -0,0 +1,18 @@
+package mining;
+
+import org.json.JSONObject;
+
+/**
+ * A callback provider to listen from streams.
+ *
+ * @author Maurice Laveaux
+ */
+public interface StreamListener {
+
+ /**
+ * This method is called by the stream when new objects are streamed in.
+ *
+ * @param data The JSON data returned.
+ */
+ public void notify(JSONObject data);
+}