mirror of
https://github.com/RipMeApp/ripme.git
synced 2025-08-23 14:02:54 +02:00
Merge pull request #1279 from cyian-1756/gyfcat-profile
Added support for Gfycat profiles
This commit is contained in:
@@ -9,9 +9,8 @@ import java.util.List;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import com.rarchives.ripme.ripper.AbstractSingleFileRipper;
|
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
@@ -20,9 +19,14 @@ import org.jsoup.select.Elements;
|
|||||||
import com.rarchives.ripme.utils.Http;
|
import com.rarchives.ripme.utils.Http;
|
||||||
|
|
||||||
|
|
||||||
public class GfycatRipper extends AbstractSingleFileRipper {
|
public class GfycatRipper extends AbstractHTMLRipper {
|
||||||
|
|
||||||
private static final String HOST = "gfycat.com";
|
private static final String HOST = "gfycat.com";
|
||||||
|
String username = "";
|
||||||
|
String cursor = "";
|
||||||
|
String count = "30";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public GfycatRipper(URL url) throws IOException {
|
public GfycatRipper(URL url) throws IOException {
|
||||||
super(url);
|
super(url);
|
||||||
@@ -49,10 +53,20 @@ public class GfycatRipper extends AbstractSingleFileRipper {
|
|||||||
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
public boolean isProfile() {
|
||||||
|
Pattern p = Pattern.compile("^https?://[wm.]*gfycat\\.com/@([a-zA-Z0-9]+).*$");
|
||||||
|
Matcher m = p.matcher(url.toExternalForm());
|
||||||
|
return m.matches();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Document getFirstPage() throws IOException {
|
public Document getFirstPage() throws IOException {
|
||||||
|
if (!isProfile()) {
|
||||||
return Http.url(url).get();
|
return Http.url(url).get();
|
||||||
|
} else {
|
||||||
|
username = getGID(url);
|
||||||
|
return Http.url(new URL("https://api.gfycat.com/v1/users/" + username + "/gfycats")).ignoreContentType().get();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -62,7 +76,7 @@ public class GfycatRipper extends AbstractSingleFileRipper {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getGID(URL url) throws MalformedURLException {
|
public String getGID(URL url) throws MalformedURLException {
|
||||||
Pattern p = Pattern.compile("^https?://[wm.]*gfycat\\.com/([a-zA-Z0-9]+).*$");
|
Pattern p = Pattern.compile("^https?://[wm.]*gfycat\\.com/@?([a-zA-Z0-9]+).*$");
|
||||||
Matcher m = p.matcher(url.toExternalForm());
|
Matcher m = p.matcher(url.toExternalForm());
|
||||||
if (m.matches()) {
|
if (m.matches()) {
|
||||||
return m.group(1);
|
return m.group(1);
|
||||||
@@ -74,9 +88,36 @@ public class GfycatRipper extends AbstractSingleFileRipper {
|
|||||||
+ " Got: " + url);
|
+ " Got: " + url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String stripHTMLTags(String t) {
|
||||||
|
t = t.replaceAll("<html>\n" +
|
||||||
|
" <head></head>\n" +
|
||||||
|
" <body>", "");
|
||||||
|
t.replaceAll("</body>\n" +
|
||||||
|
"</html>", "");
|
||||||
|
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.gfycat.com/v1/users/" + username + "/gfycats?count=" + count + "&cursor=" + cursor)).ignoreContentType().get();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getURLsFromPage(Document doc) {
|
public List<String> getURLsFromPage(Document doc) {
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
|
if (isProfile()) {
|
||||||
|
JSONObject page = new JSONObject(stripHTMLTags(doc.html()));
|
||||||
|
JSONArray content = page.getJSONArray("gfycats");
|
||||||
|
for (int i = 0; i < content.length(); i++) {
|
||||||
|
result.add(content.getJSONObject(i).getString("mp4Url"));
|
||||||
|
}
|
||||||
|
cursor = page.getString("cursor");
|
||||||
|
} else {
|
||||||
Elements videos = doc.select("script");
|
Elements videos = doc.select("script");
|
||||||
for (Element el : videos) {
|
for (Element el : videos) {
|
||||||
String json = el.html();
|
String json = el.html();
|
||||||
@@ -85,6 +126,7 @@ public class GfycatRipper extends AbstractSingleFileRipper {
|
|||||||
result.add(page.getJSONObject("video").getString("contentUrl"));
|
result.add(page.getJSONObject("video").getString("contentUrl"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -23,4 +23,9 @@ public class GfycatRipperTest extends RippersTest {
|
|||||||
GfycatRipper ripper = new GfycatRipper(new URL("https://gfycat.com/gifs/detail/limitedtestyamericancrow"));
|
GfycatRipper ripper = new GfycatRipper(new URL("https://gfycat.com/gifs/detail/limitedtestyamericancrow"));
|
||||||
testRipper(ripper);
|
testRipper(ripper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testGfycatProfile() throws IOException {
|
||||||
|
GfycatRipper ripper = new GfycatRipper(new URL("https://gfycat.com/@golbanstorage"));
|
||||||
|
testRipper(ripper);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user