From 407564feee9410b737db76dc3cd6105d60b9ebae Mon Sep 17 00:00:00 2001 From: Johannes Drescher Date: Sun, 29 Jul 2018 16:12:23 +0200 Subject: [PATCH] Added possibility to save reddit post with multiple images in separate subdirectory with title name. Added title to the saved filename when only one image/video. --- .../ripme/ripper/AbstractRipper.java | 1 + .../ripme/ripper/rippers/RedditRipper.java | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index 94ac1ec9..eeaf8fd1 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -255,6 +255,7 @@ public abstract class AbstractRipper File saveFileAs; try { if (!subdirectory.equals("")) { + subdirectory = Utils.filesystemSafe(subdirectory); subdirectory = File.separator + subdirectory; } prefix = Utils.filesystemSanitized(prefix); diff --git a/src/main/java/com/rarchives/ripme/ripper/rippers/RedditRipper.java b/src/main/java/com/rarchives/ripme/ripper/rippers/RedditRipper.java index 08229d72..2f73eccf 100644 --- a/src/main/java/com/rarchives/ripme/ripper/rippers/RedditRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/rippers/RedditRipper.java @@ -158,16 +158,16 @@ public class RedditRipper extends AlbumRipper { JSONObject data = child.getJSONObject("data"); if (kind.equals("t1")) { // Comment - handleBody(data.getString("body"), data.getString("id")); + handleBody(data.getString("body"), data.getString("id"), ""); } else if (kind.equals("t3")) { // post if (data.getBoolean("is_self")) { // TODO Parse self text - handleBody(data.getString("selftext"), data.getString("id")); + handleBody(data.getString("selftext"), data.getString("id"), data.getString("title")); } else { // Get link - handleURL(data.getString("url"), data.getString("id")); + handleURL(data.getString("url"), data.getString("id"), data.getString("title")); } if (data.has("replies") && data.get("replies") instanceof JSONObject) { JSONArray replies = data.getJSONObject("replies") @@ -180,7 +180,7 @@ public class RedditRipper extends AlbumRipper { } } - private void handleBody(String body, String id) { + private void handleBody(String body, String id, String title) { Pattern p = RipUtils.getURLRegex(); Matcher m = p.matcher(body); while (m.find()) { @@ -188,7 +188,7 @@ public class RedditRipper extends AlbumRipper { while (url.endsWith(")")) { url = url.substring(0, url.length() - 1); } - handleURL(url, id); + handleURL(url, id, title); } } @@ -218,7 +218,7 @@ public class RedditRipper extends AlbumRipper { } - private void handleURL(String theUrl, String id) { + private void handleURL(String theUrl, String id, String title) { URL originalURL; try { originalURL = new URL(theUrl); @@ -226,6 +226,14 @@ public class RedditRipper extends AlbumRipper { return; } + String subdirectory = ""; + if (Utils.getConfigBoolean("album_titles.save", true)) { + subdirectory = title; + title = "-" + title + "-"; + } else { + title = ""; + } + List urls = RipUtils.getFilesFromURL(originalURL); if (urls.size() == 1) { String url = urls.get(0).toExternalForm(); @@ -234,24 +242,24 @@ public class RedditRipper extends AlbumRipper { if (m.matches()) { // It's from reddituploads. Assume .jpg extension. String savePath = this.workingDir + File.separator; - savePath += id + "-" + m.group(1) + ".jpg"; + savePath += id + "-" + m.group(1) + title + ".jpg"; addURLToDownload(urls.get(0), new File(savePath)); } if (url.contains("v.redd.it")) { String savePath = this.workingDir + File.separator; - savePath += id + "-" + url.split("/")[3] + ".mp4"; + savePath += id + "-" + url.split("/")[3] + title + ".mp4"; addURLToDownload(parseRedditVideoMPD(urls.get(0).toExternalForm()), new File(savePath)); } else { - addURLToDownload(urls.get(0), id + "-", "", theUrl, null); + addURLToDownload(urls.get(0), id + title, "", theUrl, null); } } else if (urls.size() > 1) { for (int i = 0; i < urls.size(); i++) { - String prefix = id + "-"; + String prefix = id + ""; if (Utils.getConfigBoolean("download.save_order", true)) { prefix += String.format("%03d-", i + 1); } - addURLToDownload(urls.get(i), prefix, "", theUrl, null); + addURLToDownload(urls.get(i), prefix, subdirectory, theUrl, null); } } }