Search code examples
javatwittertwitter4jdata-extraction

Periodic Data Extraction From twitter4j Steaming API



I want to extract periodic data from twitter using twitter4j streaming API.
for example: I want 03/03/2014 to 04/05/2014 tweets data. I tried but not getting the expected result, Also googled for some solutions but not get the approach to resolve this.

I am trying with following code:

public class Twitter_Data {

    public static void main(String[] args) {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("myKey");
        cb.setOAuthConsumerSecret("mySecretCode");
        cb.setOAuthAccessToken("Token");
        cb.setOAuthAccessTokenSecret("secret token");
        TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
                .getInstance();

        StatusListener listener = new StatusListener() {
            @Override
            public void onStatus(Status status) {
                User user = status.getUser();

                // gets Username info
                long userId = user.getId();
                String userNm = user.getName();
                System.out.println("User ID: " + userId);
                System.out.println("User Name: " + userNm);

                String profileLocation = user.getLocation();
                System.out.println(profileLocation);

                long tweetId = status.getId();
                System.out.println(tweetId);
                String content = status.getText();
                System.out.println(status.toString() + "\n");
            }

        };
        FilterQuery fq = new FilterQuery();
        String keywords[] = { "Key word" };
        fq.track(keywords);
        twitterStream.addListener(listener);
        twitterStream.filter(fq);

    }

}

can any one help me, to extract periodic data with this code. Thanks in Advance.


Solution

  • I want to extract periodic data from twitter using twitter4j streaming API.

    You can't do that, the Streaming API is for tweets at the moment you start using it. In your example you'll get Tweets about your keyword that people are tweeting rigth now. With the Twitter API you can't get tweets for a certain period of time.