1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-26 07:14:38 +02:00

Fix imgur ripper for single image

This commit is contained in:
Tush-r
2024-09-09 20:06:15 +05:30
committed by soloturn
parent 169b2ecca0
commit 4cb2ef420e

View File

@@ -211,12 +211,16 @@ public class ImgurRipper extends AbstractHTMLRipper {
private void ripSingleImage(URL url) throws IOException, URISyntaxException { private void ripSingleImage(URL url) throws IOException, URISyntaxException {
String strUrl = url.toExternalForm(); String strUrl = url.toExternalForm();
Document document = getDocument(strUrl); var gid = getGID(url);
Matcher m = getEmbeddedJsonMatcher(document); var json = getSingleImageData(String.format("https://api.imgur.com/post/v1/media/%s?include=media,adconfig,account", gid));
if (m.matches()) { var media = json.getJSONArray("media");
JSONObject json = new JSONObject(m.group(1)).getJSONObject("image"); if (media.length()==0) {
addURLToDownload(extractImageUrlFromJson(json), ""); throw new IOException(String.format("Failed to fetch image for url %s", strUrl));
}
if (media.length()>1) {
LOGGER.warn(String.format("Got multiple images for url %s", strUrl));
} }
addURLToDownload(extractImageUrlFromJson((JSONObject)media.get(0)), "");
} }
private void ripAlbum(URL url) throws IOException, URISyntaxException { private void ripAlbum(URL url) throws IOException, URISyntaxException {
@@ -333,11 +337,6 @@ public class ImgurRipper extends AbstractHTMLRipper {
return imgurAlbum; return imgurAlbum;
} }
private static Matcher getEmbeddedJsonMatcher(Document doc) {
Pattern p = Pattern.compile("^.*widgetFactory.mergeConfig\\('gallery', (.*?)\\);.*$", Pattern.DOTALL);
return p.matcher(doc.body().html());
}
private static ImgurAlbum createImgurAlbumFromJsonArray(URL url, JSONArray jsonImages) throws MalformedURLException, URISyntaxException { private static ImgurAlbum createImgurAlbumFromJsonArray(URL url, JSONArray jsonImages) throws MalformedURLException, URISyntaxException {
ImgurAlbum imgurAlbum = new ImgurAlbum(url); ImgurAlbum imgurAlbum = new ImgurAlbum(url);
int imagesLength = jsonImages.length(); int imagesLength = jsonImages.length();
@@ -350,21 +349,24 @@ public class ImgurRipper extends AbstractHTMLRipper {
private static URL extractImageUrlFromJson(JSONObject json) throws MalformedURLException, URISyntaxException { private static URL extractImageUrlFromJson(JSONObject json) throws MalformedURLException, URISyntaxException {
String ext = json.getString("ext"); String ext = json.getString("ext");
if (!ext.startsWith(".")) {
ext = "." + ext;
}
if (ext.equals(".gif") && Utils.getConfigBoolean("prefer.mp4", false)) { if (ext.equals(".gif") && Utils.getConfigBoolean("prefer.mp4", false)) {
ext = ".mp4"; ext = ".mp4";
} }
return new URI( return new URI(
"http://i.imgur.com/" "https://i.imgur.com/"
+ json.getString("hash") + json.getString("id")
+ ext).toURL(); + ext).toURL();
} }
private static Document getDocument(String strUrl) throws IOException { private static JSONObject getSingleImageData(String strUrl) throws IOException {
return Jsoup.connect(strUrl) return Http.url(strUrl)
.userAgent(USER_AGENT) .userAgent(USER_AGENT)
.timeout(10 * 1000) .timeout(10 * 1000)
.maxBodySize(0) .header("Authorization", "Client-ID " + Utils.getConfigString("imgur.client_id", "546c25a59c58ad7"))
.get(); .getJSON();
} }
private static Document getAlbumData(String strUrl) throws IOException { private static Document getAlbumData(String strUrl) throws IOException {