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

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
*** GENERATED FROM project.xml - DO NOT EDIT *** *** GENERATED FROM project.xml - DO NOT EDIT ***
*** EDIT ../build.xml INSTEAD *** *** EDIT ../build.xml INSTEAD ***
@ -21,8 +20,6 @@ is divided into following sections:
--> -->
<project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="twitter_techsupportgore_bot-impl"> <project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="twitter_techsupportgore_bot-impl">
<property name="kotlin.lib" value="C:\Users\louis\AppData\Roaming\NetBeans\8.2\kotlinc\lib"/>
<typedef classpath="${kotlin.lib}/kotlin-ant.jar" resource="org/jetbrains/kotlin/ant/antlib.xml"/>
<fail message="Please build using Ant 1.8.0 or higher."> <fail message="Please build using Ant 1.8.0 or higher.">
<condition> <condition>
<not> <not>
@ -276,7 +273,6 @@ is divided into following sections:
<include name="*"/> <include name="*"/>
</dirset> </dirset>
</src> </src>
<withKotlin/>
<classpath> <classpath>
<path path="@{classpath}"/> <path path="@{classpath}"/>
</classpath> </classpath>

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;
@ -248,7 +243,7 @@ public class Hypervisor {
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,13 +307,11 @@ 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());
setPostAsShared(r);
socialMedias.forEach((s) -> { socialMedias.forEach((s) -> {
long postRef = s.postImage( long postRef = s.postImage(
formatPost(r.getTitle()), fileName); formatPost(r.getTitle()), fileName);
@ -378,7 +328,6 @@ public class Hypervisor {
+ "\" has been shared successfully."); + "\" has been shared successfully.");
} }
} }
}
/** /**
* Format a given text for being posted on the different social networks. * Format a given text for being posted on the different social networks.

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

@ -25,8 +25,10 @@ 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

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