diff --git a/src/main/java/com/rarchives/ripme/ripper/rippers/RedGIFSRipper.java b/src/main/java/com/rarchives/ripme/ripper/rippers/RedGIFSRipper.java new file mode 100644 index 000000000..4779506a6 --- /dev/null +++ b/src/main/java/com/rarchives/ripme/ripper/rippers/RedGIFSRipper.java @@ -0,0 +1,159 @@ +package com.rarchives.ripme.ripper.rippers; + + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.rarchives.ripme.ripper.AbstractHTMLRipper; +import org.json.JSONArray; +import org.json.JSONObject; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import com.rarchives.ripme.utils.Http; + + +public class RedGIFSRipper extends AbstractHTMLRipper { + + private static final String HOST = "redgifs.com"; + String username = ""; + String cursor = ""; + String count = "30"; + + + + public RedGIFSRipper(URL url) throws IOException { + super(new URL(url.toExternalForm().split("-")[0].replace("thumbs.", ""))); + } + + @Override + public String getDomain() { + return "redgifs.com"; + } + + @Override + public String getHost() { + return "redgifs"; + } + + @Override + public boolean canRip(URL url) { + return url.getHost().endsWith(HOST); + } + + @Override + public URL sanitizeURL(URL url) throws MalformedURLException { + String sUrl = url.toExternalForm(); + sUrl = sUrl.replace("/gifs/detail", ""); + sUrl = sUrl.replace("/amp", ""); + return new URL(sUrl); + } + + public boolean isProfile() { + Pattern p = Pattern.compile("^https?://[wm.]*redgifs\\.com/watch/@([a-zA-Z0-9]+).*$"); + Matcher m = p.matcher(url.toExternalForm()); + return m.matches(); + } + + @Override + public Document getFirstPage() throws IOException { + if (!isProfile()) { + return Http.url(url).get(); + } else { + username = getGID(url); + return Http.url(new URL("https://api.redgifs.com/v1/users/" + username + "/gfycats")).ignoreContentType().get(); + } + } + + @Override + public void downloadURL(URL url, int index) { + addURLToDownload(url, getPrefix(index)); + } + + @Override + public String getGID(URL url) throws MalformedURLException { + Pattern p = Pattern.compile("^https?://(thumbs\\.|[wm\\.]*)redgifs\\.com/@?([a-zA-Z0-9]+).*$"); + Matcher m = p.matcher(url.toExternalForm()); + + if (m.matches()) + return m.group(2); + + throw new MalformedURLException( + "Expected redgifs.com format: " + + "redgifs.com/watch/id or " + + "thumbs.redgifs.com/id.gif" + + " Got: " + url); + } + + private String stripHTMLTags(String t) { + t = t.replaceAll("\n" + + "
\n" + + " ", ""); + t = t.replaceAll("\n" + + "", ""); + t = t.replaceAll("\n", ""); + t = t.replaceAll("=\"\"", ""); + return t; + } + + @Override + public Document getNextPage(Document doc) throws IOException { + if (cursor.equals("")) { + throw new IOException("No more pages"); + } + return Http.url(new URL("https://api.redgifs.com/v1/users/" + username + "/gfycats?count=" + count + "&cursor=" + cursor)).ignoreContentType().get(); + } + + @Override + public List