Skip to content
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

feat: user timeline v2 #102

Merged
merged 3 commits into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;

public interface ITwitterClientV1 {

/**
* Like a tweet calling https://api.twitter.com/1.1/favorites/create.json
*
Expand Down Expand Up @@ -184,24 +184,5 @@ public interface ITwitterClientV1 {
*/
List<Tweet> getMentionsTimeline(int count, String maxId);

/**
* Get the 200 most recent Tweets posted by the user calling https://api.twitter.com/1.1/statuses/user_timeline.json
*
* @param userId the id of the user
* @return a list of the 200 most recent Tweets posted by the user
*/
List<Tweet> getUserTimeline(String userId);

/**
* Get the most recent Tweets posted by the user calling https://api.twitter.com/1.1/statuses/user_timeline.json
*
* @param userId the id of the user
* @param count Specifies the number of Tweets to try and retrieve, up to a maximum of 200 per distinct request
* @param maxId Returns results with an ID less than (that is, older than) or equal to the specified ID.
* @return a list of the most recent Tweets posted by the user
*/
List<Tweet> getUserTimeline(String userId, int count, String maxId);


}

11 changes: 11 additions & 0 deletions src/main/java/com/github/redouane59/twitter/ITwitterClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,16 @@ public interface ITwitterClientV2 {
*/
void startSampledStream(Consumer<Tweet> consumer);

/**
* Get the most recent Tweets posted by the user calling https://api.twitter.com/2/users/:id/tweets (time & tweet id arguments can be null)
*
* @return a list of the most recent Tweets posted by the user
*/
List<Tweet> getUserTimeline(String userId, int nbTweets);

/**
* Get the most recent Tweets posted by the user calling https://api.twitter.com/2/users/:id/tweets
*/
List<Tweet> getUserTimeline(String userId, int nbTweets, LocalDateTime startTime, LocalDateTime endTime, String sinceId, String untilId);
}

50 changes: 23 additions & 27 deletions src/main/java/com/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.github.redouane59.twitter.dto.tweet.HiddenResponse;
import com.github.redouane59.twitter.dto.tweet.HiddenResponse.HiddenData;
import com.github.redouane59.twitter.dto.tweet.Tweet;
import com.github.redouane59.twitter.dto.tweet.TweetListV2;
import com.github.redouane59.twitter.dto.tweet.TweetSearchResponse;
import com.github.redouane59.twitter.dto.tweet.TweetSearchResponseV1;
import com.github.redouane59.twitter.dto.tweet.TweetSearchResponseV2;
Expand All @@ -42,12 +41,10 @@
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import lombok.Getter;
Expand Down Expand Up @@ -110,22 +107,6 @@ private List<String> getUserIdsByRelation(String url) {
return result;
}

private Set<String> getUserIdsByRelationSet(String url) {
String cursor = "-1";
Set<String> result = new HashSet<>();
do {
String urlWithCursor = url + "&" + CURSOR + "=" + cursor;
Optional<IdList> idListResponse = this.requestHelperV2.getRequest(urlWithCursor, IdList.class);
if (idListResponse.isEmpty()) {
break;
}
result.addAll(idListResponse.get().getIds());
cursor = idListResponse.get().getNextCursor();
}
while (!cursor.equals("0"));
return result;
}

