0

I used the code in the first solution provided here How to retrieve more than 100 results using Twitter4j

But it just retrieves the repetition of the first 100 tweets.

I also tried to setSince and setUntil dates, but also it retrieves the repetition.

Any help please! Thanks.

Community
  • 1
  • 1
Heba
  • 319
  • 1
  • 3
  • 11

1 Answers1

0

If you want to get a continious stream of tweets based on some query filter,Implement StatusListener Class in Twitter4j. Also implement onStatus(Status status) function.Below is an example -

import twitter4j.*;

public class TwitterListener implements StatusListener {
    public void onException(Exception ex) {
        ex.printStackTrace();
    }
    public void onStatus(Status status) {
        if(status.getUser().getLang().equalsIgnoreCase("en")
                || status.getUser().getLang().equalsIgnoreCase("en_US")) {

            String twitterStream = String.valueOf(status.getId())
                + "\t" + status.getUser().getScreenName()
                + "\t" + status.getText()
                + "\t" + status.getCreatedAt();

            System.out.println(twitterStream);
        }
    }

    public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
    }
    public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
    }
    public void onScrubGeo(long userId, long upToStatusId) {
    }
    public void onStallWarning(StallWarning warning) {
    }
}

Create a class to initialize and hook to Twitter Status Stream, the code would be something like below -

ConfigurationBuilder twitterConfigBuilder = new ConfigurationBuilder();
twitterConfigBuilder.setOAuthConsumerKey("consumerKey");
twitterConfigBuilder.setOAuthConsumerSecret("consumerSecret");
twitterConfigBuilder.setOAuthAccessToken("accessToken");
twitterConfigBuilder.setOAuthAccessTokenSecret("accessTokenSecret");

TwitterListener twitterListener = new TwitterListener();

TwitterStream twitterStream
        = new TwitterStreamFactory(twitterConfigBuilder.build()).getInstance();

// Register your Listener
twitterStream.addListener(twitterListener);

FilterQuery query = new FilterQuery();
ArrayList<String> trackList = new ArrayList<String>();
// TODO, add the list of words you want to track
query.track(trackList.toArray(new String[trackList.size()]));

twitterStream.filter(query);

You should now be able to get a continious stream of tweets, you can experiment more on kind of filter you want to apply.

mbaxi
  • 1,301
  • 1
  • 8
  • 28
  • Please, is there a way to filter the returned tweets from tweets contain links? I tried to add to the trackList "-filter:links test", where test is the word I want to track, but it did not work. Note: "-filter:links test" works fine as a normal query in searching. – Heba Sep 28 '14 at 05:23
  • Do you want to get only those tweets which have "test" in its content?Just add "test" to trackList. Check the search query rules here - https://dev.twitter.com/rest/public/search – mbaxi Sep 28 '14 at 16:57
  • Yes, I want to get tweets that have "test" and not contain a link. So the search query is "-filter:links test". I tried to add this query to trackList but it did not work. On the other hand, the query of word "test" works fine when I add it to the trackList (and returns tweets with/without links). – Heba Sep 28 '14 at 17:16
  • 1
    @user1320659 - To use such a filter specification, you would need to use Class Query[Search API], but its default limit is 15 per request and 100 per page; You would need to devise a logic to cache max_id,since_id and reset them after each call/request. Alternatively you can use Streaming API Class FilterQuery and write a code to reject the tweets containing urls/links using some regex mapping to identify the url. – mbaxi Oct 01 '14 at 14:02