1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-29 16:50:10 +02:00

imgur java.nio

This commit is contained in:
soloturn
2022-01-03 19:32:40 +01:00
parent 9016782248
commit 3c89ca8428

View File

@@ -1,9 +1,10 @@
package com.rarchives.ripme.ripper.rippers; package com.rarchives.ripme.ripper.rippers;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@@ -91,7 +92,7 @@ public class ImgurRipper extends AlbumRipper {
albumDoc = Http.url(url).get(); albumDoc = Http.url(url).get();
} }
Elements elems = null; Elements elems;
/* /*
// TODO: Add config option for including username in album title. // TODO: Add config option for including username in album title.
@@ -106,15 +107,13 @@ public class ImgurRipper extends AlbumRipper {
} }
*/ */
String title = null; String title;
final String defaultTitle1 = "Imgur: The most awesome images on the Internet"; final String defaultTitle1 = "Imgur: The most awesome images on the Internet";
final String defaultTitle2 = "Imgur: The magic of the Internet"; final String defaultTitle2 = "Imgur: The magic of the Internet";
LOGGER.info("Trying to get album title"); LOGGER.info("Trying to get album title");
elems = albumDoc.select("meta[property=og:title]"); elems = albumDoc.select("meta[property=og:title]");
if (elems != null) { title = elems.attr("content");
title = elems.attr("content"); LOGGER.debug("Title is " + title);
LOGGER.debug("Title is " + title);
}
// This is here encase the album is unnamed, to prevent // This is here encase the album is unnamed, to prevent
// Imgur: The most awesome images on the Internet from being added onto the album name // Imgur: The most awesome images on the Internet from being added onto the album name
if (title.contains(defaultTitle1) || title.contains(defaultTitle2)) { if (title.contains(defaultTitle1) || title.contains(defaultTitle2)) {
@@ -124,27 +123,17 @@ public class ImgurRipper extends AlbumRipper {
title = ""; title = "";
LOGGER.debug("Trying to use title tag to get title"); LOGGER.debug("Trying to use title tag to get title");
elems = albumDoc.select("title"); elems = albumDoc.select("title");
if (elems != null) { if (elems.text().contains(defaultTitle1) || elems.text().contains(defaultTitle2)) {
if (elems.text().contains(defaultTitle1) || elems.text().contains(defaultTitle2)) { LOGGER.debug("Was unable to get album title or album was untitled");
LOGGER.debug("Was unable to get album title or album was untitled"); }
} else {
else { title = elems.text();
title = elems.text();
}
} }
} }
String albumTitle = "imgur_"; String albumTitle = "imgur_";
/*
// TODO: Add config option (see above)
if (user != null) {
albumTitle += "user_" + user;
}
*/
albumTitle += gid; albumTitle += gid;
if (title != null) { albumTitle += "_" + title;
albumTitle += "_" + title;
}
return albumTitle; return albumTitle;
} catch (IOException e) { } catch (IOException e) {
@@ -204,33 +193,25 @@ public class ImgurRipper extends AlbumRipper {
} }
private void ripAlbum(URL url, String subdirectory) throws IOException { private void ripAlbum(URL url, String subdirectory) throws IOException {
int index = 0; int index;
this.sendUpdate(STATUS.LOADING_RESOURCE, url.toExternalForm()); this.sendUpdate(STATUS.LOADING_RESOURCE, url.toExternalForm());
index = 0; index = 0;
ImgurAlbum album = getImgurAlbum(url); ImgurAlbum album = getImgurAlbum(url);
for (ImgurImage imgurImage : album.images) { for (ImgurImage imgurImage : album.images) {
stopCheck(); stopCheck();
String saveAs = workingDir.getCanonicalPath(); Path saveAs = workingDir.toPath();
if (!saveAs.endsWith(File.separator)) {
saveAs += File.separator;
}
if (subdirectory != null && !subdirectory.equals("")) { if (subdirectory != null && !subdirectory.equals("")) {
saveAs += subdirectory; saveAs.resolve(subdirectory);
} }
if (!saveAs.endsWith(File.separator)) { if (!Files.exists(saveAs)) {
saveAs += File.separator; Files.createDirectory(saveAs);
}
File subdirFile = new File(saveAs);
if (!subdirFile.exists()) {
subdirFile.mkdirs();
} }
index += 1; index += 1;
if (Utils.getConfigBoolean("download.save_order", true)) { if (Utils.getConfigBoolean("download.save_order", true)) {
saveAs += String.format("%03d_", index); saveAs.resolve(String.format("%03d_", index));
} }
saveAs += imgurImage.getSaveAs(); saveAs.resolve(imgurImage.getSaveAs().replaceAll("\\?\\d", ""));
saveAs = saveAs.replaceAll("\\?\\d", ""); addURLToDownload(imgurImage.url, saveAs);
addURLToDownload(imgurImage.url, new File(saveAs).toPath());
} }
} }
@@ -309,7 +290,7 @@ public class ImgurRipper extends AlbumRipper {
image = "http:" + thumb.select("img").attr("src"); image = "http:" + thumb.select("img").attr("src");
} else { } else {
// Unable to find image in this div // Unable to find image in this div
LOGGER.error("[!] Unable to find image in div: " + thumb.toString()); LOGGER.error("[!] Unable to find image in div: " + thumb);
continue; continue;
} }
if (image.endsWith(".gif") && Utils.getConfigBoolean("prefer.mp4", false)) { if (image.endsWith(".gif") && Utils.getConfigBoolean("prefer.mp4", false)) {
@@ -336,10 +317,6 @@ public class ImgurRipper extends AlbumRipper {
return imgurAlbum; return imgurAlbum;
} }
private static ImgurImage createImgurImageFromJson(JSONObject json) throws MalformedURLException {
return new ImgurImage(extractImageUrlFromJson(json));
}
private static URL extractImageUrlFromJson(JSONObject json) throws MalformedURLException { private static URL extractImageUrlFromJson(JSONObject json) throws MalformedURLException {
String ext = json.getString("ext"); String ext = json.getString("ext");
if (ext.equals(".gif") && Utils.getConfigBoolean("prefer.mp4", false)) { if (ext.equals(".gif") && Utils.getConfigBoolean("prefer.mp4", false)) {
@@ -374,7 +351,6 @@ public class ImgurRipper extends AlbumRipper {
* Rips all albums in an imgur user's account. * Rips all albums in an imgur user's account.
* @param url * @param url
* URL to imgur user account (http://username.imgur.com) * URL to imgur user account (http://username.imgur.com)
* @throws IOException
*/ */
private void ripUserAccount(URL url) throws IOException { private void ripUserAccount(URL url) throws IOException {
LOGGER.info("Retrieving " + url); LOGGER.info("Retrieving " + url);
@@ -390,14 +366,14 @@ public class ImgurRipper extends AlbumRipper {
URL albumURL = new URL("http:" + album.attr("href") + "/noscript"); URL albumURL = new URL("http:" + album.attr("href") + "/noscript");
try { try {
ripAlbum(albumURL, albumID); ripAlbum(albumURL, albumID);
Thread.sleep(SLEEP_BETWEEN_ALBUMS * 1000); Thread.sleep(SLEEP_BETWEEN_ALBUMS * 1000L);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error while ripping album: " + e.getMessage(), e); LOGGER.error("Error while ripping album: " + e.getMessage(), e);
} }
} }
} }
private void ripUserImages(URL url) throws IOException { private void ripUserImages(URL url) {
int page = 0; int imagesFound = 0; int imagesTotal = 0; int page = 0; int imagesFound = 0; int imagesTotal = 0;
String jsonUrl = url.toExternalForm().replace("/all", "/ajax/images"); String jsonUrl = url.toExternalForm().replace("/all", "/ajax/images");
if (jsonUrl.contains("#")) { if (jsonUrl.contains("#")) {
@@ -478,8 +454,8 @@ public class ImgurRipper extends AlbumRipper {
@Override @Override
public String getGID(URL url) throws MalformedURLException { public String getGID(URL url) throws MalformedURLException {
Pattern p = null; Pattern p;
Matcher m = null; Matcher m;
p = Pattern.compile("^https?://(www\\.|m\\.)?imgur\\.com/(a|gallery)/([a-zA-Z0-9]{5,}).*$"); p = Pattern.compile("^https?://(www\\.|m\\.)?imgur\\.com/(a|gallery)/([a-zA-Z0-9]{5,}).*$");
m = p.matcher(url.toExternalForm()); m = p.matcher(url.toExternalForm());
@@ -529,13 +505,13 @@ public class ImgurRipper extends AlbumRipper {
if (m.matches()) { if (m.matches()) {
// Imgur subreddit aggregator // Imgur subreddit aggregator
albumType = ALBUM_TYPE.SUBREDDIT; albumType = ALBUM_TYPE.SUBREDDIT;
String album = m.group(2); StringBuilder album = new StringBuilder(m.group(2));
for (int i = 3; i <= m.groupCount(); i++) { for (int i = 3; i <= m.groupCount(); i++) {
if (m.group(i) != null) { if (m.group(i) != null) {
album += "_" + m.group(i).replace("/", ""); album.append("_").append(m.group(i).replace("/", ""));
} }
} }
return album; return album.toString();
} }
p = Pattern.compile("^https?://(i\\.|www\\.|m\\.)?imgur\\.com/r/(\\w+)/([a-zA-Z0-9,]{5,}).*$"); p = Pattern.compile("^https?://(i\\.|www\\.|m\\.)?imgur\\.com/r/(\\w+)/([a-zA-Z0-9,]{5,}).*$");
m = p.matcher(url.toExternalForm()); m = p.matcher(url.toExternalForm());
@@ -568,15 +544,11 @@ public class ImgurRipper extends AlbumRipper {
throw new MalformedURLException("Unsupported imgur URL format: " + url.toExternalForm()); throw new MalformedURLException("Unsupported imgur URL format: " + url.toExternalForm());
} }
public ALBUM_TYPE getAlbumType() {
return albumType;
}
public static class ImgurImage { public static class ImgurImage {
String title = ""; String title = "";
String description = ""; String description = "";
String extension = ""; String extension;
public URL url = null; public URL url;
ImgurImage(URL url) { ImgurImage(URL url) {
this.url = url; this.url = url;
@@ -586,14 +558,7 @@ public class ImgurRipper extends AlbumRipper {
this.extension = this.extension.substring(0, this.extension.indexOf("?")); this.extension = this.extension.substring(0, this.extension.indexOf("?"));
} }
} }
ImgurImage(URL url, String title) {
this(url);
this.title = title;
}
public ImgurImage(URL url, String title, String description) {
this(url, title);
this.description = description;
}
String getSaveAs() { String getSaveAs() {
String saveAs = this.title; String saveAs = this.title;
String u = url.toExternalForm(); String u = url.toExternalForm();
@@ -613,7 +578,7 @@ public class ImgurRipper extends AlbumRipper {
public static class ImgurAlbum { public static class ImgurAlbum {
String title = null; String title = null;
public URL url = null; public URL url;
public List<ImgurImage> images = new ArrayList<>(); public List<ImgurImage> images = new ArrayList<>();
ImgurAlbum(URL url) { ImgurAlbum(URL url) {
this.url = url; this.url = url;