Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/main/java/com/rarchives/ripme/ripper/rippers/RedditRipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ else if (kind.equals("t3")) {
} else if (!data.isNull("gallery_data") && !data.isNull("media_metadata")) {
handleGallery(data.getJSONObject("gallery_data").getJSONArray("items"), data.getJSONObject("media_metadata"), data.getString("id"), data.getString("title"));
} else {
// Get link
handleURL(data.getString("url"), data.getString("id"), data.getString("title"));
// Reddit may set url to a still-image preview for video posts. Prefer
// the actual video URL when Reddit exposes it in secure_media.
handleURL(getPostMediaURL(data), data.getString("id"), data.getString("title"));
}
if (data.has("replies") && data.get("replies") instanceof JSONObject) {
JSONArray replies = data.getJSONObject("replies")
Expand All @@ -268,6 +269,22 @@ else if (kind.equals("t3")) {
}
}

private String getPostMediaURL(JSONObject data) {
if (data.optBoolean("is_video", false)) {
JSONObject secureMedia = data.optJSONObject("secure_media");
if (secureMedia != null) {
JSONObject redditVideo = secureMedia.optJSONObject("reddit_video");
if (redditVideo != null) {
String fallbackUrl = redditVideo.optString("fallback_url", "");
if (!fallbackUrl.isEmpty()) {
return fallbackUrl;
}
}
}
}
return data.getString("url");
}

private void handleBody(String body, String id, String title) {
Pattern p = RipUtils.getURLRegex();
Matcher m = p.matcher(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.nio.file.Files;
import java.nio.file.Path;

import org.json.JSONObject;

import com.rarchives.ripme.ripper.rippers.RedditRipper;

import org.junit.jupiter.api.Assertions;
Expand All @@ -20,6 +22,35 @@

public class RedditRipperTest extends RippersTest {

@Test
public void testVideoPostUsesRedditVideoURLInsteadOfPreview() throws Exception {
RedditRipper ripper = new RedditRipper(new URI("https://www.reddit.com/r/test/").toURL());
Method getPostMediaURL = RedditRipper.class.getDeclaredMethod("getPostMediaURL", JSONObject.class);
getPostMediaURL.setAccessible(true);

JSONObject post = new JSONObject("{" +
"\"url\":\"https://preview.redd.it/still-image.jpg\"," +
"\"is_video\":true," +
"\"secure_media\":{" +
"\"reddit_video\":{" +
"\"fallback_url\":\"https://v.redd.it/example/DASH_720.mp4?source=fallback\"" +
"}}}");

Assertions.assertEquals("https://v.redd.it/example/DASH_720.mp4?source=fallback",
getPostMediaURL.invoke(ripper, post));
}

@Test
public void testNonVideoPostKeepsOriginalURL() throws Exception {
RedditRipper ripper = new RedditRipper(new URI("https://www.reddit.com/r/test/").toURL());
Method getPostMediaURL = RedditRipper.class.getDeclaredMethod("getPostMediaURL", JSONObject.class);
getPostMediaURL.setAccessible(true);

JSONObject post = new JSONObject("{\"url\":\"https://i.imgur.com/image.jpg\"}");

Assertions.assertEquals("https://i.imgur.com/image.jpg", getPostMediaURL.invoke(ripper, post));
}

@Test
@Tag("flaky") // https://github.com/RipMeApp/ripme/issues/253
public void testRedditSubredditRip() throws IOException, URISyntaxException {
Expand Down