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,
SEARCH
}
private ALBUM_TYPE albumType;
private String searchText, accountName;
@ -168,47 +169,47 @@ public class TwitterRipper extends AlbumRipper {
return tweets;
}
private boolean parseTweet(JSONObject tweet) throws MalformedURLException {
if (!tweet.has("entities")) {
private int parseTweet(JSONObject tweet) throws MalformedURLException {
int parsedCount = 0;
if (!tweet.has("extended_entities")) {
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")) {
JSONArray medias = entities.getJSONArray("media");
String url;
JSONObject media;
for (int i = 0; i < medias.length(); i++) {
media = (JSONObject) medias.get(i);
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/")) {
url += ":orig";
addURLToDownload(new URL(url));
return true;
}
else {
parsedCount++;
} else {
logger.debug("Unexpected media_url: " + url);
}
}
}
}
/*
if (entities.has("urls")) {
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;
return parsedCount;
}
@Override
@ -242,9 +243,8 @@ public class TwitterRipper extends AlbumRipper {
for (JSONObject tweet : tweets) {
lastMaxID = tweet.getLong("id");
if (parseTweet(tweet)) {
parsedCount++;
}
parsedCount += parseTweet(tweet);
if (isStopped() || (isThisATest() && parsedCount > 0)) {
break;
}
@ -256,8 +256,7 @@ public class TwitterRipper extends AlbumRipper {
try {
Thread.sleep(WAIT_TIME);
}
catch (InterruptedException e) {
} catch (InterruptedException e) {
logger.error("[!] Interrupted while waiting to load more results", e);
break;
}