This commit is contained in:
nuclearfog 2022-09-06 22:19:00 +02:00
parent eaba87a689
commit 799741857d
No known key found for this signature in database
GPG Key ID: AA0271FBE406DB98
3 changed files with 61 additions and 55 deletions

View File

@ -57,18 +57,12 @@ public class UserV1 implements User {
*/ */
public UserV1(JSONObject json) throws JSONException { public UserV1(JSONObject json) throws JSONException {
String idStr = json.getString("id_str"); String idStr = json.getString("id_str");
if (ID_PATTERN.matcher(idStr).matches()) { String profileImageUrl = json.optString("profile_image_url_https");
id = Long.parseLong(idStr);
} else {
throw new JSONException("bad ID: " + idStr);
}
username = json.optString("name"); username = json.optString("name");
screenName = '@' + json.optString("screen_name"); screenName = '@' + json.optString("screen_name");
isVerified = json.optBoolean("verified"); isVerified = json.optBoolean("verified");
isLocked = json.optBoolean("protected"); isLocked = json.optBoolean("protected");
profileImageUrl = getProfileImage(json);
profileBannerUrl = json.optString("profile_banner_url"); profileBannerUrl = json.optString("profile_banner_url");
description = getDescription(json);
location = json.optString("location"); location = json.optString("location");
following = json.optInt("friends_count"); following = json.optInt("friends_count");
follower = json.optInt("followers_count"); follower = json.optInt("followers_count");
@ -77,7 +71,19 @@ public class UserV1 implements User {
followReqSent = json.optBoolean("follow_request_sent"); followReqSent = json.optBoolean("follow_request_sent");
defaultImage = json.optBoolean("default_profile_image"); defaultImage = json.optBoolean("default_profile_image");
created = StringTools.getTime1(json.optString("created_at")); created = StringTools.getTime1(json.optString("created_at"));
description = getDescription(json);
url = getUrl(json); url = getUrl(json);
if (defaultImage) {
this.profileImageUrl = profileImageUrl;
} else {
this.profileImageUrl = StringTools.createProfileImageLink(profileImageUrl);
}
if (ID_PATTERN.matcher(idStr).matches()) {
id = Long.parseLong(idStr);
} else {
throw new JSONException("bad ID: " + idStr);
}
} }
@Override @Override
@ -232,20 +238,4 @@ public class UserV1 implements User {
} }
return ""; return "";
} }
/**
* get original sized profile image url
*
* @param json root json object of user v1
* @return profile image url
*/
private String getProfileImage(JSONObject json) {
String profileImage = json.optString("profile_image_url_https");
// set profile image url
int start = profileImage.lastIndexOf('_');
int end = profileImage.lastIndexOf('.');
if (!defaultImage && start > 0 && end > 0)
return profileImage.substring(0, start) + profileImage.substring(end);
return profileImage;
}
} }

View File

