mirror of
https://github.com/RipMeApp/ripme.git
synced 2025-08-25 23:06:22 +02:00
Add cookie support for all rippers
This commit is contained in:
@@ -1,15 +1,19 @@
|
|||||||
package com.rarchives.ripme.utils;
|
package com.rarchives.ripme.utils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.ArrayUtils;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.jsoup.Connection;
|
import org.jsoup.Connection;
|
||||||
import org.jsoup.Connection.Method;
|
import org.jsoup.Connection.Method;
|
||||||
import org.jsoup.Connection.Response;
|
import org.jsoup.Connection.Response;
|
||||||
|
import org.jsoup.helper.StringUtil;
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
|
|
||||||
@@ -22,8 +26,8 @@ import com.rarchives.ripme.ripper.AbstractRipper;
|
|||||||
*/
|
*/
|
||||||
public class Http {
|
public class Http {
|
||||||
|
|
||||||
private static final int TIMEOUT = Utils.getConfigInteger("page.timeout", 5 * 1000);
|
private static final int TIMEOUT = Utils.getConfigInteger("page.timeout", 5 * 1000);
|
||||||
private static final Logger logger = Logger.getLogger(Http.class);
|
private static final Logger logger = Logger.getLogger(Http.class);
|
||||||
|
|
||||||
private int retries;
|
private int retries;
|
||||||
private String url;
|
private String url;
|
||||||
@@ -53,6 +57,43 @@ public class Http {
|
|||||||
connection.method(Method.GET);
|
connection.method(Method.GET);
|
||||||
connection.timeout(TIMEOUT);
|
connection.timeout(TIMEOUT);
|
||||||
connection.maxBodySize(0);
|
connection.maxBodySize(0);
|
||||||
|
|
||||||
|
// Extract cookies from config entry:
|
||||||
|
// Example config entry:
|
||||||
|
// cookies.reddit.com = reddit_session=<value>; other_cookie=<value>
|
||||||
|
connection.cookies(cookiesForURL(this.url));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> cookiesForURL(String u) {
|
||||||
|
Map<String, String> cookiesParsed = new HashMap<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
URL parsed = new URL(this.url);
|
||||||
|
String cookieStr = "";
|
||||||
|
|
||||||
|
String[] parts = parsed.getHost().split("\\.");
|
||||||
|
|
||||||
|
// if url is www.reddit.com, we should also use cookies from reddit.com;
|
||||||
|
// this rule is applied for all subdomains (for all rippers); e.g. also
|
||||||
|
// old.reddit.com, new.reddit.com
|
||||||
|
while (parts.length > 1) {
|
||||||
|
// Try to get cookies for this host from config
|
||||||
|
cookieStr = Utils.getConfigString("cookies." + String.join(".", parts), "");
|
||||||
|
if (cookieStr != "") {
|
||||||
|
// we found something, start parsing
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
parts = (String[]) ArrayUtils.remove(parts, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cookieStr != "") {
|
||||||
|
cookiesParsed = RipUtils.getCookiesFromString(cookieStr.trim());
|
||||||
|
}
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
logger.warn("Parsing url while getting cookies" + url, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cookiesParsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
@@ -301,7 +301,7 @@ public class RipUtils {
|
|||||||
Map<String,String> cookies = new HashMap<>();
|
Map<String,String> cookies = new HashMap<>();
|
||||||
for (String pair : line.split(";")) {
|
for (String pair : line.split(";")) {
|
||||||
String[] kv = pair.split("=");
|
String[] kv = pair.split("=");
|
||||||
cookies.put(kv[0], kv[1]);
|
cookies.put(kv[0].trim(), kv[1]);
|
||||||
}
|
}
|
||||||
return cookies;
|
return cookies;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user