diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index 031523e9..1f2a3b27 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -264,7 +264,7 @@ public abstract class AbstractRipper if (!path.endsWith(File.separator)) { path += File.separator; } - path += getHost() + "_" + getGID(this.url) + File.separator; + path += getAlbumTitle(this.url) + File.separator; this.workingDir = new File(path); if (!this.workingDir.exists()) { logger.info("[+] Creating directory: " + Utils.removeCWD(this.workingDir)); @@ -272,6 +272,10 @@ public abstract class AbstractRipper } logger.debug("Set working directory to: " + this.workingDir); } + + public String getAlbumTitle(URL url) throws MalformedURLException { + return getHost() + "_" + getGID(url); + } /** * Finds, instantiates, and returns a compatible ripper for given URL. diff --git a/src/main/java/com/rarchives/ripme/ripper/rippers/ImagefapRipper.java b/src/main/java/com/rarchives/ripme/ripper/rippers/ImagefapRipper.java index f858ed5e..a69f8748 100644 --- a/src/main/java/com/rarchives/ripme/ripper/rippers/ImagefapRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/rippers/ImagefapRipper.java @@ -19,6 +19,8 @@ public class ImagefapRipper extends AbstractRipper { HOST = "imagefap"; private static final Logger logger = Logger.getLogger(ImagefapRipper.class); + private Document albumDoc = null; + public ImagefapRipper(URL url) throws IOException { super(url); } @@ -38,18 +40,41 @@ public class ImagefapRipper extends AbstractRipper { logger.debug("Sanitized URL from " + url + " to " + newURL); return newURL; } + + public String getAlbumTitle(URL url) throws MalformedURLException { + try { + // Attempt to use album title as GID + if (albumDoc == null) { + albumDoc = Jsoup.connect(url.toExternalForm()).get(); + } + String title = albumDoc.title(); + Pattern p = Pattern.compile("^Porn pics of (.*) \\(Page 1\\)$"); + Matcher m = p.matcher(title); + if (m.matches()) { + return m.group(1); + } + } catch (IOException e) { + // Fall back to default album naming convention + } + return super.getAlbumTitle(url); + } + @Override public String getGID(URL url) throws MalformedURLException { - Pattern p = Pattern.compile("^.*imagefap.com/gallery.php\\?gid=([0-9]{1,}).*$"); - Matcher m = p.matcher(url.toExternalForm()); + Pattern p; Matcher m; + + p = Pattern.compile("^.*imagefap.com/gallery.php\\?gid=([0-9]{1,}).*$"); + m = p.matcher(url.toExternalForm()); if (m.matches()) { return m.group(1); } + p = Pattern.compile("^.*imagefap.com/pictures/([0-9]{1,}).*$"); m = p.matcher(url.toExternalForm()); if (m.matches()) { return m.group(1); } + throw new MalformedURLException( "Expected imagefap.com gallery formats: " + "imagefap.com/gallery.php?gid=####... or " @@ -61,8 +86,10 @@ public class ImagefapRipper extends AbstractRipper { public void rip() throws IOException { int index = 0; logger.info(" Retrieving " + this.url.toExternalForm()); - Document doc = Jsoup.connect(this.url.toExternalForm()).get(); - for (Element thumb : doc.select("#gallery img")) { + if (albumDoc == null) { + albumDoc = Jsoup.connect(this.url.toExternalForm()).get(); + } + for (Element thumb : albumDoc.select("#gallery img")) { if (!thumb.hasAttr("src") || !thumb.hasAttr("width")) { continue; } diff --git a/src/test/java/com/rarchives/ripme/tst/ripper/rippers/ImagefapRipperTest.java b/src/test/java/com/rarchives/ripme/tst/ripper/rippers/ImagefapRipperTest.java new file mode 100644 index 00000000..3623c0d1 --- /dev/null +++ b/src/test/java/com/rarchives/ripme/tst/ripper/rippers/ImagefapRipperTest.java @@ -0,0 +1,45 @@ +package com.rarchives.ripme.tst.ripper.rippers; + +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.rarchives.ripme.ripper.rippers.ImagefapRipper; + +public class ImagefapRipperTest extends RippersTest { + + public void testImagefapGID() throws IOException { + if (!DOWNLOAD_CONTENT) { + return; + } + Map testURLs = new HashMap(); + testURLs.put(new URL("http://www.imagefap.com/pictures/4649440/Frozen-%28Elsa-and-Anna%29?view=2"), "Frozen (Elsa and Anna)"); + for (URL url : testURLs.keySet()) { + ImagefapRipper ripper = new ImagefapRipper(url); + assertEquals(testURLs.get(url), ripper.getAlbumTitle(ripper.getURL())); + deleteDir(ripper.getWorkingDir()); + } + } + + public void testImagefapAlbums() throws IOException { + if (!DOWNLOAD_CONTENT) { + return; + } + List contentURLs = new ArrayList(); + contentURLs.add(new URL("http://www.imagefap.com/pictures/4649440/Frozen-%28Elsa-and-Anna%29?view=2")); + for (URL url : contentURLs) { + try { + ImagefapRipper ripper = new ImagefapRipper(url); + ripper.rip(); + assert(ripper.getWorkingDir().listFiles().length > 1); + deleteDir(ripper.getWorkingDir()); + } catch (Exception e) { + fail("Error while ripping URL " + url + ": " + e.getMessage()); + } + } + } + +}