What I am doing?
I am trying to build an application with a User Interface. The user will enter the search term and then using a websocket connection, I would start returning the filtered tweets to UI.
What I did?
I have a TwitterFilter
class that looks like
public TwitterFilter(String searchTerm) {
final ConfigurationBuilder configurationBuilder = getConfigurationBuilder();
twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
final StatusListener statusListener = getStatusListener();
filterQuery = new FilterQuery();
filterQuery.track(new String[] { searchTerm });
filterQuery.language(new String[] { "en" });
twitterStream.addListener(statusListener);
}
public void getFilteredTweets() {
twitterStream.filter(filterQuery);
}
private static StatusListener getStatusListener() {
return new StatusListener() {
@Override
public void onStatus(final Status status) {
System.out.println(status.getText());
}
@Override
public void onDeletionNotice(final StatusDeletionNotice statusDeletionNotice) {
}
@Override
public void onTrackLimitationNotice(final int i) {
}
@Override
public void onScrubGeo(final long userId, final long upToStatusId) {
}
@Override
public void onStallWarning(final StallWarning stallWarning) {
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
}
};
}
private static ConfigurationBuilder getConfigurationBuilder() {
final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setDebugEnabled(true);
configurationBuilder.setOAuthConsumerKey(CONSUMER_KEY);
configurationBuilder.setOAuthConsumerSecret(CONSUMER_SECRET);
configurationBuilder.setOAuthAccessToken(ACCESS_TOKEN);
configurationBuilder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
return configurationBuilder;
}
and a class with websocket code, that needs to return tweets filtered by Twitter, looks as
@ServerEndpoint("/tweets")
public class TweetStreamServer {
@OnMessage
public void tweets(final String message, final Session client) throws IOException, InterruptedException {
final TwitterFilter twitterFilter = new TwitterFilter("india");
for (final Session peer: client.getOpenSessions()) {
peer.getBasicRemote().sendText(twitterFilter.getFilteredTweets()); // compilation error
}
}
}
Problem?
Since neither filter()
or sample()
methods return anything( void
), how do I return the tweets? ( documentation )?
This program implements streaming api. Your use case requires user to enter the search term and you have to return the tweets with that search term.
Possible Implementations:
1. Use rest api:
Use oauth and login using user's credentials, then return the tweets you get.
Rate limitations: https://dev.twitter.com/docs/rate-limiting/1.1/limits You can get only 180 tweets every 15 minutes (per user).
2. Streaming api:
This is not a rest api search. When you track particular keyword or follow an user, you get the tweets whenever they are tweeted (live). This is different from searching. You cannot know when a twitter user tweets using the keywords you are tracking. Since this is a live stream.
Start the stream with possible keywords. Save the results in a database and search the database when you get a request from user. When user enters a new keyword, stop the stream, add the new keyword to the list of keywords already tracking and start the stream. [You can only have one connection to streaming api from one IP address].
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.