ImgScroll/src/main/java/com/rarchives/ripme/ripper/rippers/ImagevenueRipper.java

122 lines
3.7 KiB
Java
Raw Normal View History

2014-06-17 10:39:32 +02:00
package com.rarchives.ripme.ripper.rippers;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
2014-06-17 10:39:32 +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;
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
2014-06-17 10:39:32 +02:00
import com.rarchives.ripme.ripper.DownloadThreadPool;
import com.rarchives.ripme.utils.Http;
2014-06-17 10:39:32 +02:00
import com.rarchives.ripme.utils.Utils;
public class ImagevenueRipper extends AbstractHTMLRipper {
2014-06-17 10:39:32 +02:00
// Thread pool for finding direct image links from "image" pages (html)
private DownloadThreadPool imagevenueThreadPool = new DownloadThreadPool("imagevenue");
@Override
public DownloadThreadPool getThreadPool() {
return imagevenueThreadPool;
}
2014-06-17 10:39:32 +02:00
public ImagevenueRipper(URL url) throws IOException {
super(url);
}
@Override
public String getHost() {
return "imagevenue";
2014-06-17 10:39:32 +02:00
}
@Override
public String getDomain() {
return "imagevenue.com";
2014-06-17 10:39:32 +02:00
}
@Override
public String getGID(URL url) throws MalformedURLException {
Pattern p;
Matcher m;
p = Pattern.compile("^https?://.*imagevenue.com/galshow.php\\?gal=([a-zA-Z0-9\\-_]+).*$");
m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
throw new MalformedURLException(
"Expected imagevenue gallery format: "
+ "http://...imagevenue.com/galshow.php?gal=gallery_...."
+ " Got: " + url);
}
2014-06-17 10:39:32 +02:00
@Override
public Document getFirstPage() throws IOException {
return Http.url(url).get();
}
public List<String> getURLsFromPage(Document doc) {
List<String> imageURLs = new ArrayList<String>();
for (Element thumb : doc.select("a[target=_blank]")) {
imageURLs.add(thumb.attr("href"));
2014-06-17 10:39:32 +02:00
}
return imageURLs;
2014-06-17 10:39:32 +02:00
}
public void downloadURL(URL url, int index) {
ImagevenueImageThread t = new ImagevenueImageThread(url, index);
imagevenueThreadPool.addThread(t);
2014-06-17 10:39:32 +02:00
}
/**
* Helper class to find and download images found on "image" pages
*
2014-06-17 10:39:32 +02:00
* Handles case when site has IP-banned the user.
*/
private class ImagevenueImageThread extends Thread {
private URL url;
private int index;
public ImagevenueImageThread(URL url, int index) {
super();
this.url = url;
this.index = index;
}
@Override
public void run() {
fetchImage();
}
2014-06-17 10:39:32 +02:00
private void fetchImage() {
try {
Document doc = Http.url(url)
.retries(3)
.get();
2014-06-17 10:39:32 +02:00
// Find image
Elements images = doc.select("a > img");
if (images.size() == 0) {
logger.warn("Image not found at " + this.url);
return;
}
Element image = images.first();
String imgsrc = image.attr("src");
imgsrc = "http://" + this.url.getHost() + "/" + imgsrc;
// 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);
}
}
}
}