Display Twitter Rate Limit Status - Java Social Media

Java examples for Social Media:Twitter

Description

Display Twitter Rate Limit Status

Demo Code



import twitter4j.*;
import twitter4j.auth.AccessToken;

import java.util.Map;


public class Test3 {
    private static String consumerKey = "key";
    private static String consumerSecret = "key";
    private static String accessToken = "token";
    private static String accessTokenSecret = "token";

    public static void main(String[] args) throws InterruptedException {
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        twitter.setOAuthAccessToken(new AccessToken(accessToken,
                accessTokenSecret));/*from w  w w . j av a2 s.  c  o  m*/

        try {
            long cursor = -1;
            IDs ids;
            for (int i = 0; i < 2; i++) {
                showLimits(twitter);
                ids = twitter.getFollowersIDs(cursor);
                for (long id : ids.getIDs()) {
                    System.out.println(id);
                }
                System.out.println(ids.getNextCursor());
            }
        } catch (TwitterException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public static void showLimits(Twitter twitter) {
        try {
            Map<String, RateLimitStatus> rateLimitStatus = twitter
                    .getRateLimitStatus();
            for (String endpoint : rateLimitStatus.keySet()) {
                RateLimitStatus status = rateLimitStatus.get(endpoint);
                System.out.println("Endpoint: " + endpoint);
                System.out.println(" Limit: " + status.getLimit());
                System.out.println(" Remaining: " + status.getRemaining());
                System.out.println(" ResetTimeInSeconds: "
                        + status.getResetTimeInSeconds());
                System.out.println(" SecondsUntilReset: "
                        + status.getSecondsUntilReset());
            }
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get rate limit status: "
                    + te.getMessage());
        }
    }
}

Related Tutorials