Merge pull request #869 from cyian-1756/gelbooruRipper

Gelbooru ripper
This commit is contained in:
cyian-1756 2018-08-20 16:34:16 -04:00 committed by GitHub
commit 183d969bb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 16 deletions

View File

@ -16,27 +16,38 @@ import org.apache.log4j.Logger;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
public class XbooruRipper extends AbstractHTMLRipper { public class BooruRipper extends AbstractHTMLRipper {
private static final Logger logger = Logger.getLogger(XbooruRipper.class); private static final Logger logger = Logger.getLogger(BooruRipper.class);
private static Pattern gidPattern = null; private static Pattern gidPattern = null;
public XbooruRipper(URL url) throws IOException { public BooruRipper(URL url) throws IOException {
super(url); super(url);
} }
@Override @Override
public String getDomain() { public boolean canRip(URL url) {
return "xbooru.com"; if (url.toExternalForm().contains("xbooru") || url.toExternalForm().contains("gelbooru")) {
return true;
}
return false;
} }
@Override @Override
public String getHost() { public String getHost() {
return "xbooru"; logger.info(url.toExternalForm().split("/")[2]);
return url.toExternalForm().split("/")[2].split("\\.")[0];
}
@Override
public String getDomain() {
return url.toExternalForm().split("/")[2];
} }
private String getPage(int num) throws MalformedURLException { private String getPage(int num) throws MalformedURLException {
return "http://xbooru.com/index.php?page=dapi&s=post&q=index&pid=" + num + "&tags=" + getTerm(url); return "http://" + getHost() + ".com/index.php?page=dapi&s=post&q=index&pid=" + num + "&tags=" + getTerm(url);
} }
@Override @Override
@ -72,7 +83,7 @@ public class XbooruRipper extends AbstractHTMLRipper {
private String getTerm(URL url) throws MalformedURLException { private String getTerm(URL url) throws MalformedURLException {
if (gidPattern == null) { if (gidPattern == null) {
gidPattern = Pattern.compile("^https?://(www\\.)?xbooru\\.com/(index.php)?.*([?&]tags=([a-zA-Z0-9$_.+!*'(),%-]+))(&|(#.*)?$)"); gidPattern = Pattern.compile("^https?://(www\\.)?(x|gel)booru\\.com/(index.php)?.*([?&]tags=([a-zA-Z0-9$_.+!*'(),%-]+))(&|(#.*)?$)");
} }
Matcher m = gidPattern.matcher(url.toExternalForm()); Matcher m = gidPattern.matcher(url.toExternalForm());
@ -80,17 +91,17 @@ public class XbooruRipper extends AbstractHTMLRipper {
return m.group(4); return m.group(4);
} }
throw new MalformedURLException("Expected xbooru.com URL format: xbooru.com/index.php?tags=searchterm - got " + url + " instead"); throw new MalformedURLException("Expected xbooru.com URL format: " + getHost() + ".com/index.php?tags=searchterm - got " + url + " instead");
} }
@Override @Override
public String getGID(URL url) throws MalformedURLException { public String getGID(URL url) throws MalformedURLException {
try { try {
return Utils.filesystemSafe(new URI(getTerm(url)).getPath()); return Utils.filesystemSafe(new URI(getTerm(url).replaceAll("&tags=", "")).getPath());
} catch (URISyntaxException ex) { } catch (URISyntaxException ex) {
logger.error(ex); logger.error(ex);
} }
throw new MalformedURLException("Expected xbooru.com URL format: xbooru.com/index.php?tags=searchterm - got " + url + " instead"); throw new MalformedURLException("Expected xbooru.com URL format: " + getHost() + ".com/index.php?tags=searchterm - got " + url + " instead");
} }
} }

View File

@ -3,17 +3,17 @@ package com.rarchives.ripme.tst.ripper.rippers;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import com.rarchives.ripme.ripper.rippers.XbooruRipper; import com.rarchives.ripme.ripper.rippers.BooruRipper;
public class XbooruRipperTest extends RippersTest { public class BooruRipperTest extends RippersTest {
public void testRip() throws IOException { public void testRip() throws IOException {
XbooruRipper ripper = new XbooruRipper(new URL("http://xbooru.com/index.php?page=post&s=list&tags=furry")); BooruRipper ripper = new BooruRipper(new URL("http://xbooru.com/index.php?page=post&s=list&tags=furry"));
testRipper(ripper); testRipper(ripper);
} }
public void testGetGID() throws IOException { public void testGetGID() throws IOException {
URL url = new URL("http://xbooru.com/index.php?page=post&s=list&tags=furry"); URL url = new URL("http://xbooru.com/index.php?page=post&s=list&tags=furry");
XbooruRipper ripper = new XbooruRipper(url); BooruRipper ripper = new BooruRipper(url);
assertEquals("furry", ripper.getGID(url)); assertEquals("furry", ripper.getGID(url));
} }
} }