summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-08 19:05:36 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-08 19:05:36 +0200
commit9fe9d1b848156928a0d3efde4fff80a78f1bf9e1 (patch)
treee1e987f71ecb90d513e994b9cc4f3aa6c3656bdb /src/main
parent5f892ac1e0fe6a21a00808cfb0afae2a2c2071dc (diff)
downloadDatafiller-9fe9d1b848156928a0d3efde4fff80a78f1bf9e1.tar.gz
Replace JSON by GSON, adding extra validations
Also change reader method, tweets are not received via an observed but by submitting from the caller. Added TODO WTF here and there, formatted with Alt + Shift + F.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/DataFiller.java107
-rw-r--r--src/main/Main.java82
2 files changed, 100 insertions, 89 deletions
diff --git a/src/main/DataFiller.java b/src/main/DataFiller.java
index c9a5811..a65a032 100644
--- a/src/main/DataFiller.java
+++ b/src/main/DataFiller.java
@@ -1,21 +1,21 @@
package main;
+import data.Tweet;
import database.DBConnection;
import database.QueryUtils;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
+import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
-import org.json.JSONException;
-import org.json.JSONObject;
/**
* Process that incoming tweets and fill the database.
*
* @author Maurice Laveaux
*/
-public class DataFiller implements ResultListener {
+public class DataFiller {
/**
* The main database connection to fill.
@@ -26,7 +26,7 @@ public class DataFiller implements ResultListener {
* A single insert tweet that can be used.
*/
private PreparedStatement m_insertTweet;
-
+
/**
* A single insert profiles that can be used.
*/
@@ -35,7 +35,7 @@ public class DataFiller implements ResultListener {
* A single insert ispostedby that can be used.
*/
private PreparedStatement m_insertPosted;
-
+
/**
* A single insert brand that can be used.
*/
@@ -63,7 +63,7 @@ public class DataFiller implements ResultListener {
*/
public DataFiller(DBConnection connection) {
try {
- m_connection = connection;
+ m_connection = connection;
m_insertTweet = m_connection.create(QueryUtils.insertTweet());
m_insertProfile = m_connection.create(QueryUtils.insertProfile());
m_insertPosted = m_connection.create(QueryUtils.insertPosted());
@@ -71,79 +71,68 @@ public class DataFiller implements ResultListener {
m_insertHash = m_connection.create(QueryUtils.insertHash());
m_insertUrl = m_connection.create(QueryUtils.insertUrl());
m_insertMentions = m_connection.create(QueryUtils.insertMentions());
-
-
-
+
} catch (SQLException ex) {
throw new RuntimeException(ex.getMessage());
}
}
- @Override
- public void tweetReceived(JSONObject tweet) {
+ public void processTweet(Tweet tweet) {
try {
-
- for(int i=0;i<tweet.getJSONObject("entities").getJSONArray("hashtags").length();i++){
- String text = tweet.getJSONObject("entities").getJSONArray("hashtags").getJSONObject(i).getString("text");
- QueryUtils.setInsertHashParams(m_insertHash, tweet.getLong("id"), text);
+ for (Tweet.Hashtag hashtag : tweet.entities.hashtags) {
+ QueryUtils.setInsertHashParams(m_insertHash, tweet.id, hashtag.text);
m_insertHash.executeUpdate();
}
- for(int i=0;i<tweet.getJSONObject("entities").getJSONArray("urls").length();i++){
- String text = tweet.getJSONObject("entities").getJSONArray("urls").getJSONObject(i).getString("expanded_url");
- QueryUtils.setInsertHashParams(m_insertUrl, tweet.getLong("id"), text);
+ for (Tweet.Url url : tweet.entities.urls) {
+ QueryUtils.setInsertHashParams(m_insertUrl, tweet.id, url.expanded_url);
m_insertUrl.executeUpdate();
}
- for(int i=0;i<tweet.getJSONObject("entities").getJSONArray("user_mentions").length();i++){
- Long id = tweet.getJSONObject("entities").getJSONArray("user_mentions").getJSONObject(i).getLong("id");
- QueryUtils.setInsertMentionsParams(m_insertMentions, tweet.getLong("id"), id);
+ for (Tweet.Mention mention : tweet.entities.user_mentions) {
+ QueryUtils.setInsertMentionsParams(m_insertMentions, tweet.id, mention.id);
m_insertMentions.executeUpdate();
}
-
- QueryUtils.setInsertParams(m_insertTweet,m_insertProfile,m_insertPosted, tweet);
+
+ QueryUtils.setInsertParams(m_insertTweet, m_insertProfile, m_insertPosted, tweet);
m_insertTweet.executeUpdate();
m_insertProfile.executeUpdate();
m_insertPosted.executeUpdate();
- ArrayList<String> brands=getBrands(tweet);
- for(String brand : brands){
- QueryUtils.setInsertBrandParams(m_insertBrand, tweet.getLong("id"), brand);
+ List<String> brands = getBrands(tweet);
+ for (String brand : brands) {
+ QueryUtils.setInsertBrandParams(m_insertBrand, tweet.id, brand);
m_insertBrand.executeUpdate();
}
- } catch (SQLException | JSONException ex) {
+ } catch (SQLException ex) {
Logger.getLogger(DataFiller.class.getName()).log(Level.SEVERE, null, ex);
}
}
- ArrayList<String> getBrands(JSONObject tweet){
- ArrayList<String> result= new ArrayList<String>();
- String text=null;
- try {
- text = tweet.getString("text");
- text = text.toLowerCase();
- } catch (JSONException ex) {
-
+
+ ArrayList<String> getBrands(Tweet tweet) {
+ ArrayList<String> result = new ArrayList<>();
+ String text = tweet.text.toLowerCase();
+ if (text.contains("samsung") || text.contains("galaxy")) {
+ result.add("Samsung");
}
- if(text.contains("samsung")||text.contains("galaxy")){
- result.add("Samsung");
- }
- if (text.contains("htc")||text.contains("one")){
- result.add("HTC");
- }
- if (text.contains("apple")||text.contains("iphone")){
- result.add("Apple");
- }
- if (text.contains("sony")||text.contains("xperia")){
- result.add("Sony");
- }
- if (text.contains("huawei")||text.contains("ascend")){
- result.add("Huawei");
- }
- if (text.contains("lg")){
- result.add("LG");
- }
-
- if(result.isEmpty()) {
- result.add("geen");
- System.out.println(text);
- }
- return result;
+ if (text.contains("htc") || text.contains("one")) {
+ result.add("HTC");
+ }
+ if (text.contains("apple") || text.contains("iphone")) {
+ result.add("Apple");
+ }
+ if (text.contains("sony") || text.contains("xperia")) {
+ result.add("Sony");
}
+ if (text.contains("huawei") || text.contains("ascend")) {
+ result.add("Huawei");
+ }
+ if (text.contains("lg")) {
+ result.add("LG");
+ }
+
+ // TODO: WTF IS THIS PILE OF SHIT?!
+ if (result.isEmpty()) {
+ result.add("geen");
+ System.out.println(text);
+ }
+ return result;
+ }
}
diff --git a/src/main/Main.java b/src/main/Main.java
index f4f644b..5a8e137 100644
--- a/src/main/Main.java
+++ b/src/main/Main.java
@@ -1,9 +1,13 @@
package main;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonSyntaxException;
+import data.Tweet;
import database.DBConnection;
-import io.DataReader;
-import io.InputReader;
-import java.util.Arrays;
+import io.FileTweetReader;
+import io.ITweetReader;
+import io.TweetReader;
+import java.io.IOException;
/**
* The main class.
@@ -12,6 +16,7 @@ public class Main {
/**
* The main method of the application.
+ *
* @param args the global arguments to pass to the program.
*/
public static void main(String[] args) {
@@ -22,43 +27,56 @@ public class Main {
System.exit(1);
}
}
-
+
private String m_hostaddress;
-
+
private String m_filename;
-
+
public Main(String[] args) {
/* parse the global options. */
parseGlobalOptions(args);
-
+
if (m_hostaddress == null) {
throw new IllegalArgumentException("Missing --dbhost to specify the hostaddress.");
- }
-
+ }
+
DBConnection connection = new DBConnection(m_hostaddress, "5432", "Twitter", "postgres", "2IOC02");
- /* create the object that fills the database */
+ /* create the object that fills the database */
DataFiller filler = new DataFiller(connection);
-
- if (m_filename == null) {
- InputReader reader = new InputReader(filler);
-
- reader.startLoop();
- } else {
- DataReader reader = new DataReader(m_filename, filler);
- reader.startloop();
+
+ ITweetReader reader = null;
+ try {
+ if (m_filename == null) {
+ reader = new TweetReader(System.in);
+ } else {
+ reader = new FileTweetReader(m_filename);
+ }
+
+ Tweet tweet;
+ while ((tweet = reader.getTweet()) != null) {
+ filler.processTweet(tweet);
+ }
+ } catch (JsonSyntaxException ex) {
+ System.err.println("Got an invalid tweet: " + ex);
+ } catch (IOException ex) {
+ System.err.println("Cannot open tweets: " + ex);
+ } finally {
+ if (reader != null) {
+ reader.close();
+ }
}
-
+
connection.close();
-
+
System.out.print("exit succesfull.");
}
-
+
private void parseGlobalOptions(String[] args) {
/* parse global options */
for (int i = 0; i < args.length; i++) {
if ("--help".equals(args[i])) {
printHelp();
- } else if("--dbhost".equals(args[i])) {
+ } else if ("--dbhost".equals(args[i])) {
m_hostaddress = getParam(args, ++i);
} else if (args[i].startsWith("-")) {
throw new IllegalArgumentException("Invalid option: " + args[i]);
@@ -67,29 +85,33 @@ public class Main {
m_filename = getParam(args, i);
}
}
-
+
if (args.length == 0) {
- throw new IllegalArgumentException("No parameters specified, see --help for usage info.");
+ throw new IllegalArgumentException("No parameters specified, see --help for usage info.");
}
}
-
- /** Read an extra option for a command. */
+
+ /**
+ * Read an extra option for a command.
+ */
private String getParam(String[] args, Integer index) {
if (index + 1 <= args.length) {
- index++;
+ index++;
return args[index - 1];
} else {
throw new IllegalArgumentException("An extra option was missing.");
}
}
-
- /** Print some useful help messages. */
+
+ /**
+ * Print some useful help messages.
+ */
private void printHelp() {
for (String line : HELP) {
System.out.println(line);
}
}
-
+
private final static String[] HELP = {
"Global options:",
" --help Print this help text.",