diff --git a/src/main/java/com/rarchives/ripme/ripper/rippers/XbooruRipper.java b/src/main/java/com/rarchives/ripme/ripper/rippers/XbooruRipper.java index 6d7d70fc..12f4bdf2 100644 --- a/src/main/java/com/rarchives/ripme/ripper/rippers/XbooruRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/rippers/XbooruRipper.java @@ -1,4 +1,3 @@ - package com.rarchives.ripme.ripper.rippers; import com.rarchives.ripme.ripper.AbstractHTMLRipper; @@ -18,80 +17,79 @@ import java.util.regex.Pattern; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; -/** - * - * @author - */ -public class XbooruRipper extends AbstractHTMLRipper{ - private static Pattern gidPattern=null; +public class XbooruRipper extends AbstractHTMLRipper { + private static Pattern gidPattern = null; - public XbooruRipper(URL url) throws IOException { - super(url); - } + public XbooruRipper(URL url) throws IOException { + super(url); + } - @Override - public String getDomain() { - return "xbooru.com"; - } + @Override + public String getDomain() { + return "xbooru.com"; + } - @Override - public String getHost() { - return "xbooru"; - } - - private String getPage(int num) throws MalformedURLException{ - return "http://xbooru.com/index.php?page=dapi&s=post&q=index&pid="+num+"&tags="+getTerm(url); - } + @Override + public String getHost() { + return "xbooru"; + } - @Override - public Document getFirstPage() throws IOException { - return Http.url(getPage(0)).get(); - } + private String getPage(int num) throws MalformedURLException { + return "http://xbooru.com/index.php?page=dapi&s=post&q=index&pid=" + num + "&tags=" + getTerm(url); + } - @Override - public Document getNextPage(Document doc) throws IOException { - int offset=Integer.parseInt(doc.getElementsByTag("posts").first().attr("offset")); - int num=Integer.parseInt(doc.getElementsByTag("posts").first().attr("count")); - - if(offset+100>num) - return null; - - return Http.url(getPage(offset/100+1)).get(); - } + @Override + public Document getFirstPage() throws IOException { + return Http.url(getPage(0)).get(); + } - @Override - public List getURLsFromPage(Document page) { - List res=new ArrayList(100); - for(Element e:page.getElementsByTag("post")) - res.add(e.absUrl("file_url")+"#"+e.attr("id")); - return res; - } + @Override + public Document getNextPage(Document doc) throws IOException { + int offset = Integer.parseInt(doc.getElementsByTag("posts").first().attr("offset")); + int num = Integer.parseInt(doc.getElementsByTag("posts").first().attr("count")); - @Override - public void downloadURL(URL url, int index) { - addURLToDownload(url,Utils.getConfigBoolean("download.save_order",true)?url.getRef()+"-":""); - } - - private String getTerm(URL url) throws MalformedURLException{ - if(gidPattern==null) - gidPattern=Pattern.compile("^https?://(www\\.)?xbooru\\.com/(index.php)?.*([?&]tags=([a-zA-Z0-9$_.+!*'(),%-]+))(\\&|(#.*)?$)"); + if (offset + 100 > num) { + return null; + } - Matcher m = gidPattern.matcher(url.toExternalForm()); - if(m.matches()) - return m.group(4); - - throw new MalformedURLException("Expected xbooru.com URL format: xbooru.com/index.php?tags=searchterm - got "+url+" instead"); - } + return Http.url(getPage(offset / 100 + 1)).get(); + } - @Override - public String getGID(URL url) throws MalformedURLException { - try { - return Utils.filesystemSafe(new URI(getTerm(url)).getPath()); - } catch (URISyntaxException ex) { - Logger.getLogger(PahealRipper.class.getName()).log(Level.SEVERE, null, ex); - } - - throw new MalformedURLException("Expected xbooru.com URL format: xbooru.com/index.php?tags=searchterm - got "+url+" instead"); - } - + @Override + public List getURLsFromPage(Document page) { + List res = new ArrayList(100); + for (Element e : page.getElementsByTag("post")) { + res.add(e.absUrl("file_url") + "#" + e.attr("id")); + } + return res; + } + + @Override + public void downloadURL(URL url, int index) { + addURLToDownload(url, Utils.getConfigBoolean("download.save_order", true) ? url.getRef() + "-" : ""); + } + + private String getTerm(URL url) throws MalformedURLException { + if (gidPattern == null) { + gidPattern = Pattern.compile("^https?://(www\\.)?xbooru\\.com/(index.php)?.*([?&]tags=([a-zA-Z0-9$_.+!*'(),%-]+))(\\&|(#.*)?$)"); + } + + Matcher m = gidPattern.matcher(url.toExternalForm()); + if (m.matches()) { + return m.group(4); + } + + throw new MalformedURLException("Expected xbooru.com URL format: xbooru.com/index.php?tags=searchterm - got " + url + " instead"); + } + + @Override + public String getGID(URL url) throws MalformedURLException { + try { + return Utils.filesystemSafe(new URI(getTerm(url)).getPath()); + } catch (URISyntaxException ex) { + Logger.getLogger(PahealRipper.class.getName()).log(Level.SEVERE, null, ex); + } + + throw new MalformedURLException("Expected xbooru.com URL format: xbooru.com/index.php?tags=searchterm - got " + url + " instead"); + } }