2014-06-13 07:44:59 +02:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2014-06-25 04:05:54 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-06-13 07:44:59 +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-25 04:05:54 +02:00
|
|
|
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
|
2014-06-13 07:44:59 +02:00
|
|
|
import com.rarchives.ripme.ripper.DownloadThreadPool;
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2014-06-13 07:44:59 +02:00
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2014-06-25 04:05:54 +02:00
|
|
|
public class ImagebamRipper extends AbstractHTMLRipper {
|
2014-06-13 07:44:59 +02:00
|
|
|
|
2014-06-25 04:05:54 +02:00
|
|
|
// Current HTML document
|
|
|
|
private Document albumDoc = null;
|
2014-06-13 07:44:59 +02:00
|
|
|
|
|
|
|
// Thread pool for finding direct image links from "image" pages (html)
|
|
|
|
private DownloadThreadPool imagebamThreadPool = new DownloadThreadPool("imagebam");
|
2014-06-25 04:05:54 +02:00
|
|
|
@Override
|
|
|
|
public DownloadThreadPool getThreadPool() {
|
|
|
|
return imagebamThreadPool;
|
|
|
|
}
|
2014-06-13 07:44:59 +02:00
|
|
|
|
|
|
|
public ImagebamRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getHost() {
|
2014-06-25 04:05:54 +02:00
|
|
|
return "imagebam";
|
2014-06-13 07:44:59 +02:00
|
|
|
}
|
2014-06-25 04:05:54 +02:00
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
|
|
|
return "imagebam.com";
|
2014-06-13 07:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Pattern p;
|
|
|
|
Matcher m;
|
|
|
|
|
|
|
|
p = Pattern.compile("^https?://[wm.]*imagebam.com/gallery/([a-zA-Z0-9]+).*$");
|
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new MalformedURLException(
|
|
|
|
"Expected imagebam gallery format: "
|
|
|
|
+ "http://www.imagebam.com/gallery/galleryid"
|
|
|
|
+ " Got: " + url);
|
|
|
|
}
|
2014-06-25 04:05:54 +02:00
|
|
|
|
2014-06-13 07:44:59 +02:00
|
|
|
@Override
|
2014-06-25 04:05:54 +02:00
|
|
|
public Document getFirstPage() throws IOException {
|
|
|
|
if (albumDoc == null) {
|
|
|
|
albumDoc = Http.url(url).get();
|
2014-06-13 07:44:59 +02:00
|
|
|
}
|
2014-06-25 04:05:54 +02:00
|
|
|
return albumDoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Document getNextPage(Document doc) throws IOException {
|
|
|
|
// Find next page
|
|
|
|
Elements hrefs = doc.select("a.pagination_current + a.pagination_link");
|
|
|
|
if (hrefs.size() == 0) {
|
|
|
|
throw new IOException("No more pages");
|
|
|
|
}
|
|
|
|
String nextUrl = "http://www.imagebam.com" + hrefs.first().attr("href");
|
|
|
|
sleep(500);
|
|
|
|
return Http.url(nextUrl).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> getURLsFromPage(Document doc) {
|
|
|
|
List<String> imageURLs = new ArrayList<String>();
|
|
|
|
for (Element thumb : doc.select("div > a[target=_blank]:not(.footera)")) {
|
|
|
|
imageURLs.add(thumb.attr("href"));
|
|
|
|
}
|
|
|
|
return imageURLs;
|
|
|
|
}
|
2014-06-13 07:44:59 +02:00
|
|
|
|
2014-06-25 04:05:54 +02:00
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
ImagebamImageThread t = new ImagebamImageThread(url, index);
|
|
|
|
imagebamThreadPool.addThread(t);
|
|
|
|
sleep(500);
|
2014-06-13 07:44:59 +02:00
|
|
|
}
|
|
|
|
|
2014-06-25 04:05:54 +02:00
|
|
|
@Override
|
|
|
|
public String getAlbumTitle(URL url) throws MalformedURLException {
|
|
|
|
try {
|
|
|
|
// Attempt to use album title as GID
|
|
|
|
Elements elems = getFirstPage().select("legend");
|
|
|
|
String title = elems.first().text();
|
|
|
|
logger.info("Title text: '" + title + "'");
|
|
|
|
Pattern p = Pattern.compile("^(.*)\\s\\d* image.*$");
|
|
|
|
Matcher m = p.matcher(title);
|
|
|
|
if (m.matches()) {
|
|
|
|
return getHost() + "_" + getGID(url) + " (" + m.group(1).trim() + ")";
|
|
|
|
}
|
|
|
|
logger.info("Doesn't match " + p.pattern());
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Fall back to default album naming convention
|
|
|
|
logger.warn("Failed to get album title from " + url, e);
|
|
|
|
}
|
|
|
|
return super.getAlbumTitle(url);
|
2014-06-13 07:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class to find and download images found on "image" pages
|
|
|
|
*
|
|
|
|
* Handles case when site has IP-banned the user.
|
|
|
|
*/
|
|
|
|
private class ImagebamImageThread extends Thread {
|
|
|
|
private URL url;
|
|
|
|
private int index;
|
|
|
|
|
|
|
|
public ImagebamImageThread(URL url, int index) {
|
|
|
|
super();
|
|
|
|
this.url = url;
|
|
|
|
this.index = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
fetchImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fetchImage() {
|
|
|
|
try {
|
2014-06-22 02:08:42 +02:00
|
|
|
Document doc = Http.url(url).get();
|
2014-06-13 07:44:59 +02:00
|
|
|
// Find image
|
|
|
|
Elements images = doc.select("td > img");
|
|
|
|
if (images.size() == 0) {
|
|
|
|
logger.warn("Image not found at " + this.url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Element image = images.first();
|
|
|
|
String imgsrc = image.attr("src");
|
2014-06-20 13:09:36 +02:00
|
|
|
logger.info("Found URL " + imgsrc);
|
2014-06-13 07:44:59 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|