2014-07-25 08:38:31 +02:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
2014-08-03 21:45:11 +02:00
|
|
|
import java.util.HashMap;
|
2014-07-25 08:38:31 +02:00
|
|
|
import java.util.List;
|
2014-08-03 21:45:11 +02:00
|
|
|
import java.util.Map;
|
2014-07-25 08:38:31 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2014-08-03 21:45:11 +02:00
|
|
|
import org.jsoup.Connection.Method;
|
|
|
|
import org.jsoup.Connection.Response;
|
2014-07-25 08:38:31 +02:00
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
|
|
|
|
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
|
2014-08-03 21:45:11 +02:00
|
|
|
import com.rarchives.ripme.utils.Base64;
|
2014-07-25 08:38:31 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
|
|
|
|
|
|
|
public class TwodgalleriesRipper extends AbstractHTMLRipper {
|
|
|
|
|
|
|
|
private int offset = 0;
|
2014-08-03 21:45:11 +02:00
|
|
|
private Map<String,String> cookies = new HashMap<String,String>();
|
2014-07-25 08:38:31 +02:00
|
|
|
|
|
|
|
public TwodgalleriesRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getHost() {
|
|
|
|
return "2dgalleries";
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
|
|
|
return "2dgalleries.com";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Pattern p; Matcher m;
|
|
|
|
|
2014-08-03 21:45:11 +02:00
|
|
|
p = Pattern.compile("^.*2dgalleries.com/artist/([a-zA-Z0-9\\-]+).*$");
|
2014-07-25 08:38:31 +02:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1);
|
|
|
|
}
|
|
|
|
throw new MalformedURLException(
|
|
|
|
"Expected 2dgalleries.com album format: "
|
2014-08-03 21:45:11 +02:00
|
|
|
+ "2dgalleries.com/artist/..."
|
2014-07-25 08:38:31 +02:00
|
|
|
+ " Got: " + url);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getURL(String userid, int offset) {
|
2014-08-03 21:45:11 +02:00
|
|
|
return "http://en.2dgalleries.com/artist/" + userid
|
|
|
|
+ "?timespan=4"
|
|
|
|
+ "&order=1"
|
|
|
|
+ "&catid=2"
|
2014-07-25 08:38:31 +02:00
|
|
|
+ "&offset=" + offset
|
2014-08-03 21:45:11 +02:00
|
|
|
+ "&ajx=1&pager=1";
|
2014-07-25 08:38:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Document getFirstPage() throws IOException {
|
2014-08-03 21:45:11 +02:00
|
|
|
try {
|
|
|
|
login();
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error("Failed to login", e);
|
|
|
|
}
|
2014-07-25 08:38:31 +02:00
|
|
|
String url = getURL(getGID(this.url), offset);
|
2014-08-03 21:45:11 +02:00
|
|
|
return Http.url(url)
|
|
|
|
.cookies(cookies)
|
|
|
|
.get();
|
2014-07-25 08:38:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Document getNextPage(Document doc) throws IOException {
|
2014-08-03 21:45:11 +02:00
|
|
|
offset += 24;
|
2014-07-25 08:38:31 +02:00
|
|
|
String url = getURL(getGID(this.url), offset);
|
|
|
|
sleep(500);
|
2014-08-03 21:45:11 +02:00
|
|
|
Document nextDoc = Http.url(url)
|
|
|
|
.cookies(cookies)
|
|
|
|
.get();
|
|
|
|
if (nextDoc.select("div.hcaption > img").size() == 0) {
|
2014-07-25 08:38:31 +02:00
|
|
|
throw new IOException("No more images to retrieve");
|
|
|
|
}
|
|
|
|
return nextDoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> getURLsFromPage(Document doc) {
|
|
|
|
List<String> imageURLs = new ArrayList<String>();
|
2014-08-03 21:45:11 +02:00
|
|
|
for (Element thumb : doc.select("div.hcaption > img")) {
|
2014-07-25 08:38:31 +02:00
|
|
|
String image = thumb.attr("src");
|
|
|
|
image = image.replace("/200H/", "/");
|
|
|
|
if (image.startsWith("//")) {
|
|
|
|
image = "http:" + image;
|
|
|
|
} else if (image.startsWith("/")) {
|
|
|
|
image = "http://en.2dgalleries.com" + image;
|
|
|
|
}
|
|
|
|
imageURLs.add(image);
|
|
|
|
}
|
|
|
|
return imageURLs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
addURLToDownload(url, getPrefix(index));
|
|
|
|
}
|
2014-08-03 21:45:11 +02:00
|
|
|
|
|
|
|
private void login() throws IOException {
|
|
|
|
Response resp = Http.url(this.url).response();
|
|
|
|
cookies = resp.cookies();
|
|
|
|
String ctoken = resp.parse().select("form > input[name=ctoken]").first().attr("value");
|
|
|
|
|
|
|
|
Map<String,String> postdata = new HashMap<String,String>();
|
|
|
|
postdata.put("user[login]", new String(Base64.decode("cmlwbWU=")));
|
|
|
|
postdata.put("user[password]", new String(Base64.decode("cmlwcGVy")));
|
|
|
|
postdata.put("rememberme", "1");
|
|
|
|
postdata.put("ctoken", ctoken);
|
|
|
|
|
|
|
|
resp = Http.url("http://en.2dgalleries.com/account/login")
|
|
|
|
.referrer("http://en.2dgalleries.com/")
|
|
|
|
.cookies(cookies)
|
|
|
|
.data(postdata)
|
|
|
|
.method(Method.POST)
|
|
|
|
.response();
|
|
|
|
cookies = resp.cookies();
|
|
|
|
}
|
2014-07-25 08:38:31 +02:00
|
|
|
}
|