parent
cc1ccc63bb
commit
50213fc661
|
|
@ -7,10 +7,12 @@ import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.io.output.TeeOutputStream;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
|
@ -86,7 +88,7 @@ public class ImgurRequest {
|
||||||
if (connection.getResponseCode() == 200) {
|
if (connection.getResponseCode() == 200) {
|
||||||
|
|
||||||
InputStream response = connection.getInputStream();
|
InputStream response = connection.getInputStream();
|
||||||
saveImages(response);
|
saveArray(response);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
title = "error code "
|
title = "error code "
|
||||||
|
|
@ -107,7 +109,58 @@ public class ImgurRequest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveImages(final InputStream response) {
|
// sort is usually time but i thought i'd be nice to yall.
|
||||||
|
public void saveSubmitted(final String user, final int pages) {
|
||||||
|
totalImages = imagesComplete = 0;
|
||||||
|
scannedAllPages = false;
|
||||||
|
busy = true;
|
||||||
|
predictedTotal = pages * 60;
|
||||||
|
title = user;
|
||||||
|
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/account/" + user
|
||||||
|
+ "/submissions/" + page + ".json";
|
||||||
|
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) ((new URL(
|
||||||
|
path)).openConnection());
|
||||||
|
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.addRequestProperty("Authorization",
|
||||||
|
"client-id " + CLIENT_ID);
|
||||||
|
connection.connect();
|
||||||
|
|
||||||
|
if (connection.getResponseCode() == 200) {
|
||||||
|
|
||||||
|
InputStream response = connection.getInputStream();
|
||||||
|
|
||||||
|
saveArray(response);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
title = "error code "
|
||||||
|
+ connection.getResponseCode();
|
||||||
|
busy = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
busy = false;
|
||||||
|
}
|
||||||
|
pagesScanned++;
|
||||||
|
}
|
||||||
|
|
||||||
|
scannedAllPages = true;
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveArray(final InputStream response) {
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
|
@ -134,8 +187,10 @@ public class ImgurRequest {
|
||||||
|
|
||||||
JsonNode item = imagesIterator.next();
|
JsonNode item = imagesIterator.next();
|
||||||
|
|
||||||
|
boolean album = item.get("is_album").asBoolean();
|
||||||
|
|
||||||
|
if (!album) {
|
||||||
String id = item.get("id").asText();
|
String id = item.get("id").asText();
|
||||||
// boolean album = item.get("is_album").asBoolean();
|
|
||||||
int fileSize = item.get("size").asInt();
|
int fileSize = item.get("size").asInt();
|
||||||
|
|
||||||
String extension = item.get("type").asText();
|
String extension = item.get("type").asText();
|
||||||
|
|
@ -144,6 +199,7 @@ public class ImgurRequest {
|
||||||
String subreddit = item.get("section").asText();
|
String subreddit = item.get("section").asText();
|
||||||
|
|
||||||
saveID(id, subreddit + "\\", extension, fileSize);
|
saveID(id, subreddit + "\\", extension, fileSize);
|
||||||
|
} else System.out.println("Theres an album here but we dont care yet");
|
||||||
|
|
||||||
imagesComplete++;
|
imagesComplete++;
|
||||||
|
|
||||||
|
|
@ -188,16 +244,23 @@ public class ImgurRequest {
|
||||||
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
|
InputStream in = new URL("http://i.imgur.com/" + hash
|
||||||
+ extension).openConnection().getInputStream();
|
+ extension).openConnection().getInputStream();
|
||||||
OutputStream out = new FileOutputStream(baseDir + subfolder
|
OutputStream fileOut = new FileOutputStream(baseDir + subfolder
|
||||||
+ hash + extension);
|
+ hash + extension);
|
||||||
copy(in, out);
|
|
||||||
|
copy(in, fileOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
listener.newImage(ImageIO.read(new File(baseDir + subfolder
|
// listener.newImage(ImageIO.read(new File(baseDir +
|
||||||
+ hash + extension)));
|
// subfolder
|
||||||
|
// + hash + extension)));
|
||||||
|
|
||||||
|
listener.newImage(ImageIO.read(new URL(
|
||||||
|
"http://i.imgur.com/" + hash + extension)
|
||||||
|
.openConnection().getInputStream()));
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// eh
|
// eh
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue