-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add non-public tweet metrics and misc fields for API v2 #366
Open
xberger
wants to merge
3
commits into
redouane59:develop
Choose a base branch
from
xberger:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,9 +102,12 @@ public class TwitterClient implements ITwitterClientV1, ITwitterClientV2, ITwitt | |
.setSerializationInclusion(JsonInclude.Include.NON_NULL) | ||
.findAndRegisterModules(); | ||
public static final String TWEET_FIELDS = "tweet.fields"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use the same code style to avoid this |
||
public static final String | ||
ALL_TWEET_FIELDS = | ||
"attachments,author_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,public_metrics,referenced_tweets,source,text,withheld,context_annotations,conversation_id,reply_settings"; | ||
public static final String ALL_TWEET_FIELDS_PUBLIC = | ||
"attachments,author_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,referenced_tweets,source,text,withheld,context_annotations,conversation_id,reply_settings,public_metrics"; | ||
public static final String ALL_TWEET_FIELDS_NON_PUBLIC = | ||
ALL_TWEET_FIELDS_PUBLIC + ",non_public_metrics,organic_metrics"; | ||
public static final String ALL_TWEET_FIELDS_NON_PUBLIC_PROMOTED = | ||
ALL_TWEET_FIELDS_NON_PUBLIC + ",promoted_metrics"; | ||
public static final String EXPANSION = "expansions"; | ||
public static final String | ||
ALL_EXPANSIONS = | ||
|
@@ -154,7 +157,12 @@ public TwitterClient() { | |
public TwitterClient(TwitterCredentials credentials) { | ||
this(credentials, new ServiceBuilder(credentials.getApiKey()).apiSecret(credentials.getApiSecretKey())); | ||
} | ||
|
||
|
||
public TwitterClient(TwitterCredentials credentials, boolean useConsumerKey) { | ||
this(credentials, new ServiceBuilder(credentials.getApiKey()).apiSecret(credentials.getApiSecretKey())); | ||
setUseConsumerKey(useConsumerKey); | ||
} | ||
|
||
public TwitterClient(TwitterCredentials credentials, HttpClient httpClient) { | ||
this(credentials, | ||
new ServiceBuilder(credentials.getApiKey()).apiSecret(credentials.getApiSecretKey()).httpClient(httpClient)); | ||
|
@@ -235,6 +243,15 @@ public void setAutomaticRetry(boolean automaticRetry) { | |
requestHelperV2.setAutomaticRetry(automaticRetry); | ||
} | ||
|
||
/** | ||
* Set the behavior of authentication context used in requests. | ||
* | ||
* @param useConsumerKey Using consumer key for user context if true; or else app-only bearer token. Default is false. | ||
*/ | ||
public void setUseConsumerKey(boolean useConsumerKey) { | ||
requestHelperV2.setUseConsumerKey(useConsumerKey); | ||
} | ||
|
||
// can manage up to 5000 results / call . Max 15 calls / 15min ==> 75.000 | ||
// results max. / 15min | ||
private List<String> getUserIdsByRelation(String url) { | ||
|
@@ -515,7 +532,7 @@ public TweetList getLikedTweets(final String userId) { | |
public TweetList getLikedTweets(final String userId, AdditionalParameters additionalParameters) { | ||
String url = getUrlHelper().getLikedTweetsUrl(userId); | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
if (!additionalParameters.isRecursiveCall()) { | ||
return getRequestHelper().getRequestWithParameters(url, parameters, TweetList.class).orElseThrow(NoSuchElementException::new); | ||
} | ||
|
@@ -576,7 +593,7 @@ public UserList getMutedUsers() { | |
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(EXPANSION, PINNED_TWEET_ID); | ||
parameters.put(MAX_RESULTS, "1000"); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
return requestHelperV1.getRequestWithParameters(url, parameters, UserList.class).orElseThrow(NoSuchElementException::new); | ||
} | ||
|
||
|
@@ -646,7 +663,7 @@ public UserList getSpaceBuyers(final String spaceId) { | |
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(EXPANSION, PINNED_TWEET_ID); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
return getRequestHelperV2().getRequestWithParameters(url, parameters, UserList.class) | ||
.orElseThrow(NoSuchElementException::new); | ||
} | ||
|
@@ -751,7 +768,7 @@ public UserList getListMembers(final String listId) { | |
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(EXPANSION, PINNED_TWEET_ID); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
return getUsersRecursively(Integer.MAX_VALUE, url, parameters); | ||
} | ||
|
||
|
@@ -788,21 +805,31 @@ public boolean deleteTweet(final String tweetId) { | |
|
||
@Override | ||
public Tweet getTweet(String tweetId) { | ||
return getTweet(tweetId, REQUEST_TWEET_FIELDS_SCOPE.PUBLIC); | ||
} | ||
|
||
@Override | ||
public Tweet getTweet(String tweetId, REQUEST_TWEET_FIELDS_SCOPE requestTweetFieldsScope) { | ||
String url = getUrlHelper().getTweetUrl(tweetId); | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(EXPANSION, ALL_EXPANSIONS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, getFieldsForRequestTweetFieldsScope(requestTweetFieldsScope)); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS); | ||
return getRequestHelper().getRequestWithParameters(url, parameters, TweetV2.class).orElseThrow(NoSuchElementException::new); | ||
} | ||
|
||
@Override | ||
public TweetList getTweets(List<String> tweetIds) { | ||
return getTweets(tweetIds, REQUEST_TWEET_FIELDS_SCOPE.PUBLIC); | ||
} | ||
|
||
@Override | ||
public TweetList getTweets(List<String> tweetIds, REQUEST_TWEET_FIELDS_SCOPE requestTweetFieldsScope) { | ||
String url = getUrlHelper().getTweetsUrl(); | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(EXPANSION, ALL_EXPANSIONS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, getFieldsForRequestTweetFieldsScope(requestTweetFieldsScope)); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS); | ||
StringBuilder result = new StringBuilder(); | ||
|
@@ -841,7 +868,7 @@ public TweetList searchTweets(String query) { | |
public TweetList searchTweets(String query, AdditionalParameters additionalParameters) { | ||
Map<String, String> parameters = additionalParameters.getMapFromParameters(); | ||
parameters.put(QUERY, query); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(EXPANSION, ALL_EXPANSIONS); | ||
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS); | ||
|
@@ -865,10 +892,10 @@ public TweetList searchAllTweets(final String query, AdditionalParameters additi | |
Map<String, String> parameters = additionalParameters.getMapFromParameters(); | ||
parameters.put(QUERY, query); | ||
if (additionalParameters.getMaxResults() <= 100) { | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
} else { | ||
LOGGER.warn("removing context_annotations from tweet_fields because max_result is greater 100"); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS.replace(",context_annotations", "")); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC.replace(",context_annotations", "")); | ||
} | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(EXPANSION, ALL_EXPANSIONS); | ||
|
@@ -1004,7 +1031,7 @@ public Future<Response> startFilteredStream(Consumer<Tweet> consumer) { | |
String url = urlHelper.getFilteredStreamUrl(); | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(EXPANSION, ALL_EXPANSIONS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS); | ||
return requestHelperV2.getAsyncRequest(url, parameters, consumer); | ||
|
@@ -1020,7 +1047,7 @@ public Future<Response> startFilteredStream(IAPIEventListener listener, int back | |
String url = urlHelper.getFilteredStreamUrl(); | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(EXPANSION, ALL_EXPANSIONS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS); | ||
if (backfillMinutes > 0) { | ||
|
@@ -1100,7 +1127,7 @@ public Future<Response> startSampledStream(Consumer<Tweet> consumer) { | |
String url = urlHelper.getSampledStreamUrl(); | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(EXPANSION, ALL_EXPANSIONS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS); | ||
return requestHelperV2.getAsyncRequest(url, parameters, consumer); | ||
|
@@ -1116,7 +1143,7 @@ public Future<Response> startSampledStream(IAPIEventListener listener, int backf | |
String url = urlHelper.getSampledStreamUrl(); | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put(EXPANSION, ALL_EXPANSIONS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS); | ||
if (backfillMinutes > 0) { | ||
|
@@ -1127,13 +1154,23 @@ public Future<Response> startSampledStream(IAPIEventListener listener, int backf | |
|
||
@Override | ||
public TweetList getUserTimeline(final String userId) { | ||
return getUserTimeline(userId, AdditionalParameters.builder().maxResults(100).build()); | ||
return getUserTimeline(userId, AdditionalParameters.builder().maxResults(100).build(), REQUEST_TWEET_FIELDS_SCOPE.PUBLIC); | ||
} | ||
|
||
@Override | ||
public TweetList getUserTimeline(final String userId, AdditionalParameters additionalParameters) { | ||
return getUserTimeline(userId, additionalParameters, REQUEST_TWEET_FIELDS_SCOPE.PUBLIC); | ||
} | ||
|
||
@Override | ||
public TweetList getUserTimeline(final String userId, REQUEST_TWEET_FIELDS_SCOPE requestTweetFieldsScope) { | ||
return getUserTimeline(userId, AdditionalParameters.builder().maxResults(100).build(), requestTweetFieldsScope); | ||
} | ||
|
||
@Override | ||
public TweetList getUserTimeline(String userId, AdditionalParameters additionalParameters) { | ||
public TweetList getUserTimeline(String userId, AdditionalParameters additionalParameters, REQUEST_TWEET_FIELDS_SCOPE requestTweetFieldsScope) { | ||
Map<String, String> parameters = additionalParameters.getMapFromParameters(); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, getFieldsForRequestTweetFieldsScope(requestTweetFieldsScope)); | ||
parameters.put(USER_FIELDS, ALL_USER_FIELDS); | ||
parameters.put(PLACE_FIELDS, ALL_PLACE_FIELDS); | ||
parameters.put(POLL_FIELDS, ALL_POLL_FIELDS); | ||
|
@@ -1157,7 +1194,7 @@ public TweetList getUserMentions(final String userId) { | |
@Override | ||
public TweetList getUserMentions(final String userId, AdditionalParameters additionalParameters) { | ||
Map<String, String> parameters = additionalParameters.getMapFromParameters(); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); | ||
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS_PUBLIC); | ||
String url = urlHelper.getUserMentionsUrl(userId); | ||
if (!additionalParameters.isRecursiveCall()) { | ||
return getRequestHelperV2().getRequestWithParameters(url, parameters, TweetList.class).orElseThrow(NoSuchElementException::new); | ||
|
@@ -1378,4 +1415,18 @@ public String getUserIdFromAccessToken() { | |
} | ||
return accessToken.substring(0, accessToken.indexOf("-")); | ||
} | ||
|
||
public String getFieldsForRequestTweetFieldsScope(REQUEST_TWEET_FIELDS_SCOPE requestTweetFieldsScope) { | ||
switch (requestTweetFieldsScope) { | ||
case NON_PUBLIC: | ||
return ALL_TWEET_FIELDS_NON_PUBLIC; | ||
case NON_PUBLIC_PROMOTED: | ||
return ALL_TWEET_FIELDS_NON_PUBLIC_PROMOTED; | ||
case PUBLIC: | ||
return ALL_TWEET_FIELDS_PUBLIC; | ||
default: | ||
throw new RuntimeException("REQUEST_TWEET_FIELDS_SCOPE " + requestTweetFieldsScope + " not implemented."); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Little question, isn't it possible to add the
requestTweetFieldsScope
parameter inside theadditionalParameters
object?It will avoid creating new constructors no ?