2014-03-10 01:11:29 +01:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2016-12-20 09:10:00 +01:00
|
|
|
import java.net.HttpURLConnection;
|
2014-03-10 01:11:29 +01:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2017-12-24 12:45:13 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
2014-03-10 01:11:29 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2016-12-20 09:10:00 +01:00
|
|
|
|
2014-06-14 08:14:24 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2014-03-10 01:11:29 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2016-12-20 09:10:00 +01:00
|
|
|
import org.jsoup.HttpStatusException;
|
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
import com.rarchives.ripme.ripper.AlbumRipper;
|
2014-06-20 13:09:36 +02:00
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2014-03-10 01:11:29 +01:00
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
public class TumblrRipper extends AlbumRipper {
|
2014-03-10 01:11:29 +01:00
|
|
|
|
|
|
|
private static final String DOMAIN = "tumblr.com",
|
2014-08-01 05:49:56 +02:00
|
|
|
HOST = "tumblr",
|
|
|
|
IMAGE_PATTERN = "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)";
|
2016-12-20 09:10:00 +01:00
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
private enum ALBUM_TYPE {
|
|
|
|
SUBDOMAIN,
|
|
|
|
TAG,
|
|
|
|
POST
|
|
|
|
}
|
|
|
|
private ALBUM_TYPE albumType;
|
|
|
|
private String subdomain, tagName, postNumber;
|
2014-08-02 18:27:48 +02:00
|
|
|
|
2018-01-18 03:05:02 +01:00
|
|
|
private static final String TUMBLR_AUTH_CONFIG_KEY = "tumblr.auth";
|
2016-12-20 09:10:00 +01:00
|
|
|
|
|
|
|
private static boolean useDefaultApiKey = false; // fall-back for bad user-specified key
|
2018-01-18 03:05:02 +01:00
|
|
|
private static final List<String> APIKEYS = Arrays.asList("JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX",
|
|
|
|
"FQrwZMCxVnzonv90rgNUJcAk4FpnoS0mYuSuGYqIpM2cFgp9L4",
|
|
|
|
"qpdkY6nMknksfvYAhf2xIHp0iNRLkMlcWShxqzXyFJRxIsZ1Zz");
|
|
|
|
private static int genNum = new Random().nextInt(APIKEYS.size());
|
|
|
|
private static final String API_KEY = APIKEYS.get(genNum); // Select random API key from APIKEYS
|
2016-12-20 09:10:00 +01:00
|
|
|
|
2018-01-18 03:05:02 +01:00
|
|
|
/**
|
2018-04-19 15:14:24 +02:00
|
|
|
* Gets the API key.
|
2018-01-18 03:05:02 +01:00
|
|
|
* Chooses between default/included keys & user specified ones (from the config file).
|
|
|
|
* @return Tumblr API key
|
|
|
|
*/
|
|
|
|
public static String getApiKey() {
|
2017-12-24 12:45:13 +01:00
|
|
|
if (useDefaultApiKey || Utils.getConfigString(TUMBLR_AUTH_CONFIG_KEY, "JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX").equals("JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX")) {
|
|
|
|
logger.info("Using api key: " + API_KEY);
|
2016-12-20 09:10:00 +01:00
|
|
|
return API_KEY;
|
2017-12-24 12:45:13 +01:00
|
|
|
} else {
|
2018-01-18 03:05:02 +01:00
|
|
|
String userDefinedAPIKey = Utils.getConfigString(TUMBLR_AUTH_CONFIG_KEY, "JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX");
|
|
|
|
logger.info("Using user tumblr.auth api key: " + userDefinedAPIKey);
|
|
|
|
return userDefinedAPIKey;
|
2016-12-20 09:10:00 +01:00
|
|
|
}
|
2018-04-19 15:14:24 +02:00
|
|
|
|
2014-06-14 08:14:24 +02:00
|
|
|
}
|
2014-03-10 01:11:29 +01:00
|
|
|
|
|
|
|
public TumblrRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
if (API_KEY == null) {
|
|
|
|
throw new IOException("Could not find tumblr authentication key in configuration");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canRip(URL url) {
|
|
|
|
return url.getHost().endsWith(DOMAIN);
|
|
|
|
}
|
2018-04-19 15:14:24 +02:00
|
|
|
|
2018-01-18 03:05:02 +01:00
|
|
|
/**
|
|
|
|
* Sanitizes URL.
|
|
|
|
* @param url URL to be sanitized.
|
|
|
|
* @return Sanitized URL
|
2018-04-19 15:14:24 +02:00
|
|
|
* @throws MalformedURLException
|
2018-01-18 03:05:02 +01:00
|
|
|
*/
|
2014-03-10 01:11:29 +01:00
|
|
|
@Override
|
|
|
|
public URL sanitizeURL(URL url) throws MalformedURLException {
|
2014-06-14 08:14:24 +02:00
|
|
|
String u = url.toExternalForm();
|
|
|
|
// Convert <FQDN>.tumblr.com/path to <FQDN>/path if needed
|
|
|
|
if (StringUtils.countMatches(u, ".") > 2) {
|
|
|
|
url = new URL(u.replace(".tumblr.com", ""));
|
|
|
|
if (isTumblrURL(url)) {
|
|
|
|
logger.info("Detected tumblr site: " + url);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.info("Not a tumblr site: " + url);
|
|
|
|
}
|
|
|
|
}
|
2014-03-10 01:11:29 +01:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
private boolean isTumblrURL(URL url) {
|
2014-06-14 08:14:24 +02:00
|
|
|
String checkURL = "http://api.tumblr.com/v2/blog/";
|
|
|
|
checkURL += url.getHost();
|
2016-12-20 09:10:00 +01:00
|
|
|
checkURL += "/info?api_key=" + getApiKey();
|
2014-06-14 08:14:24 +02:00
|
|
|
try {
|
2014-06-22 02:08:42 +02:00
|
|
|
JSONObject json = Http.url(checkURL)
|
|
|
|
.getJSON();
|
2014-06-14 08:14:24 +02:00
|
|
|
int status = json.getJSONObject("meta").getInt("status");
|
|
|
|
return status == 200;
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error("Error while checking possible tumblr domain: " + url.getHost(), e);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
@Override
|
|
|
|
public void rip() throws IOException {
|
|
|
|
String[] mediaTypes;
|
2017-12-17 06:51:42 +01:00
|
|
|
boolean exceededRateLimit = false;
|
2014-03-10 01:11:29 +01:00
|
|
|
if (albumType == ALBUM_TYPE.POST) {
|
|
|
|
mediaTypes = new String[] { "post" };
|
|
|
|
} else {
|
|
|
|
mediaTypes = new String[] { "photo", "video" };
|
|
|
|
}
|
|
|
|
int offset;
|
|
|
|
for (String mediaType : mediaTypes) {
|
2014-06-20 13:09:36 +02:00
|
|
|
if (isStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
2017-12-17 06:51:42 +01:00
|
|
|
|
|
|
|
if (exceededRateLimit) {
|
|
|
|
break;
|
|
|
|
}
|
2014-03-10 01:11:29 +01:00
|
|
|
offset = 0;
|
|
|
|
while (true) {
|
2014-06-14 08:14:24 +02:00
|
|
|
if (isStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
2016-12-20 09:10:00 +01:00
|
|
|
|
2017-12-17 06:51:42 +01:00
|
|
|
if (exceededRateLimit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
String apiURL = getTumblrApiURL(mediaType, offset);
|
2014-06-20 13:09:36 +02:00
|
|
|
logger.info("Retrieving " + apiURL);
|
|
|
|
sendUpdate(STATUS.LOADING_RESOURCE, apiURL);
|
2016-12-20 09:10:00 +01:00
|
|
|
|
|
|
|
JSONObject json = null;
|
|
|
|
boolean retry = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
json = Http.url(apiURL).getJSON();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Throwable cause = e.getCause();
|
|
|
|
if (cause instanceof HttpStatusException) {
|
|
|
|
HttpStatusException status = (HttpStatusException)cause;
|
|
|
|
if (status.getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED && !useDefaultApiKey) {
|
|
|
|
retry = true;
|
2017-12-17 06:51:42 +01:00
|
|
|
} else if (status.getStatusCode() == 429) {
|
|
|
|
logger.error("Tumblr rate limit has been exceeded");
|
|
|
|
sendUpdate(STATUS.DOWNLOAD_ERRORED,"Tumblr rate limit has been exceeded");
|
|
|
|
exceededRateLimit = true;
|
|
|
|
break;
|
2016-12-20 09:10:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retry) {
|
|
|
|
useDefaultApiKey = true;
|
|
|
|
String apiKey = getApiKey();
|
|
|
|
|
|
|
|
String message = "401 Unauthorized. Will retry with default Tumblr API key: " + apiKey;
|
|
|
|
logger.info(message);
|
|
|
|
sendUpdate(STATUS.DOWNLOAD_WARN, message);
|
|
|
|
|
|
|
|
Utils.setConfigString(TUMBLR_AUTH_CONFIG_KEY, apiKey); // save the default key to the config
|
|
|
|
|
|
|
|
// retry loading the JSON
|
|
|
|
|
|
|
|
apiURL = getTumblrApiURL(mediaType, offset);
|
|
|
|
logger.info("Retrieving " + apiURL);
|
|
|
|
sendUpdate(STATUS.LOADING_RESOURCE, apiURL);
|
|
|
|
|
|
|
|
json = Http.url(apiURL).getJSON();
|
|
|
|
}
|
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
} catch (InterruptedException e) {
|
2014-03-10 01:12:10 +01:00
|
|
|
logger.error("[!] Interrupted while waiting to load next album:", e);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-20 09:10:00 +01:00
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
if (!handleJSON(json)) {
|
2014-03-10 01:12:10 +01:00
|
|
|
// Returns false if an error occurs and we should stop.
|
2014-03-10 01:11:29 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-12-20 09:10:00 +01:00
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
offset += 20;
|
|
|
|
}
|
2014-06-14 08:14:24 +02:00
|
|
|
if (isStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
2014-03-10 01:11:29 +01:00
|
|
|
}
|
|
|
|
waitForThreads();
|
|
|
|
}
|
2016-12-20 09:10:00 +01:00
|
|
|
|
2014-06-22 02:08:42 +02:00
|
|
|
private boolean handleJSON(JSONObject json) {
|
2014-03-10 01:11:29 +01:00
|
|
|
JSONArray posts, photos;
|
|
|
|
JSONObject post, photo;
|
2014-08-01 05:49:56 +02:00
|
|
|
Pattern p;
|
|
|
|
Matcher m;
|
|
|
|
p = Pattern.compile(IMAGE_PATTERN);
|
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
URL fileURL;
|
|
|
|
|
|
|
|
posts = json.getJSONObject("response").getJSONArray("posts");
|
|
|
|
if (posts.length() == 0) {
|
2014-03-10 01:12:10 +01:00
|
|
|
logger.info(" Zero posts returned.");
|
2014-03-10 01:11:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < posts.length(); i++) {
|
|
|
|
post = posts.getJSONObject(i);
|
|
|
|
if (post.has("photos")) {
|
|
|
|
photos = post.getJSONArray("photos");
|
|
|
|
for (int j = 0; j < photos.length(); j++) {
|
|
|
|
photo = photos.getJSONObject(j);
|
|
|
|
try {
|
2017-12-21 09:40:32 +01:00
|
|
|
if (Utils.getConfigBoolean("tumblr.get_raw_image", false)) {
|
2017-12-24 12:45:13 +01:00
|
|
|
String urlString = photo.getJSONObject("original_size").getString("url").replaceAll("https", "http");
|
|
|
|
urlString = urlString.replaceAll("https?://[a-sA-Z0-9_\\-\\.]*\\.tumblr", "http://data.tumblr");
|
2017-12-21 09:40:32 +01:00
|
|
|
urlString = urlString.replaceAll("_\\d+\\.", "_raw.");
|
|
|
|
fileURL = new URL(urlString);
|
|
|
|
} else {
|
2018-04-19 15:14:24 +02:00
|
|
|
fileURL = new URL(photo.getJSONObject("original_size").getString("url").replaceAll("http:", "https:"));
|
2017-12-21 09:40:32 +01:00
|
|
|
}
|
2014-08-01 05:49:56 +02:00
|
|
|
m = p.matcher(fileURL.toString());
|
2017-05-10 01:13:42 +02:00
|
|
|
if (m.matches()) {
|
2014-08-01 05:49:56 +02:00
|
|
|
addURLToDownload(fileURL);
|
2017-05-10 01:13:42 +02:00
|
|
|
} else {
|
2014-08-01 05:49:56 +02:00
|
|
|
URL redirectedURL = Http.url(fileURL).ignoreContentType().response().url();
|
|
|
|
addURLToDownload(redirectedURL);
|
|
|
|
}
|
2014-03-10 01:11:29 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("[!] Error while parsing photo in " + photo, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (post.has("video_url")) {
|
|
|
|
try {
|
2017-12-16 06:13:35 +01:00
|
|
|
fileURL = new URL(post.getString("video_url").replaceAll("http", "https"));
|
2014-03-10 01:11:29 +01:00
|
|
|
addURLToDownload(fileURL);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("[!] Error while parsing video in " + post, e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (albumType == ALBUM_TYPE.POST) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-20 09:10:00 +01:00
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
private String getTumblrApiURL(String mediaType, int offset) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2016-12-20 09:10:00 +01:00
|
|
|
if (albumType == ALBUM_TYPE.POST) {
|
2014-03-10 01:11:29 +01:00
|
|
|
sb.append("http://api.tumblr.com/v2/blog/")
|
|
|
|
.append(subdomain)
|
2014-06-14 08:14:24 +02:00
|
|
|
.append("/posts?id=")
|
2014-03-10 01:11:29 +01:00
|
|
|
.append(postNumber)
|
|
|
|
.append("&api_key=")
|
2016-12-20 09:10:00 +01:00
|
|
|
.append(getApiKey());
|
2014-03-10 01:11:29 +01:00
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
sb.append("http://api.tumblr.com/v2/blog/")
|
|
|
|
.append(subdomain)
|
2014-06-14 08:14:24 +02:00
|
|
|
.append("/posts/")
|
2014-03-10 01:11:29 +01:00
|
|
|
.append(mediaType)
|
|
|
|
.append("?api_key=")
|
2016-12-20 09:10:00 +01:00
|
|
|
.append(getApiKey())
|
2014-03-10 01:11:29 +01:00
|
|
|
.append("&offset=")
|
|
|
|
.append(offset);
|
|
|
|
if (albumType == ALBUM_TYPE.TAG) {
|
|
|
|
sb.append("&tag=")
|
|
|
|
.append(tagName);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getHost() {
|
|
|
|
return HOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
2017-10-24 16:33:28 +02:00
|
|
|
final String DOMAIN_REGEX = "^https?://([a-zA-Z0-9\\-.]+)";
|
2014-06-14 08:14:24 +02:00
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
Pattern p;
|
|
|
|
Matcher m;
|
2014-06-14 08:14:24 +02:00
|
|
|
|
2014-03-10 01:11:29 +01:00
|
|
|
// Tagged URL
|
2014-06-14 08:14:24 +02:00
|
|
|
p = Pattern.compile(DOMAIN_REGEX + "/tagged/([a-zA-Z0-9\\-%]+).*$");
|
2014-03-10 01:11:29 +01:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
this.albumType = ALBUM_TYPE.TAG;
|
|
|
|
this.subdomain = m.group(1);
|
|
|
|
this.tagName = m.group(2);
|
|
|
|
this.tagName = this.tagName.replace('-', '+').replace("_", "%20");
|
2014-04-25 04:46:53 +02:00
|
|
|
return this.subdomain + "_tag_" + this.tagName.replace("%20", " ");
|
2014-03-10 01:11:29 +01:00
|
|
|
}
|
|
|
|
// Post URL
|
2014-06-14 08:14:24 +02:00
|
|
|
p = Pattern.compile(DOMAIN_REGEX + "/post/([0-9]+).*$");
|
2014-03-10 01:11:29 +01:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
this.albumType = ALBUM_TYPE.POST;
|
|
|
|
this.subdomain = m.group(1);
|
|
|
|
this.postNumber = m.group(2);
|
|
|
|
return this.subdomain + "_post_" + this.postNumber;
|
|
|
|
}
|
|
|
|
// Subdomain-level URL
|
2015-12-21 00:26:26 +01:00
|
|
|
p = Pattern.compile(DOMAIN_REGEX + "/?$");
|
2014-03-10 01:11:29 +01:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
this.albumType = ALBUM_TYPE.SUBDOMAIN;
|
|
|
|
this.subdomain = m.group(1);
|
|
|
|
return this.subdomain;
|
|
|
|
}
|
2014-06-14 08:14:24 +02:00
|
|
|
throw new MalformedURLException("Expected format: http://subdomain[.tumblr.com][/tagged/tag|/post/postno]");
|
2014-03-10 01:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|