1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-28 08:10:11 +02:00

Logger changes and using config now to store login cookies.

This commit is contained in:
MrPlaygon
2019-02-28 19:45:30 +01:00
parent 82e98abf7f
commit da47ca0c9d

View File

@@ -4,16 +4,18 @@ import com.rarchives.ripme.ripper.AbstractHTMLRipper;
import com.rarchives.ripme.ripper.DownloadThreadPool; import com.rarchives.ripme.ripper.DownloadThreadPool;
import com.rarchives.ripme.ui.RipStatusMessage.STATUS; import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
import com.rarchives.ripme.utils.Http; import com.rarchives.ripme.utils.Http;
import com.rarchives.ripme.utils.Utils;
import java.io.File; import java.io.ByteArrayInputStream;
import java.io.FileInputStream; import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -72,13 +74,14 @@ public class DeviantartRipper extends AbstractHTMLRipper {
private int offset = 0; private int offset = 0;
private boolean usingCatPath = false; private boolean usingCatPath = false;
private int downloadCount = 0; private int downloadCount = 0;
private Map<String, String> cookies; private Map<String, String> cookies = null;
private DownloadThreadPool deviantartThreadPool = new DownloadThreadPool("deviantart"); private DownloadThreadPool deviantartThreadPool = new DownloadThreadPool("deviantart");
private ArrayList<String> names = new ArrayList<String>(); private ArrayList<String> names = new ArrayList<String>();
// Constants // Constants
private final String referer = "https://www.deviantart.com/"; private final String referer = "https://www.deviantart.com/";
private final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0"; private final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0";
private final String utilsKey = "DeviantartLogin.cookies";
@Override @Override
public DownloadThreadPool getThreadPool() { public DownloadThreadPool getThreadPool() {
@@ -116,10 +119,15 @@ public class DeviantartRipper extends AbstractHTMLRipper {
*/ */
private void login() throws IOException { private void login() throws IOException {
File f = new File("DACookie.toDelete"); try {
if (!f.exists()) { String dACookies = Utils.getConfigString(utilsKey, null);
f.createNewFile(); this.cookies = dACookies != null ? deserialize(dACookies) : null;
f.deleteOnExit(); } catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (this.cookies == null) {
LOGGER.info("Log in now");
// Do login now
// Load login page // Load login page
Response res = Http.url("https://www.deviantart.com/users/login").connection().method(Method.GET) Response res = Http.url("https://www.deviantart.com/users/login").connection().method(Method.GET)
@@ -130,8 +138,7 @@ public class DeviantartRipper extends AbstractHTMLRipper {
Element form = doc.getElementById("login"); Element form = doc.getElementById("login");
String token = form.select("input[name=\"validate_token\"]").first().attr("value"); String token = form.select("input[name=\"validate_token\"]").first().attr("value");
String key = form.select("input[name=\"validate_key\"]").first().attr("value"); String key = form.select("input[name=\"validate_key\"]").first().attr("value");
System.out.println( LOGGER.info("Token: " + token + " & Key: " + key);
"------------------------------" + token + " & " + key + "------------------------------");
// Build Login Data // Build Login Data
HashMap<String, String> loginData = new HashMap<String, String>(); HashMap<String, String> loginData = new HashMap<String, String>();
@@ -156,36 +163,13 @@ public class DeviantartRipper extends AbstractHTMLRipper {
// Apply agegate // Apply agegate
this.cookies.put("agegate_state", "1"); this.cookies.put("agegate_state", "1");
// Write Cookie to file for other RipMe Instances or later use
Utils.setConfigString(utilsKey, serialize(new HashMap<String, String>(this.cookies)));
Utils.saveConfig(); // save now because of other instances that might work simultaneously
// Write Cookie to file for other RipMe Instances
try {
FileOutputStream fileOut = new FileOutputStream(f);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this.cookies);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
} }
} else { LOGGER.info("DA Cookies: " + this.cookies);
// When cookie file already exists (from another RipMe instance)
while (this.cookies == null) {
try {
Thread.sleep(2000);
FileInputStream fileIn = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(fileIn);
this.cookies = (Map<String, String>) in.readObject();
in.close();
fileIn.close();
} catch (IOException | ClassNotFoundException | InterruptedException i) {
i.printStackTrace();
}
}
}
System.out.println("------------------------------" + this.cookies + "------------------------------");
} }
/** /**
@@ -199,14 +183,12 @@ public class DeviantartRipper extends AbstractHTMLRipper {
updateCookie(re.cookies()); updateCookie(re.cookies());
Document docu = re.parse(); Document docu = re.parse();
Elements messages = docu.getElementsByClass("message"); Elements messages = docu.getElementsByClass("message");
System.out.println("------------------------------Current Offset: " + this.offset LOGGER.info("Current Offset: " + this.offset);
+ " - More Pages?------------------------------");
if (messages.size() > 0) { if (messages.size() > 0) {
// if message exists -> last page // if message exists -> last page
System.out.println("------------------------------Messages amount: " + messages.size() LOGGER.info("Messages amount: " + messages.size() + " - Next Page does not exists");
+ " - Next Page does not exists------------------------------");
throw new IOException("No more pages"); throw new IOException("No more pages");
} }
@@ -238,9 +220,8 @@ public class DeviantartRipper extends AbstractHTMLRipper {
} }
System.out.println("------------------------------Amount of Images on Page: " + result.size() LOGGER.info("Amount of Images on Page: " + result.size());
+ "------------------------------"); LOGGER.info(page.location());
System.out.println("------------------------------" + page.location() + "------------------------------");
return result; return result;
} }
@@ -251,10 +232,8 @@ public class DeviantartRipper extends AbstractHTMLRipper {
@Override @Override
protected void downloadURL(URL url, int index) { protected void downloadURL(URL url, int index) {
this.downloadCount += 1; this.downloadCount += 1;
System.out.println("------------------------------Download URL Number " + this.downloadCount LOGGER.info("Downloading URL Number " + this.downloadCount);
+ "------------------------------"); LOGGER.info("Deviant Art URL: " + url.toExternalForm());
System.out.println(
"------------------------------DAURL: " + url.toExternalForm() + "------------------------------");
try { try {
Response re = Http.url(urlWithParams(this.offset)).cookies(getDACookie()).referrer(referer) Response re = Http.url(urlWithParams(this.offset)).cookies(getDACookie()).referrer(referer)
.userAgent(userAgent).response(); .userAgent(userAgent).response();
@@ -326,8 +305,7 @@ public class DeviantartRipper extends AbstractHTMLRipper {
} else if (artistM.matches()) { } else if (artistM.matches()) {
albumname = artistM.group(1); albumname = artistM.group(1);
} }
System.out.println("------------------------------Album Name: " + artist + "_" + what + "_" + albumname LOGGER.info("Album Name: " + artist + "_" + what + "_" + albumname);
+ "------------------------------");
return artist + "_" + what + "_" + albumname; return artist + "_" + what + "_" + albumname;
@@ -372,17 +350,49 @@ public class DeviantartRipper extends AbstractHTMLRipper {
private void updateCookie(Map<String, String> m) { private void updateCookie(Map<String, String> m) {
System.out.println("------------------------------Updating Cookies------------------------------"); LOGGER.info("Updating Cookies");
System.out.println( LOGGER.info("Old Cookies: " + this.cookies + " ");
"------------------------------Old Cookies: " + this.cookies + " ------------------------------"); LOGGER.info("New Cookies: " + m + " ");
System.out.println("------------------------------New Cookies: " + m + " ------------------------------");
this.cookies.putAll(m); this.cookies.putAll(m);
this.cookies.put("agegate_state", "1"); this.cookies.put("agegate_state", "1");
System.out.println( LOGGER.info("Merged Cookies: " + this.cookies + " ");
"------------------------------Merged Cookies: " + this.cookies + " ------------------------------");
} }
/**
* Serializes an Object and returns a String ready to store Used to store
* cookies in the config file because the deviantart cookies contain all sort of
* special characters like ; , = : and so on.
*
* @param o Object to serialize
* @return The serialized base64 encoded object
* @throws IOException
*/
private String serialize(Serializable o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
/**
* Recreates the object from the base64 encoded String. Used for Cookies
*
* @param s the base64 encoded string
* @return the Cookie Map
* @throws IOException
* @throws ClassNotFoundException
*/
private Map<String, String> deserialize(String s) throws IOException, ClassNotFoundException {
byte[] data = Base64.getDecoder().decode(s);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
HashMap<String, String> o = (HashMap<String, String>) ois.readObject(); // Unchecked cast here but should never
// be something else
ois.close();
return o;
}
/** /**
* Analyzes an image page like * Analyzes an image page like
* https://www.deviantart.com/kageuri/art/RUBY-568396655 . * https://www.deviantart.com/kageuri/art/RUBY-568396655 .
@@ -420,9 +430,7 @@ public class DeviantartRipper extends AbstractHTMLRipper {
*/ */
private void getFullSizeURL() { private void getFullSizeURL() {
System.out.println("------------------------------------------------------------"); LOGGER.info("Searching max. Resolution for " + url);
System.out.println("------------------------------Searching max. Resolution for " + url
+ "------------------------------");
sendUpdate(STATUS.LOADING_RESOURCE, "Searching max. resolution for " + url); sendUpdate(STATUS.LOADING_RESOURCE, "Searching max. resolution for " + url);
try { try {
Response re = Http.url(url).connection().referrer(referer).userAgent(userAgent).cookies(getDACookie()) Response re = Http.url(url).connection().referrer(referer).userAgent(userAgent).cookies(getDACookie())
@@ -449,8 +457,7 @@ public class DeviantartRipper extends AbstractHTMLRipper {
// Download Button // Download Button
if (downloadButton != null) { if (downloadButton != null) {
System.out.println("------------------------------Download Button found: " LOGGER.info("Download Button found: " + downloadButton.attr("href"));
+ downloadButton.attr("href") + "------------------------------");
Response download = Http.url(downloadButton.attr("href")).connection().cookies(getDACookie()) Response download = Http.url(downloadButton.attr("href")).connection().cookies(getDACookie())
.method(Method.GET).referrer(referer).userAgent(userAgent).ignoreContentType(true) .method(Method.GET).referrer(referer).userAgent(userAgent).ignoreContentType(true)
@@ -459,9 +466,9 @@ public class DeviantartRipper extends AbstractHTMLRipper {
String[] filetypePart = download.header("Content-Disposition").split("\\."); String[] filetypePart = download.header("Content-Disposition").split("\\.");
System.out.println("------------------------------Found Image URL------------------------------"); LOGGER.info("Found Image URL");
System.out.println("------------------------------" + url + "------------------------------"); LOGGER.info(url);
System.out.println("------------------------------" + location + "------------------------------"); LOGGER.info(location);
addURLToDownload(location, "", "", "", new HashMap<String, String>(), addURLToDownload(location, "", "", "", new HashMap<String, String>(),
title + "." + filetypePart[filetypePart.length - 1]); title + "." + filetypePart[filetypePart.length - 1]);
@@ -475,22 +482,19 @@ public class DeviantartRipper extends AbstractHTMLRipper {
String source = ""; String source = "";
if (image == null) { if (image == null) {
System.out.println( LOGGER.error("ERROR on " + url);
"------------------------------!!!ERROR on " + url + " !!!------------------------------");
System.out.println("------------------------------!!!Cookies: " + getDACookie() LOGGER.error("Cookies: " + getDACookie() + " ");
+ " ------------------------------"); LOGGER.error(div);
System.out.println(div); sendUpdate(STATUS.DOWNLOAD_ERRORED, "ERROR at\n" + url);
sendUpdate(STATUS.DOWNLOAD_ERRORED, "!!!ERROR!!!\n" + url);
return; return;
} }
// When it is text art (e.g. story) the only image is the avator (profile // When it is text art (e.g. story) the only image is the avator (profile
// picture) // picture)
if (image.hasClass("avatar")) { if (image.hasClass("avatar")) {
System.out.println( LOGGER.error("No Image found, probably text art");
"------------------------------No Image found, probably text art------------------------------"); LOGGER.error(url);
System.out.println(url);
return; return;
} }
@@ -500,17 +504,16 @@ public class DeviantartRipper extends AbstractHTMLRipper {
// Image page uses scaled down version. Split at /v1/ to receive max size. // Image page uses scaled down version. Split at /v1/ to receive max size.
if (parts.length > 2) { if (parts.length > 2) {
System.out.println( LOGGER.error("Unexpected URL Format");
"------------------------------Unexpected URL Format------------------------------"); sendUpdate(STATUS.DOWNLOAD_ERRORED, "Unexpected URL Format");
sendUpdate(STATUS.DOWNLOAD_WARN, "Unexpected URL Format - Risky Try");
return; return;
} }
String[] tmpParts = parts[0].split("\\."); String[] tmpParts = parts[0].split("\\.");
System.out.println("------------------------------Found Image URL------------------------------"); LOGGER.info("Found Image URL");
System.out.println("------------------------------" + url + "------------------------------"); LOGGER.info(url);
System.out.println("------------------------------" + parts[0] + "------------------------------"); LOGGER.info(parts[0]);
addURLToDownload(new URL(parts[0]), "", "", "", new HashMap<String, String>(), addURLToDownload(new URL(parts[0]), "", "", "", new HashMap<String, String>(),
title + "." + tmpParts[tmpParts.length - 1]); title + "." + tmpParts[tmpParts.length - 1]);
@@ -520,8 +523,7 @@ public class DeviantartRipper extends AbstractHTMLRipper {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println( LOGGER.error("No Full Size URL for: " + url);
"------------------------------No Full Size URL for: " + url + "------------------------------");
sendUpdate(STATUS.DOWNLOAD_ERRORED, "No image found for " + url); sendUpdate(STATUS.DOWNLOAD_ERRORED, "No image found for " + url);
return; return;