// can manage up to 200 results/call . Max 15 calls/15min ==> 3.000 results max./15min
private List<User> getUsersInfoByRelation(String url) {
String token = null;
Expand Down Expand Up @@ -295,7 +276,7 @@ public Tweet getTweet(String tweetId) {
@Override
public List<Tweet> getTweets(List<String> tweetIds) {
String url = this.getUrlHelper().getTweetListUrl(tweetIds);
List<TweetData> result = this.requestHelperV2.getRequest(url, TweetListV2.class).orElseThrow(NoSuchElementException::new).getData();
List<TweetData> result = this.requestHelperV2.getRequest(url, TweetSearchResponseV2.class).orElseThrow(NoSuchElementException::new).getData();
return result.stream().map(tweetData -> TweetV2.builder().data(tweetData).build()).collect(Collectors.toList());
}

Expand Down Expand Up @@ -519,16 +500,31 @@ public List<Tweet> getMentionsTimeline(int count, String maxId) {
}

@Override
public List<Tweet> getUserTimeline(final String userId) {
int maxCount = 200;
String url = this.urlHelper.getUserTimelineUrl(userId, maxCount);
return List.of(this.requestHelper.getRequest(url, TweetV1[].class).orElseThrow(NoSuchElementException::new));
public List<Tweet> getUserTimeline(final String userId, int nbTweets) {
return this.getUserTimeline(userId, nbTweets, null, null, null, null);
}

@Override
public List<Tweet> getUserTimeline(final String userId, final int count, final String maxId) {
String url = this.urlHelper.getUserTimelineUrl(userId, count, maxId);
return List.of(this.requestHelper.getRequest(url, TweetV1[].class).orElseThrow(NoSuchElementException::new));
public List<Tweet> getUserTimeline(String userId, int nbTweets, LocalDateTime startTime, LocalDateTime endTime, String sinceId, String untilId) {
String token = null;
List<Tweet> result = new ArrayList<>();
int apiResultLimit = 100;
int missingTweets = nbTweets;
do {
String url = this.urlHelper.getUserTimelineUrl(userId, Math.min(apiResultLimit, missingTweets), startTime, endTime, sinceId, untilId);
if (token != null) {
url = url + "&" + PAGINATION_TOKEN + "=" + token;
}
Optional<TweetSearchResponseV2> tweetListDTO = this.requestHelperV2.getRequest(url, TweetSearchResponseV2.class);
if (tweetListDTO.isEmpty() || tweetListDTO.get().getData() == null) {
break;
}
result.addAll(tweetListDTO.get().getData());
token = tweetListDTO.get().getMeta().getNextToken();
missingTweets -= apiResultLimit;
}
while (token != null && missingTweets > 0);
return result;
}


Expand Down

This file was deleted.

24 changes: 18 additions & 6 deletions src/main/java/com/github/redouane59/twitter/helpers/URLHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.redouane59.twitter.helpers;

import java.time.LocalDateTime;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class URLHelper {
public static final String GET_BEARER_TOKEN_URL = "https://api.twitter.com/oauth2/token";
public static final String GET_OAUTH1_TOKEN_URL = "https://api.twitter.com/oauth/request_token";
public static final String GET_OAUTH1_ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
private static final String MAX_RESULTS = "max_results";


public String getSearchTweet30DaysUrl(String envName) {
Expand Down Expand Up @@ -290,11 +292,21 @@ public String getMentionsTimelineUrl(int count, String maxId) {
+ "&" + MAX_ID + "=" + maxId;
}

public String getUserTimelineUrl(final String userId, final int count) {
return ROOT_URL_V1 + STATUSES + "/user_timeline.json?user_id=" + userId + "&" + COUNT + "=" + count;
}

public String getUserTimelineUrl(final String userId, final int count, final String maxId) {
return ROOT_URL_V1 + STATUSES + "/user_timeline.json?user_id=" + userId + "&" + COUNT + "=" + count + "&" + MAX_ID + "=" + maxId;
public String getUserTimelineUrl(String userId, int maxResult, LocalDateTime startTime, LocalDateTime endTime, String sinceId, String untilId) {
String result = ROOT_URL_V2 + USERS + "/" + userId + TWEETS + "?" + MAX_RESULTS + "=" + maxResult;
if (startTime != null) {
result += "&start_time=" + ConverterHelper.getStringFromDateV2(startTime);
}
if (endTime != null) {
result += "&end_time=" + ConverterHelper.getStringFromDateV2(endTime);
}
if (sinceId != null) {
result += "&since_id=" + sinceId;
}
if (untilId != null) {
result += "&until_id=" + untilId;
}
result += "&" + ALL_TWEET_FIELDS;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,6 @@ public void testGetMentionsTimelineWithMaxId() {
assertTrue(result.size() > 0);
}

@Test
public void testGetUserTimeline() {
List<Tweet> result = twitterClient.getUserTimeline("1120050519182016513");
assertTrue(result.size() > 0);
}

@Test
public void testGetUserTimelineWithMaxId() {
List<Tweet> result = twitterClient.getUserTimeline("1120050519182016513", 10, "1300007914674040832");
assertTrue(result.size() > 0);
}


/*

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,34 @@ public void testGetBearerToken() {
assertTrue(token.length() > 50);
}

@Test
public void testGetUserTimeline() {
List<? extends Tweet> result = twitterClient.getUserTimeline("1120050519182016513", 150);
assertEquals(result.size(), 150);
assertNotNull(result.get(0).getId());
assertNotNull(result.get(0).getText());
}

@Test
public void testGetUserTimelineWithDates() {
List<? extends Tweet> result = twitterClient.getUserTimeline("1120050519182016513", 20,
ConverterHelper.dayBeforeNow(5),
ConverterHelper.dayBeforeNow(1),
null, null);
assertEquals(result.size(), 20);
assertNotNull(result.get(0).getId());
assertNotNull(result.get(0).getText());
}

@Test
public void testGetUserTimelineWithIds() {
List<? extends Tweet> result = twitterClient.getUserTimeline("1307302673318895621", 10,
null, null,
"1339662509201121280",
"1339667017109032966");
assertEquals(result.size(), 2);
assertNotNull(result.get(0).getId());
assertNotNull(result.get(0).getText());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.github.redouane59.twitter.helpers.URLHelper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -81,12 +82,6 @@ public void testUrlGetRateLimitStatus() {
URLHelper.RATE_LIMIT_URL);
}

@Test
public void testGetUserTweetUrlById() {
assertEquals("https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=12345&count=1&trim_user=true&include_rts=false",
urlHelper.getUserTweetsUrl("12345", 1));
}

@Test
public void testSearchTweetsUrlStandard() {
https:
Expand Down Expand Up @@ -232,14 +227,16 @@ public void testGetMentionsTimelineUrlWithMaxId() {

@Test
public void testGetUserTimelineUrl() {
assertEquals("https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=99999&count=200",
urlHelper.getUserTimelineUrl("99999", 200));
assertEquals("https://api.twitter.com/2/users/99999/tweets?max_results=200&" + URLHelper.ALL_TWEET_FIELDS,
urlHelper.getUserTimelineUrl("99999", 200, null, null, null, null));
}

@Test
public void testGetUserTimelineUrlWithMaxId() {
assertEquals("https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=99999&count=10&max_id=12345",
urlHelper.getUserTimelineUrl("99999", 10, "12345"));
public void testGetUserTimelineUrlWithDates() {
assertEquals(
"https://api.twitter.com/2/users/99999/tweets?max_results=100&start_time=2020-01-01T00:00:00.000Z&end_time=2020-02-01T00:00:00.000Z&"
+ URLHelper.ALL_TWEET_FIELDS,
urlHelper.getUserTimelineUrl("99999", 100, LocalDateTime.of(2020, 1, 1, 0, 0), LocalDateTime.of(2020, 2, 1, 0, 0), null, null));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.redouane59.twitter.unit;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.github.redouane59.twitter.TwitterClient;
import com.github.redouane59.twitter.dto.tweet.TweetSearchResponseV2;
import java.io.File;
import java.io.IOException;
import org.junit.jupiter.api.Test;

public class UserTimelineDeserializerV2Test {

private File tweetFile1 = new File(getClass().getClassLoader().getResource("tests/user_timeline_example_v2.json").getFile());
private TweetSearchResponseV2 tweetListV2 = TwitterClient.OBJECT_MAPPER.readValue(tweetFile1, TweetSearchResponseV2.class);

public UserTimelineDeserializerV2Test() throws IOException {
}

@Test
public void testSize() {
assertEquals(10, tweetListV2.getData().size());
}

@Test
public void testId() {
assertEquals("1339667017109032966", tweetListV2.getData().get(0).getId());
}

@Test
public void testText() {
assertEquals("@RedTheOne 🤖 : Salut!", tweetListV2.getData().get(0).getText());
}

}
Loading