Skip to content

Commit

Permalink
feat: pagination for likes and retweets endpoints (#353)
Browse files Browse the repository at this point in the history
* feat: pagination for likes and retweets endpoints
  • Loading branch information
redouane59 authored Jan 21, 2022
1 parent e96c24d commit 9e33c00
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 28 deletions.
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
test-twitter-credentials.json
/.idea/libraries
/.idea/*.xml
/.target
*.iml
*.class
src/main/resources/twitter-credentials.json
src/main/resources/twitter-credentials2.json
src/main/java/com/socialmediaraiser/okhttpCache/
src/main/java/io/github/redouane59/okhttpCache/
src/main/java/com/socialmediaraiser/okhttpCache/*
src/main/java/io/github/redouane59/okhttpCache/*
target/
*/target/**
*/target/*
Expand All @@ -17,8 +12,6 @@ target/
/okhttpCache/*
*.asc
bundle.jar
test-twitter-credentials.json

/.vscode
/.settings
.classpath
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,5 @@

<url>https://github.com/Redouane59/twittered</url>

<version>2.15-SNAPSHOT</version>
<version>2.15</version>
</project>
18 changes: 17 additions & 1 deletion src/main/java/io/github/redouane59/twitter/ITwitterClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,20 @@ public interface ITwitterClientV2 {
LikeResponse unlikeTweet(String tweetId);

/**
* Allows you to get information about a Tweet’s liking users calling https://api.twitter.com/2/tweets/:id/liking_users
* Allows you to get information about a Tweet’s all liking users calling https://api.twitter.com/2/tweets/:id/liking_users
*
* @param tweetId ID of the Tweet to request liking users of.
*/
UserList getLikingUsers(String tweetId);

/**
* Allows you to get information about a Tweet’s liking users calling https://api.twitter.com/2/tweets/:id/liking_users
*
* @param tweetId ID of the Tweet to request liking users of.
* @param maxResults the maximum results to be returned.
*/
UserList getLikingUsers(String tweetId, int maxResults);

/**
* Allows you to get information about a user’s liked Tweets calling https://api.twitter.com/2/users/:id/liked_tweets
*
Expand Down Expand Up @@ -443,6 +451,14 @@ public interface ITwitterClientV2 {
* Allows you to get information about who has Retweeted a Tweet calling https://api.twitter.com/2/tweets/:id/retweeted_by
*
* @param tweetId the id of the tweet
* @param maxResults the maximum results to be returned.
*/
UserList getRetweetingUsers(String tweetId, int maxResults);

/**
* Allows you to get information about all users who have Retweeted a Tweet calling https://api.twitter.com/2/tweets/:id/retweeted_by
*
* @param tweetId the id of the tweet
*/
UserList getRetweetingUsers(String tweetId);

Expand Down
52 changes: 45 additions & 7 deletions src/main/java/io/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,20 +448,58 @@ public LikeResponse unlikeTweet(String tweetId) {
}

@Override
public UserList getRetweetingUsers(String tweetId) {
String url = urlHelper.getRetweetersUrl(tweetId);
public UserList getRetweetingUsers(String tweetId, int maxResults) {
String url = urlHelper.getRetweetersUrl(tweetId);
return getUsersRecursively(maxResults, url);
}


/**
* Used for get liking users and get retweeting users endpoints recursively calls
*/
private UserList getUsersRecursively(int maxResults, String url) {
Map<String, String> parameters = new HashMap<>();
parameters.put(USER_FIELDS, ALL_USER_FIELDS);
parameters.put(EXPANSION, PINNED_TWEET_ID);
return getRequestHelper().getRequestWithParameters(url, parameters, UserList.class).orElseThrow(NoSuchElementException::new);
UserList result = UserList.builder().data(new ArrayList<>()).meta(new UserMeta()).build();
String next;

do {
parameters.put(MAX_RESULTS, String.valueOf(Math.min(100, maxResults - result.getData().size())));
Optional<UserList> userList = getRequestHelper().getRequestWithParameters(url, parameters, UserList.class);
if (!userList.isPresent() || userList.get().getData() == null) {
result.getMeta().setNextToken(null);
break;
}
result.getData().addAll(userList.get().getData());

UserMeta meta = UserMeta.builder()
.resultCount(result.getData().size())
.nextToken(userList.get().getMeta().getNextToken())
.build();
result.setMeta(meta);
next = userList.get().getMeta().getNextToken();
parameters.put(AdditionalParameters.PAGINATION_TOKEN, next);
} while (next != null && result.getData().size() < maxResults);

return result;
}


@Override
public UserList getRetweetingUsers(String tweetId) {
return getRetweetingUsers(tweetId, Integer.MAX_VALUE);
}

@Override
public UserList getLikingUsers(final String tweetId, int maxResults) {
String url = getUrlHelper().getLikingUsersUrl(tweetId);
return getUsersRecursively(maxResults, url);
}

@Override
public UserList getLikingUsers(final String tweetId) {
String url = getUrlHelper().getLikingUsersUrl(tweetId);
Map<String, String> parameters = new HashMap<>();
parameters.put(USER_FIELDS, ALL_USER_FIELDS);
return getRequestHelper().getRequestWithParameters(url, parameters, UserList.class).orElseThrow(NoSuchElementException::new);
return getLikingUsers(tweetId, Integer.MAX_VALUE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,31 @@ public void testGetTweetByIdWithExpansions() {
}

@Test
public void testGetLikingUsers() {
public void testGetAllLikingUsers() {
UserList result = twitterClient.getLikingUsers("1395447825366847488");
assertNotNull(result.getData().get(0).getId());
assertNotNull(result.getData().get(0).getName());
assertNotNull(result.getData().get(0).getDisplayedName());
assertNotNull(result.getData().get(0).getProfileImageUrl());
assertNotNull(result.getData().get(0).getCreatedAt());
assertTrue(result.getData().size() > 40);
assertTrue(result.getData().size() > 105);
}

@Test
public void testGetLikingUsersWithMaxResults() {
UserList result = twitterClient.getLikingUsers("1395447825366847488", 100);
assertNotNull(result.getData().get(0).getId());
assertNotNull(result.getData().get(0).getName());
assertNotNull(result.getData().get(0).getDisplayedName());
assertNotNull(result.getData().get(0).getProfileImageUrl());
assertNotNull(result.getData().get(0).getCreatedAt());
assertEquals(100, result.getData().size());
result = twitterClient.getLikingUsers("1395447825366847488", 50);
assertEquals(50, result.getData().size());
result = twitterClient.getLikingUsers("1395447825366847488", 999999);
assertTrue(result.getData().size() > 100);
assertTrue(result.getData().size() < 1000);

}

@Test
Expand Down Expand Up @@ -514,17 +531,34 @@ public void testGetAllTweetCountWithParams() {
}

@Test
public void testGetRetweetingUsers() {
String tweetId = "1415348607813832708";
public void testAllGetRetweetingUsers() {
String tweetId = "1460323737035677698";
UserList result = twitterClient.getRetweetingUsers(tweetId);
assertNotNull(result.getData());
assertTrue(result.getData().size() > 10);
assertTrue(result.getData().size() > 200);
assertTrue(result.getData().size() < 400);
assertNotNull(result.getData().get(0).getId());
assertNotNull(result.getData().get(0).getName());
assertNotNull(result.getData().get(0).getDisplayedName());
assertNotNull(result.getData().get(0).getDateOfCreation());
}

@Test
public void testGetRetweetingUsersWithMaxResults() {
String tweetId = "1460323737035677698";
UserList result = twitterClient.getRetweetingUsers(tweetId, 50);
assertNotNull(result.getData());
assertNotNull(result.getData().get(0).getId());
assertNotNull(result.getData().get(0).getName());
assertNotNull(result.getData().get(0).getDisplayedName());
assertNotNull(result.getData().get(0).getDateOfCreation());
assertEquals(50, result.getData().size());
result = twitterClient.getRetweetingUsers(tweetId, 150);
assertEquals(150, result.getData().size());


}

@Test
public void testSpacesSearchAndLookup() {
SpaceState expectedState = SpaceState.LIVE;
Expand Down
6 changes: 0 additions & 6 deletions test-twitter-credentials.json

This file was deleted.

0 comments on commit 9e33c00

Please sign in to comment.