mirror of
https://github.com/RipMeApp/ripme.git
synced 2025-08-10 16:04:19 +02:00
RedgifsRipper.java - format
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
package com.rarchives.ripme.ripper.rippers;
|
||||
|
||||
import com.rarchives.ripme.utils.Http;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
@@ -21,8 +17,10 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.rarchives.ripme.ripper.AbstractJSONRipper;
|
||||
import com.rarchives.ripme.utils.Http;
|
||||
|
||||
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 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 Pattern PROFILE_PATTERN = Pattern.compile("^https?://[a-zA-Z0-9.]*redgifs\\.com/users/([a-zA-Z0-9_.-]+).*$");
|
||||
private static final Pattern SEARCH_PATTERN = Pattern.compile("^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_-]+).*$");
|
||||
private static final Pattern PROFILE_PATTERN = Pattern
|
||||
.compile("^https?://[a-zA-Z0-9.]*redgifs\\.com/users/([a-zA-Z0-9_.-]+).*$");
|
||||
private static final Pattern SEARCH_PATTERN = Pattern.compile(
|
||||
"^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.
|
||||
@@ -55,7 +57,9 @@ public class RedgifsRipper extends AbstractJSONRipper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDomain() { return "redgifs.com"; }
|
||||
public String getDomain() {
|
||||
return "redgifs.com";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
@@ -137,7 +141,8 @@ public class RedgifsRipper extends AbstractJSONRipper {
|
||||
if (m.matches()) {
|
||||
var sText = m.group(1);
|
||||
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 = sText.replaceAll("[^A-Za-z0-9_-]", "-");
|
||||
@@ -226,9 +231,9 @@ public class RedgifsRipper extends AbstractJSONRipper {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all images for a gif url with multiple images
|
||||
*
|
||||
* @param galleryID gallery id
|
||||
* @param gifID gif id with multiple images for logging
|
||||
* @return List<String>
|
||||
@@ -239,7 +244,8 @@ public class RedgifsRipper extends AbstractJSONRipper {
|
||||
return list;
|
||||
}
|
||||
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")) {
|
||||
var hdURL = ((JSONObject) gif).getJSONObject("urls").getString("hd");
|
||||
list.add(hdURL);
|
||||
@@ -249,9 +255,11 @@ public class RedgifsRipper extends AbstractJSONRipper {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper method for retrieving video URLs for usage in RipUtils.
|
||||
* Most of the code is lifted from getFirstPage and getURLsFromJSON
|
||||
*
|
||||
* @param url URL to redgif page
|
||||
* @return URL to video
|
||||
* @throws IOException
|
||||
@@ -276,9 +284,9 @@ public class RedgifsRipper extends AbstractJSONRipper {
|
||||
return gif.getJSONObject("urls").getString("hd");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetch a temorary auth token for the rip
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
private URL getSearchOrTagsURL() throws IOException, URISyntaxException {
|
||||
@@ -355,7 +367,8 @@ public class RedgifsRipper extends AbstractJSONRipper {
|
||||
case "gifs" -> tabType = "gifs";
|
||||
case "images" -> tabType = "images";
|
||||
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));
|
||||
|
Reference in New Issue
Block a user