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;
|
2014-06-22 02:08:42 +02:00
|
|
|
import java.util.ArrayList;
|
2015-01-11 00:39:18 +01:00
|
|
|
import java.util.Arrays;
|
2014-06-22 02:08:42 +02:00
|
|
|
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;
|
2014-09-08 00:36:08 +02:00
|
|
|
import com.rarchives.ripme.ripper.rippers.ripperhelpers.ChanSite;
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2017-05-09 23:02:24 +02:00
|
|
|
import com.rarchives.ripme.utils.RipUtils;
|
|
|
|
|
2014-06-23 04:17:40 +02:00
|
|
|
public class ChanRipper extends AbstractHTMLRipper {
|
2017-10-24 16:33:28 +02:00
|
|
|
private static List<ChanSite> explicit_domains = Arrays.asList(
|
2018-06-06 17:35:39 +02:00
|
|
|
new ChanSite(Arrays.asList("boards.4chan.org"), Arrays.asList("4cdn.org", "is.4chan.org", "is2.4chan.org", "is3.4chan.org")),
|
2014-11-14 15:03:13 +01:00
|
|
|
new ChanSite(Arrays.asList("4archive.org"), Arrays.asList("imgur.com")),
|
2017-11-10 15:29:26 +01:00
|
|
|
new ChanSite(Arrays.asList("archive.4plebs.org"), Arrays.asList("img.4plebs.org"))
|
2014-09-08 00:36:08 +02:00
|
|
|
);
|
2016-12-20 04:49:36 +01:00
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
private static List<String> url_piece_blacklist = Arrays.asList(
|
2014-09-08 00:36:08 +02:00
|
|
|
"=http",
|
|
|
|
"http://imgops.com/",
|
|
|
|
"iqdb.org",
|
|
|
|
"saucenao.com"
|
|
|
|
);
|
2016-12-20 04:49:36 +01:00
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
private ChanSite chanSite;
|
|
|
|
private Boolean generalChanSite = true;
|
2016-12-20 04:49:36 +01:00
|
|
|
|
2014-04-13 01:54:45 +02:00
|
|
|
public ChanRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
2014-09-08 00:36:08 +02:00
|
|
|
for (ChanSite _chanSite : explicit_domains) {
|
2014-11-14 15:03:13 +01:00
|
|
|
if (_chanSite.domains.contains(url.getHost())) {
|
|
|
|
chanSite = _chanSite;
|
|
|
|
generalChanSite = false;
|
2014-09-08 00:36:08 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-14 15:03:13 +01:00
|
|
|
if (chanSite == null) {
|
|
|
|
chanSite = new ChanSite(Arrays.asList(url.getHost()));
|
2015-02-06 08:58:17 +01:00
|
|
|
}
|
2014-04-13 01:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2015-02-23 04:18:37 +01:00
|
|
|
@Override
|
|
|
|
public String getAlbumTitle(URL url) throws MalformedURLException {
|
|
|
|
try {
|
|
|
|
// Attempt to use album title as GID
|
|
|
|
Document doc = getFirstPage();
|
2017-11-11 07:56:36 +01:00
|
|
|
try {
|
|
|
|
String subject = doc.select(".post.op > .postinfo > .subject").first().text();
|
2017-11-10 15:29:26 +01:00
|
|
|
return getHost() + "_" + getGID(url) + "_" + subject;
|
2017-11-11 07:56:36 +01:00
|
|
|
} catch (NullPointerException e) {
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.warn("Failed to get thread title from " + url);
|
2017-11-10 15:29:26 +01:00
|
|
|
}
|
2015-02-23 04:18:37 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
// Fall back to default album naming convention
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.warn("Failed to get album title from " + url, e);
|
2015-02-23 04:18:37 +01:00
|
|
|
}
|
2017-11-14 19:22:20 +01:00
|
|
|
// Fall back on the GID
|
|
|
|
return getHost() + "_" + getGID(url);
|
2015-02-23 04:18:37 +01:00
|
|
|
}
|
|
|
|
|
2014-04-13 01:54:45 +02:00
|
|
|
@Override
|
2015-02-06 08:58:17 +01:00
|
|
|
public boolean canRip(URL url) {
|
2014-09-08 00:36:08 +02:00
|
|
|
for (ChanSite _chanSite : explicit_domains) {
|
2014-11-14 15:03:13 +01:00
|
|
|
if (_chanSite.domains.contains(url.getHost())) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-03 10:13:14 +02:00
|
|
|
}
|
2018-06-07 11:08:09 +02:00
|
|
|
if (url.toExternalForm().contains("desuchan.net") && url.toExternalForm().contains("/res/")) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-06 17:34:16 +02:00
|
|
|
return false;
|
2014-04-13 01:54:45 +02:00
|
|
|
}
|
2015-02-06 08:58:17 +01:00
|
|
|
|
2014-09-08 00:36:08 +02:00
|
|
|
/**
|
2015-02-23 04:18:37 +01:00
|
|
|
* For example the archives are all known. (Check 4chan-x)
|
2014-09-08 00:36:08 +02:00
|
|
|
* Should be based on the software the specific chan uses.
|
|
|
|
* FoolFuuka uses the same (url) layout as 4chan
|
2017-05-09 23:02:24 +02:00
|
|
|
*
|
|
|
|
* @param url
|
2017-05-10 00:03:12 +02:00
|
|
|
* @return
|
2017-05-09 23:02:24 +02:00
|
|
|
* The thread id in string form
|
|
|
|
* @throws java.net.MalformedURLException */
|
2014-04-13 01:54:45 +02:00
|
|
|
@Override
|
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
2015-02-23 04:18:37 +01:00
|
|
|
Pattern p;
|
|
|
|
Matcher m;
|
2014-04-13 01:54:45 +02:00
|
|
|
|
2015-02-06 08:58:17 +01:00
|
|
|
String u = url.toExternalForm();
|
2014-11-14 15:03:13 +01:00
|
|
|
if (u.contains("/thread/") || u.contains("/res/")) {
|
2014-09-08 00:36:08 +02:00
|
|
|
p = Pattern.compile("^.*\\.[a-z]{1,3}/[a-zA-Z0-9]+/(thread|res)/([0-9]+)(\\.html|\\.php)?.*$");
|
2014-04-25 04:42:10 +02:00
|
|
|
m = p.matcher(u);
|
|
|
|
if (m.matches()) {
|
2014-05-03 10:13:14 +02:00
|
|
|
return m.group(2);
|
2014-04-25 04:42:10 +02:00
|
|
|
}
|
2015-02-06 23:00:06 +01:00
|
|
|
|
|
|
|
// Drawchan is weird, has drawchan.net/dc/dw/res/####.html
|
|
|
|
p = Pattern.compile("^.*\\.[a-z]{1,3}/[a-zA-Z0-9]+/[a-zA-Z0-9]+/res/([0-9]+)(\\.html|\\.php)?.*$");
|
|
|
|
m = p.matcher(u);
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1);
|
|
|
|
}
|
2017-11-14 19:39:38 +01:00
|
|
|
// xchan
|
|
|
|
p = Pattern.compile("^.*\\.[a-z]{1,3}/board/[a-zA-Z0-9]+/thread/([0-9]+)/?.*$");
|
|
|
|
m = p.matcher(u);
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1);
|
|
|
|
}
|
2014-04-25 04:42:10 +02:00
|
|
|
}
|
2014-04-13 01:54:45 +02:00
|
|
|
|
|
|
|
throw new MalformedURLException(
|
|
|
|
"Expected *chan URL formats: "
|
2014-09-08 00:36:08 +02:00
|
|
|
+ ".*/@/(res|thread)/####.html"
|
2014-04-25 04:42:10 +02:00
|
|
|
+ " Got: " + u);
|
2014-04-13 01:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-22 02:08:42 +02:00
|
|
|
public String getDomain() {
|
|
|
|
return this.url.getHost();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Document getFirstPage() throws IOException {
|
|
|
|
return Http.url(this.url).get();
|
|
|
|
}
|
|
|
|
|
2015-02-06 12:01:02 +01:00
|
|
|
private boolean isURLBlacklisted(String url) {
|
|
|
|
for (String blacklist_item : url_piece_blacklist) {
|
|
|
|
if (url.contains(blacklist_item)) {
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.debug("Skipping link that contains '"+blacklist_item+"': " + url);
|
2015-02-06 12:01:02 +01:00
|
|
|
return true;
|
2016-12-20 04:49:36 +01:00
|
|
|
}
|
2015-02-06 12:01:02 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
@Override
|
|
|
|
public List<String> getURLsFromPage(Document page) {
|
2017-10-24 16:33:28 +02:00
|
|
|
List<String> imageURLs = new ArrayList<>();
|
2014-04-13 01:54:45 +02:00
|
|
|
Pattern p; Matcher m;
|
2014-06-22 02:08:42 +02:00
|
|
|
for (Element link : page.select("a")) {
|
2016-12-20 04:49:36 +01:00
|
|
|
if (!link.hasAttr("href")) {
|
2014-04-13 01:54:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-14 15:03:13 +01:00
|
|
|
String href = link.attr("href").trim();
|
|
|
|
|
2015-02-06 12:01:02 +01:00
|
|
|
if (isURLBlacklisted(href)) {
|
|
|
|
continue;
|
2014-04-13 01:54:45 +02:00
|
|
|
}
|
2015-02-06 12:01:02 +01:00
|
|
|
//Check all blacklist items
|
2014-09-08 00:36:08 +02:00
|
|
|
Boolean self_hosted = false;
|
2014-11-14 15:03:13 +01:00
|
|
|
if (!generalChanSite) {
|
|
|
|
for (String cdnDomain : chanSite.cdnDomains) {
|
2017-05-10 02:50:32 +02:00
|
|
|
if (href.contains(cdnDomain)) {
|
2014-09-08 00:36:08 +02:00
|
|
|
self_hosted = true;
|
2016-12-20 04:49:36 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-03 10:13:14 +02:00
|
|
|
}
|
2014-11-14 15:03:13 +01:00
|
|
|
|
2017-05-10 02:50:32 +02:00
|
|
|
if (self_hosted || generalChanSite) {
|
2014-09-08 00:36:08 +02:00
|
|
|
p = Pattern.compile("^.*\\.(jpg|jpeg|png|gif|apng|webp|tif|tiff|webm)$", Pattern.CASE_INSENSITIVE);
|
|
|
|
m = p.matcher(href);
|
2014-11-14 15:03:13 +01:00
|
|
|
if (m.matches()) {
|
2014-09-08 00:36:08 +02:00
|
|
|
if (href.startsWith("//")) {
|
|
|
|
href = "http:" + href;
|
|
|
|
}
|
|
|
|
if (href.startsWith("/")) {
|
|
|
|
href = "http://" + this.url.getHost() + href;
|
|
|
|
}
|
|
|
|
// Don't download the same URL twice
|
|
|
|
if (imageURLs.contains(href)) {
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.debug("Already attempted: " + href);
|
2014-09-08 00:36:08 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
imageURLs.add(href);
|
2015-02-06 12:01:02 +01:00
|
|
|
if (isThisATest()) {
|
|
|
|
break;
|
|
|
|
}
|
2014-04-13 01:54:45 +02:00
|
|
|
}
|
2014-09-08 00:36:08 +02:00
|
|
|
} else {
|
2017-05-09 23:02:24 +02:00
|
|
|
//Copied code from RedditRipper, getFilesFromURL should also implement stuff like flickr albums
|
|
|
|
URL originalURL;
|
|
|
|
try {
|
|
|
|
originalURL = new URL(href);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-10 00:03:12 +02:00
|
|
|
List<URL> urls = RipUtils.getFilesFromURL(originalURL);
|
2017-05-10 02:50:32 +02:00
|
|
|
for (URL imageurl : urls) {
|
2017-05-09 23:02:24 +02:00
|
|
|
imageURLs.add(imageurl.toString());
|
2017-05-10 00:03:12 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-06 12:01:02 +01:00
|
|
|
|
|
|
|
if (isStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
2014-04-13 01:54:45 +02:00
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
return imageURLs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
2017-11-14 19:22:20 +01:00
|
|
|
addURLToDownload(url, getPrefix(index));
|
2016-12-20 04:49:36 +01:00
|
|
|
}
|
2014-09-08 00:36:08 +02:00
|
|
|
}
|