2014-05-08 20:31:29 +02:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
2014-05-14 07:40:47 +02:00
|
|
|
import java.io.File;
|
2014-05-08 20:31:29 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2014-06-23 04:12:29 +02:00
|
|
|
import java.util.ArrayList;
|
2014-06-20 13:09:36 +02:00
|
|
|
import java.util.HashMap;
|
2014-06-23 04:12:29 +02:00
|
|
|
import java.util.List;
|
2014-06-20 13:09:36 +02:00
|
|
|
import java.util.Map;
|
2014-05-08 20:31:29 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
|
2014-06-01 08:53:35 +02:00
|
|
|
import com.rarchives.ripme.ripper.DownloadThreadPool;
|
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2014-05-26 09:31:58 +02:00
|
|
|
import com.rarchives.ripme.utils.Utils;
|
2014-05-08 20:31:29 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
public class EHentaiRipper extends AbstractHTMLRipper {
|
2014-06-01 08:53:35 +02:00
|
|
|
// All sleep times are in milliseconds
|
2014-06-23 04:12:29 +02:00
|
|
|
private static final int PAGE_SLEEP_TIME = 3000;
|
|
|
|
private static final int IMAGE_SLEEP_TIME = 1500;
|
|
|
|
private static final int IP_BLOCK_SLEEP_TIME = 60 * 1000;
|
2014-06-01 08:53:35 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
private String lastURL = null;
|
2014-05-08 20:31:29 +02:00
|
|
|
|
2014-06-01 08:53:35 +02:00
|
|
|
// Thread pool for finding direct image links from "image" pages (html)
|
|
|
|
private DownloadThreadPool ehentaiThreadPool = new DownloadThreadPool("ehentai");
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public DownloadThreadPool getThreadPool() {
|
|
|
|
return ehentaiThreadPool;
|
|
|
|
}
|
2014-06-01 08:53:35 +02:00
|
|
|
|
|
|
|
// Current HTML document
|
2014-05-08 20:31:29 +02:00
|
|
|
private Document albumDoc = null;
|
2017-02-22 07:24:52 +01:00
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
private static final Map<String,String> cookies = new HashMap<>();
|
2014-06-20 13:09:36 +02:00
|
|
|
static {
|
|
|
|
cookies.put("nw", "1");
|
|
|
|
cookies.put("tip", "1");
|
|
|
|
}
|
2014-05-08 20:31:29 +02:00
|
|
|
|
|
|
|
public EHentaiRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getHost() {
|
2014-06-23 04:12:29 +02:00
|
|
|
return "e-hentai";
|
2014-05-08 20:31:29 +02:00
|
|
|
}
|
2017-02-22 07:24:52 +01:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
2017-02-22 07:24:52 +01:00
|
|
|
return "e-hentai.org";
|
2014-05-08 20:31:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getAlbumTitle(URL url) throws MalformedURLException {
|
|
|
|
try {
|
|
|
|
// Attempt to use album title as GID
|
|
|
|
if (albumDoc == null) {
|
2014-06-23 04:12:29 +02:00
|
|
|
albumDoc = getPageWithRetries(url);
|
2014-05-08 20:31:29 +02:00
|
|
|
}
|
|
|
|
Elements elems = albumDoc.select("#gn");
|
2014-06-23 04:12:29 +02:00
|
|
|
return getHost() + "_" + elems.first().text();
|
2014-05-08 20:31:29 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
// Fall back to default album naming convention
|
2014-06-01 08:53:35 +02:00
|
|
|
logger.warn("Failed to get album title from " + url, e);
|
2014-05-08 20:31:29 +02:00
|
|
|
}
|
|
|
|
return super.getAlbumTitle(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Pattern p;
|
|
|
|
Matcher m;
|
|
|
|
|
2017-02-22 07:24:52 +01:00
|
|
|
p = Pattern.compile("^https?://e-hentai\\.org/g/([0-9]+)/([a-fA-F0-9]+)/$");
|
2014-05-08 20:31:29 +02:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
2014-05-14 07:56:02 +02:00
|
|
|
return m.group(1) + "-" + m.group(2);
|
2014-05-08 20:31:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new MalformedURLException(
|
2017-02-22 07:24:52 +01:00
|
|
|
"Expected e-hentai.org gallery format: "
|
|
|
|
+ "http://e-hentai.org/g/####/####/"
|
2014-05-08 20:31:29 +02:00
|
|
|
+ " Got: " + url);
|
|
|
|
}
|
2017-02-22 07:24:52 +01:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
/**
|
|
|
|
* Attempts to get page, checks for IP ban, waits.
|
|
|
|
* @param url
|
|
|
|
* @return Page document
|
|
|
|
* @throws IOException If page loading errors, or if retries are exhausted
|
|
|
|
*/
|
|
|
|
private Document getPageWithRetries(URL url) throws IOException {
|
|
|
|
Document doc;
|
|
|
|
int retries = 3;
|
2014-06-01 08:53:35 +02:00
|
|
|
while (true) {
|
2014-06-23 04:12:29 +02:00
|
|
|
sendUpdate(STATUS.LOADING_RESOURCE, url.toExternalForm());
|
|
|
|
logger.info("Retrieving " + url);
|
|
|
|
doc = Http.url(url)
|
|
|
|
.referrer(this.url)
|
|
|
|
.cookies(cookies)
|
|
|
|
.get();
|
|
|
|
if (doc.toString().contains("IP address will be automatically banned")) {
|
2014-06-01 08:53:35 +02:00
|
|
|
if (retries == 0) {
|
2014-06-23 04:12:29 +02:00
|
|
|
throw new IOException("Hit rate limit and maximum number of retries, giving up");
|
2014-06-01 08:53:35 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
logger.warn("Hit rate limit while loading " + url + ", sleeping for " + IP_BLOCK_SLEEP_TIME + "ms, " + retries + " retries remaining");
|
2014-06-01 08:53:35 +02:00
|
|
|
retries--;
|
|
|
|
try {
|
|
|
|
Thread.sleep(IP_BLOCK_SLEEP_TIME);
|
|
|
|
} catch (InterruptedException e) {
|
2014-06-23 04:12:29 +02:00
|
|
|
throw new IOException("Interrupted while waiting for rate limit to subside");
|
2014-06-07 10:05:30 +02:00
|
|
|
}
|
2014-05-26 11:13:56 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
else {
|
|
|
|
return doc;
|
2014-06-01 08:53:35 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-01 08:53:35 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public Document getFirstPage() throws IOException {
|
|
|
|
if (albumDoc == null) {
|
|
|
|
albumDoc = getPageWithRetries(this.url);
|
|
|
|
}
|
|
|
|
this.lastURL = this.url.toExternalForm();
|
|
|
|
return albumDoc;
|
|
|
|
}
|
2014-05-08 20:31:29 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public Document getNextPage(Document doc) throws IOException {
|
|
|
|
// Check if we've stopped
|
|
|
|
if (isStopped()) {
|
|
|
|
throw new IOException("Ripping interrupted");
|
|
|
|
}
|
|
|
|
// Find next page
|
|
|
|
Elements hrefs = doc.select(".ptt a");
|
|
|
|
if (hrefs.size() == 0) {
|
|
|
|
logger.info("doc: " + doc.html());
|
|
|
|
throw new IOException("No navigation links found");
|
2014-05-08 20:31:29 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
// Ensure next page is different from the current page
|
|
|
|
String nextURL = hrefs.last().attr("href");
|
|
|
|
if (nextURL.equals(this.lastURL)) {
|
|
|
|
logger.info("lastURL = nextURL : " + nextURL);
|
|
|
|
throw new IOException("Reached last page of results");
|
|
|
|
}
|
|
|
|
// Sleep before loading next page
|
|
|
|
sleep(PAGE_SLEEP_TIME);
|
|
|
|
// Load next page
|
|
|
|
Document nextPage = getPageWithRetries(new URL(nextURL));
|
|
|
|
this.lastURL = nextURL;
|
|
|
|
return nextPage;
|
|
|
|
}
|
2014-05-08 20:31:29 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public List<String> getURLsFromPage(Document page) {
|
2017-10-24 16:33:28 +02:00
|
|
|
List<String> imageURLs = new ArrayList<>();
|
2014-06-23 04:12:29 +02:00
|
|
|
Elements thumbs = page.select("#gdt > .gdtm a");
|
|
|
|
// Iterate over images on page
|
|
|
|
for (Element thumb : thumbs) {
|
|
|
|
imageURLs.add(thumb.attr("href"));
|
|
|
|
}
|
|
|
|
return imageURLs;
|
2014-05-08 20:31:29 +02:00
|
|
|
}
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
EHentaiImageThread t = new EHentaiImageThread(url, index, this.workingDir);
|
|
|
|
ehentaiThreadPool.addThread(t);
|
|
|
|
try {
|
|
|
|
Thread.sleep(IMAGE_SLEEP_TIME);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
logger.warn("Interrupted while waiting to load next image", e);
|
|
|
|
}
|
2014-05-08 20:31:29 +02:00
|
|
|
}
|
2014-06-01 08:53:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class to find and download images found on "image" pages
|
2017-02-22 07:24:52 +01:00
|
|
|
*
|
2014-06-01 08:53:35 +02:00
|
|
|
* Handles case when site has IP-banned the user.
|
|
|
|
*/
|
|
|
|
private class EHentaiImageThread extends Thread {
|
|
|
|
private URL url;
|
|
|
|
private int index;
|
|
|
|
private File workingDir;
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
EHentaiImageThread(URL url, int index, File workingDir) {
|
2014-06-01 08:53:35 +02:00
|
|
|
super();
|
|
|
|
this.url = url;
|
|
|
|
this.index = index;
|
|
|
|
this.workingDir = workingDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
fetchImage();
|
|
|
|
}
|
2017-02-22 07:24:52 +01:00
|
|
|
|
2014-06-01 08:53:35 +02:00
|
|
|
private void fetchImage() {
|
|
|
|
try {
|
2014-06-23 04:12:29 +02:00
|
|
|
Document doc = getPageWithRetries(this.url);
|
2014-06-01 08:53:35 +02:00
|
|
|
|
|
|
|
// Find image
|
|
|
|
Elements images = doc.select(".sni > a > img");
|
|
|
|
if (images.size() == 0) {
|
|
|
|
// Attempt to find image elsewise (Issue #41)
|
|
|
|
images = doc.select("img#img");
|
|
|
|
if (images.size() == 0) {
|
|
|
|
logger.warn("Image not found at " + this.url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Element image = images.first();
|
|
|
|
String imgsrc = image.attr("src");
|
|
|
|
logger.info("Found URL " + imgsrc + " via " + images.get(0));
|
|
|
|
Pattern p = Pattern.compile("^http://.*/ehg/image.php.*&n=([^&]+).*$");
|
|
|
|
Matcher m = p.matcher(imgsrc);
|
|
|
|
if (m.matches()) {
|
|
|
|
// Manually discover filename from URL
|
|
|
|
String savePath = this.workingDir + File.separator;
|
|
|
|
if (Utils.getConfigBoolean("download.save_order", true)) {
|
|
|
|
savePath += String.format("%03d_", index);
|
|
|
|
}
|
|
|
|
savePath += m.group(1);
|
|
|
|
addURLToDownload(new URL(imgsrc), new File(savePath));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Provide prefix and let the AbstractRipper "guess" the filename
|
|
|
|
String prefix = "";
|
|
|
|
if (Utils.getConfigBoolean("download.save_order", true)) {
|
|
|
|
prefix = String.format("%03d_", index);
|
|
|
|
}
|
|
|
|
addURLToDownload(new URL(imgsrc), prefix);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error("[!] Exception while loading/parsing " + this.url, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 07:24:52 +01:00
|
|
|
}
|