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

Fix authorization for searchTweets/Add non recursive options for searchTweets and getUserTimeline #459

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ This project is a JAVA library which allows you to consume the Twitter API.

### Configuration

![Maven Central](https://img.shields.io/maven-central/v/io.github.redouane59.twitter/twittered)
![Maven Central](https://img.shields.io/maven-central/v/io.github.hemisphire.twitter/twittered)

In your pom.xml, add the following dependency and replace `VERSION` with the version you wish:

```xml
<dependency>
<groupId>io.github.redouane59.twitter</groupId>
<groupId>io.github.hemisphire.twitter</groupId>
<artifactId>twittered</artifactId>
<version>VERSION</version>
</dependency>
Expand All @@ -30,7 +30,7 @@ repositories {
Then add the following line to your `dependencies` block:

```kotlin
implementation("io.github.redouane59.twitter:twittered:VERSION")
implementation("io.github.hemisphire.twitter:twittered:VERSION")
```

To be able to see library logs, also add sl4j references :
Expand Down
20 changes: 17 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@
<groupId>org.apache.maven.plugins</groupId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -101,7 +115,7 @@
<artifactId>lombok</artifactId>
<groupId>org.projectlombok</groupId>
<scope>provided</scope>
<version>1.18.22</version>
<version>1.18.30</version>
</dependency>
<dependency>
<artifactId>scribejava-apis</artifactId>
Expand Down Expand Up @@ -159,7 +173,7 @@
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<groupId>io.github.redouane59.twitter</groupId>
<groupId>io.github.hemisphire.twitter</groupId>

<licenses>
<license>
Expand Down Expand Up @@ -197,5 +211,5 @@

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

<version>2.23</version>
<version>2.26</version>
</project>
34 changes: 34 additions & 0 deletions src/main/java/io/github/redouane59/twitter/ITwitterClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ public interface ITwitterClientV2 {
*/
TweetList searchTweets(String query, AdditionalParameters additionalParameters);

/**
* Search tweets from last 7 days calling https://api.twitter.com/2/tweets/search
*
* @param query the search query
* @return a TweetList object containing a list of tweets
*/
TweetList searchTweetsNonRecursively(String query);

/**
* Search tweets from last 7 days calling https://api.twitter.com/2/tweets/search
*
* @param query the search query
* @param additionalParameters accepted parameters are recursiveCall, startTime, endTime, sinceId, untilId, maxResults
* @return a TweetList object containing a list of tweets and the next token if recursiveCall is set to false
*/
TweetList searchTweetsNonRecursively(String query, AdditionalParameters additionalParameters);

/**
* Search archived tweets calling https://api.twitter.com/2/tweets/search/all
*
Expand Down Expand Up @@ -304,6 +321,23 @@ public interface ITwitterClientV2 {
*/
TweetList getUserTimeline(String userId, AdditionalParameters additionalParameters);

/**
* Get the most recent Tweets posted by the user calling https://api.twitter.com/2/users/:id/tweets
*
* @param userId Unique identifier of the Twitter account (user ID) for whom to return results.
* @return a TweetList object containing a list of tweets
*/
TweetList getUserTimelineNonRecursively(String userId);

/**
* 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)
*
* @param userId identifier of the Twitter account (user ID) for whom to return results.
* @param additionalParameters accepted parameters recursiveCall, startTime, endTime, sinceId, untilId, maxResults
* @return a TweetList object containing a list of tweets and the next token if recursiveCall is set to false
*/
TweetList getUserTimelineNonRecursively(String userId, AdditionalParameters additionalParameters);

/**
* Get the most recent mentions received posted by the user calling https://api.twitter.com/2/users/:id/mentions
*
Expand Down
76 changes: 74 additions & 2 deletions src/main/java/io/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -968,12 +968,35 @@ public TweetList searchTweets(String query, AdditionalParameters additionalParam
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS);
String url = urlHelper.getSearchRecentTweetsUrl();
if (!additionalParameters.isRecursiveCall()) {
return getRequestHelper().getRequestWithParameters(url, parameters, TweetList.class).orElseThrow(NoSuchElementException::new);
return getRequestHelperV2().getRequestWithParameters(url, parameters, TweetList.class).orElseThrow(NoSuchElementException::new);
}
if (additionalParameters.getMaxResults() <= 0) {
parameters.put(MAX_RESULTS, String.valueOf(100));
}
return getTweetsRecursively(url, parameters, getRequestHelper());
return getTweetsRecursively(url, parameters, getRequestHelperV2());
}

@Override
public TweetList searchTweetsNonRecursively(String query) {
return searchTweetsNonRecursively(query, AdditionalParameters.builder().maxResults(100).build());
}

@Override
public TweetList searchTweetsNonRecursively(String query, AdditionalParameters additionalParameters) {
Map<String, String> parameters = additionalParameters.getMapFromParameters();
parameters.put(QUERY, query);
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS);
parameters.put(USER_FIELDS, ALL_USER_FIELDS);
parameters.put(EXPANSION, ALL_EXPANSIONS);
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS);
String url = urlHelper.getSearchRecentTweetsUrl();
if (!additionalParameters.isRecursiveCall()) {
return getRequestHelperV2().getRequestWithParameters(url, parameters, TweetList.class).orElseThrow(NoSuchElementException::new);
}
if (additionalParameters.getMaxResults() <= 0) {
parameters.put(MAX_RESULTS, String.valueOf(100));
}
return getTweetsOnce(url, parameters, getRequestHelperV2());
}

@Override
Expand Down Expand Up @@ -1004,6 +1027,31 @@ public TweetList searchAllTweets(final String query, AdditionalParameters additi
return getTweetsRecursively(url, parameters, getRequestHelperV2());
}

/**
* Call an endpoint related to tweets recursively until next_token is null to provide a full result
*/
private TweetList getTweetsOnce(String url, Map<String, String> parameters, AbstractRequestHelper requestHelper) {
String next;
TweetList result = TweetList.builder().data(new ArrayList<>()).meta(new TweetMeta()).build();
String newestId = null;
Optional<TweetList> tweetList = requestHelper.getRequestWithParameters(url, parameters, TweetList.class);
if (!tweetList.isPresent() || tweetList.get().getData() == null) {
result.getMeta().setNextToken(null);
return result;
}
result.getData().addAll(tweetList.get().getData());
newestId = tweetList.get().getMeta().getNewestId();
TweetMeta meta = TweetMeta.builder()
.resultCount(result.getData().size())
.oldestId(tweetList.get().getMeta().getOldestId())
.newestId(newestId)
.nextToken(tweetList.get().getMeta().getNextToken())
.build();
result.setMeta(meta);
result.setIncludes(tweetList.get().getIncludes());
return result;
}

/**
* Call an endpoint related to tweets recursively until next_token is null to provide a full result
*/
Expand Down Expand Up @@ -1282,6 +1330,30 @@ public TweetList getUserTimeline(String userId, AdditionalParameters additionalP
return getTweetsRecursively(url, parameters, getRequestHelperV2());
}

@Override
public TweetList getUserTimelineNonRecursively(final String userId) {
return getUserTimelineNonRecursively(userId, AdditionalParameters.builder().maxResults(100).build());
}

@Override
public TweetList getUserTimelineNonRecursively(String userId, AdditionalParameters additionalParameters) {
Map<String, String> parameters = additionalParameters.getMapFromParameters();
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS);
parameters.put(USER_FIELDS, ALL_USER_FIELDS);
parameters.put(PLACE_FIELDS, ALL_PLACE_FIELDS);
parameters.put(POLL_FIELDS, ALL_POLL_FIELDS);
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS);
parameters.put(EXPANSION, ALL_EXPANSIONS);
String url = urlHelper.getUserTimelineUrl(userId);
if (!additionalParameters.isRecursiveCall()) {
return getRequestHelperV2().getRequestWithParameters(url, parameters, TweetList.class).orElseThrow(NoSuchElementException::new);
}
if (additionalParameters.getMaxResults() <= 0) {
parameters.put(MAX_RESULTS, String.valueOf(100));
}
return getTweetsOnce(url, parameters, getRequestHelperV2());
}

@Override
public TweetList getUserMentions(final String userId) {
return getUserMentions(userId, AdditionalParameters.builder().maxResults(100).build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.redouane59.twitter.dto.tweet;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.jackson.Jacksonized;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Jacksonized
public class AdditionalProperties {

@JsonProperty("media_keys")
private String[] mediaKeys;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.redouane59.twitter.dto.tweet;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -17,6 +18,6 @@
public class Attachments {

@JsonProperty("media_keys")
private String[] mediaKeys;
private List<String> mediaKeys;

}
24 changes: 24 additions & 0 deletions src/main/java/io/github/redouane59/twitter/dto/tweet/Reposts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.redouane59.twitter.dto.tweet;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.github.redouane59.twitter.dto.tweet.AdditionalProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.jackson.Jacksonized;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Jacksonized
public class Reposts {

@JsonProperty("media_keys")
private int count;
private AdditionalProperties additionalProperties;

}
15 changes: 14 additions & 1 deletion src/main/java/io/github/redouane59/twitter/dto/tweet/Tweet.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public interface Tweet {
/**
* Get the attachments of the tweet
*/
Attachments getAttachments();
List<Attachments> getAttachments();

/**
* Get the source label of the tweet
Expand Down Expand Up @@ -164,5 +164,18 @@ public interface Tweet {
*/
List<StreamRules.StreamRule> getMatchingRules();

/**
* Get the additional properties of the tweet
*/
AdditionalProperties getAdditionalProperties();

/**
* Get the reposts of the tweet
*/
Reposts getReposts();

/**
* Get the edit history of the tweet
*/
List<String> getEditHistoryTweetIds();
}
22 changes: 20 additions & 2 deletions src/main/java/io/github/redouane59/twitter/dto/tweet/TweetV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,21 @@ public Geo getGeo() {
}

@Override
public Attachments getAttachments() {
public AdditionalProperties getAdditionalProperties() {
LOGGER.error(NOT_IMPLEMENTED_EXCEPTION);
return new Attachments();
return new AdditionalProperties();
}

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

@Override
public Reposts getReposts() {
LOGGER.error(NOT_IMPLEMENTED_EXCEPTION);
return new Reposts();
}

@Override
Expand All @@ -115,6 +127,12 @@ public String getSource() {
return null;
}

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

@Override
public List<MediaEntityV1> getMedia() {
if (entities != null) {
Expand Down
Loading