added the xkcd downloader project to the repo and reorderd repo directories

This commit is contained in:
Hannes
2017-11-10 00:33:27 +01:00
parent 0cbc960c2a
commit f123be3c5d
26 changed files with 1189 additions and 0 deletions

View 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

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}

View File

@@ -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/");
}
}

View File

@@ -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 {
}

View File

@@ -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());
}
}