Skip to content

Commit

Permalink
v2.16 (#355)
Browse files Browse the repository at this point in the history
* feat: add get list member endpoint V2 (#354)

* feat: add get list member endpoint

* tests

* cred back

* back 2

* feat: add place includes (#357)

* update version
  • Loading branch information
redouane59 authored Mar 12, 2022
1 parent 2a6386a commit 4d7a12c
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 13 deletions.
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</version>
<version>2.16</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/io/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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<String, String> parameters = new HashMap<>();
parameters.put(USER_FIELDS, ALL_USER_FIELDS);
parameters.put(EXPANSION, PINNED_TWEET_ID);
private UserList getUsersRecursively(int maxResults, String url, Map<String, String> parameters) {
UserList result = UserList.builder().data(new ArrayList<>()).meta(new UserMeta()).build();
String next;

Expand Down Expand Up @@ -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<String, String> parameters = new HashMap<>();
parameters.put(USER_FIELDS, ALL_USER_FIELDS);
parameters.put(EXPANSION, PINNED_TWEET_ID);
return getUsersRecursively(maxResults, url, parameters);
}

@Override
Expand Down Expand Up @@ -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<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);
return getUsersRecursively(Integer.MAX_VALUE, url, parameters);
}

@Override
public TwitterListList getUserOwnedLists(final String userId) {
String url = getUrlHelper().getOwnedListUrl(userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -152,6 +153,11 @@ public interface Tweet {
*/
List<? extends MediaEntity> getMedia();

/**
* Get the {@link Place place} of the tweet
*/
List<Place> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,6 +121,12 @@ public List<MediaEntityV1> getMedia() {
return Collections.emptyList();
}

@Override
public List<Place> getPlaces() {
LOGGER.error(NOT_IMPLEMENTED_EXCEPTION);
return Collections.emptyList();
}

@Override
public List<StreamRule> getMatchingRules() {
LOGGER.error(NOT_IMPLEMENTED_EXCEPTION);
Expand Down
45 changes: 43 additions & 2 deletions src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,6 +148,14 @@ public List<MediaEntityV2> getMedia() {
return includes.getMedia();
}

@Override
public List<Place> getPlaces() {
if (includes == null) {
return Collections.emptyList();
}
return includes.getPlaces();
}

@Override
public int getRetweetCount() {
if (data == null) {
Expand Down Expand Up @@ -327,13 +336,19 @@ public TweetType getTweetType() {

@Override
public List<MediaEntityV2> getMedia() {
LOGGER.error(NOT_IMPLEMENTED_EXCEPTION);
LOGGER.info(NOT_IMPLEMENTED_EXCEPTION);
return Collections.emptyList();
}

@Override
public List<Place> getPlaces() {
LOGGER.info(NOT_IMPLEMENTED_EXCEPTION);
return Collections.emptyList();
}

@Override
public List<StreamRule> getMatchingRules() {
LOGGER.error(NOT_IMPLEMENTED_EXCEPTION);
LOGGER.info(NOT_IMPLEMENTED_EXCEPTION);
return Collections.emptyList();
}

Expand Down Expand Up @@ -367,6 +382,7 @@ public static class Includes {
private List<UserV2.UserData> users;
private List<TweetV2.TweetData> tweets;
private List<TweetV2.MediaEntityV2> media;
private List<TweetV2.Place> places;
}


Expand Down Expand Up @@ -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<Double> bbox;
private JsonNode properties;
}
}

@Getter
@Setter
public static class MediaPublicMetricsDTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -230,4 +231,18 @@ public void testEntitiesMedia() {
assertEquals(1280, ev2.getWidth());
assertEquals("test", ev2.getAltText());
}

@Test
public void testIncludesPlace() {
List<Place> 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);
}
}
20 changes: 20 additions & 0 deletions src/test/resources/tests/tweet_example_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
}
2 changes: 1 addition & 1 deletion test-twitter-credentials.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"apiSecretKey": "REPLACE ME",
"accessToken": "REPLACE ME",
"accessTokenSecret": "REPLACE ME"
}
}

0 comments on commit 4d7a12c

Please sign in to comment.