Created the Hyervisor

This commit is contained in:
Louis Vallat 2019-04-24 11:26:16 +02:00
parent 105b59abf8
commit ae1765851b
4 changed files with 97 additions and 16 deletions

View File

@ -0,0 +1,82 @@
/*
* 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 twitter_techsupportgore_bot;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
/**
* Singleton Hypervisor. This is the object that does everything. So we want it
* to be unique. That's why it's a singleton.
*
* @author louis
*/
public class Hypervisor {
/**
* Private constructor so nobody except this obect can build this object.
*/
private Hypervisor() {
}
/**
* The singleton.
*/
private static Hypervisor SINGLETON = null;
/**
* Get the Singleton Hypervisor instance.
*
* @return the instance.
*/
public static Hypervisor getSingleton() {
if (SINGLETON == null) {
SINGLETON = new Hypervisor();
}
return SINGLETON;
}
/**
* Save Image from URL. Modified version of the code that can be found atF
* https://www.programcreek.com/2012/12/download-image-from-url-in-java/
*
* @param imageUrl the image URL.
* @throws IOException
*/
public static void saveImage(String imageUrl) throws IOException {
URL url = new URL(imageUrl);
String fileName = url.getFile();
String destName = "../downloads" + fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("?"));
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destName);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
}

View File

@ -16,18 +16,9 @@
*/
package twitter_techsupportgore_bot;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Collection;
import java.util.TreeMap;
import java.util.TreeSet;
import twitter_techsupportgore_bot.reddit_handler.*;
@ -38,7 +29,15 @@ import twitter_techsupportgore_bot.reddit_handler.*;
*/
public class Twitter_techsupportgore_bot {
private static HttpURLConnection con;
/**
* Delay between two scans, in seconds.
*/
private static final int DELAY = 5;
/**
* Subreddit to extract info from.
*/
private static final String SUBREDDIT = "techsupportgore";
/**
* Launch the bot.
@ -51,9 +50,9 @@ public class Twitter_techsupportgore_bot {
ProtocolException, IOException {
TreeSet<RedditPost> postsIndexed = new TreeSet<>();
RedditExtractor red = new RedditExtractor("techsupportgore");
RedditExtractor red = new RedditExtractor(SUBREDDIT);
for (RedditPost r : red.getRedditPosts()) {
System.out.println(r.getTitle());
Hypervisor.saveImage(r.getUrl());
}
}
}
}

View File

@ -132,7 +132,7 @@ public final class RedditExtractor {
String id = childData.get("id").toString();
String title = childData.get("title") != null
? childData.get("title").toString() : this.sub.getName();
title = title.replaceAll("\"", "").replace("\\", "\"");
title = title.replace("\"", "").replace("\\", "\"");
String author = childData.get("author") != null
? childData.get("author").toString() : "anonymous";
boolean quarantine = childData.get("quarantine").getAsBoolean();
@ -146,7 +146,7 @@ public final class RedditExtractor {
JsonArray previewImages = new JsonParser().parse(preview.get("images").toString()).getAsJsonArray();
JsonObject source = new JsonParser().parse(previewImages.get(0).toString()).getAsJsonObject();
JsonObject urlSrc = new JsonParser().parse(source.get("source").toString()).getAsJsonObject();
url = urlSrc.get("url").toString().replace("amp;", "");
url = urlSrc.get("url").toString().replace("amp;", "").replace("\"", "");
} catch (NullPointerException n) {
url = childData.get("url").getAsString();
}

View File

@ -120,7 +120,7 @@ public abstract class RedditPost {
* @return if the URL is correct.
*/
public boolean hasMediaUrl() {
return !"".equals(url);
return url.contains(".jpg") || url.contains(".png");
}
/**