bug fix, added tostring

This commit is contained in:
nuclearfog 2022-12-21 21:27:51 +01:00
parent 1274e4ea85
commit e8c2957ee9
No known key found for this signature in database
GPG Key ID: 03488A185C476379
5 changed files with 61 additions and 32 deletions

View File

@ -1274,12 +1274,11 @@ public class Twitter implements Connection {
ResponseBody body = response.body();
if (body != null && response.code() == 200) {
JSONObject json = new JSONObject(body.string());
JSONObject data = json.getJSONObject("data");
UserV2Map userMap = new UserV2Map(json, settings.getLogin().getId());
MediaV2Map mediaMap = new MediaV2Map(json);
PollV2Map pollMap = new PollV2Map(json);
LocationV2Map locationMap = new LocationV2Map(json);
return new TweetV2(data, userMap, mediaMap, pollMap, locationMap, statusCompat);
return new TweetV2(json, userMap, mediaMap, pollMap, locationMap, statusCompat);
}
throw new TwitterException(response);
} catch (IOException | JSONException err) {

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api.twitter.impl.v1;
import androidx.annotation.NonNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -28,10 +30,10 @@ public class MediaV1 implements Media {
*/
public MediaV1(JSONObject json) throws JSONException {
String type = json.getString("type");
preview = json.optString("expanded_url");
preview = json.optString("media_url_https");
switch (type) {
case "photo":
url = json.getString("url");
url = json.getString("media_url_https");
this.type = PHOTO;
break;
@ -86,4 +88,30 @@ public class MediaV1 implements Media {
public String getPreviewUrl() {
return preview;
}
@NonNull
@Override
public String toString() {
String tostring;
switch (type) {
case PHOTO:
tostring = "photo:";
break;
case VIDEO:
tostring = "video:";
break;
case GIF:
tostring = "gif:";
break;
default:
tostring = "none:";
break;
}
tostring += "url=\"" + url + "\"";
return tostring;
}
}

View File

@ -95,7 +95,7 @@ public class TweetV1 implements Status {
if (mediaArray != null && mediaArray.length() > 0) {
medias = new Media[mediaArray.length()];
for (int i = 0; i < mediaArray.length(); i++) {
JSONObject mediaItem = mediaArray.getJSONObject(0);
JSONObject mediaItem = mediaArray.getJSONObject(i);
medias[i] = new MediaV1(mediaItem);
}
}

View File

@ -98,19 +98,19 @@ public class MediaV2 implements Media {
String tostring;
switch (type) {
case PHOTO:
tostring = "photo";
tostring = "photo:";
break;
case VIDEO:
tostring = "video";
tostring = "video:";
break;
case GIF:
tostring = "gif";
tostring = "gif:";
break;
default:
tostring = "none";
tostring = "none:";
break;
}
tostring += "url=\"" + url + "\"";

View File

@ -92,19 +92,19 @@ public class TweetV2 implements Status {
* @param tweetCompat tweet v1.1 object
*/
public TweetV2(JSONObject json, @NonNull UserV2Map userMap, @Nullable MediaV2Map mediaMap, @Nullable PollV2Map pollMap, @Nullable LocationV2Map locationMap, @Nullable Status tweetCompat) throws JSONException {
JSONObject publicMetrics = json.getJSONObject("public_metrics");
JSONObject nonPublicMetrics = json.optJSONObject("non_public_metrics");
JSONObject entities = json.optJSONObject("entities");
JSONObject geoJson = json.optJSONObject("geo");
JSONObject attachments = json.optJSONObject("attachments");
JSONArray tweetReferences = json.optJSONArray("referenced_tweets");
String idStr = json.getString("id");
String textStr = json.optString("text", "");
String timeStr = json.optString("created_at", "");
String replyUserIdStr = json.optString("in_reply_to_user_id", "-1");
String conversationIdStr = json.optString("conversation_id", "-1");
String authorId = json.getString("author_id");
JSONObject data = json.getJSONObject("data");
JSONObject publicMetrics = data.getJSONObject("public_metrics");
JSONObject nonPublicMetrics = data.optJSONObject("non_public_metrics");
JSONObject entities = data.optJSONObject("entities");
JSONObject geoJson = data.optJSONObject("geo");
JSONObject attachments = data.optJSONObject("attachments");
JSONArray tweetReferences = data.optJSONArray("referenced_tweets");
String idStr = data.getString("id");
String textStr = data.optString("text", "");
String timeStr = data.optString("created_at", "");
String replyUserIdStr = data.optString("in_reply_to_user_id", "-1");
String conversationIdStr = data.optString("conversation_id", "-1");
String authorId = data.getString("author_id");
// string to long conversion
try {
id = Long.parseLong(idStr);
@ -128,16 +128,8 @@ public class TweetV2 implements Status {
retweetCount = publicMetrics.getInt("retweet_count");
favoriteCount = publicMetrics.getInt("like_count");
timestamp = StringTools.getTime(timeStr, StringTools.TIME_TWITTER_V2);
source = json.optString("source", "unknown");
sensitive = json.optBoolean("possibly_sensitive", false);
// add missing attributes using API v1.1
if (tweetCompat != null) {
replyName = tweetCompat.getReplyName();
embedded = tweetCompat.getEmbeddedStatus();
retweeted = tweetCompat.isReposted();
favorited = tweetCompat.isFavorited();
source = tweetCompat.getSource(); // fix for any reason Twitter doesn't return source information anymore
}
source = data.optString("source", "unknown");
sensitive = data.optBoolean("possibly_sensitive", false);
// add media
if (attachments != null) {
JSONArray mediaKeys = attachments.optJSONArray("media_keys");
@ -218,6 +210,16 @@ public class TweetV2 implements Status {
}
}
}
// add/override missing attributes using API v1.1
if (tweetCompat != null) {
replyName = tweetCompat.getReplyName();
embedded = tweetCompat.getEmbeddedStatus();
retweeted = tweetCompat.isReposted();
favorited = tweetCompat.isFavorited();
// fixme: for any reason Twitter API 2.0 doesn't return the attributes below
source = tweetCompat.getSource();
medias = tweetCompat.getMedia();
}
}