2014-06-22 02:08:42 +02:00
|
|
|
package com.rarchives.ripme.ripper;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
import org.json.JSONObject;
|
2014-06-22 02:08:42 +02:00
|
|
|
|
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
|
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2014-06-23 04:17:40 +02:00
|
|
|
/**
|
|
|
|
* Simplified ripper, designed for ripping from sites by parsing JSON.
|
|
|
|
*/
|
2014-06-23 04:12:29 +02:00
|
|
|
public abstract class AbstractJSONRipper extends AlbumRipper {
|
2014-06-22 02:08:42 +02:00
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
protected AbstractJSONRipper(URL url) throws IOException {
|
2014-06-22 02:08:42 +02:00
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
protected abstract String getDomain();
|
2017-12-28 06:04:23 +01:00
|
|
|
@Override
|
2014-06-22 02:08:42 +02:00
|
|
|
public abstract String getHost();
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
protected abstract JSONObject getFirstPage() throws IOException;
|
|
|
|
protected JSONObject getNextPage(JSONObject doc) throws IOException {
|
2014-06-25 04:05:54 +02:00
|
|
|
throw new IOException("getNextPage not implemented");
|
|
|
|
}
|
2017-10-24 16:33:28 +02:00
|
|
|
protected abstract List<String> getURLsFromJSON(JSONObject json);
|
|
|
|
protected abstract void downloadURL(URL url, int index);
|
|
|
|
private DownloadThreadPool getThreadPool() {
|
2014-06-23 04:12:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
protected boolean keepSortOrder() {
|
2014-06-22 02:08:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canRip(URL url) {
|
|
|
|
return url.getHost().endsWith(getDomain());
|
|
|
|
}
|
2017-06-19 19:32:57 +02:00
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
@Override
|
|
|
|
public URL sanitizeURL(URL url) throws MalformedURLException {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void rip() throws IOException {
|
|
|
|
int index = 0;
|
|
|
|
logger.info("Retrieving " + this.url);
|
|
|
|
sendUpdate(STATUS.LOADING_RESOURCE, this.url.toExternalForm());
|
2014-06-23 04:12:29 +02:00
|
|
|
JSONObject json = getFirstPage();
|
2014-06-22 02:08:42 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
while (json != null) {
|
|
|
|
List<String> imageURLs = getURLsFromJSON(json);
|
2015-02-10 08:29:29 +01:00
|
|
|
// Remove all but 1 image
|
|
|
|
if (isThisATest()) {
|
|
|
|
while (imageURLs.size() > 1) {
|
|
|
|
imageURLs.remove(1);
|
|
|
|
}
|
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
|
|
|
|
if (imageURLs.size() == 0) {
|
|
|
|
throw new IOException("No images found at " + this.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String imageURL : imageURLs) {
|
|
|
|
if (isStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
2018-05-24 16:29:47 +02:00
|
|
|
if (alreadyDownloadedUrls >= Utils.getConfigInteger("history.end_rip_after_already_seen", 1000000000) && !isThisATest()) {
|
|
|
|
sendUpdate(STATUS.DOWNLOAD_COMPLETE, "Already seen the last " + alreadyDownloadedUrls + " images ending rip");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
index += 1;
|
2015-02-10 08:29:29 +01:00
|
|
|
logger.debug("Found image url #" + index+ ": " + imageURL);
|
2014-06-22 02:08:42 +02:00
|
|
|
downloadURL(new URL(imageURL), index);
|
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
|
2015-02-10 08:29:29 +01:00
|
|
|
if (isStopped() || isThisATest()) {
|
2014-06-23 04:12:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
try {
|
2014-06-23 04:12:29 +02:00
|
|
|
sendUpdate(STATUS.LOADING_RESOURCE, "next page");
|
|
|
|
json = getNextPage(json);
|
2014-06-22 02:08:42 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
logger.info("Can't get next page: " + e.getMessage());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
|
|
|
|
// If they're using a thread pool, wait for it.
|
|
|
|
if (getThreadPool() != null) {
|
2015-02-10 08:29:29 +01:00
|
|
|
logger.debug("Waiting for threadpool " + getThreadPool().getClass().getName());
|
2014-06-23 04:12:29 +02:00
|
|
|
getThreadPool().waitForThreads();
|
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
waitForThreads();
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
protected String getPrefix(int index) {
|
2014-06-22 02:08:42 +02:00
|
|
|
String prefix = "";
|
|
|
|
if (keepSortOrder() && Utils.getConfigBoolean("download.save_order", true)) {
|
|
|
|
prefix = String.format("%03d_", index);
|
|
|
|
}
|
|
|
|
return prefix;
|
|
|
|
}
|
2018-05-24 16:29:47 +02:00
|
|
|
}
|