2014-05-07 07:12:52 +02:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2014-06-23 04:12:29 +02:00
|
|
|
import java.util.ArrayList;
|
2014-05-07 07:12:52 +02:00
|
|
|
import java.util.HashMap;
|
2014-06-23 04:12:29 +02:00
|
|
|
import java.util.List;
|
2014-05-07 07:12:52 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import org.jsoup.Connection.Method;
|
|
|
|
import org.jsoup.Connection.Response;
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
|
2014-05-07 07:12:52 +02:00
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2014-05-07 07:12:52 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
public class GifyoRipper extends AbstractHTMLRipper {
|
2014-05-07 07:12:52 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
private int page = 0;
|
|
|
|
private Map<String,String> cookies = new HashMap<String,String>();
|
2014-05-07 07:12:52 +02:00
|
|
|
|
|
|
|
public GifyoRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public String getHost() {
|
|
|
|
return "gifyo";
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
|
|
|
return "gifyo.com";
|
2014-05-07 07:12:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Pattern p = Pattern.compile("^https?://[w.]*gifyo.com/([a-zA-Z0-9\\-_]+)/?$");
|
2014-05-07 07:12:52 +02:00
|
|
|
Matcher m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
2014-06-23 04:12:29 +02:00
|
|
|
return m.group(1);
|
2014-05-07 07:12:52 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
throw new MalformedURLException("Gifyo user not found in " + url + ", expected http://gifyo.com/username");
|
2014-05-07 07:12:52 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
|
2014-05-07 07:12:52 +02:00
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public URL sanitizeURL(URL url) throws MalformedURLException {
|
|
|
|
return new URL("http://gifyo.com/" + getGID(url) + "/");
|
2014-05-07 07:12:52 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Document getFirstPage() throws IOException {
|
|
|
|
Response resp = Http.url(this.url)
|
|
|
|
.ignoreContentType()
|
|
|
|
.response();
|
|
|
|
cookies = resp.cookies();
|
2014-05-07 07:12:52 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
Document doc = resp.parse();
|
|
|
|
if (doc.html().contains("profile is private")) {
|
|
|
|
sendUpdate(STATUS.RIP_ERRORED, "User has private profile");
|
|
|
|
throw new IOException("User has private profile");
|
|
|
|
}
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2014-05-07 07:12:52 +02:00
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public Document getNextPage(Document doc) throws IOException {
|
|
|
|
page++;
|
|
|
|
Map<String,String> postData = new HashMap<String,String>();
|
|
|
|
postData.put("cmd", "refreshData");
|
|
|
|
postData.put("view", "gif");
|
|
|
|
postData.put("layout", "grid");
|
|
|
|
postData.put("page", Integer.toString(page));
|
|
|
|
Response resp = Http.url(this.url)
|
|
|
|
.ignoreContentType()
|
|
|
|
.data(postData)
|
|
|
|
.cookies(cookies)
|
|
|
|
.method(Method.POST)
|
|
|
|
.response();
|
|
|
|
cookies.putAll(resp.cookies());
|
|
|
|
Document nextDoc = resp.parse();
|
|
|
|
if (nextDoc.select("div.gif img").size() == 0) {
|
|
|
|
throw new IOException("No more images found");
|
|
|
|
}
|
|
|
|
sleep(2000);
|
|
|
|
return nextDoc;
|
2014-05-07 07:12:52 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
|
2014-05-07 07:12:52 +02:00
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public List<String> getURLsFromPage(Document doc) {
|
|
|
|
List<String> imageURLs = new ArrayList<String>();
|
2015-02-10 08:29:29 +01:00
|
|
|
for (Element image : doc.select("img.profile_gif")) {
|
|
|
|
String imageUrl = image.attr("data-animated");
|
2014-06-23 04:12:29 +02:00
|
|
|
if (imageUrl.startsWith("//")) {
|
|
|
|
imageUrl = "http:" + imageUrl;
|
|
|
|
}
|
|
|
|
imageUrl = imageUrl.replace("/medium/", "/large/");
|
|
|
|
imageUrl = imageUrl.replace("_s.gif", ".gif");
|
|
|
|
imageURLs.add(imageUrl);
|
2014-05-07 07:12:52 +02:00
|
|
|
}
|
2015-02-10 08:29:29 +01:00
|
|
|
logger.debug("Found " + imageURLs.size() + " images");
|
2014-06-23 04:12:29 +02:00
|
|
|
return imageURLs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
addURLToDownload(url);
|
2014-05-07 07:12:52 +02:00
|
|
|
}
|
|
|
|
}
|