Twitter: added support for video and multiple-image tweets (#478)

This commit is contained in:
torbica 2017-05-09 23:42:14 +02:00 committed by metaprime
parent a38597d6fe
commit 6ca4ebd176

View File

@ -34,6 +34,7 @@ public class TwitterRipper extends AlbumRipper {
ACCOUNT, ACCOUNT,
SEARCH SEARCH
} }
private ALBUM_TYPE albumType; private ALBUM_TYPE albumType;
private String searchText, accountName; private String searchText, accountName;
@ -168,47 +169,47 @@ public class TwitterRipper extends AlbumRipper {
return tweets; return tweets;
} }
private boolean parseTweet(JSONObject tweet) throws MalformedURLException { private int parseTweet(JSONObject tweet) throws MalformedURLException {
if (!tweet.has("entities")) { int parsedCount = 0;
if (!tweet.has("extended_entities")) {
logger.error("XXX Tweet doesn't have entitites"); logger.error("XXX Tweet doesn't have entitites");
return false; return 0;
} }
JSONObject entities = tweet.getJSONObject("entities"); JSONObject entities = tweet.getJSONObject("extended_entities");
if (entities.has("media")) { if (entities.has("media")) {
JSONArray medias = entities.getJSONArray("media"); JSONArray medias = entities.getJSONArray("media");
String url; String url;
JSONObject media; JSONObject media;
for (int i = 0; i < medias.length(); i++) { for (int i = 0; i < medias.length(); i++) {
media = (JSONObject) medias.get(i); media = (JSONObject) medias.get(i);
url = media.getString("media_url"); url = media.getString("media_url");
if (media.getString("type").equals("video")) {
JSONArray variants = media.getJSONObject("video_info").getJSONArray("variants");
for (int j = 0; j < medias.length(); j++) {
JSONObject variant = (JSONObject) variants.get(i);
if (variant.has("bitrate") && variant.getInt("bitrate") == 832000) {
addURLToDownload(new URL(variant.getString("url")));
parsedCount++;
break;
}
}
} else if (media.getString("type").equals("photo")) {
if (url.contains(".twimg.com/")) { if (url.contains(".twimg.com/")) {
url += ":orig"; url += ":orig";
addURLToDownload(new URL(url)); addURLToDownload(new URL(url));
return true; parsedCount++;
} } else {
else {
logger.debug("Unexpected media_url: " + url); logger.debug("Unexpected media_url: " + url);
} }
} }
} }
}
/*
if (entities.has("urls")) { return parsedCount;
JSONArray urls = entities.getJSONArray("urls");
JSONObject url;
for (int i = 0; i < urls.length(); i++) {
url = (JSONObject) urls.get(i);
if (url.get("expanded_url") != null) {
handleTweetedURL(url.getString("url"));
} else {
handleTweetedURL(url.getString("expanded_url"));
}
}
}
*/
return false;
} }
@Override @Override
@ -242,9 +243,8 @@ public class TwitterRipper extends AlbumRipper {
for (JSONObject tweet : tweets) { for (JSONObject tweet : tweets) {
lastMaxID = tweet.getLong("id"); lastMaxID = tweet.getLong("id");
if (parseTweet(tweet)) { parsedCount += parseTweet(tweet);
parsedCount++;
}
if (isStopped() || (isThisATest() && parsedCount > 0)) { if (isStopped() || (isThisATest() && parsedCount > 0)) {
break; break;
} }
@ -256,8 +256,7 @@ public class TwitterRipper extends AlbumRipper {
try { try {
Thread.sleep(WAIT_TIME); Thread.sleep(WAIT_TIME);
} } catch (InterruptedException e) {
catch (InterruptedException e) {
logger.error("[!] Interrupted while waiting to load more results", e); logger.error("[!] Interrupted while waiting to load more results", e);
break; break;
} }