@ -40,11 +40,9 @@ public class UserV2 implements User {
private int following; private int following;
private int follower; private int follower;
private int tweetCount; private int tweetCount;
private int likeCount;
private boolean isCurrentUser; private boolean isCurrentUser;
private boolean isVerified; private boolean isVerified;
private boolean isProtected; private boolean isProtected;
private boolean followReqSent;
private boolean defaultImage; private boolean defaultImage;
/** /**
@ -52,36 +50,37 @@ public class UserV2 implements User {
* @param twitterId ID of the current user * @param twitterId ID of the current user
*/ */
public UserV2(JSONObject json, long twitterId) throws JSONException { public UserV2(JSONObject json, long twitterId) throws JSONException {
JSONObject metrics = json.optJSONObject("public_metrics");
String idStr = json.getString("id"); String idStr = json.getString("id");
if (ID_PATTERN.matcher(idStr).matches()) { String profileImageUrl = json.optString("profile_image_url");
id = Long.parseLong(idStr);
} else {
throw new JSONException("bad ID: " + idStr);
}
username = json.optString("name"); username = json.optString("name");
screenName = '@' + json.optString("username"); screenName = '@' + json.optString("username");
isProtected = json.optBoolean("protected"); isProtected = json.optBoolean("protected");
location = json.optString("location"); location = json.optString("location");
isVerified = json.optBoolean("verified"); isVerified = json.optBoolean("verified");
profileImageUrl = json.optString("profile_image_url");
profileBannerUrl = json.optString("profile_banner_url"); profileBannerUrl = json.optString("profile_banner_url");
created = StringTools.getTime2(json.optString("created_at")); created = StringTools.getTime2(json.optString("created_at"));
description = getDescription(json); defaultImage = profileImageUrl.contains("default_profile_images");
url = getUrl(json);
isCurrentUser = id == twitterId;
JSONObject metrics = json.optJSONObject("public_metrics"); url = getUrl(json);
description = getDescription(json);
isCurrentUser = id == twitterId;
if (metrics != null) { if (metrics != null) {
following = metrics.optInt("following_count"); following = metrics.optInt("following_count");
follower = metrics.optInt("followers_count"); follower = metrics.optInt("followers_count");
tweetCount = metrics.optInt("tweet_count"); tweetCount = metrics.optInt("tweet_count");
} }
defaultImage = profileImageUrl.contains("default_profile_images"); if (defaultImage) {
this.profileImageUrl = profileImageUrl;
// not yet implemented in API 2.0 } else {
// todo check if Twitter added these values this.profileImageUrl = StringTools.createProfileImageLink(profileImageUrl);
likeCount = -1; }
followReqSent = false; if (ID_PATTERN.matcher(idStr).matches()) {
id = Long.parseLong(idStr);
} else {
throw new JSONException("bad ID: " + idStr);
}
} }
@Override @Override
@ -141,7 +140,8 @@ public class UserV2 implements User {
@Override @Override
public boolean followRequested() { public boolean followRequested() {
return followReqSent; // todo not yet implemented in API V2
return false;
} }
@Override @Override
@ -161,7 +161,8 @@ public class UserV2 implements User {
@Override @Override
public int getFavoriteCount() { public int getFavoriteCount() {
return likeCount; // todo not yet implemented in API V2
return -1;
} }
@Override @Override
@ -225,11 +226,9 @@ public class UserV2 implements User {
* @return expanded url * @return expanded url
*/ */
private String getUrl(JSONObject json) { private String getUrl(JSONObject json) {
JSONObject entities = json.optJSONObject("entities");
if (entities != null) {
JSONObject urlJson = entities.optJSONObject("url");
if (urlJson != null) {
try { try {
JSONObject entities = json.getJSONObject("entities");
JSONObject urlJson = entities.getJSONObject("url");
JSONArray urls = urlJson.getJSONArray("urls"); JSONArray urls = urlJson.getJSONArray("urls");
if (urls.length() > 0) { if (urls.length() > 0) {
return urls.getJSONObject(0).getString("display_url"); return urls.getJSONObject(0).getString("display_url");
@ -237,8 +236,6 @@ public class UserV2 implements User {
} catch (JSONException e) { } catch (JSONException e) {
// ignore // ignore
} }
}
}
return ""; return "";
} }
} }

View File

@ -340,4 +340,23 @@ public final class StringTools {
throw new IOException("error generating signature!"); throw new IOException("error generating signature!");
} }
} }
/**
* formate user profile image link. (remove suffix but keep the file extension if any)
*
* @param profileImage user profile image
* @return formatted link
*/
public static String createProfileImageLink(String profileImage) {
// set profile image url
int suffix = profileImage.lastIndexOf('_');
int extension = profileImage.lastIndexOf('.');
if (suffix > 0 && extension > 0) {
if (suffix > extension)
return profileImage.substring(0, suffix);
else
return profileImage.substring(0, suffix) + profileImage.substring(extension);
}
return profileImage;
}
} }