Thursday, 18 December 2014

Java code to extract tweets since any date using Twitter4J API


/* Below is the code to extract tweets since any date or any given date  using twitter 4J API */       


         Twitter twitter = new TwitterFactory().getInstance();
         twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
         String accessToken = 'asdasdsdsaasdsaedadasdadsadasd'; /* enter your twitter access token */
        String accessTokenSecret = '2020202-asdsadsadasdsadsadsadsa'; /enter your twitter secret token */
         AccessToken oathAccessToken = new AccessToken(accessToken, accessTokenSecret);
        twitter.setOAuthAccessToken(oathAccessToken);
        Query query = new Query("Iron Man");
        Date date = new Date();
        String modifiedDate= new SimpleDateFormat("yyyymmdd").format(date);
        query.setSince(modifiedDate);
        QueryResult result;
            do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                 System.out.println("@"+tweet.getUser().getScreenName() + "|" + tweet.getText()+"|"+ tweet.isRetweeted() );
                 }
              } while ((query = result.nextQuery()) != null);


To create twitter app account and get the access tokens, check out the link below:
http://dwbitechguru.blogspot.ca/2014/12/how-to-create-twitter-app-account-and.html