Ripper now can download videos again and no longer uses the same api key every time

This commit is contained in:
cyian-1756 2018-05-31 16:42:06 -04:00
parent 2ecffc3497
commit 9a8888d445

View File

@ -23,8 +23,8 @@ import com.rarchives.ripme.utils.Utils;
public class TumblrRipper extends AlbumRipper { public class TumblrRipper extends AlbumRipper {
private static final String DOMAIN = "tumblr.com", private static final String DOMAIN = "tumblr.com",
HOST = "tumblr", HOST = "tumblr",
IMAGE_PATTERN = "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)"; IMAGE_PATTERN = "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)";
private enum ALBUM_TYPE { private enum ALBUM_TYPE {
SUBDOMAIN, SUBDOMAIN,
@ -37,11 +37,8 @@ public class TumblrRipper extends AlbumRipper {
private static final String TUMBLR_AUTH_CONFIG_KEY = "tumblr.auth"; private static final String TUMBLR_AUTH_CONFIG_KEY = "tumblr.auth";
private static boolean useDefaultApiKey = false; // fall-back for bad user-specified key private static boolean useDefaultApiKey = false; // fall-back for bad user-specified key
private static final List<String> APIKEYS = Arrays.asList("JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX", private static String API_KEY = null;
"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
/** /**
* Gets the API key. * Gets the API key.
@ -49,6 +46,10 @@ public class TumblrRipper extends AlbumRipper {
* @return Tumblr API key * @return Tumblr API key
*/ */
public static String getApiKey() { public static String getApiKey() {
if (API_KEY == null) {
API_KEY = pickRandomApiKey();
}
if (useDefaultApiKey || Utils.getConfigString(TUMBLR_AUTH_CONFIG_KEY, "JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX").equals("JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX")) { if (useDefaultApiKey || Utils.getConfigString(TUMBLR_AUTH_CONFIG_KEY, "JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX").equals("JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX")) {
logger.info("Using api key: " + API_KEY); logger.info("Using api key: " + API_KEY);
return API_KEY; return API_KEY;
@ -60,9 +61,19 @@ public class TumblrRipper extends AlbumRipper {
} }
private static String pickRandomApiKey() {
final List<String> APIKEYS = Arrays.asList("JFNLu3CbINQjRdUvZibXW9VpSEVYYtiPJ86o8YmvgLZIoKyuNX",
"FQrwZMCxVnzonv90rgNUJcAk4FpnoS0mYuSuGYqIpM2cFgp9L4",
"qpdkY6nMknksfvYAhf2xIHp0iNRLkMlcWShxqzXyFJRxIsZ1Zz");
int genNum = new Random().nextInt(APIKEYS.size());
logger.info(genNum);
final String API_KEY = APIKEYS.get(genNum); // Select random API key from APIKEYS
return API_KEY;
}
public TumblrRipper(URL url) throws IOException { public TumblrRipper(URL url) throws IOException {
super(url); super(url);
if (API_KEY == null) { if (getApiKey() == null) {
throw new IOException("Could not find tumblr authentication key in configuration"); throw new IOException("Could not find tumblr authentication key in configuration");
} }
} }
@ -100,7 +111,7 @@ public class TumblrRipper extends AlbumRipper {
checkURL += "/info?api_key=" + getApiKey(); checkURL += "/info?api_key=" + getApiKey();
try { try {
JSONObject json = Http.url(checkURL) JSONObject json = Http.url(checkURL)
.getJSON(); .getJSON();
int status = json.getJSONObject("meta").getInt("status"); int status = json.getJSONObject("meta").getInt("status");
return status == 200; return status == 200;
} catch (IOException e) { } catch (IOException e) {
@ -245,11 +256,11 @@ public class TumblrRipper extends AlbumRipper {
} }
} else if (post.has("video_url")) { } else if (post.has("video_url")) {
try { try {
fileURL = new URL(post.getString("video_url").replaceAll("http", "https")); fileURL = new URL(post.getString("video_url").replaceAll("http:", "https:"));
addURLToDownload(fileURL); addURLToDownload(fileURL);
} catch (Exception e) { } catch (Exception e) {
logger.error("[!] Error while parsing video in " + post, e); logger.error("[!] Error while parsing video in " + post, e);
return true; return true;
} }
} }
if (albumType == ALBUM_TYPE.POST) { if (albumType == ALBUM_TYPE.POST) {
@ -263,24 +274,24 @@ public class TumblrRipper extends AlbumRipper {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (albumType == ALBUM_TYPE.POST) { if (albumType == ALBUM_TYPE.POST) {
sb.append("http://api.tumblr.com/v2/blog/") sb.append("http://api.tumblr.com/v2/blog/")
.append(subdomain) .append(subdomain)
.append("/posts?id=") .append("/posts?id=")
.append(postNumber) .append(postNumber)
.append("&api_key=") .append("&api_key=")
.append(getApiKey()); .append(getApiKey());
return sb.toString(); return sb.toString();
} }
sb.append("http://api.tumblr.com/v2/blog/") sb.append("http://api.tumblr.com/v2/blog/")
.append(subdomain) .append(subdomain)
.append("/posts/") .append("/posts/")
.append(mediaType) .append(mediaType)
.append("?api_key=") .append("?api_key=")
.append(getApiKey()) .append(getApiKey())
.append("&offset=") .append("&offset=")
.append(offset); .append(offset);
if (albumType == ALBUM_TYPE.TAG) { if (albumType == ALBUM_TYPE.TAG) {
sb.append("&tag=") sb.append("&tag=")
.append(tagName); .append(tagName);
} }
return sb.toString(); return sb.toString();
} }