mirror of
https://github.com/13hannes11/xkcd_poster_creator.git
synced 2024-09-03 21:50:58 +02:00
added the xkcd downloader project to the repo and reorderd repo directories
This commit is contained in:
11
xkcd_downloader/.settings/org.eclipse.jdt.core.prefs
Normal file
11
xkcd_downloader/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.kuchelmeister.xkcd.downloader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class ImageDownloader {
|
||||
|
||||
List<String> downloadedImages;
|
||||
|
||||
public ImageDownloader() {
|
||||
downloadedImages = new LinkedList<>();
|
||||
}
|
||||
|
||||
private void downloadImage(final String url, final String folderPath) throws IOException {
|
||||
final InputStream in = new URL(url).openStream();
|
||||
final String fileName = url.substring(url.lastIndexOf("/")).replaceAll("[^A-Za-z0-9.]", "");
|
||||
|
||||
downloadedImages.add(folderPath + fileName);
|
||||
|
||||
Files.copy(in, Paths.get(folderPath + fileName));
|
||||
|
||||
System.out.println("Saved: " + folderPath + fileName);
|
||||
}
|
||||
|
||||
public void downloadAllImages(final List<String> images, final String folderPath) {
|
||||
for (final String img : images) {
|
||||
try {
|
||||
this.downloadImage(img, folderPath);
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package org.kuchelmeister.xkcd.downloader;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class ImageFinder {
|
||||
public static final String NUM_REPLACE = "<number>";
|
||||
public static final String XKCD_URL = "https://xkcd.com/" + NUM_REPLACE + "/info.0.json";
|
||||
|
||||
/**
|
||||
* Number of tries until fetching is aborted
|
||||
*/
|
||||
public static final int ABORT_THRESHOLD = 10;
|
||||
|
||||
private final List<String> imageURLs;
|
||||
|
||||
public ImageFinder() {
|
||||
this.imageURLs = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void findImages() {
|
||||
int err_counter = 0;
|
||||
int counter = 1;
|
||||
while (err_counter < ABORT_THRESHOLD) {
|
||||
final String url = XKCD_URL.replace(NUM_REPLACE, Integer.toString(counter++));
|
||||
|
||||
try {
|
||||
final JSONObject jsonObj = readJsonFromUrl(url);
|
||||
|
||||
this.getImageURLs().add(jsonObj.get("img").toString());
|
||||
System.out.println(jsonObj.get("img").toString());
|
||||
|
||||
err_counter = 0;
|
||||
} catch (JSONException | IOException e) {
|
||||
err_counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONObject readJsonFromUrl(final String url) throws IOException, JSONException {
|
||||
final InputStream is = new URL(url).openStream();
|
||||
try {
|
||||
final BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
|
||||
final String jsonText = readAll(rd);
|
||||
final JSONObject json = new JSONObject(jsonText);
|
||||
return json;
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static String readAll(final Reader rd) throws IOException {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
int cp;
|
||||
while ((cp = rd.read()) != -1) {
|
||||
sb.append((char) cp);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the imageURLs
|
||||
*/
|
||||
public List<String> getImageURLs() {
|
||||
return imageURLs;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.kuchelmeister.xkcd.downloader;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(final String[] args) {
|
||||
final ImageFinder finder = new ImageFinder();
|
||||
final ImageDownloader downloader = new ImageDownloader();
|
||||
|
||||
finder.findImages();
|
||||
downloader.downloadAllImages(finder.getImageURLs(), "C:/Users/Hannes/Pictures/xkcd/");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.kuchelmeister.xkcd.downloader;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ ImageDonwloaderTest.class })
|
||||
public class AllTests {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.kuchelmeister.xkcd.downloader;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ImageDonwloaderTest {
|
||||
@Test
|
||||
public void noFolderExistsDownloadTest() {
|
||||
final String url = "https://imgs.xkcd.com/comics/barrel_cropped_(1).jpg";
|
||||
final String folderDIR = "testing";
|
||||
final ImageDownloader downloader = new ImageDownloader();
|
||||
|
||||
final List<String> urls = new LinkedList<>();
|
||||
|
||||
urls.add(url);
|
||||
|
||||
final File f = new File(folderDIR);
|
||||
if (f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
downloader.downloadAllImages(urls, folderDIR);
|
||||
|
||||
org.junit.Assert.assertEquals(true, f.exists());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user