From 1ce343d52381610c8ca395535f544b01d37791fc Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Thu, 2 May 2019 16:07:56 +0200 Subject: [PATCH] minor update, more code optimization --- .../nbproject/build-impl.xml | 2714 ++++++++--------- .../ConfigFileReader.java | 15 +- .../TwitterTechSupportGoreBot/Hypervisor.java | 167 +- .../redditHandler/RedditPostImage.java | 6 +- .../redditHandler/RedditPostLink.java | 16 +- .../redditHandler/RedditPostText.java | 6 +- .../redditHandler/RedditPostVideo.java | 6 +- 7 files changed, 1448 insertions(+), 1482 deletions(-) diff --git a/twitter_techsupportgore_bot/nbproject/build-impl.xml b/twitter_techsupportgore_bot/nbproject/build-impl.xml index 072efff..63f8c2a 100644 --- a/twitter_techsupportgore_bot/nbproject/build-impl.xml +++ b/twitter_techsupportgore_bot/nbproject/build-impl.xml @@ -1,5 +1,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set src.dir - Must set test.src.dir - Must set build.dir - Must set dist.dir - Must set build.classes.dir - Must set dist.javadoc.dir - Must set build.test.classes.dir - Must set build.test.results.dir - Must set build.classes.excludes - Must set dist.jar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - No tests executed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set JVM to use for profiling in profiler.info.jvm - Must set profiler agent JVM arguments in profiler.info.jvmargs.agent - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - To run this application from the command line without Ant, try: - - java -jar "${dist.jar.resolved}" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - Must select one file in the IDE or set run.class - - - - Must select one file in the IDE or set run.class - - - + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set debug.class - - - - - Must select one file in the IDE or set debug.class - - - - - Must set fix.includes - - - - - - - + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + - + - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - Must select one file in the IDE or set profile.class - This target only works when run from inside the NetBeans IDE. - - - - - - - - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - - - - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - - - - - - - - - - - - - - + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - Must select one file in the IDE or set run.class - - - - - - Must select some files in the IDE or set test.includes - - - - - Must select one file in the IDE or set run.class - - - - - Must select one file in the IDE or set applet.url - - - - + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + - - - - - - - - Some tests failed; see details above. - - - - - - - - - Must select some files in the IDE or set test.includes - - - - Some tests failed; see details above. - - - - Must select some files in the IDE or set test.class - Must select some method in the IDE or set test.method - - - - Some tests failed; see details above. - - - + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + - - Must select one file in the IDE or set test.class - - - - Must select one file in the IDE or set test.class - Must select some method in the IDE or set test.method - - - - - - - - - - - - + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + - - Must select one file in the IDE or set applet.url - - - - - - - + + Must select one file in the IDE or set applet.url + + + + + + + - - Must select one file in the IDE or set applet.url - - - - - - - - + + Must select one file in the IDE or set applet.url + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + --> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/ConfigFileReader.java b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/ConfigFileReader.java index aeef8db..70e2c08 100644 --- a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/ConfigFileReader.java +++ b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/ConfigFileReader.java @@ -32,7 +32,7 @@ public final class ConfigFileReader { /** * The config file name. */ - private static final String CONFIGFILE = "settings.conf"; + private static String CONFIGFILE = "settings.conf"; /** * The properties object. @@ -49,6 +49,19 @@ public final class ConfigFileReader { readConfigFile(); } + /** + * Create a new config file reader that automatically reads the config file, + * precising the config file name. + * + * @param conf the cnfig file path name. + * @throws NoSuchFile + * @throws NotSufficientRights + */ + public ConfigFileReader(String conf) throws NoSuchFile, NotSufficientRights { + CONFIGFILE = conf; + readConfigFile(); + } + /** * Read the config file. * diff --git a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/Hypervisor.java b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/Hypervisor.java index 522c12f..26ff926 100644 --- a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/Hypervisor.java +++ b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/Hypervisor.java @@ -16,11 +16,7 @@ */ package TwitterTechSupportGoreBot; -import TwitterTechSupportGoreBot.redditHandler.RedditPostImage; import TwitterTechSupportGoreBot.redditHandler.RedditPost; -import TwitterTechSupportGoreBot.redditHandler.RedditPostText; -import TwitterTechSupportGoreBot.redditHandler.RedditPostLink; -import TwitterTechSupportGoreBot.redditHandler.RedditPostVideo; import TwitterTechSupportGoreBot.socialMediaHandler.*; import TwitterTechSupportGoreBot.redditHandler.*; import TwitterTechSupportGoreBot.exceptions.*; @@ -31,7 +27,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; -import java.util.HashMap; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -60,7 +55,7 @@ public class Hypervisor { * SQLITE table name. */ private final String tableName; - + /** * Delay between two scans, in seconds. */ @@ -202,7 +197,7 @@ public class Hypervisor { /** * Add a given reddit post to the database. - * + * * @param current a given reddit post to add to the database * * @throws ClassNotFoundException @@ -211,46 +206,46 @@ public class Hypervisor { private void addRedditPostToDatabase(RedditPost current) throws ClassNotFoundException, SQLException { createTable(); - if (!isInDatabase(current.getPostId())) { - PreparedStatement ajout = this.connexion.prepareStatement( - "INSERT INTO " + this.tableName - + "(" - + "postType, " - + "postId, " - + "title, " - + "quarantine, " - + "score, " - + "postHint, " - + "crosspostable, " - + "over18, " - + "author, " - + "permalink, " - + "spoiler, " - + "url, " - + "shared" - + ") " - + "VALUES " - + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" - ); - ajout.setString(1, - current.isImage() ? "image" - : current.isLink() ? "link" - : current.isText() ? "text" - : "video"); - ajout.setString(2, current.getPostId()); - ajout.setString(3, current.getTitle()); - ajout.setBoolean(4, current.isQuarantine()); - ajout.setDouble(5, current.getScore()); - ajout.setString(6, current.getPostHint()); - ajout.setBoolean(7, current.isCrosspostable()); - ajout.setBoolean(8, current.isOver18()); - ajout.setString(9, current.getAuthor()); - ajout.setString(10, current.getPermalink()); - ajout.setBoolean(11, current.isSpoiler()); - ajout.setString(12, current.getUrl()); - ajout.setBoolean(13, false); - ajout.execute(); - + if (!isInDatabase(current.getPostId())) { + PreparedStatement ajout = this.connexion.prepareStatement( + "INSERT INTO " + this.tableName + + "(" + + "postType, " + + "postId, " + + "title, " + + "quarantine, " + + "score, " + + "postHint, " + + "crosspostable, " + + "over18, " + + "author, " + + "permalink, " + + "spoiler, " + + "url, " + + "shared" + + ") " + + "VALUES " + + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" + ); + ajout.setString(1, + current.isImage() ? "image" + : current.isLink() ? "link" + : current.isText() ? "text" + : "video"); + ajout.setString(2, current.getPostId()); + ajout.setString(3, current.getTitle()); + ajout.setBoolean(4, current.isQuarantine()); + ajout.setDouble(5, current.getScore()); + ajout.setString(6, current.getPostHint()); + ajout.setBoolean(7, current.isCrosspostable()); + ajout.setBoolean(8, current.isOver18()); + ajout.setString(9, current.getAuthor()); + ajout.setString(10, current.getPermalink()); + ajout.setBoolean(11, current.isSpoiler()); + ajout.setString(12, current.getUrl()); + ajout.setBoolean(13, true); + ajout.execute(); + } } @@ -273,49 +268,6 @@ public class Hypervisor { } } - /** - * Check if a post is marked as shared in database. - * - * @param postId the post id - * @return if the post is shared in the database - * - * @throws SQLException - * @throws ClassNotFoundException - */ - private boolean isShared(String postId) - throws SQLException, ClassNotFoundException { - PreparedStatement recherche = this.connexion.prepareStatement( - "SELECT shared FROM " + this.tableName + " " - + "WHERE postId = '" + postId + "'"); - try (ResultSet resultats = recherche.executeQuery()) { - if (resultats.next()) { - return resultats.getBoolean("shared"); - } - return false; - } - } - - /** - * Set a post as shared onsocial networks. - * - * @param postId the post id - * - * @throws SQLException - * @throws ClassNotFoundException - */ - private void setPostAsShared(RedditPost r) - throws SQLException, ClassNotFoundException { - System.out.println("[+] Post \"" - + r.getTitle() - + "\" is about to be shared."); - PreparedStatement update = this.connexion.prepareStatement( - "UPDATE " + this.tableName + " " - + "SET shared=? " - + "WHERE postId = '" + r.getPostId() + "';"); - update.setBoolean(1, true); - update.execute(); - } - /** * Create our working table if it doesn't exists yet. * @@ -355,28 +307,25 @@ public class Hypervisor { */ private void computeRedditPost(RedditPost r) throws SQLException, ClassNotFoundException { - if (!isShared(r.getPostId()) && !r.isQuarantine()) { + if (!isInDatabase(r.getPostId()) && !r.isQuarantine() && r.hasMediaUrl()) { System.out.println( "[*] Computing the post \"" + r.getTitle() + "\""); addRedditPostToDatabase(r); - if (r.hasMediaUrl()) { - String fileName = saveImage(r.getUrl()); - setPostAsShared(r); - socialMedias.forEach((s) -> { - long postRef = s.postImage( - formatPost(r.getTitle()), fileName); - if (postRef != 0 && postRef != -1) { - s.replyText(formatPost("from /u/" + r.getAuthor() + " " - + "on /r/" + this.subreddit + " " - + "at link : https://www.reddit.com" - + r.getPermalink()), postRef); - } - }); - deleteFile(fileName); - System.out.println("[+] Post \"" - + r.getTitle() - + "\" has been shared successfully."); - } + String fileName = saveImage(r.getUrl()); + socialMedias.forEach((s) -> { + long postRef = s.postImage( + formatPost(r.getTitle()), fileName); + if (postRef != 0 && postRef != -1) { + s.replyText(formatPost("from /u/" + r.getAuthor() + " " + + "on /r/" + this.subreddit + " " + + "at link : https://www.reddit.com" + + r.getPermalink()), postRef); + } + }); + deleteFile(fileName); + System.out.println("[+] Post \"" + + r.getTitle() + + "\" has been shared successfully."); } } diff --git a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostImage.java b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostImage.java index 4132bd1..f889507 100644 --- a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostImage.java +++ b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostImage.java @@ -25,8 +25,10 @@ public class RedditPostImage extends RedditPost { public RedditPostImage(String id, String title, boolean quarantine, double score, String postHint, boolean crosspostable, - boolean over18, String author, String permalink, boolean spoiler, String url) { - super(id, title, quarantine, score, postHint, crosspostable, over18, author, permalink, spoiler, url); + boolean over18, String author, String permalink, + boolean spoiler, String url) { + super(id, title, quarantine, score, postHint, + crosspostable, over18, author, permalink, spoiler, url); } @Override diff --git a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostLink.java b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostLink.java index 18cbdfc..e9580b3 100644 --- a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostLink.java +++ b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostLink.java @@ -17,16 +17,18 @@ package TwitterTechSupportGoreBot.redditHandler; /** - * Reddit link post object representation. - * + * Reddit link post object representation. + * * @author louis */ public class RedditPostLink extends RedditPost { - public RedditPostLink(String id, String title, boolean quarantine, - double score, String postHint, boolean crosspostable, - boolean over18, String author, String permalink, boolean spoiler, String url) { - super(id, title, quarantine, score, postHint, crosspostable, over18, author, permalink, spoiler, url); + public RedditPostLink(String id, String title, boolean quarantine, + double score, String postHint, boolean crosspostable, + boolean over18, String author, String permalink, + boolean spoiler, String url) { + super(id, title, quarantine, score, postHint, + crosspostable, over18, author, permalink, spoiler, url); } @Override @@ -48,5 +50,5 @@ public class RedditPostLink extends RedditPost { public boolean isLink() { return true; } - + } diff --git a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostText.java b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostText.java index dc7dfd2..687f9ab 100644 --- a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostText.java +++ b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostText.java @@ -25,8 +25,10 @@ public class RedditPostText extends RedditPost { public RedditPostText(String id, String title, boolean quarantine, double score, String postHint, boolean crosspostable, - boolean over18, String author, String permalink, boolean spoiler, String url) { - super(id, title, quarantine, score, postHint, crosspostable, over18, author, permalink, spoiler, url); + boolean over18, String author, String permalink, + boolean spoiler, String url) { + super(id, title, quarantine, score, postHint, + crosspostable, over18, author, permalink, spoiler, url); } @Override diff --git a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostVideo.java b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostVideo.java index c5b6819..5153be9 100644 --- a/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostVideo.java +++ b/twitter_techsupportgore_bot/src/TwitterTechSupportGoreBot/redditHandler/RedditPostVideo.java @@ -25,8 +25,10 @@ public class RedditPostVideo extends RedditPost { public RedditPostVideo(String id, String title, boolean quarantine, double score, String postHint, boolean crosspostable, - boolean over18, String author, String permalink, boolean spoiler, String url) { - super(id, title, quarantine, score, postHint, crosspostable, over18, author, permalink, spoiler, url); + boolean over18, String author, String permalink, + boolean spoiler, String url) { + super(id, title, quarantine, score, postHint, + crosspostable, over18, author, permalink, spoiler, url); } @Override