added an 'undetermined' category of reddit posts as some subs don't have post_hint value, used to determine if a post is an image or a video for example. this fixes a but where all of these posts that don't have a post_hint value (quite a lot) would simply be ignored by the system

This commit is contained in:
Louis Vallat 2020-08-26 23:24:00 +02:00
parent 12b2a4f723
commit ec67b3d43a
9 changed files with 110 additions and 130 deletions

View File

@ -16,25 +16,18 @@
*/
package xyz.vallat.louis;
import xyz.vallat.louis.socialMediaHandler.SocialMediaPoster;
import xyz.vallat.louis.redditHandler.RedditExtractor;
import xyz.vallat.louis.exceptions.NoSuchFile;
import xyz.vallat.louis.exceptions.NoSuchOrder;
import xyz.vallat.louis.exceptions.NotSufficientRights;
import xyz.vallat.louis.exceptions.NoSuchProperty;
import xyz.vallat.louis.exceptions.NotSufficientRights;
import xyz.vallat.louis.redditHandler.RedditExtractor;
import xyz.vallat.louis.redditHandler.RedditPost;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import xyz.vallat.louis.socialMediaHandler.SocialMediaPoster;
import java.io.*;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Singleton Hypervisor. This is the object that does everything. So we want it
@ -127,7 +120,6 @@ public class Hypervisor {
* Get the Singleton Hypervisor instance.
*
* @return the instance.
*
* @throws ClassNotFoundException
* @throws NotSufficientRights
* @throws SQLException
@ -207,7 +199,6 @@ public class Hypervisor {
* Add a given reddit post to the database.
*
* @param current a given reddit post to add to the database
*
* @throws ClassNotFoundException
* @throws SQLException
*/
@ -239,7 +230,8 @@ public class Hypervisor {
current.isImage() ? "image"
: current.isLink() ? "link"
: current.isText() ? "text"
: "video");
: current.isVideo() ? "video"
: null);
ajout.setString(2, current.getPostId());
ajout.setString(3, current.getTitle());
ajout.setBoolean(4, current.isQuarantine());
@ -262,7 +254,6 @@ public class Hypervisor {
*
* @param postId the post id
* @return if the post is in the database
*
* @throws SQLException
* @throws ClassNotFoundException
*/
@ -309,7 +300,6 @@ public class Hypervisor {
* Compute a given reddit post.
*
* @param r the reddit post to compute.
*
* @throws ClassNotFoundException
* @throws SQLException
*/

View File

@ -62,7 +62,7 @@ public class RedditReposterBot {
System.out.println("[*] App version " + VERSION);
Hypervisor master = Hypervisor.getSingleton();
master.addSocialMedia(new TwitterBot());
//master.addSocialMedia(new TwitterBot());
master.run();
}
}

View File

@ -28,6 +28,7 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
/**
@ -46,7 +47,6 @@ public final class RedditExtractor {
* Main Constructor.
*
* @param subreddit Subreddit name. Just after /r/
*
* @throws IOException
* @throws NoSuchProperty
* @throws NoSuchFile
@ -169,7 +169,8 @@ public final class RedditExtractor {
boolean quarantine = childData.get("quarantine")
.getAsBoolean();
double score = childData.get("score").getAsDouble();
String postHint = childData.get("post_hint").getAsString();
JsonElement postHintElement = childData.get("post_hint");
String postHint = postHintElement == null ? "" : postHintElement.getAsString();
boolean crosspostable = !childData.get("is_crosspostable")
.getAsBoolean();
boolean over18 = childData.get("over_18").getAsBoolean();
@ -190,7 +191,6 @@ public final class RedditExtractor {
}
String permalink = childData.get("permalink").getAsString();
boolean spoiler = childData.get("spoiler").getAsBoolean();
if (postHint.contains("video")) {
set.add(new RedditPostVideo(
id, title, quarantine, score, postHint,
@ -211,6 +211,10 @@ public final class RedditExtractor {
id, title, quarantine, score, postHint,
crosspostable, over18, author,
permalink, spoiler, url));
} else {
set.add(new RedditPostUndetermined(id, title, quarantine, score, postHint,
crosspostable, over18, author,
permalink, spoiler, url));
}
}
} catch (NullPointerException e) {

View File

@ -240,26 +240,40 @@ public abstract class RedditPost {
*
* @return if the post is an image.
*/
public abstract boolean isImage();
public boolean isImage() {
return false;
}
;
/**
* Is this post a text?
*
* @return if the post is a text.
*/
public abstract boolean isText();
public boolean isText() {
return false;
}
;
/**
* Is this post a video?
*
* @return if the post is a video.
*/
public abstract boolean isVideo();
public boolean isVideo() {
return false;
}
/**
* Is this post a link?
*
* @return if the post is a link.
*/
public abstract boolean isLink();
public boolean isLink() {
return false;
}
;
}

View File

@ -35,19 +35,4 @@ public class RedditPostImage extends RedditPost {
public boolean isImage() {
return true;
}
@Override
public boolean isText() {
return false;
}
@Override
public boolean isVideo() {
return false;
}
@Override
public boolean isLink() {
return false;
}
}

View File

@ -31,21 +31,6 @@ public class RedditPostLink extends RedditPost {
crosspostable, over18, author, permalink, spoiler, url);
}
@Override
public boolean isImage() {
return false;
}
@Override
public boolean isText() {
return false;
}
@Override
public boolean isVideo() {
return false;
}
@Override
public boolean isLink() {
return true;

View File

@ -31,23 +31,8 @@ public class RedditPostText extends RedditPost {
crosspostable, over18, author, permalink, spoiler, url);
}
@Override
public boolean isImage() {
return false;
}
@Override
public boolean isText() {
return true;
}
@Override
public boolean isVideo() {
return false;
}
@Override
public boolean isLink() {
return false;
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2019 louis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package xyz.vallat.louis.redditHandler;
/**
* Reddit image post object representation.
*
* @author louis
*/
public class RedditPostUndetermined extends RedditPost {
public RedditPostUndetermined(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);
}
}

View File

@ -31,24 +31,8 @@ public class RedditPostVideo extends RedditPost {
crosspostable, over18, author, permalink, spoiler, url);
}
@Override
public boolean isImage() {
return false;
}
@Override
public boolean isText() {
return false;
}
@Override
public boolean isVideo() {
return true;
}
@Override
public boolean isLink() {
return false;
}
}