2014-03-13 10:26:55 +01:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
2018-04-14 23:45:56 +02:00
|
|
|
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
|
|
|
|
import com.rarchives.ripme.utils.Base64;
|
|
|
|
import com.rarchives.ripme.utils.Http;
|
|
|
|
import com.rarchives.ripme.utils.Utils;
|
2014-03-13 10:26:55 +01:00
|
|
|
import java.io.IOException;
|
2017-04-29 21:35:39 +02:00
|
|
|
import java.net.HttpURLConnection;
|
2014-03-13 10:26:55 +01:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
2014-06-22 02:08:42 +02:00
|
|
|
import java.util.HashSet;
|
2014-03-13 10:26:55 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2014-06-22 02:08:42 +02:00
|
|
|
import java.util.Set;
|
2014-03-13 10:26:55 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
import org.jsoup.Connection.Method;
|
|
|
|
import org.jsoup.Connection.Response;
|
2014-11-30 06:14:57 +01:00
|
|
|
import org.jsoup.Jsoup;
|
2014-03-13 10:26:55 +01:00
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
2014-11-30 06:14:57 +01:00
|
|
|
import org.jsoup.safety.Whitelist;
|
2014-05-17 18:50:07 +02:00
|
|
|
import org.jsoup.select.Elements;
|
2014-03-13 10:26:55 +01:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
public class DeviantartRipper extends AbstractHTMLRipper {
|
2014-03-13 10:26:55 +01:00
|
|
|
|
2015-01-26 06:12:30 +01:00
|
|
|
private static final int PAGE_SLEEP_TIME = 3000,
|
2015-02-08 08:49:21 +01:00
|
|
|
IMAGE_SLEEP_TIME = 2000;
|
2014-03-13 10:26:55 +01:00
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
private Map<String,String> cookies = new HashMap<>();
|
|
|
|
private Set<String> triedURLs = new HashSet<>();
|
2014-06-01 11:02:36 +02:00
|
|
|
|
2014-03-13 10:26:55 +01:00
|
|
|
public DeviantartRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-22 02:08:42 +02:00
|
|
|
public String getHost() {
|
|
|
|
return "deviantart";
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
|
|
|
return "deviantart.com";
|
2014-03-13 10:26:55 +01:00
|
|
|
}
|
2014-11-29 05:59:39 +01:00
|
|
|
@Override
|
|
|
|
public boolean hasDescriptionSupport() {
|
2017-05-15 19:24:36 +02:00
|
|
|
return true;
|
2014-11-29 05:59:39 +01:00
|
|
|
}
|
2014-03-13 10:26:55 +01:00
|
|
|
@Override
|
|
|
|
public URL sanitizeURL(URL url) throws MalformedURLException {
|
|
|
|
String u = url.toExternalForm();
|
2015-01-26 06:12:30 +01:00
|
|
|
|
|
|
|
if (u.replace("/", "").endsWith(".deviantart.com")) {
|
|
|
|
// Root user page, get all albums
|
|
|
|
if (!u.endsWith("/")) {
|
|
|
|
u += "/";
|
|
|
|
}
|
|
|
|
u += "gallery/?";
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
Pattern p = Pattern.compile("^https?://([a-zA-Z0-9\\-]+)\\.deviantart\\.com/favou?rites/([0-9]+)/*?$");
|
2016-04-17 06:26:27 +02:00
|
|
|
Matcher m = p.matcher(url.toExternalForm());
|
|
|
|
if (!m.matches()) {
|
|
|
|
String subdir = "/";
|
|
|
|
if (u.contains("catpath=scraps")) {
|
|
|
|
subdir = "scraps";
|
|
|
|
}
|
|
|
|
u = u.replaceAll("\\?.*", "?catpath=" + subdir);
|
2014-06-22 02:08:42 +02:00
|
|
|
}
|
2014-03-13 10:26:55 +01:00
|
|
|
return new URL(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-22 02:08:42 +02:00
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Pattern p = Pattern.compile("^https?://([a-zA-Z0-9\\-]+)\\.deviantart\\.com(/gallery)?/?(\\?.*)?$");
|
|
|
|
Matcher m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
// Root gallery
|
|
|
|
if (url.toExternalForm().contains("catpath=scraps")) {
|
|
|
|
return m.group(1) + "_scraps";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return m.group(1);
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 16:33:28 +02:00
|
|
|
p = Pattern.compile("^https?://([a-zA-Z0-9\\-]+)\\.deviantart\\.com/gallery/([0-9]+).*$");
|
2014-06-22 02:08:42 +02:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
// Subgallery
|
|
|
|
return m.group(1) + "_" + m.group(2);
|
|
|
|
}
|
2017-10-24 16:33:28 +02:00
|
|
|
p = Pattern.compile("^https?://([a-zA-Z0-9\\-]+)\\.deviantart\\.com/favou?rites/([0-9]+)/.*?$");
|
2016-04-17 06:26:27 +02:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1) + "_faves_" + m.group(2);
|
|
|
|
}
|
2017-10-24 16:33:28 +02:00
|
|
|
p = Pattern.compile("^https?://([a-zA-Z0-9\\-]+)\\.deviantart\\.com/favou?rites/?$");
|
2014-06-26 09:10:44 +02:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
// Subgallery
|
|
|
|
return m.group(1) + "_faves";
|
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
throw new MalformedURLException("Expected URL format: http://username.deviantart.com/[/gallery/#####], got: " + url);
|
|
|
|
}
|
2014-05-16 07:44:57 +02:00
|
|
|
|
2018-04-14 23:45:56 +02:00
|
|
|
/**
|
|
|
|
* Gets first page.
|
|
|
|
* Will determine if login is supplied,
|
|
|
|
* if there is a login, then login and add that login cookies.
|
|
|
|
* Otherwise, just bypass the age gate with an anonymous flag.
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2014-06-22 02:08:42 +02:00
|
|
|
@Override
|
|
|
|
public Document getFirstPage() throws IOException {
|
2018-04-14 23:45:56 +02:00
|
|
|
|
|
|
|
//Test to see if there is a login:
|
|
|
|
String username = Utils.getConfigString("deviantart.username", new String(Base64.decode("Z3JhYnB5")));
|
|
|
|
String password = Utils.getConfigString("deviantart.password", new String(Base64.decode("ZmFrZXJz")));
|
|
|
|
|
|
|
|
if(username == null || password == null) {
|
|
|
|
logger.debug("No DeviantArt login provided.");
|
2017-04-29 21:35:39 +02:00
|
|
|
cookies.put("agegate_state","1"); // Bypasses the age gate
|
2018-04-14 23:45:56 +02:00
|
|
|
} else {
|
|
|
|
// Attempt Login
|
|
|
|
try {
|
|
|
|
cookies = loginToDeviantart();
|
2018-04-14 23:55:45 +02:00
|
|
|
} catch (IOException e) {
|
2018-04-14 23:45:56 +02:00
|
|
|
logger.warn("Failed to login: ", e);
|
|
|
|
cookies.put("agegate_state","1"); // Bypasses the age gate
|
|
|
|
}
|
2014-06-01 11:02:36 +02:00
|
|
|
}
|
2018-04-14 23:45:56 +02:00
|
|
|
|
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
return Http.url(this.url)
|
|
|
|
.cookies(cookies)
|
|
|
|
.get();
|
|
|
|
}
|
2018-04-14 23:45:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param page
|
|
|
|
* @param id
|
|
|
|
* @return
|
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
private String jsonToImage(Document page, String id) {
|
2017-04-27 06:13:11 +02:00
|
|
|
Elements js = page.select("script[type=\"text/javascript\"]");
|
|
|
|
for (Element tag : js) {
|
|
|
|
if (tag.html().contains("window.__pageload")) {
|
2017-04-29 21:35:39 +02:00
|
|
|
try {
|
|
|
|
String script = tag.html();
|
|
|
|
script = script.substring(script.indexOf("window.__pageload"));
|
2017-10-24 16:33:28 +02:00
|
|
|
if (!script.contains(id)) {
|
2017-04-29 21:35:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
script = script.substring(script.indexOf(id));
|
2017-05-15 19:24:36 +02:00
|
|
|
// first },"src":"url" after id
|
|
|
|
script = script.substring(script.indexOf("},\"src\":\"") + 9, script.indexOf("\",\"type\""));
|
2017-04-29 21:35:39 +02:00
|
|
|
return script.replace("\\/", "/");
|
|
|
|
} catch (StringIndexOutOfBoundsException e) {
|
|
|
|
logger.debug("Unable to get json link from " + page.location());
|
|
|
|
}
|
2017-04-27 06:13:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
@Override
|
|
|
|
public List<String> getURLsFromPage(Document page) {
|
2017-10-24 16:33:28 +02:00
|
|
|
List<String> imageURLs = new ArrayList<>();
|
2014-05-26 09:31:58 +02:00
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
// Iterate over all thumbnails
|
2017-04-26 03:17:26 +02:00
|
|
|
for (Element thumb : page.select("div.zones-container span.thumb")) {
|
2014-06-22 02:08:42 +02:00
|
|
|
if (isStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Element img = thumb.select("img").get(0);
|
|
|
|
if (img.attr("transparent").equals("false")) {
|
|
|
|
continue; // a.thumbs to other albums are invisible
|
2014-05-16 07:44:57 +02:00
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
// Get full-sized image via helper methods
|
|
|
|
String fullSize = null;
|
2017-05-15 19:24:36 +02:00
|
|
|
if (thumb.attr("data-super-full-img").contains("//orig")) {
|
|
|
|
fullSize = thumb.attr("data-super-full-img");
|
|
|
|
} else {
|
|
|
|
String spanUrl = thumb.attr("href");
|
|
|
|
String fullSize1 = jsonToImage(page,spanUrl.substring(spanUrl.lastIndexOf('-') + 1));
|
|
|
|
if (fullSize1 == null || !fullSize1.contains("//orig")) {
|
2017-04-29 21:35:39 +02:00
|
|
|
fullSize = smallToFull(img.attr("src"), spanUrl);
|
2017-05-15 19:24:36 +02:00
|
|
|
}
|
|
|
|
if (fullSize == null && fullSize1 != null) {
|
2017-04-29 21:35:39 +02:00
|
|
|
fullSize = fullSize1;
|
|
|
|
}
|
2017-05-15 19:24:36 +02:00
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
if (fullSize == null) {
|
2017-05-15 19:24:36 +02:00
|
|
|
if (thumb.attr("data-super-full-img") != null) {
|
|
|
|
fullSize = thumb.attr("data-super-full-img");
|
2017-04-29 21:35:39 +02:00
|
|
|
} else if (thumb.attr("data-super-img") != null) {
|
2017-05-15 19:24:36 +02:00
|
|
|
fullSize = thumb.attr("data-super-img");
|
2017-04-29 21:35:39 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-13 10:26:55 +01:00
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
if (triedURLs.contains(fullSize)) {
|
|
|
|
logger.warn("Already tried to download " + fullSize);
|
|
|
|
continue;
|
2014-05-16 07:44:57 +02:00
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
triedURLs.add(fullSize);
|
|
|
|
imageURLs.add(fullSize);
|
2015-02-06 08:58:17 +01:00
|
|
|
|
|
|
|
if (isThisATest()) {
|
|
|
|
// Only need one image for a test
|
|
|
|
break;
|
|
|
|
}
|
2014-03-13 10:26:55 +01:00
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
return imageURLs;
|
2014-03-13 10:26:55 +01:00
|
|
|
}
|
2014-11-29 05:59:39 +01:00
|
|
|
@Override
|
|
|
|
public List<String> getDescriptionsFromPage(Document page) {
|
2017-10-24 16:33:28 +02:00
|
|
|
List<String> textURLs = new ArrayList<>();
|
2014-11-29 05:59:39 +01:00
|
|
|
// Iterate over all thumbnails
|
2017-04-26 04:18:03 +02:00
|
|
|
for (Element thumb : page.select("div.zones-container span.thumb")) {
|
|
|
|
logger.info(thumb.attr("href"));
|
2014-11-29 05:59:39 +01:00
|
|
|
if (isStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Element img = thumb.select("img").get(0);
|
|
|
|
if (img.attr("transparent").equals("false")) {
|
|
|
|
continue; // a.thumbs to other albums are invisible
|
|
|
|
}
|
|
|
|
textURLs.add(thumb.attr("href"));
|
2017-04-26 04:18:03 +02:00
|
|
|
|
2014-11-29 05:59:39 +01:00
|
|
|
}
|
|
|
|
return textURLs;
|
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
@Override
|
|
|
|
public Document getNextPage(Document page) throws IOException {
|
2015-02-06 08:58:17 +01:00
|
|
|
if (isThisATest()) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-04-27 07:18:42 +02:00
|
|
|
Elements nextButtons = page.select("link[rel=\"next\"]");
|
2014-06-22 02:08:42 +02:00
|
|
|
if (nextButtons.size() == 0) {
|
2017-04-27 07:18:42 +02:00
|
|
|
if (page.select("link[rel=\"prev\"]").size() == 0) {
|
|
|
|
throw new IOException("No next page found");
|
|
|
|
} else {
|
|
|
|
throw new IOException("Hit end of pages");
|
|
|
|
}
|
2014-05-17 07:20:06 +02:00
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
Element a = nextButtons.first();
|
|
|
|
String nextPage = a.attr("href");
|
|
|
|
if (nextPage.startsWith("/")) {
|
|
|
|
nextPage = "http://" + this.url.getHost() + nextPage;
|
|
|
|
}
|
2015-01-26 06:12:30 +01:00
|
|
|
if (!sleep(PAGE_SLEEP_TIME)) {
|
2014-06-22 02:08:42 +02:00
|
|
|
throw new IOException("Interrupted while waiting to load next page: " + nextPage);
|
|
|
|
}
|
|
|
|
logger.info("Found next page: " + nextPage);
|
|
|
|
return Http.url(nextPage)
|
|
|
|
.cookies(cookies)
|
|
|
|
.get();
|
2014-05-17 07:20:06 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 08:49:21 +01:00
|
|
|
@Override
|
|
|
|
public boolean keepSortOrder() {
|
|
|
|
// Don't keep sort order (do not add prefixes).
|
|
|
|
// Causes file duplication, as outlined in https://github.com/4pr0n/ripme/issues/113
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
addURLToDownload(url, getPrefix(index), "", this.url.toExternalForm(), cookies);
|
2015-01-26 06:12:30 +01:00
|
|
|
sleep(IMAGE_SLEEP_TIME);
|
2014-05-17 07:20:06 +02:00
|
|
|
}
|
|
|
|
|
2014-05-26 09:31:58 +02:00
|
|
|
/**
|
|
|
|
* Tries to get full size image from thumbnail URL
|
|
|
|
* @param thumb Thumbnail URL
|
|
|
|
* @param throwException Whether or not to throw exception when full size image isn't found
|
|
|
|
* @return Full-size image URL
|
|
|
|
* @throws Exception If it can't find the full-size URL
|
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
private static String thumbToFull(String thumb, boolean throwException) throws Exception {
|
2014-03-13 10:26:55 +01:00
|
|
|
thumb = thumb.replace("http://th", "http://fc");
|
2017-10-24 16:33:28 +02:00
|
|
|
List<String> fields = new ArrayList<>(Arrays.asList(thumb.split("/")));
|
2014-03-13 10:26:55 +01:00
|
|
|
fields.remove(4);
|
2014-05-17 18:50:07 +02:00
|
|
|
if (!fields.get(4).equals("f") && throwException) {
|
|
|
|
// Not a full-size image
|
|
|
|
throw new Exception("Can't get full size image from " + thumb);
|
|
|
|
}
|
2014-03-13 10:26:55 +01:00
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
result.append("/");
|
|
|
|
}
|
|
|
|
result.append(fields.get(i));
|
|
|
|
}
|
|
|
|
return result.toString();
|
|
|
|
}
|
2017-05-15 19:24:36 +02:00
|
|
|
|
2014-11-29 05:59:39 +01:00
|
|
|
/**
|
|
|
|
* Attempts to download description for image.
|
|
|
|
* Comes in handy when people put entire stories in their description.
|
|
|
|
* If no description was found, returns null.
|
2017-04-27 06:13:11 +02:00
|
|
|
* @param url The URL the description will be retrieved from
|
|
|
|
* @param page The gallery page the URL was found on
|
|
|
|
* @return A String[] with first object being the description, and the second object being image file name if found.
|
2014-11-29 05:59:39 +01:00
|
|
|
*/
|
|
|
|
@Override
|
2017-04-27 06:13:11 +02:00
|
|
|
public String[] getDescription(String url,Document page) {
|
2015-02-06 08:58:17 +01:00
|
|
|
if (isThisATest()) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-11-29 05:59:39 +01:00
|
|
|
try {
|
|
|
|
// Fetch the image page
|
2017-04-27 06:13:11 +02:00
|
|
|
Response resp = Http.url(url)
|
2014-11-29 05:59:39 +01:00
|
|
|
.referrer(this.url)
|
|
|
|
.cookies(cookies)
|
|
|
|
.response();
|
|
|
|
cookies.putAll(resp.cookies());
|
2014-03-13 10:26:55 +01:00
|
|
|
|
2014-11-30 06:14:57 +01:00
|
|
|
// Try to find the description
|
2017-04-29 21:35:39 +02:00
|
|
|
Document documentz = resp.parse();
|
|
|
|
Element ele = documentz.select("div.dev-description").first();
|
|
|
|
if (ele == null) {
|
2014-11-29 05:59:39 +01:00
|
|
|
throw new IOException("No description found");
|
|
|
|
}
|
2014-11-30 06:14:57 +01:00
|
|
|
documentz.outputSettings(new Document.OutputSettings().prettyPrint(false));
|
|
|
|
ele.select("br").append("\\n");
|
|
|
|
ele.select("p").prepend("\\n\\n");
|
2017-04-27 06:13:11 +02:00
|
|
|
String fullSize = null;
|
|
|
|
Element thumb = page.select("div.zones-container span.thumb[href=\"" + url + "\"]").get(0);
|
|
|
|
if (!thumb.attr("data-super-full-img").isEmpty()) {
|
|
|
|
fullSize = thumb.attr("data-super-full-img");
|
|
|
|
String[] split = fullSize.split("/");
|
|
|
|
fullSize = split[split.length - 1];
|
|
|
|
} else {
|
|
|
|
String spanUrl = thumb.attr("href");
|
|
|
|
fullSize = jsonToImage(page,spanUrl.substring(spanUrl.lastIndexOf('-') + 1));
|
2017-04-29 21:35:39 +02:00
|
|
|
if (fullSize != null) {
|
|
|
|
String[] split = fullSize.split("/");
|
|
|
|
fullSize = split[split.length - 1];
|
|
|
|
}
|
2017-04-27 06:13:11 +02:00
|
|
|
}
|
|
|
|
if (fullSize == null) {
|
|
|
|
return new String[] {Jsoup.clean(ele.html().replaceAll("\\\\n", System.getProperty("line.separator")), "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false))};
|
|
|
|
}
|
2017-04-27 07:18:42 +02:00
|
|
|
fullSize = fullSize.substring(0, fullSize.lastIndexOf("."));
|
2017-04-27 06:13:11 +02:00
|
|
|
return new String[] {Jsoup.clean(ele.html().replaceAll("\\\\n", System.getProperty("line.separator")), "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false)),fullSize};
|
2014-11-30 06:14:57 +01:00
|
|
|
// TODO Make this not make a newline if someone just types \n into the description.
|
2014-11-29 05:59:39 +01:00
|
|
|
} catch (IOException ioe) {
|
2017-04-29 21:35:39 +02:00
|
|
|
logger.info("Failed to get description at " + url + ": '" + ioe.getMessage() + "'");
|
2014-11-29 05:59:39 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 19:24:36 +02:00
|
|
|
|
2014-05-26 09:31:58 +02:00
|
|
|
/**
|
|
|
|
* If largest resolution for image at 'thumb' is found, starts downloading
|
|
|
|
* and returns null.
|
|
|
|
* If it finds a larger resolution on another page, returns the image URL.
|
|
|
|
* @param thumb Thumbnail URL
|
|
|
|
* @param page Page the thumbnail is retrieved from
|
|
|
|
* @return Highest-resolution version of the image based on thumbnail URL and the page.
|
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
private String smallToFull(String thumb, String page) {
|
2014-05-17 18:50:07 +02:00
|
|
|
try {
|
2014-06-01 11:02:36 +02:00
|
|
|
// Fetch the image page
|
2014-06-22 02:08:42 +02:00
|
|
|
Response resp = Http.url(page)
|
|
|
|
.referrer(this.url)
|
|
|
|
.cookies(cookies)
|
|
|
|
.response();
|
|
|
|
cookies.putAll(resp.cookies());
|
2015-03-30 10:32:27 +02:00
|
|
|
Document doc = resp.parse();
|
2017-04-29 21:35:39 +02:00
|
|
|
Elements els = doc.select("img.dev-content-full");
|
|
|
|
String fsimage = null;
|
2015-03-30 10:32:27 +02:00
|
|
|
// Get the largest resolution image on the page
|
|
|
|
if (els.size() > 0) {
|
|
|
|
// Large image
|
2017-04-29 21:35:39 +02:00
|
|
|
fsimage = els.get(0).attr("src");
|
2015-03-30 10:32:27 +02:00
|
|
|
logger.info("Found large-scale: " + fsimage);
|
2017-04-29 21:35:39 +02:00
|
|
|
if (fsimage.contains("//orig")) {
|
|
|
|
return fsimage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Try to find the download button
|
|
|
|
els = doc.select("a.dev-page-download");
|
|
|
|
if (els.size() > 0) {
|
|
|
|
// Full-size image
|
|
|
|
String downloadLink = els.get(0).attr("href");
|
2017-04-30 04:07:49 +02:00
|
|
|
logger.info("Found download button link: " + downloadLink);
|
2017-04-29 21:35:39 +02:00
|
|
|
HttpURLConnection con = (HttpURLConnection) new URL(downloadLink).openConnection();
|
|
|
|
con.setRequestProperty("Referer",this.url.toString());
|
|
|
|
String cookieString = "";
|
|
|
|
for (Map.Entry<String, String> entry : cookies.entrySet()) {
|
|
|
|
cookieString = cookieString + entry.getKey() + "=" + entry.getValue() + "; ";
|
|
|
|
}
|
|
|
|
cookieString = cookieString.substring(0,cookieString.length() - 1);
|
|
|
|
con.setRequestProperty("Cookie",cookieString);
|
2017-10-24 16:33:28 +02:00
|
|
|
con.setRequestProperty("User-Agent", USER_AGENT);
|
2017-04-29 21:35:39 +02:00
|
|
|
con.setInstanceFollowRedirects(true);
|
|
|
|
con.connect();
|
|
|
|
int code = con.getResponseCode();
|
2017-04-30 04:07:49 +02:00
|
|
|
String location = con.getURL().toString();
|
2017-04-29 21:35:39 +02:00
|
|
|
con.disconnect();
|
|
|
|
if (location.contains("//orig")) {
|
|
|
|
fsimage = location;
|
2017-04-30 04:07:49 +02:00
|
|
|
logger.info("Found image download: " + location);
|
2017-04-29 21:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fsimage != null) {
|
2015-03-30 10:32:27 +02:00
|
|
|
return fsimage;
|
2014-05-17 18:50:07 +02:00
|
|
|
}
|
2015-03-30 10:32:27 +02:00
|
|
|
throw new IOException("No download page found");
|
2014-05-17 18:50:07 +02:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
try {
|
|
|
|
logger.info("Failed to get full size download image at " + page + " : '" + ioe.getMessage() + "'");
|
|
|
|
String lessThanFull = thumbToFull(thumb, false);
|
|
|
|
logger.info("Falling back to less-than-full-size image " + lessThanFull);
|
|
|
|
return lessThanFull;
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 10:26:55 +01:00
|
|
|
/**
|
2014-06-01 11:02:36 +02:00
|
|
|
* Logs into deviant art. Required to rip full-size NSFW content.
|
2014-03-13 10:26:55 +01:00
|
|
|
* @return Map of cookies containing session data.
|
|
|
|
*/
|
|
|
|
private Map<String, String> loginToDeviantart() throws IOException {
|
|
|
|
// Populate postData fields
|
2017-10-24 16:33:28 +02:00
|
|
|
Map<String,String> postData = new HashMap<>();
|
2014-06-01 11:02:36 +02:00
|
|
|
String username = Utils.getConfigString("deviantart.username", new String(Base64.decode("Z3JhYnB5")));
|
|
|
|
String password = Utils.getConfigString("deviantart.password", new String(Base64.decode("ZmFrZXJz")));
|
2014-03-13 10:26:55 +01:00
|
|
|
if (username == null || password == null) {
|
|
|
|
throw new IOException("could not find username or password in config");
|
|
|
|
}
|
2014-06-22 02:08:42 +02:00
|
|
|
Response resp = Http.url("http://www.deviantart.com/")
|
|
|
|
.response();
|
2014-03-13 10:26:55 +01:00
|
|
|
for (Element input : resp.parse().select("form#form-login input[type=hidden]")) {
|
|
|
|
postData.put(input.attr("name"), input.attr("value"));
|
|
|
|
}
|
|
|
|
postData.put("username", username);
|
|
|
|
postData.put("password", password);
|
|
|
|
postData.put("remember_me", "1");
|
|
|
|
|
|
|
|
// Send login request
|
2014-06-22 02:08:42 +02:00
|
|
|
resp = Http.url("https://www.deviantart.com/users/login")
|
2014-03-13 10:26:55 +01:00
|
|
|
.userAgent(USER_AGENT)
|
|
|
|
.data(postData)
|
|
|
|
.cookies(resp.cookies())
|
|
|
|
.method(Method.POST)
|
2014-06-22 02:08:42 +02:00
|
|
|
.response();
|
2014-03-13 10:26:55 +01:00
|
|
|
|
|
|
|
// Assert we are logged in
|
|
|
|
if (resp.hasHeader("Location") && resp.header("Location").contains("password")) {
|
|
|
|
// Wrong password
|
2014-06-22 02:08:42 +02:00
|
|
|
throw new IOException("Wrong password");
|
2014-03-13 10:26:55 +01:00
|
|
|
}
|
|
|
|
if (resp.url().toExternalForm().contains("bad_form")) {
|
|
|
|
throw new IOException("Login form was incorrectly submitted");
|
|
|
|
}
|
|
|
|
if (resp.cookie("auth_secure") == null ||
|
|
|
|
resp.cookie("auth") == null) {
|
|
|
|
throw new IOException("No auth_secure or auth cookies received");
|
|
|
|
}
|
|
|
|
// We are logged in, save the cookies
|
|
|
|
return resp.cookies();
|
|
|
|
}
|
|
|
|
}
|