minor update, more code optimization

This commit is contained in:
Louis Vallat 2019-05-02 16:07:56 +02:00
parent 930306a581
commit 1ce343d523
7 changed files with 1448 additions and 1482 deletions

File diff suppressed because it is too large Load Diff

View File

@ -32,7 +32,7 @@ public final class ConfigFileReader {
/** /**
* The config file name. * The config file name.
*/ */
private static final String CONFIGFILE = "settings.conf"; private static String CONFIGFILE = "settings.conf";
/** /**
* The properties object. * The properties object.
@ -49,6 +49,19 @@ public final class ConfigFileReader {
readConfigFile(); 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. * Read the config file.
* *

View File

@ -16,11 +16,7 @@
*/ */
package TwitterTechSupportGoreBot; package TwitterTechSupportGoreBot;
import TwitterTechSupportGoreBot.redditHandler.RedditPostImage;
import TwitterTechSupportGoreBot.redditHandler.RedditPost; import TwitterTechSupportGoreBot.redditHandler.RedditPost;
import TwitterTechSupportGoreBot.redditHandler.RedditPostText;
import TwitterTechSupportGoreBot.redditHandler.RedditPostLink;
import TwitterTechSupportGoreBot.redditHandler.RedditPostVideo;
import TwitterTechSupportGoreBot.socialMediaHandler.*; import TwitterTechSupportGoreBot.socialMediaHandler.*;
import TwitterTechSupportGoreBot.redditHandler.*; import TwitterTechSupportGoreBot.redditHandler.*;
import TwitterTechSupportGoreBot.exceptions.*; import TwitterTechSupportGoreBot.exceptions.*;
@ -31,7 +27,6 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -60,7 +55,7 @@ public class Hypervisor {
* SQLITE table name. * SQLITE table name.
*/ */
private final String tableName; private final String tableName;
/** /**
* Delay between two scans, in seconds. * Delay between two scans, in seconds.
*/ */
@ -202,7 +197,7 @@ public class Hypervisor {
/** /**
* Add a given reddit post to the database. * Add a given reddit post to the database.
* *
* @param current a given reddit post to add to the database * @param current a given reddit post to add to the database
* *
* @throws ClassNotFoundException * @throws ClassNotFoundException
@ -211,46 +206,46 @@ public class Hypervisor {
private void addRedditPostToDatabase(RedditPost current) private void addRedditPostToDatabase(RedditPost current)
throws ClassNotFoundException, SQLException { throws ClassNotFoundException, SQLException {
createTable(); createTable();
if (!isInDatabase(current.getPostId())) { if (!isInDatabase(current.getPostId())) {
PreparedStatement ajout = this.connexion.prepareStatement( PreparedStatement ajout = this.connexion.prepareStatement(
"INSERT INTO " + this.tableName "INSERT INTO " + this.tableName
+ "(" + "("
+ "postType, " + "postType, "
+ "postId, " + "postId, "
+ "title, " + "title, "
+ "quarantine, " + "quarantine, "
+ "score, " + "score, "
+ "postHint, " + "postHint, "
+ "crosspostable, " + "crosspostable, "
+ "over18, " + "over18, "
+ "author, " + "author, "
+ "permalink, " + "permalink, "
+ "spoiler, " + "spoiler, "
+ "url, " + "url, "
+ "shared" + "shared"
+ ") " + ") "
+ "VALUES " + "VALUES "
+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
); );
ajout.setString(1, ajout.setString(1,
current.isImage() ? "image" current.isImage() ? "image"
: current.isLink() ? "link" : current.isLink() ? "link"
: current.isText() ? "text" : current.isText() ? "text"
: "video"); : "video");
ajout.setString(2, current.getPostId()); ajout.setString(2, current.getPostId());
ajout.setString(3, current.getTitle()); ajout.setString(3, current.getTitle());
ajout.setBoolean(4, current.isQuarantine()); ajout.setBoolean(4, current.isQuarantine());
ajout.setDouble(5, current.getScore()); ajout.setDouble(5, current.getScore());
ajout.setString(6, current.getPostHint()); ajout.setString(6, current.getPostHint());
ajout.setBoolean(7, current.isCrosspostable()); ajout.setBoolean(7, current.isCrosspostable());
ajout.setBoolean(8, current.isOver18()); ajout.setBoolean(8, current.isOver18());
ajout.setString(9, current.getAuthor()); ajout.setString(9, current.getAuthor());
ajout.setString(10, current.getPermalink()); ajout.setString(10, current.getPermalink());
ajout.setBoolean(11, current.isSpoiler()); ajout.setBoolean(11, current.isSpoiler());
ajout.setString(12, current.getUrl()); ajout.setString(12, current.getUrl());
ajout.setBoolean(13, false); ajout.setBoolean(13, true);
ajout.execute(); 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. * Create our working table if it doesn't exists yet.
* *
@ -355,28 +307,25 @@ public class Hypervisor {
*/ */
private void computeRedditPost(RedditPost r) private void computeRedditPost(RedditPost r)
throws SQLException, ClassNotFoundException { throws SQLException, ClassNotFoundException {
if (!isShared(r.getPostId()) && !r.isQuarantine()) { if (!isInDatabase(r.getPostId()) && !r.isQuarantine() && r.hasMediaUrl()) {
System.out.println( System.out.println(
"[*] Computing the post \"" + r.getTitle() + "\""); "[*] Computing the post \"" + r.getTitle() + "\"");
addRedditPostToDatabase(r); addRedditPostToDatabase(r);
if (r.hasMediaUrl()) { String fileName = saveImage(r.getUrl());
String fileName = saveImage(r.getUrl()); socialMedias.forEach((s) -> {
setPostAsShared(r); long postRef = s.postImage(
socialMedias.forEach((s) -> { formatPost(r.getTitle()), fileName);
long postRef = s.postImage( if (postRef != 0 && postRef != -1) {
formatPost(r.getTitle()), fileName); s.replyText(formatPost("from /u/" + r.getAuthor() + " "
if (postRef != 0 && postRef != -1) { + "on /r/" + this.subreddit + " "
s.replyText(formatPost("from /u/" + r.getAuthor() + " " + "at link : https://www.reddit.com"
+ "on /r/" + this.subreddit + " " + r.getPermalink()), postRef);
+ "at link : https://www.reddit.com" }
+ r.getPermalink()), postRef); });
} deleteFile(fileName);
}); System.out.println("[+] Post \""
deleteFile(fileName); + r.getTitle()
System.out.println("[+] Post \"" + "\" has been shared successfully.");
+ r.getTitle()
+ "\" has been shared successfully.");
}
} }
} }

View File

@ -25,8 +25,10 @@ public class RedditPostImage extends RedditPost {
public RedditPostImage(String id, String title, boolean quarantine, public RedditPostImage(String id, String title, boolean quarantine,
double score, String postHint, boolean crosspostable, double score, String postHint, boolean crosspostable,
boolean over18, String author, String permalink, boolean spoiler, String url) { boolean over18, String author, String permalink,
super(id, title, quarantine, score, postHint, crosspostable, over18, author, permalink, spoiler, url); boolean spoiler, String url) {
super(id, title, quarantine, score, postHint,
crosspostable, over18, author, permalink, spoiler, url);
} }
@Override @Override

View File

@ -17,16 +17,18 @@
package TwitterTechSupportGoreBot.redditHandler; package TwitterTechSupportGoreBot.redditHandler;
/** /**
* Reddit link post object representation. * Reddit link post object representation.
* *
* @author louis * @author louis
*/ */
public class RedditPostLink extends RedditPost { public class RedditPostLink extends RedditPost {
public RedditPostLink(String id, String title, boolean quarantine, public RedditPostLink(String id, String title, boolean quarantine,
double score, String postHint, boolean crosspostable, double score, String postHint, boolean crosspostable,
boolean over18, String author, String permalink, boolean spoiler, String url) { boolean over18, String author, String permalink,
super(id, title, quarantine, score, postHint, crosspostable, over18, author, permalink, spoiler, url); boolean spoiler, String url) {
super(id, title, quarantine, score, postHint,
crosspostable, over18, author, permalink, spoiler, url);
} }
@Override @Override
@ -48,5 +50,5 @@ public class RedditPostLink extends RedditPost {
public boolean isLink() { public boolean isLink() {
return true; return true;
} }
} }

View File

@ -25,8 +25,10 @@ public class RedditPostText extends RedditPost {
public RedditPostText(String id, String title, boolean quarantine, public RedditPostText(String id, String title, boolean quarantine,
double score, String postHint, boolean crosspostable, double score, String postHint, boolean crosspostable,
boolean over18, String author, String permalink, boolean spoiler, String url) { boolean over18, String author, String permalink,
super(id, title, quarantine, score, postHint, crosspostable, over18, author, permalink, spoiler, url); boolean spoiler, String url) {
super(id, title, quarantine, score, postHint,
crosspostable, over18, author, permalink, spoiler, url);
} }
@Override @Override

View File

@ -25,8 +25,10 @@ public class RedditPostVideo extends RedditPost {
public RedditPostVideo(String id, String title, boolean quarantine, public RedditPostVideo(String id, String title, boolean quarantine,
double score, String postHint, boolean crosspostable, double score, String postHint, boolean crosspostable,
boolean over18, String author, String permalink, boolean spoiler, String url) { boolean over18, String author, String permalink,
super(id, title, quarantine, score, postHint, crosspostable, over18, author, permalink, spoiler, url); boolean spoiler, String url) {
super(id, title, quarantine, score, postHint,
crosspostable, over18, author, permalink, spoiler, url);
} }
@Override @Override