1
votes

I want to stream tweets from twitter in my java application. I am currently able to do that using Twitter4J. Here is my code sample -

public static void main(String args[])
{
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("PEBF3A1wUnNLfT83jpjGBEVNn");
        cb.setOAuthConsumerSecret("Cqcuw6xyQ2tVtkGdy76s9fQuDigyDuJwxrgMETNhfuORloNFju");
        cb.setOAuthAccessToken("2492966954-Fut0P36Enh0V1UAAVODUHSTGvYKy4lscWIEpaej");
        cb.setOAuthAccessTokenSecret("x8onfYsnZvgImnyLVd1ncwvMhwNtrieU16gTkywUZOzpP");

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

        StatusListener listener = new StatusListener(){

            public void onStatus(Status status) 
            {
                System.out.println();
                System.out.println("*****************************************************************");
                User user = status.getUser();
                // gets Username
                String username = status.getUser().getScreenName();
                System.out.println(username);
                String profileLocation = user.getLocation();
                System.out.println(profileLocation);
                long tweetId = status.getId(); 
                System.out.println(tweetId);
                String content = status.getText();
                System.out.println(content +"\n");
            }
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

            public void onException(Exception ex) {
                ex.printStackTrace();
            }

            @Override
            public void onScrubGeo(long arg0, long arg1) {
                // TODO Auto-generated method stub

            }
            @Override
            public void onStallWarning(StallWarning arg0) {
                // TODO Auto-generated method stub

            }
        };

        List<String> queries = new ArrayList<String>();
        queries.add("#carb0nx");

        twitterStream.addListener(listener);
        //twitterStream.firehose(20);

        String[] trackQueries = (String[]) queries.toArray(new String[queries.size()]);

        FilterQuery filterQuery = new FilterQuery();
        twitterStream.filter(filterQuery.track(trackQueries));
}

The above program fetches only the tweets which are getting added for the hashtag after running the program. I want to get all the older tweets as well before getting the newly added tweets.

Thanks in advance.enter code here

1

1 Answers

0
votes

The code you have posted uses streaming api. So you will get only new tweets.

Check out this example for old tweets:

https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/search/SearchTweets.java

Also check out https://dev.twitter.com/docs/rate-limiting/1.1 for REST API Rate Limiting