1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-18 11:41:21 +02:00

new URL is deprecated, use new URI, reddit.

This commit is contained in:
soloturn
2023-06-15 15:17:21 +02:00
parent 029b03c74d
commit 6ffd6f34d0

View File

@@ -3,6 +3,8 @@ package com.rarchives.ripme.ripper.rippers;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@@ -56,19 +58,19 @@ public class RedditRipper extends AlbumRipper {
} }
@Override @Override
public URL sanitizeURL(URL url) throws MalformedURLException { public URL sanitizeURL(URL url) throws MalformedURLException, URISyntaxException {
String u = url.toExternalForm(); String u = url.toExternalForm();
// Strip '/u/' from URL // Strip '/u/' from URL
u = u.replaceAll("reddit\\.com/u/", "reddit.com/user/"); u = u.replaceAll("reddit\\.com/u/", "reddit.com/user/");
return new URL(u); return new URI(u).toURL();
} }
private URL getJsonURL(URL url) throws MalformedURLException { private URL getJsonURL(URL url) throws MalformedURLException, URISyntaxException {
// Convert gallery to post link and append ".json" // Convert gallery to post link and append ".json"
Pattern p = Pattern.compile("^https?://[a-zA-Z0-9.]{0,4}reddit\\.com/gallery/([a-zA-Z0-9]+).*$"); Pattern p = Pattern.compile("^https?://[a-zA-Z0-9.]{0,4}reddit\\.com/gallery/([a-zA-Z0-9]+).*$");
Matcher m = p.matcher(url.toExternalForm()); Matcher m = p.matcher(url.toExternalForm());
if (m.matches()) { if (m.matches()) {
return new URL("https://reddit.com/" +m.group(m.groupCount())+ ".json"); return new URI("https://reddit.com/" +m.group(m.groupCount())+ ".json").toURL();
} }
// Append ".json" to URL in appropriate location. // Append ".json" to URL in appropriate location.
@@ -76,11 +78,12 @@ public class RedditRipper extends AlbumRipper {
if (url.getQuery() != null) { if (url.getQuery() != null) {
result += "?" + url.getQuery(); result += "?" + url.getQuery();
} }
return new URL(result); return new URI(result).toURL();
} }
@Override @Override
public void rip() throws IOException { public void rip() throws IOException {
try {
URL jsonURL = getJsonURL(this.url); URL jsonURL = getJsonURL(this.url);
while (true) { while (true) {
if (shouldAddURL()) { if (shouldAddURL()) {
@@ -92,12 +95,15 @@ public class RedditRipper extends AlbumRipper {
break; break;
} }
} }
} catch (URISyntaxException e) {
new IOException(e.getMessage());
}
waitForThreads(); waitForThreads();
} }
private URL getAndParseAndReturnNext(URL url) throws IOException { private URL getAndParseAndReturnNext(URL url) throws IOException, URISyntaxException {
JSONArray jsonArray = getJsonArrayFromURL(url), children; JSONArray jsonArray = getJsonArrayFromURL(url), children;
JSONObject json, data; JSONObject json, data;
URL nextURL = null; URL nextURL = null;
@@ -118,7 +124,7 @@ public class RedditRipper extends AlbumRipper {
if (children.getJSONObject(j).getString("kind").equals("t3") && if (children.getJSONObject(j).getString("kind").equals("t3") &&
children.getJSONObject(j).getJSONObject("data").getBoolean("is_self") children.getJSONObject(j).getJSONObject("data").getBoolean("is_self")
) { ) {
URL selfPostURL = new URL(children.getJSONObject(j).getJSONObject("data").getString("url")); URL selfPostURL = new URI(children.getJSONObject(j).getJSONObject("data").getString("url")).toURL();
System.out.println(selfPostURL.toExternalForm()); System.out.println(selfPostURL.toExternalForm());
saveText(getJsonArrayFromURL(getJsonURL(selfPostURL))); saveText(getJsonArrayFromURL(getJsonURL(selfPostURL)));
} }
@@ -134,7 +140,7 @@ public class RedditRipper extends AlbumRipper {
else { else {
nextURLString = nextURLString.concat("?after=" + data.getString("after")); nextURLString = nextURLString.concat("?after=" + data.getString("after"));
} }
nextURL = new URL(nextURLString); nextURL = new URI(nextURLString).toURL();
} }
} }
@@ -378,8 +384,8 @@ public class RedditRipper extends AlbumRipper {
baseURL = doc.select("MPD > Period > AdaptationSet > Representation[height=" + height + "]").select("BaseURL").text(); baseURL = doc.select("MPD > Period > AdaptationSet > Representation[height=" + height + "]").select("BaseURL").text();
} }
} }
return new URL(vidURL + "/" + baseURL); return new URI(vidURL + "/" + baseURL).toURL();
} catch (IOException e) { } catch (IOException | URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
@@ -389,8 +395,8 @@ public class RedditRipper extends AlbumRipper {
private void handleURL(String theUrl, String id, String title) { private void handleURL(String theUrl, String id, String title) {
URL originalURL; URL originalURL;
try { try {
originalURL = new URL(theUrl); originalURL = new URI(theUrl).toURL();
} catch (MalformedURLException e) { } catch (MalformedURLException | URISyntaxException e) {
return; return;
} }
String subdirectory = ""; String subdirectory = "";
@@ -455,12 +461,12 @@ public class RedditRipper extends AlbumRipper {
try { try {
URL mediaURL; URL mediaURL;
if (!media.getJSONObject("s").isNull("gif")) { if (!media.getJSONObject("s").isNull("gif")) {
mediaURL = new URL(media.getJSONObject("s").getString("gif").replaceAll("&", "&")); mediaURL = new URI(media.getJSONObject("s").getString("gif").replaceAll("&", "&")).toURL();
} else { } else {
mediaURL = new URL(media.getJSONObject("s").getString("u").replaceAll("&", "&")); mediaURL = new URI(media.getJSONObject("s").getString("u").replaceAll("&", "&")).toURL();
} }
addURLToDownload(mediaURL, prefix, subdirectory); addURLToDownload(mediaURL, prefix, subdirectory);
} catch (MalformedURLException | JSONException e) { } catch (MalformedURLException | JSONException | URISyntaxException e) {
LOGGER.error("[!] Unable to parse gallery JSON:\ngallery_data:\n" + data +"\nmedia_metadata:\n" + metadata); LOGGER.error("[!] Unable to parse gallery JSON:\ngallery_data:\n" + data +"\nmedia_metadata:\n" + metadata);
} }
} }