DeviantartRipper can now rip more than one page again

This commit is contained in:
cyian-1756 2018-06-29 21:09:06 -04:00
parent e83032906d
commit 17a3e4eb50

View File

@ -1,6 +1,6 @@
package com.rarchives.ripme.ripper.rippers; package com.rarchives.ripme.ripper.rippers;
import com.rarchives.ripme.ripper.AbstractHTMLRipper; import com.rarchives.ripme.ripper.AbstractJSONRipper;
import com.rarchives.ripme.utils.Base64; import com.rarchives.ripme.utils.Base64;
import com.rarchives.ripme.utils.Http; import com.rarchives.ripme.utils.Http;
import com.rarchives.ripme.utils.RipUtils; import com.rarchives.ripme.utils.RipUtils;
@ -18,15 +18,23 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.jsoup.Connection.Method;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Connection.Response; import org.jsoup.Connection.Response;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
public class DeviantartRipper extends AbstractHTMLRipper {
public class DeviantartRipper extends AbstractJSONRipper {
String requestID;
String galleryID;
String username;
String baseApiUrl = "https://www.deviantart.com/dapi/v1/gallery/";
String csrf;
Map<String, String> pageCookies = new HashMap<>();
private static final int PAGE_SLEEP_TIME = 3000, private static final int PAGE_SLEEP_TIME = 3000,
IMAGE_SLEEP_TIME = 2000; IMAGE_SLEEP_TIME = 2000;
@ -50,10 +58,10 @@ public class DeviantartRipper extends AbstractHTMLRipper {
public String getDomain() { public String getDomain() {
return "deviantart.com"; return "deviantart.com";
} }
@Override // @Override
public boolean hasDescriptionSupport() { // public boolean hasDescriptionSupport() {
return true; // return true;
} // }
@Override @Override
public URL sanitizeURL(URL url) throws MalformedURLException { public URL sanitizeURL(URL url) throws MalformedURLException {
String u = url.toExternalForm(); String u = url.toExternalForm();
@ -120,7 +128,7 @@ public class DeviantartRipper extends AbstractHTMLRipper {
* @throws IOException * @throws IOException
*/ */
@Override @Override
public Document getFirstPage() throws IOException { public JSONObject getFirstPage() throws IOException {
// Base64 da login // Base64 da login
// username: Z3JhYnB5 // username: Z3JhYnB5
@ -133,124 +141,103 @@ public class DeviantartRipper extends AbstractHTMLRipper {
cookies.put("agegate_state","1"); // Bypasses the age gate cookies.put("agegate_state","1"); // Bypasses the age gate
} }
return Http.url(this.url) Response res = Http.url(this.url)
.cookies(cookies) .cookies(cookies)
.get(); .response();
Document page = res.parse();
JSONObject firstPageJSON = getFirstPageJSON(page);
requestID = firstPageJSON.getJSONObject("dapx").getString("requestid");
galleryID = page.select("input[name=set]").attr("value");
username = page.select("div.tt-tv150").attr("username");
csrf = firstPageJSON.getString("csrf");
pageCookies = res.cookies();
return requestPage(0, galleryID, username, requestID, csrf, pageCookies);
} }
/** private JSONObject requestPage(int offset, String galleryID, String username, String requestID, String csfr, Map<String, String> c) {
* LOGGER.debug("offset: " + Integer.toString(offset));
* @param page LOGGER.debug("galleryID: " + galleryID);
* @param id LOGGER.debug("username: " + username);
* @return LOGGER.debug("requestID: " + requestID);
*/ String url = baseApiUrl + galleryID + "?iid=" + requestID;
private String jsonToImage(Document page, String id) {
Elements js = page.select("script[type=\"text/javascript\"]");
for (Element tag : js) {
if (tag.html().contains("window.__pageload")) {
try { try {
String script = tag.html(); Document doc = Http.url(url).cookies(c).data("username", username).data("offset", Integer.toString(offset))
script = script.substring(script.indexOf("window.__pageload")); .data("limit", "24").data("_csrf", csfr).data("id", requestID)
if (!script.contains(id)) { .ignoreContentType().post();
continue; return new JSONObject(doc.body().text());
} catch (IOException e) {
LOGGER.error("Got error trying to get page: " + e.getMessage());
e.printStackTrace();
return null;
} }
script = script.substring(script.indexOf(id));
// first },"src":"url" after id
script = script.substring(script.indexOf("},\"src\":\"") + 9, script.indexOf("\",\"type\""));
return script.replace("\\/", "/");
} catch (StringIndexOutOfBoundsException e) {
LOGGER.debug("Unable to get json link from " + page.location());
} }
private JSONObject getFirstPageJSON(Document doc) {
for (Element js : doc.select("script")) {
LOGGER.info(js.html());
if (js.html().contains("requestid")) {
String json = js.html().replaceAll("window.__initial_body_data=", "").replaceAll("\\);", "")
.replaceAll(";__wake\\(.+", "");
LOGGER.info("json: " + json);
JSONObject j = new JSONObject(json);
return j;
} }
} }
return null; return null;
} }
@Override @Override
public List<String> getURLsFromPage(Document page) { public List<String> getURLsFromJSON(JSONObject json) {
List<String> imageURLs = new ArrayList<>(); List<String> imageURLs = new ArrayList<>();
LOGGER.info(json);
JSONArray results = json.getJSONObject("content").getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
LOGGER.info(results.getJSONObject(i).toString());
Document doc = Jsoup.parseBodyFragment(results.getJSONObject(i).getString("html"));
try {
String imageURL = doc.select("span").first().attr("data-super-full-img");
if (!imageURL.isEmpty()) {
imageURLs.add(imageURL);
}
} catch (NullPointerException e) {
LOGGER.info(i + " does not contain any images");
}
// Iterate over all thumbnails
for (Element thumb : page.select("div.zones-container span.thumb")) {
if (isStopped()) {
break;
}
Element img = thumb.select("img").get(0);
if (img.attr("transparent").equals("false")) {
continue; // a.thumbs to other albums are invisible
}
// Get full-sized image via helper methods
String fullSize = null;
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")) {
fullSize = smallToFull(img.attr("src"), spanUrl);
}
if (fullSize == null && fullSize1 != null) {
fullSize = fullSize1;
}
}
if (fullSize == null) {
if (thumb.attr("data-super-full-img") != null) {
fullSize = thumb.attr("data-super-full-img");
} else if (thumb.attr("data-super-img") != null) {
fullSize = thumb.attr("data-super-img");
} else {
continue;
}
}
if (triedURLs.contains(fullSize)) {
LOGGER.warn("Already tried to download " + fullSize);
continue;
}
triedURLs.add(fullSize);
imageURLs.add(fullSize);
if (isThisATest()) {
// Only need one image for a test
break;
}
} }
return imageURLs; return imageURLs;
} }
@Override // @Override
public List<String> getDescriptionsFromPage(Document page) { // public List<String> getDescriptionsFromPage(Document page) {
List<String> textURLs = new ArrayList<>(); // List<String> textURLs = new ArrayList<>();
// Iterate over all thumbnails // // Iterate over all thumbnails
for (Element thumb : page.select("div.zones-container span.thumb")) { // for (Element thumb : page.select("div.zones-container span.thumb")) {
LOGGER.info(thumb.attr("href")); // LOGGER.info(thumb.attr("href"));
if (isStopped()) { // if (isStopped()) {
break; // break;
} // }
Element img = thumb.select("img").get(0); // Element img = thumb.select("img").get(0);
if (img.attr("transparent").equals("false")) { // if (img.attr("transparent").equals("false")) {
continue; // a.thumbs to other albums are invisible // continue; // a.thumbs to other albums are invisible
} // }
textURLs.add(thumb.attr("href")); // textURLs.add(thumb.attr("href"));
//
// }
// return textURLs;
// }
}
return textURLs;
}
@Override @Override
public Document getNextPage(Document page) throws IOException { public JSONObject getNextPage(JSONObject page) throws IOException {
if (isThisATest()) { boolean hasMore = page.getJSONObject("content").getBoolean("has_more");
return null; if (hasMore) {
} return requestPage(page.getJSONObject("content").getInt("next_offset"), galleryID, username, requestID, csrf, pageCookies);
String baseURL = "https://www.deviantart.com/dapi/v1/gallery/";
String id = page.select("div[gmi-name=gallery]").first().attr("gmi-itemid");
baseURL = baseURL + id;
String requestID = getRequestID(page);
Document d = Http.url(baseURL).data("idd", requestID).post();
LOGGER.info(d.html());
return d;
} }
private String getRequestID(Document doc) { throw new IOException("No more pages");
Pattern p = Pattern.compile("requestid\":\"([a-zA-Z0-9]+)\"");
Matcher m = p.matcher(doc.html());
return "590m257da2ea3eea661e272dde2948081c4d";
} }
@Override @Override
@ -299,53 +286,53 @@ public class DeviantartRipper extends AbstractHTMLRipper {
* @param page The gallery page the URL was found on * @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. * @return A String[] with first object being the description, and the second object being image file name if found.
*/ */
@Override // @Override
public String[] getDescription(String url,Document page) { // public String[] getDescription(String url,Document page) {
if (isThisATest()) { // if (isThisATest()) {
return null; // return null;
} // }
try { // try {
// Fetch the image page // // Fetch the image page
Response resp = Http.url(url) // Response resp = Http.url(url)
.referrer(this.url) // .referrer(this.url)
.cookies(cookies) // .cookies(cookies)
.response(); // .response();
cookies.putAll(resp.cookies()); // cookies.putAll(resp.cookies());
//
// Try to find the description // // Try to find the description
Document documentz = resp.parse(); // Document documentz = resp.parse();
Element ele = documentz.select("div.dev-description").first(); // Element ele = documentz.select("div.dev-description").first();
if (ele == null) { // if (ele == null) {
throw new IOException("No description found"); // throw new IOException("No description found");
} // }
documentz.outputSettings(new Document.OutputSettings().prettyPrint(false)); // documentz.outputSettings(new Document.OutputSettings().prettyPrint(false));
ele.select("br").append("\\n"); // ele.select("br").append("\\n");
ele.select("p").prepend("\\n\\n"); // ele.select("p").prepend("\\n\\n");
String fullSize = null; // String fullSize = null;
Element thumb = page.select("div.zones-container span.thumb[href=\"" + url + "\"]").get(0); // Element thumb = page.select("div.zones-container span.thumb[href=\"" + url + "\"]").get(0);
if (!thumb.attr("data-super-full-img").isEmpty()) { // if (!thumb.attr("data-super-full-img").isEmpty()) {
fullSize = thumb.attr("data-super-full-img"); // fullSize = thumb.attr("data-super-full-img");
String[] split = fullSize.split("/"); // String[] split = fullSize.split("/");
fullSize = split[split.length - 1]; // fullSize = split[split.length - 1];
} else { // } else {
String spanUrl = thumb.attr("href"); // String spanUrl = thumb.attr("href");
fullSize = jsonToImage(page,spanUrl.substring(spanUrl.lastIndexOf('-') + 1)); // fullSize = jsonToImage(page,spanUrl.substring(spanUrl.lastIndexOf('-') + 1));
if (fullSize != null) { // if (fullSize != null) {
String[] split = fullSize.split("/"); // String[] split = fullSize.split("/");
fullSize = split[split.length - 1]; // fullSize = split[split.length - 1];
} // }
} // }
if (fullSize == null) { // if (fullSize == null) {
return new String[] {Jsoup.clean(ele.html().replaceAll("\\\\n", System.getProperty("line.separator")), "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false))}; // return new String[] {Jsoup.clean(ele.html().replaceAll("\\\\n", System.getProperty("line.separator")), "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false))};
} // }
fullSize = fullSize.substring(0, fullSize.lastIndexOf(".")); // fullSize = fullSize.substring(0, fullSize.lastIndexOf("."));
return new String[] {Jsoup.clean(ele.html().replaceAll("\\\\n", System.getProperty("line.separator")), "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false)),fullSize}; // return new String[] {Jsoup.clean(ele.html().replaceAll("\\\\n", System.getProperty("line.separator")), "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false)),fullSize};
// TODO Make this not make a newline if someone just types \n into the description. // // TODO Make this not make a newline if someone just types \n into the description.
} catch (IOException ioe) { // } catch (IOException ioe) {
LOGGER.info("Failed to get description at " + url + ": '" + ioe.getMessage() + "'"); // LOGGER.info("Failed to get description at " + url + ": '" + ioe.getMessage() + "'");
return null; // return null;
} // }
} // }
/** /**
* If largest resolution for image at 'thumb' is found, starts downloading * If largest resolution for image at 'thumb' is found, starts downloading