added metrics methods

This commit is contained in:
nuclearfog 2022-09-03 21:03:07 +02:00
parent 8aee25bb40
commit f5499889fb
No known key found for this signature in database
GPG Key ID: AA0271FBE406DB98
5 changed files with 143 additions and 1 deletions

View File

@ -26,7 +26,6 @@ android {
proguardFile 'proguard-rules.pro'
}
debug {
proguardFile 'proguard-debug.pro'
applicationIdSuffix '.debug'
versionNameSuffix '.DEBUG'
}

View File

@ -11,6 +11,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.nuclearfog.twidda.backend.api.impl.DirectmessageV1;
import org.nuclearfog.twidda.backend.api.impl.LocationV1;
import org.nuclearfog.twidda.backend.api.impl.MetricsImpl;
import org.nuclearfog.twidda.backend.api.impl.RelationV1;
import org.nuclearfog.twidda.backend.api.impl.TrendV1;
import org.nuclearfog.twidda.backend.api.impl.TweetV1;
@ -31,6 +32,7 @@ import org.nuclearfog.twidda.backend.utils.Tokens;
import org.nuclearfog.twidda.database.FilterDatabase;
import org.nuclearfog.twidda.database.GlobalSettings;
import org.nuclearfog.twidda.model.Location;
import org.nuclearfog.twidda.model.Metrics;
import org.nuclearfog.twidda.model.Relation;
import org.nuclearfog.twidda.model.Trend;
import org.nuclearfog.twidda.model.Tweet;
@ -1210,6 +1212,30 @@ public class Twitter implements GlobalSettings.SettingsListener {
}
}
/**
* get tweet metrics (views, link clicks, etc.)
*
* @param tweetId ID of the tweet to get the metrics from
* @return tweet metrics
*/
public Metrics getTweetMetrics(long tweetId) throws TwitterException {
List<String> params = new ArrayList<>();
params.add(MetricsImpl.PARAMS);
try {
Response response = get(TWEET_UNI + tweetId, params);
ResponseBody body = response.body();
if (body != null && response.code() == 200) {
JSONObject json = new JSONObject(body.string());
if (json.opt("data") != null) {
return new MetricsImpl(json);
}
}
throw new TwitterException(response);
} catch (IOException | JSONException err) {
throw new TwitterException(err);
}
}
/**
* upload media file to twitter and generate a media ID
*

View File

@ -0,0 +1,65 @@
package org.nuclearfog.twidda.backend.api.impl;
import org.json.JSONException;
import org.json.JSONObject;
import org.nuclearfog.twidda.model.Metrics;
/**
* Implementation of {@link Metrics} using API V2
*
* @author nuclearfog
*/
public class MetricsImpl implements Metrics {
public static final String PARAMS = "tweet.fields=organic_metrics";
private int impressions;
private int retweets;
private int likes;
private int replies;
private int linkClicks;
private int profileClicks;
/**
* @param json tweet json object containing metrics information
*/
public MetricsImpl(JSONObject json) throws JSONException {
JSONObject organic = json.getJSONObject("data").getJSONObject("organic_metrics");
impressions = organic.optInt("impression_count", 0);
retweets = organic.optInt("retweet_count", 0);
likes = organic.optInt("like_count", 0);
replies = organic.optInt("reply_count", 0);
linkClicks = organic.optInt("url_link_clicks", 0);
profileClicks = organic.optInt("user_profile_", 0);
}
@Override
public int getViews() {
return impressions;
}
@Override
public int getRetweets() {
return retweets;
}
@Override
public int getLikes() {
return likes;
}
@Override
public int getReplies() {
return replies;
}
@Override
public int getLinkClicks() {
return linkClicks;
}
@Override
public int getProfileClicks() {
return profileClicks;
}
}

View File

@ -100,6 +100,7 @@ public class TweetAction extends AsyncTask<Long, Tweet, Void> {
if (newTweet != null) {
publishProgress(newTweet);
}
// fall through
case LOAD:
newTweet = twitter.showTweet(ids[0]);

View File

@ -0,0 +1,51 @@
package org.nuclearfog.twidda.model;
/**
* Tweet metrics class containing information like views and link clicks
*
* @author nuclearfog
*/
public interface Metrics {
/**
* get view count of the tweet
*
* @return view count
*/
int getViews();
/**
* get retweet count
*
* @return retweet count
*/
int getRetweets();
/**
* get like/favorite count
*
* @return like count
*/
int getLikes();
/**
* get reply count
*
* @return reply count
*/
int getReplies();
/**
* get link click count
*
* @return click count
*/
int getLinkClicks();
/**
* get profile click count
*
* @return click count
*/
int getProfileClicks();
}