added image listener
whenever a new image is downloaded its sent to the imagelistener definedmaster
parent
a2a9cccce9
commit
cc1ccc63bb
|
|
@ -0,0 +1,6 @@
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
|
||||||
|
public interface ImageListener {
|
||||||
|
public abstract void newImage(BufferedImage image);
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,8 @@ import java.net.URL;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
|
@ -15,25 +17,26 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* now you need to have an instance.
|
* now you need to have an instance. because only one request at a time. new
|
||||||
* because only one request at a time.
|
* request? new instance. later, TODO, make there be a busy variable or
|
||||||
* new request? new instance.
|
* something.
|
||||||
* later, TODO, make there be a busy
|
*
|
||||||
* variable or something.
|
|
||||||
* @author Marcus
|
* @author Marcus
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ImgurRequest {
|
public class ImgurRequest {
|
||||||
|
|
||||||
// used if no path is pre specified.
|
// used if no path is pre specified.
|
||||||
private String baseDir = System.getenv("USERPROFILE") + "\\Desktop\\Imgur\\";
|
private String baseDir = System.getenv("USERPROFILE")
|
||||||
|
+ "\\Desktop\\Imgur\\";
|
||||||
|
|
||||||
// my personal API key so i can access imgur api and stuff
|
// my personal API key so i can access imgur api and stuff
|
||||||
private String CLIENT_ID = "76535d44f1f94da";
|
private String CLIENT_ID = "76535d44f1f94da";
|
||||||
|
|
||||||
// to track progress
|
// to track progress
|
||||||
// prediction because divide by zero errors are infectious
|
// prediction because divide by zero errors are infectious
|
||||||
private volatile int totalImages = 0, imagesComplete = 0, predictedTotal = 1;
|
private volatile int totalImages = 0, imagesComplete = 0,
|
||||||
|
predictedTotal = 1;
|
||||||
|
|
||||||
// busy? ornahhhh
|
// busy? ornahhhh
|
||||||
private boolean busy = false;
|
private boolean busy = false;
|
||||||
|
|
@ -44,24 +47,40 @@ public class ImgurRequest {
|
||||||
// title so this can have a label
|
// title so this can have a label
|
||||||
private String title = "";
|
private String title = "";
|
||||||
|
|
||||||
|
// how many pages scanned and to be scanned
|
||||||
|
private int pagesScanned = 0, pagesToScan = 0;
|
||||||
|
|
||||||
|
private ImageListener listener;
|
||||||
|
|
||||||
|
public ImgurRequest(ImageListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
// sort is usually time but i thought i'd be nice to yall.
|
// sort is usually time but i thought i'd be nice to yall.
|
||||||
public void saveSubreddit(final String subreddit, final int pages, final String sort) {
|
public void saveSubreddit(final String subreddit, final int pages,
|
||||||
|
final String sort) {
|
||||||
totalImages = imagesComplete = 0;
|
totalImages = imagesComplete = 0;
|
||||||
scannedAllPages = false;
|
scannedAllPages = false;
|
||||||
busy = true;
|
busy = true;
|
||||||
predictedTotal = pages * 60;
|
predictedTotal = pages * 60;
|
||||||
title = subreddit;
|
title = subreddit;
|
||||||
new Thread(new Runnable() {public void run() {
|
pagesToScan = pages;
|
||||||
|
pagesScanned = 0;
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
|
||||||
// https://api.imgur.com/3/gallery/r/{subreddit}/{sort}/{page}
|
// https://api.imgur.com/3/gallery/r/{subreddit}/{sort}/{page}
|
||||||
for (int page = 0; page < pages; page++) {
|
for (int page = 0; page < pages; page++) {
|
||||||
try {
|
try {
|
||||||
String path = "https://api.imgur.com/3/gallery/r/" + subreddit + "/" + sort + "/" + page + ".json";
|
String path = "https://api.imgur.com/3/gallery/r/"
|
||||||
|
+ subreddit + "/" + sort + "/" + page + ".json";
|
||||||
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) ((new URL(path)).openConnection());
|
HttpURLConnection connection = (HttpURLConnection) ((new URL(
|
||||||
|
path)).openConnection());
|
||||||
|
|
||||||
connection.setRequestMethod("GET");
|
connection.setRequestMethod("GET");
|
||||||
connection.addRequestProperty("Authorization", "client-id " + CLIENT_ID);
|
connection.addRequestProperty("Authorization",
|
||||||
|
"client-id " + CLIENT_ID);
|
||||||
connection.connect();
|
connection.connect();
|
||||||
|
|
||||||
if (connection.getResponseCode() == 200) {
|
if (connection.getResponseCode() == 200) {
|
||||||
|
|
@ -70,20 +89,21 @@ public class ImgurRequest {
|
||||||
saveImages(response);
|
saveImages(response);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
title = "error code " + connection.getResponseCode();
|
title = "error code "
|
||||||
|
+ connection.getResponseCode();
|
||||||
busy = false;
|
busy = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
scannedAllPages = true;
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
busy = false;
|
busy = false;
|
||||||
}
|
}
|
||||||
|
pagesScanned++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}}).start();
|
scannedAllPages = true;
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -156,7 +176,8 @@ public class ImgurRequest {
|
||||||
return ".jpg";
|
return ".jpg";
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveID(String hash, String subfolder, String extension, int filesize) {
|
public void saveID(String hash, String subfolder, String extension,
|
||||||
|
int filesize) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// make sure our directories exist no matter what
|
// make sure our directories exist no matter what
|
||||||
|
|
@ -165,10 +186,22 @@ public class ImgurRequest {
|
||||||
|
|
||||||
// if we haven't fully saved this yet...
|
// if we haven't fully saved this yet...
|
||||||
if (!(new File(baseDir + subfolder + hash + extension).length() == filesize)) {
|
if (!(new File(baseDir + subfolder + hash + extension).length() == filesize)) {
|
||||||
InputStream in = new URL("http://i.imgur.com/" + hash + extension).openConnection().getInputStream();
|
InputStream in = new URL("http://i.imgur.com/" + hash
|
||||||
OutputStream out = new FileOutputStream(baseDir + subfolder + hash + extension);
|
+ extension).openConnection().getInputStream();
|
||||||
|
OutputStream out = new FileOutputStream(baseDir + subfolder
|
||||||
|
+ hash + extension);
|
||||||
copy(in, out);
|
copy(in, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (listener != null) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
listener.newImage(ImageIO.read(new File(baseDir + subfolder
|
||||||
|
+ hash + extension)));
|
||||||
|
} catch (Exception e) {
|
||||||
|
// eh
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.out.println(hash);
|
System.out.println(hash);
|
||||||
|
|
@ -203,9 +236,13 @@ public class ImgurRequest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public double getProgress() {
|
public double getProgress() {
|
||||||
return scannedAllPages ? (double)imagesComplete/(double)totalImages : (double)imagesComplete/(double)predictedTotal;
|
return scannedAllPages ? (double) imagesComplete / (double) totalImages
|
||||||
|
: (double) imagesComplete / (double) predictedTotal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getScanProgress() {
|
||||||
|
return (double) (pagesScanned) / pagesToScan;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getImagesDiscovered() {
|
public int getImagesDiscovered() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue