From c9c85429456543cd4fedc8083b1b9614e1b84bc8 Mon Sep 17 00:00:00 2001 From: cyian-1756 Date: Mon, 12 Mar 2018 12:40:13 -0400 Subject: [PATCH] Added another overload to addURLToDownload which allows the ripper to set the name of the file; Fixed 8muses filename length issue --- .../ripme/ripper/AbstractRipper.java | 29 +++++++++++++++---- .../ripper/rippers/EightmusesRipper.java | 4 +-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index ff6b4102..6068ed18 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -174,7 +174,7 @@ public abstract class AbstractRipper * URL of the file * @param saveAs * Path of the local file to save the content to. - * @return True on success, flase on failure. + * @return True on success, false on failure. */ public abstract boolean addURLToDownload(URL url, File saveAs); @@ -206,11 +206,13 @@ public abstract class AbstractRipper * The HTTP referrer to use while downloading this file. * @param cookies * The cookies to send to the server while downloading this file. + * @param fileName + * The name that file will be written to * @return * True if downloaded successfully * False if failed to download */ - protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map cookies) { + protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map cookies, String fileName) { // Don't re-add the url if it was downloaded in a previous rip if (Utils.getConfigBoolean("remember.url_history", true) && !isThisATest()) { if (hasDownloadedURL(url.toExternalForm())) { @@ -225,9 +227,18 @@ public abstract class AbstractRipper logger.debug("Ripper has been stopped"); return false; } - logger.debug("url: " + url + ", prefix: " + prefix + ", subdirectory" + subdirectory + ", referrer: " + referrer + ", cookies: " + cookies); - String saveAs = url.toExternalForm(); - saveAs = saveAs.substring(saveAs.lastIndexOf('/')+1); + logger.debug("url: " + url + ", prefix: " + prefix + ", subdirectory" + subdirectory + ", referrer: " + referrer + ", cookies: " + cookies + ", fileName: " + fileName); + String saveAs; + if (fileName != null) { + saveAs = fileName; + // Get the extension of the file + String extension = url.toExternalForm().substring(url.toExternalForm().lastIndexOf(".") + 1); + saveAs = saveAs + "." + extension; + } else { + saveAs = url.toExternalForm(); + saveAs = saveAs.substring(saveAs.lastIndexOf('/')+1); + } + if (saveAs.indexOf('?') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('?')); } if (saveAs.indexOf('#') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('#')); } if (saveAs.indexOf('&') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('&')); } @@ -274,7 +285,11 @@ public abstract class AbstractRipper * @return True on success, flase on failure. */ protected boolean addURLToDownload(URL url, String prefix, String subdirectory) { - return addURLToDownload(url, prefix, subdirectory, null, null); + return addURLToDownload(url, prefix, subdirectory, null, null, null); + } + + protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map cookies) { + return addURLToDownload(url, prefix, subdirectory, referrer, cookies, null); } /** @@ -290,6 +305,8 @@ public abstract class AbstractRipper // Use empty subdirectory return addURLToDownload(url, prefix, ""); } + + /** * Waits for downloading threads to complete. */ diff --git a/src/main/java/com/rarchives/ripme/ripper/rippers/EightmusesRipper.java b/src/main/java/com/rarchives/ripme/ripper/rippers/EightmusesRipper.java index 43873cf9..80ac5b93 100644 --- a/src/main/java/com/rarchives/ripme/ripper/rippers/EightmusesRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/rippers/EightmusesRipper.java @@ -125,7 +125,7 @@ public class EightmusesRipper extends AbstractHTMLRipper { logger.info("Retrieving full-size image location from " + imageHref); image = getFullSizeImage(imageHref); URL imageUrl = new URL(image); - addURLToDownload(imageUrl, getPrefix(x), getSubdir(page.select("title").text()), this.url.toExternalForm(), cookies); + addURLToDownload(imageUrl, getPrefix(x), getSubdir(page.select("title").text()), this.url.toExternalForm(), cookies, ""); // X is our page index x++; @@ -180,6 +180,6 @@ public class EightmusesRipper extends AbstractHTMLRipper { @Override public String getPrefix(int index) { - return String.format("%03d_", index); + return String.format("%03d", index); } }