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

Undo gfycat changes, again.

This commit is contained in:
Tushar
2019-02-06 13:51:02 +05:30
parent 994dafb217
commit 661514ea98

View File

@@ -10,12 +10,15 @@ 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.AbstractSingleFileRipper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import com.rarchives.ripme.utils.Http; import com.rarchives.ripme.utils.Http;
import com.rarchives.ripme.utils.Utils;
public class GfycatRipper extends AbstractSingleFileRipper { public class GfycatRipper extends AbstractSingleFileRipper {
@@ -65,62 +68,47 @@ public class GfycatRipper extends AbstractSingleFileRipper {
return m.group(1); return m.group(1);
} }
throw new MalformedURLException("Expected gfycat.com format:" + "gfycat.com/id" + " Got: " + url); throw new MalformedURLException(
"Expected gfycat.com format:"
+ "gfycat.com/id"
+ " Got: " + url);
} }
@Override @Override
public List<String> getURLsFromPage(Document doc) { public List<String> getURLsFromPage(Document doc) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
Elements videos = doc.select("video source"); Elements videos = doc.select("script");
String vidUrl = videos.first().attr("src"); for (Element el : videos) {
// Check preference for mp4 over webm/gif. String json = el.html();
if (Utils.getConfigBoolean("prefer.mp4", false)) { if (json.startsWith("{")) {
for (Element e : videos) { JSONObject page = new JSONObject(json);
if (e.hasAttr("src") && e.attr("src").endsWith(".mp4")) { result.add(page.getJSONObject("video").getString("contentUrl"));
vidUrl = e.attr("src");
break;
} }
} }
}
if (vidUrl.startsWith("//")) {
vidUrl = "http:" + vidUrl;
}
result.add(vidUrl);
return result; return result;
} }
/** /**
* Helper method for retrieving video URLs. * Helper method for retrieving video URLs.
* * @param url URL to gfycat page
* @param url
* URL to gfycat page
* @return URL to video * @return URL to video
* @throws IOException * @throws IOException
*/ */
public static String getVideoURL(URL url) throws IOException { public static String getVideoURL(URL url) throws IOException {
LOGGER.info("Retrieving " + url.toExternalForm()); LOGGER.info("Retrieving " + url.toExternalForm());
// Sanitize the URL first //Sanitize the URL first
url = new URL(url.toExternalForm().replace("/gifs/detail", "")); url = new URL(url.toExternalForm().replace("/gifs/detail", ""));
Document doc = Http.url(url).get(); Document doc = Http.url(url).get();
Elements videos = doc.select("video source"); Elements videos = doc.select("script");
if (videos.isEmpty()) { for (Element el : videos) {
throw new IOException("Could not find source at " + url); String json = el.html();
} if (json.startsWith("{")) {
String vidUrl = videos.first().attr("src"); JSONObject page = new JSONObject(json);
// Check preference for mp4 over webm/gif. return page.getJSONObject("video").getString("contentUrl");
if (Utils.getConfigBoolean("prefer.mp4", false)) {
for (Element e : videos) {
if (e.hasAttr("src") && e.attr("src").endsWith(".mp4")) {
vidUrl = e.attr("src");
break;
} }
} }
} throw new IOException();
if (vidUrl.startsWith("//")) {
vidUrl = "http:" + vidUrl;
}
return vidUrl;
} }
} }