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

RedgifsRipper.java - format

This commit is contained in:
MetaPrime
2025-01-06 01:54:07 -08:00
parent 28511a4402
commit ef17b233f2

View File

@@ -1,9 +1,5 @@
package com.rarchives.ripme.ripper.rippers; package com.rarchives.ripme.ripper.rippers;
import com.rarchives.ripme.utils.Http;
import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
@@ -21,8 +17,10 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.json.JSONObject;
import com.rarchives.ripme.ripper.AbstractJSONRipper; import com.rarchives.ripme.ripper.AbstractJSONRipper;
import com.rarchives.ripme.utils.Http;
public class RedgifsRipper extends AbstractJSONRipper { public class RedgifsRipper extends AbstractJSONRipper {
@@ -34,10 +32,14 @@ public class RedgifsRipper extends AbstractJSONRipper {
private static final String SEARCH_ENDPOINT = "https://api.redgifs.com/v2/search/%s"; private static final String SEARCH_ENDPOINT = "https://api.redgifs.com/v2/search/%s";
private static final String TAGS_ENDPOINT = "https://api.redgifs.com/v2/gifs/search"; private static final String TAGS_ENDPOINT = "https://api.redgifs.com/v2/gifs/search";
private static final String TEMPORARY_AUTH_ENDPOINT = "https://api.redgifs.com/v2/auth/temporary"; private static final String TEMPORARY_AUTH_ENDPOINT = "https://api.redgifs.com/v2/auth/temporary";
private static final Pattern PROFILE_PATTERN = Pattern.compile("^https?://[a-zA-Z0-9.]*redgifs\\.com/users/([a-zA-Z0-9_.-]+).*$"); private static final Pattern PROFILE_PATTERN = Pattern
private static final Pattern SEARCH_PATTERN = Pattern.compile("^https?:\\/\\/[a-zA-Z0-9.]*redgifs\\.com\\/search(?:\\/[a-zA-Z]+)?\\?.*?query=([a-zA-Z0-9-_+%]+).*$"); .compile("^https?://[a-zA-Z0-9.]*redgifs\\.com/users/([a-zA-Z0-9_.-]+).*$");
private static final Pattern TAGS_PATTERN = Pattern.compile("^https?:\\/\\/[a-zA-Z0-9.]*redgifs\\.com\\/gifs\\/([a-zA-Z0-9_.,-]+).*$"); private static final Pattern SEARCH_PATTERN = Pattern.compile(
private static final Pattern SINGLETON_PATTERN = Pattern.compile("^https?://[a-zA-Z0-9.]*redgifs\\.com/watch/([a-zA-Z0-9_-]+).*$"); "^https?:\\/\\/[a-zA-Z0-9.]*redgifs\\.com\\/search(?:\\/[a-zA-Z]+)?\\?.*?query=([a-zA-Z0-9-_+%]+).*$");
private static final Pattern TAGS_PATTERN = Pattern
.compile("^https?:\\/\\/[a-zA-Z0-9.]*redgifs\\.com\\/gifs\\/([a-zA-Z0-9_.,-]+).*$");
private static final Pattern SINGLETON_PATTERN = Pattern
.compile("^https?://[a-zA-Z0-9.]*redgifs\\.com/watch/([a-zA-Z0-9_-]+).*$");
/** /**
* Keep a single auth token for the complete lifecycle of the app. * Keep a single auth token for the complete lifecycle of the app.
@@ -55,7 +57,9 @@ public class RedgifsRipper extends AbstractJSONRipper {
} }
@Override @Override
public String getDomain() { return "redgifs.com"; } public String getDomain() {
return "redgifs.com";
}
@Override @Override
public String getHost() { public String getHost() {
@@ -137,7 +141,8 @@ public class RedgifsRipper extends AbstractJSONRipper {
if (m.matches()) { if (m.matches()) {
var sText = m.group(1); var sText = m.group(1);
if (sText == null || sText.isBlank()) { if (sText == null || sText.isBlank()) {
throw new MalformedURLException(String.format("Expected redgifs.com/search?query=searchtext\n Got %s", url)); throw new MalformedURLException(
String.format("Expected redgifs.com/search?query=searchtext\n Got %s", url));
} }
sText = URLDecoder.decode(sText, StandardCharsets.UTF_8); sText = URLDecoder.decode(sText, StandardCharsets.UTF_8);
sText = sText.replaceAll("[^A-Za-z0-9_-]", "-"); sText = sText.replaceAll("[^A-Za-z0-9_-]", "-");
@@ -226,9 +231,9 @@ public class RedgifsRipper extends AbstractJSONRipper {
return result; return result;
} }
/** /**
* Get all images for a gif url with multiple images * Get all images for a gif url with multiple images
*
* @param galleryID gallery id * @param galleryID gallery id
* @param gifID gif id with multiple images for logging * @param gifID gif id with multiple images for logging
* @return List<String> * @return List<String>
@@ -239,7 +244,8 @@ public class RedgifsRipper extends AbstractJSONRipper {
return list; return list;
} }
try { try {
var json = Http.url(String.format(GALLERY_ENDPOINT, galleryID)).header("Authorization", "Bearer " + authToken).getJSON(); var json = Http.url(String.format(GALLERY_ENDPOINT, galleryID))
.header("Authorization", "Bearer " + authToken).getJSON();
for (var gif : json.getJSONArray("gifs")) { for (var gif : json.getJSONArray("gifs")) {
var hdURL = ((JSONObject) gif).getJSONObject("urls").getString("hd"); var hdURL = ((JSONObject) gif).getJSONObject("urls").getString("hd");
list.add(hdURL); list.add(hdURL);
@@ -249,9 +255,11 @@ public class RedgifsRipper extends AbstractJSONRipper {
} }
return list; return list;
} }
/** /**
* Static helper method for retrieving video URLs for usage in RipUtils. * Static helper method for retrieving video URLs for usage in RipUtils.
* Most of the code is lifted from getFirstPage and getURLsFromJSON * Most of the code is lifted from getFirstPage and getURLsFromJSON
*
* @param url URL to redgif page * @param url URL to redgif page
* @return URL to video * @return URL to video
* @throws IOException * @throws IOException
@@ -276,9 +284,9 @@ public class RedgifsRipper extends AbstractJSONRipper {
return gif.getJSONObject("urls").getString("hd"); return gif.getJSONObject("urls").getString("hd");
} }
/** /**
* Fetch a temorary auth token for the rip * Fetch a temorary auth token for the rip
*
* @throws IOException * @throws IOException
*/ */
private static void fetchAuthToken() throws IOException { private static void fetchAuthToken() throws IOException {
@@ -289,11 +297,15 @@ public class RedgifsRipper extends AbstractJSONRipper {
} }
/** /**
* Map browser url query params to search or tags endpoint query params and return the complete url. * Map browser url query params to search or tags endpoint query params and
* return the complete url.
* *
* Search text for search url comes from the query params, whereas search text for tags url comes from the path. * Search text for search url comes from the query params, whereas search text
* for tags url comes from the path.
*
* Tab type for search url comes from the path whereas, tab type for tags url
* comes from query params.
* *
* Tab type for search url comes from the path whereas, tab type for tags url comes from query params.
* @return Search or tags endpoint url * @return Search or tags endpoint url
*/ */
private URL getSearchOrTagsURL() throws IOException, URISyntaxException { private URL getSearchOrTagsURL() throws IOException, URISyntaxException {
@@ -355,7 +367,8 @@ public class RedgifsRipper extends AbstractJSONRipper {
case "gifs" -> tabType = "gifs"; case "gifs" -> tabType = "gifs";
case "images" -> tabType = "images"; case "images" -> tabType = "images";
case "search" -> LOGGER.warn("No tab selected, defaulting to gifs"); case "search" -> LOGGER.warn("No tab selected, defaulting to gifs");
default -> LOGGER.warn(String.format("Unsupported search tab %s, defaulting to gifs", subpaths[subpaths.length-1])); default -> LOGGER.warn(String.format("Unsupported search tab %s, defaulting to gifs",
subpaths[subpaths.length - 1]));
} }
} }
uri = new URIBuilder(String.format(SEARCH_ENDPOINT, tabType)); uri = new URIBuilder(String.format(SEARCH_ENDPOINT, tabType));