added image listener

whenever a new image is downloaded its sent to the imagelistener defined
master
Marcus Gosselin 2014-11-16 09:05:02 -08:00
parent a2a9cccce9
commit cc1ccc63bb
2 changed files with 105 additions and 62 deletions

View File

@ -0,0 +1,6 @@
import java.awt.image.BufferedImage;
public interface ImageListener {
public abstract void newImage(BufferedImage image);
}

View File

@ -8,6 +8,8 @@ import java.net.URL;
import java.sql.Connection;
import java.util.Iterator;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -15,25 +17,26 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* now you need to have an instance.
* because only one request at a time.
* new request? new instance.
* later, TODO, make there be a busy
* variable or something.
* now you need to have an instance. because only one request at a time. new
* request? new instance. later, TODO, make there be a busy variable or
* something.
*
* @author Marcus
*
*/
public class ImgurRequest {
// 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
private String CLIENT_ID = "76535d44f1f94da";
// to track progress
// 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
private boolean busy = false;
@ -44,24 +47,40 @@ public class ImgurRequest {
// title so this can have a label
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.
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;
scannedAllPages = false;
busy = true;
predictedTotal = pages * 60;
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}
for (int page = 0; page < pages; page++) {
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.addRequestProperty("Authorization", "client-id " + CLIENT_ID);
connection.addRequestProperty("Authorization",
"client-id " + CLIENT_ID);
connection.connect();
if (connection.getResponseCode() == 200) {
@ -70,20 +89,21 @@ public class ImgurRequest {
saveImages(response);
} else {
title = "error code " + connection.getResponseCode();
title = "error code "
+ connection.getResponseCode();
busy = false;
}
scannedAllPages = true;
} catch (Exception e) {
e.printStackTrace();
busy = false;
}
pagesScanned++;
}
}}).start();
scannedAllPages = true;
}
}).start();
}
@ -156,7 +176,8 @@ public class ImgurRequest {
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 {
// make sure our directories exist no matter what
@ -165,10 +186,22 @@ public class ImgurRequest {
// if we haven't fully saved this yet...
if (!(new File(baseDir + subfolder + hash + extension).length() == filesize)) {
InputStream in = new URL("http://i.imgur.com/" + hash + extension).openConnection().getInputStream();
OutputStream out = new FileOutputStream(baseDir + subfolder + hash + extension);
InputStream in = new URL("http://i.imgur.com/" + hash
+ extension).openConnection().getInputStream();
OutputStream out = new FileOutputStream(baseDir + subfolder
+ hash + extension);
copy(in, out);
}
if (listener != null) {
try {
listener.newImage(ImageIO.read(new File(baseDir + subfolder
+ hash + extension)));
} catch (Exception e) {
// eh
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(hash);
@ -203,9 +236,13 @@ public class ImgurRequest {
}
}
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() {