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

126 lines
4.0 KiB
Java
Raw Normal View History

2014-04-13 01:54:45 +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-04-13 01:54:45 +02:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
2014-06-23 04:17:40 +02:00
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
import com.rarchives.ripme.utils.Http;
2014-04-13 01:54:45 +02:00
2014-06-23 04:17:40 +02:00
public class ChanRipper extends AbstractHTMLRipper {
2014-04-13 01:54:45 +02:00
public ChanRipper(URL url) throws IOException {
super(url);
}
@Override
public String getHost() {
String host = this.url.getHost();
host = host.substring(0, host.lastIndexOf('.'));
if (host.contains(".")) {
// Host has subdomain (www)
host = host.substring(host.lastIndexOf('.') + 1);
}
String board = this.url.toExternalForm().split("/")[3];
return host + "_" + board;
}
@Override
public boolean canRip(URL url) {
// TODO Whitelist?
if (url.getHost().equals("anon-ib.com")) {
return true;
}
2014-04-25 04:42:10 +02:00
return url.getHost().contains("chan") &&
( url.toExternalForm().contains("/res/") // Most chans
|| url.toExternalForm().contains("/thread/")); // 4chan
2014-04-13 01:54:45 +02:00
}
@Override
public String getGID(URL url) throws MalformedURLException {
Pattern p; Matcher m;
2014-04-25 04:42:10 +02:00
String u = url.toExternalForm();
if (u.contains("/res/")) {
p = Pattern.compile("^.*(chan|anon-ib).*\\.[a-z]{2,3}/[a-zA-Z0-9/]+/res/([0-9]+)(\\.html|\\.php)?.*$");
2014-04-25 04:42:10 +02:00
m = p.matcher(u);
if (m.matches()) {
return m.group(2);
2014-04-25 04:42:10 +02:00
}
}
else if (u.contains("/thread/")) {
p = Pattern.compile("^.*chan.*\\.[a-z]{2,3}/[a-zA-Z0-9]+/thread/([0-9]+)(\\.html|\\.php)?.*$");
m = p.matcher(u);
if (m.matches()) {
return m.group(1);
}
2014-04-13 01:54:45 +02:00
}
throw new MalformedURLException(
"Expected *chan URL formats: "
+ "*chan.com/@/res/####.html"
2014-04-25 04:42:10 +02:00
+ " Got: " + u);
2014-04-13 01:54:45 +02:00
}
@Override
public String getDomain() {
return this.url.getHost();
}
@Override
public Document getFirstPage() throws IOException {
return Http.url(this.url).get();
}
@Override
public List<String> getURLsFromPage(Document page) {
List<String> imageURLs = new ArrayList<String>();
2014-04-13 01:54:45 +02:00
Pattern p; Matcher m;
for (Element link : page.select("a")) {
2014-04-13 01:54:45 +02:00
if (!link.hasAttr("href")) {
continue;
}
2014-04-25 04:42:10 +02:00
if (!link.attr("href").contains("/src/")
&& !link.attr("href").contains("4cdn.org")) {
2014-04-13 01:54:45 +02:00
logger.debug("Skipping link that does not contain /src/: " + link.attr("href"));
continue;
}
if (link.attr("href").contains("=http")
|| link.attr("href").contains("http://imgops.com/")) {
logger.debug("Skipping link that contains '=http' or 'imgops.com': " + link.attr("href"));
continue;
}
2014-05-11 18:52:00 +02:00
p = Pattern.compile("^.*\\.(jpg|jpeg|png|gif|webm)$", Pattern.CASE_INSENSITIVE);
2014-04-13 01:54:45 +02:00
m = p.matcher(link.attr("href"));
if (m.matches()) {
String image = link.attr("href");
if (image.startsWith("//")) {
image = "http:" + image;
}
if (image.startsWith("/")) {
image = "http://" + this.url.getHost() + image;
}
// Don't download the same URL twice
if (imageURLs.contains(image)) {
2014-04-13 01:54:45 +02:00
logger.debug("Already attempted: " + image);
continue;
}
imageURLs.add(image);
2014-04-13 01:54:45 +02:00
}
}
return imageURLs;
}
@Override
public void downloadURL(URL url, int index) {
addURLToDownload(url, getPrefix(index));
2014-04-13 01:54:45 +02:00
}
}