2018-06-26 08:28:14 +02:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
2014-06-20 10:59:01 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2018-06-26 08:28:14 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-06-20 10:59:01 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2018-07-26 17:09:04 +02:00
|
|
|
import com.rarchives.ripme.ripper.AbstractSingleFileRipper;
|
2014-06-20 10:59:01 +02:00
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2014-06-20 10:59:01 +02:00
|
|
|
|
2018-06-26 08:28:14 +02:00
|
|
|
|
2018-07-26 17:09:04 +02:00
|
|
|
public class GfycatRipper extends AbstractSingleFileRipper {
|
2018-06-27 07:25:58 +02:00
|
|
|
|
2014-06-20 10:59:01 +02:00
|
|
|
private static final String HOST = "gfycat.com";
|
|
|
|
|
|
|
|
public GfycatRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
2018-06-26 08:28:14 +02:00
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
|
|
|
return "gfycat.com";
|
|
|
|
}
|
|
|
|
|
2014-06-20 10:59:01 +02:00
|
|
|
@Override
|
|
|
|
public String getHost() {
|
2018-06-26 08:28:14 +02:00
|
|
|
return "gfycat";
|
2014-06-20 10:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canRip(URL url) {
|
|
|
|
return url.getHost().endsWith(HOST);
|
|
|
|
}
|
2017-05-10 00:22:55 +02:00
|
|
|
|
2014-06-20 10:59:01 +02:00
|
|
|
@Override
|
|
|
|
public URL sanitizeURL(URL url) throws MalformedURLException {
|
2018-01-05 23:01:49 +01:00
|
|
|
url = new URL(url.toExternalForm().replace("/gifs/detail", ""));
|
|
|
|
|
2014-06-20 10:59:01 +02:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2018-06-26 08:28:14 +02:00
|
|
|
@Override
|
|
|
|
public Document getFirstPage() throws IOException {
|
|
|
|
return Http.url(url).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
addURLToDownload(url, getPrefix(index));
|
|
|
|
}
|
|
|
|
|
2014-06-20 10:59:01 +02:00
|
|
|
@Override
|
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Pattern p = Pattern.compile("^https?://[wm.]*gfycat\\.com/([a-zA-Z0-9]+).*$");
|
|
|
|
Matcher m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new MalformedURLException(
|
|
|
|
"Expected gfycat.com format:"
|
|
|
|
+ "gfycat.com/id"
|
|
|
|
+ " Got: " + url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-26 08:28:14 +02:00
|
|
|
public List<String> getURLsFromPage(Document doc) {
|
|
|
|
List<String> result = new ArrayList<>();
|
|
|
|
Elements videos = doc.select("source#mp4Source");
|
|
|
|
String vidUrl = videos.first().attr("src");
|
|
|
|
if (vidUrl.startsWith("//")) {
|
|
|
|
vidUrl = "http:" + vidUrl;
|
|
|
|
}
|
|
|
|
result.add(vidUrl);
|
|
|
|
return result;
|
2014-06-25 11:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for retrieving video URLs.
|
|
|
|
* @param url URL to gfycat page
|
|
|
|
* @return URL to video
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public static String getVideoURL(URL url) throws IOException {
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.info("Retrieving " + url.toExternalForm());
|
2018-06-26 08:28:14 +02:00
|
|
|
|
2018-01-05 23:01:49 +01:00
|
|
|
//Sanitize the URL first
|
|
|
|
url = new URL(url.toExternalForm().replace("/gifs/detail", ""));
|
2018-06-26 08:28:14 +02:00
|
|
|
|
2014-06-25 11:03:47 +02:00
|
|
|
Document doc = Http.url(url).get();
|
2015-12-19 15:25:04 +01:00
|
|
|
Elements videos = doc.select("source#mp4Source");
|
2018-05-30 04:48:44 +02:00
|
|
|
if (videos.isEmpty()) {
|
2014-06-20 10:59:01 +02:00
|
|
|
throw new IOException("Could not find source#mp4source at " + url);
|
|
|
|
}
|
|
|
|
String vidUrl = videos.first().attr("src");
|
|
|
|
if (vidUrl.startsWith("//")) {
|
|
|
|
vidUrl = "http:" + vidUrl;
|
|
|
|
}
|
2014-06-25 11:03:47 +02:00
|
|
|
return vidUrl;
|
2014-06-20 10:59:01 +02:00
|
|
|
}
|
|
|
|
}
|