From e09ac16f9e2b82b1d62bbdaa8b888669b8775a9c Mon Sep 17 00:00:00 2001 From: Redouane Bali Date: Fri, 21 Jan 2022 14:57:55 +0100 Subject: [PATCH 1/3] feat: add get list member endpoint V2 (#354) * feat: add get list member endpoint * tests * cred back * back 2 --- pom.xml | 2 +- .../redouane59/twitter/ITwitterClientV2.java | 7 ++++ .../redouane59/twitter/TwitterClient.java | 32 +++++++++++++------ .../twitter/nrt/ITwitterClientV2Test.java | 13 ++++++++ test-twitter-credentials.json | 2 +- 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 9b7b8ede..ad7201fd 100644 --- a/pom.xml +++ b/pom.xml @@ -194,5 +194,5 @@ https://github.com/Redouane59/twittered - 2.15 + 2.16-SNAPSHOT \ No newline at end of file diff --git a/src/main/java/io/github/redouane59/twitter/ITwitterClientV2.java b/src/main/java/io/github/redouane59/twitter/ITwitterClientV2.java index e3e009c3..f19b8127 100644 --- a/src/main/java/io/github/redouane59/twitter/ITwitterClientV2.java +++ b/src/main/java/io/github/redouane59/twitter/ITwitterClientV2.java @@ -594,6 +594,13 @@ public interface ITwitterClientV2 { */ TwitterList getList(String listId); + /** + * Returns a list of users who are members of the specified List + * + * @param listId The ID of the List whose members you would like to retrieve. + */ + UserList getListMembers(String listId); + /** * Returns all Lists owned by the specified user calling https://api.twitter.com/2/users/:id/owned_lists * diff --git a/src/main/java/io/github/redouane59/twitter/TwitterClient.java b/src/main/java/io/github/redouane59/twitter/TwitterClient.java index 3815332b..2093ccb9 100644 --- a/src/main/java/io/github/redouane59/twitter/TwitterClient.java +++ b/src/main/java/io/github/redouane59/twitter/TwitterClient.java @@ -449,18 +449,19 @@ public LikeResponse unlikeTweet(String tweetId) { @Override public UserList getRetweetingUsers(String tweetId, int maxResults) { - String url = urlHelper.getRetweetersUrl(tweetId); - return getUsersRecursively(maxResults, url); + String url = urlHelper.getRetweetersUrl(tweetId); + Map parameters = new HashMap<>(); + parameters.put(USER_FIELDS, ALL_USER_FIELDS); + parameters.put(EXPANSION, PINNED_TWEET_ID); + return getUsersRecursively(maxResults, url, parameters); } + // @todo see if it cannot be mixed with other similar function /** - * Used for get liking users and get retweeting users endpoints recursively calls + * Used for get liking users, get retweeting users and get members endpoints recursively calls */ - private UserList getUsersRecursively(int maxResults, String url) { - Map parameters = new HashMap<>(); - parameters.put(USER_FIELDS, ALL_USER_FIELDS); - parameters.put(EXPANSION, PINNED_TWEET_ID); + private UserList getUsersRecursively(int maxResults, String url, Map parameters) { UserList result = UserList.builder().data(new ArrayList<>()).meta(new UserMeta()).build(); String next; @@ -493,8 +494,11 @@ public UserList getRetweetingUsers(String tweetId) { @Override public UserList getLikingUsers(final String tweetId, int maxResults) { - String url = getUrlHelper().getLikingUsersUrl(tweetId); - return getUsersRecursively(maxResults, url); + String url = getUrlHelper().getLikingUsersUrl(tweetId); + Map parameters = new HashMap<>(); + parameters.put(USER_FIELDS, ALL_USER_FIELDS); + parameters.put(EXPANSION, PINNED_TWEET_ID); + return getUsersRecursively(maxResults, url, parameters); } @Override @@ -741,6 +745,16 @@ public TwitterList getList(final String listId) { return getRequestHelperV1().getRequestWithParameters(url, parameters, TwitterList.class).orElseThrow(NoSuchElementException::new); } + @Override + public UserList getListMembers(final String listId) { + String url = getUrlHelper().getAddListMemberUrl(listId); + Map parameters = new HashMap<>(); + parameters.put(EXPANSION, PINNED_TWEET_ID); + parameters.put(USER_FIELDS, ALL_USER_FIELDS); + parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS); + return getUsersRecursively(Integer.MAX_VALUE, url, parameters); + } + @Override public TwitterListList getUserOwnedLists(final String userId) { String url = getUrlHelper().getOwnedListUrl(userId); diff --git a/src/test/java/io/github/redouane59/twitter/nrt/ITwitterClientV2Test.java b/src/test/java/io/github/redouane59/twitter/nrt/ITwitterClientV2Test.java index 72189a4d..766b41c6 100644 --- a/src/test/java/io/github/redouane59/twitter/nrt/ITwitterClientV2Test.java +++ b/src/test/java/io/github/redouane59/twitter/nrt/ITwitterClientV2Test.java @@ -606,6 +606,19 @@ public void testGetList() { assertNotNull(twitterList.getIncludes().getUsers().get(0).getCreatedAt()); } + @Test + public void testGetListMembers() { + String listId = "1449313282892910592"; + UserList members = twitterClient.getListMembers(listId); + assertNotNull(members); + assertTrue(members.getData().size() > 1); + assertNotNull(members.getData().get(0).getId()); + assertNotNull(members.getData().get(0).getName()); + assertNotNull(members.getData().get(0).getCreatedAt()); + assertTrue(members.getData().get(0).getFollowersCount() > 0); + assertTrue(members.getData().get(0).getFollowingCount() > 0); + } + @Test public void testGetUserOwnedList() { TwitterListList lists = twitterClient.getUserOwnedLists(userId); diff --git a/test-twitter-credentials.json b/test-twitter-credentials.json index 23906bba..f8233470 100644 --- a/test-twitter-credentials.json +++ b/test-twitter-credentials.json @@ -3,4 +3,4 @@ "apiSecretKey": "REPLACE ME", "accessToken": "REPLACE ME", "accessTokenSecret": "REPLACE ME" -} +} \ No newline at end of file From b729791f15035f567e24d18690cf3e5f7d1a63db Mon Sep 17 00:00:00 2001 From: Redouane Bali Date: Sat, 5 Feb 2022 16:33:45 +0100 Subject: [PATCH 2/3] feat: add place includes (#357) --- .../redouane59/twitter/dto/tweet/Tweet.java | 6 +++ .../redouane59/twitter/dto/tweet/TweetV1.java | 7 +++ .../redouane59/twitter/dto/tweet/TweetV2.java | 45 ++++++++++++++++++- .../twitter/unit/TweetDeserializerV2Test.java | 15 +++++++ .../resources/tests/tweet_example_v2.json | 20 +++++++++ 5 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/redouane59/twitter/dto/tweet/Tweet.java b/src/main/java/io/github/redouane59/twitter/dto/tweet/Tweet.java index 9e7a35fe..2f11f621 100644 --- a/src/main/java/io/github/redouane59/twitter/dto/tweet/Tweet.java +++ b/src/main/java/io/github/redouane59/twitter/dto/tweet/Tweet.java @@ -1,6 +1,7 @@ package io.github.redouane59.twitter.dto.tweet; import io.github.redouane59.twitter.dto.stream.StreamRules; +import io.github.redouane59.twitter.dto.tweet.TweetV2.Place; import io.github.redouane59.twitter.dto.tweet.entities.Entities; import io.github.redouane59.twitter.dto.tweet.entities.MediaEntity; import io.github.redouane59.twitter.dto.user.User; @@ -152,6 +153,11 @@ public interface Tweet { */ List getMedia(); + /** + * Get the {@link Place place} of the tweet + */ + List getPlaces(); + /** * When an activity is delivered through a filtered stream connection, the matching_rules list contains which list of filters matched against the * Tweet delivered. diff --git a/src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV1.java b/src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV1.java index 1f40041d..3b438d18 100644 --- a/src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV1.java +++ b/src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV1.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import io.github.redouane59.twitter.dto.stream.StreamRules.StreamRule; +import io.github.redouane59.twitter.dto.tweet.TweetV2.Place; import io.github.redouane59.twitter.dto.tweet.entities.BaseEntity; import io.github.redouane59.twitter.dto.tweet.entities.Entities; import io.github.redouane59.twitter.dto.tweet.entities.HashtagEntity; @@ -120,6 +121,12 @@ public List getMedia() { return Collections.emptyList(); } + @Override + public List getPlaces() { + LOGGER.error(NOT_IMPLEMENTED_EXCEPTION); + return Collections.emptyList(); + } + @Override public List getMatchingRules() { LOGGER.error(NOT_IMPLEMENTED_EXCEPTION); diff --git a/src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV2.java b/src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV2.java index 061dbe93..2c093330 100644 --- a/src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV2.java +++ b/src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV2.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.JsonNode; import io.github.redouane59.twitter.dto.stream.StreamRules; import io.github.redouane59.twitter.dto.stream.StreamRules.StreamRule; import io.github.redouane59.twitter.dto.tweet.entities.BaseEntity; @@ -147,6 +148,14 @@ public List getMedia() { return includes.getMedia(); } + @Override + public List getPlaces() { + if (includes == null) { + return Collections.emptyList(); + } + return includes.getPlaces(); + } + @Override public int getRetweetCount() { if (data == null) { @@ -327,13 +336,19 @@ public TweetType getTweetType() { @Override public List getMedia() { - LOGGER.error(NOT_IMPLEMENTED_EXCEPTION); + LOGGER.info(NOT_IMPLEMENTED_EXCEPTION); + return Collections.emptyList(); + } + + @Override + public List getPlaces() { + LOGGER.info(NOT_IMPLEMENTED_EXCEPTION); return Collections.emptyList(); } @Override public List getMatchingRules() { - LOGGER.error(NOT_IMPLEMENTED_EXCEPTION); + LOGGER.info(NOT_IMPLEMENTED_EXCEPTION); return Collections.emptyList(); } @@ -367,6 +382,7 @@ public static class Includes { private List users; private List tweets; private List media; + private List places; } @@ -515,6 +531,31 @@ public long getId() { } } + @Getter + @Setter + public static class Place { + + private Geo geo; + @JsonProperty("country_code") + private String countryCode; + private String name; + private String id; + @JsonProperty("place_type") + private String placeType; + private String country; + @JsonProperty("full_name") + private String fullName; + + @Getter + @Setter + public static class Geo { + + private String type; + private List bbox; + private JsonNode properties; + } + } + @Getter @Setter public static class MediaPublicMetricsDTO { diff --git a/src/test/java/io/github/redouane59/twitter/unit/TweetDeserializerV2Test.java b/src/test/java/io/github/redouane59/twitter/unit/TweetDeserializerV2Test.java index 193515eb..a0755bd7 100644 --- a/src/test/java/io/github/redouane59/twitter/unit/TweetDeserializerV2Test.java +++ b/src/test/java/io/github/redouane59/twitter/unit/TweetDeserializerV2Test.java @@ -10,6 +10,7 @@ import io.github.redouane59.twitter.dto.tweet.ReplySettings; import io.github.redouane59.twitter.dto.tweet.Tweet; import io.github.redouane59.twitter.dto.tweet.TweetV2; +import io.github.redouane59.twitter.dto.tweet.TweetV2.Place; import io.github.redouane59.twitter.dto.tweet.entities.HashtagEntity; import io.github.redouane59.twitter.dto.tweet.entities.MediaEntity; import io.github.redouane59.twitter.dto.tweet.entities.SymbolEntity; @@ -230,4 +231,18 @@ public void testEntitiesMedia() { assertEquals(1280, ev2.getWidth()); assertEquals("test", ev2.getAltText()); } + + @Test + public void testIncludesPlace() { + List places = tweetv2.getPlaces(); + Place place = places.get(0); + assertEquals("US", place.getCountryCode()); + assertEquals("Manhattan", place.getName()); + assertEquals("01a9a39529b27f36", place.getId()); + assertEquals("city", place.getPlaceType()); + assertEquals("United States", place.getCountry()); + assertEquals("Manhattan, NY", place.getFullName()); + assertEquals("Feature", place.getGeo().getType()); + assertTrue(place.getGeo().getBbox().size() > 0); + } } diff --git a/src/test/resources/tests/tweet_example_v2.json b/src/test/resources/tests/tweet_example_v2.json index ee26e6ed..07eaa1ce 100644 --- a/src/test/resources/tests/tweet_example_v2.json +++ b/src/test/resources/tests/tweet_example_v2.json @@ -139,6 +139,26 @@ "created_at": "2007-11-03T17:51:40.000Z", "url": "https://t.co/VDTjQvcKXp" } + ], + "places": [ + { + "geo": { + "type": "Feature", + "bbox": [ + -74.026675, + 40.683935, + -73.910408, + 40.877483 + ], + "properties": {} + }, + "country_code": "US", + "name": "Manhattan", + "id": "01a9a39529b27f36", + "place_type": "city", + "country": "United States", + "full_name": "Manhattan, NY" + } ] } } \ No newline at end of file From 4848e83204f38e7801563e27dd0cfbff80642f9a Mon Sep 17 00:00:00 2001 From: Redouane59 Date: Sat, 12 Mar 2022 09:25:39 +0100 Subject: [PATCH 3/3] update version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ad7201fd..74b1fc0b 100644 --- a/pom.xml +++ b/pom.xml @@ -194,5 +194,5 @@ https://github.com/Redouane59/twittered - 2.16-SNAPSHOT + 2.16 \ No newline at end of file