Example usage for twitter4j Twitter getRateLimitStatus

List of usage examples for twitter4j Twitter getRateLimitStatus

Introduction

In this page you can find the example usage for twitter4j Twitter getRateLimitStatus.

Prototype

Map<String, RateLimitStatus> getRateLimitStatus() throws TwitterException;

Source Link

Document

Returns the current rate limits for methods belonging to the specified resource families.
Each 1.1 API resource belongs to a "resource family" which is indicated in its method documentation.

Usage

From source file:account.GetRateLimitStatus.java

License:Apache License

public static ConstVars getRateLimit(String[] args) {

    ConstVars StaticVars = new ConstVars();

    try {/*ww  w .  j  av a2 s. co  m*/

        // init Twitter OAuth
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey(args[1]).setOAuthConsumerSecret(args[2])
                .setOAuthAccessToken(args[3]).setOAuthAccessTokenSecret(args[4]);

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        // it returns RateLimits of all end-points
        Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus();

        // get RateLimit of required end-point
        RateLimitStatus status = rateLimitStatus.get(args[0]);
        String Endpoint = args[0];
        int Limit = status.getLimit();

        int Remaining = status.getRemaining();
        int ResetTimeInSeconds = status.getResetTimeInSeconds();
        int SecondsUntilReset = status.getSecondsUntilReset();

        // set and return rate limit info to ConstVars's variables
        StaticVars.Endpoint = Endpoint;
        StaticVars.Limit = Limit;
        StaticVars.Remaining = Remaining;
        StaticVars.ResetTimeInSeconds = ResetTimeInSeconds;
        StaticVars.SecondsUntilReset = SecondsUntilReset;

    } catch (TwitterException te) {
        if (args.length == 6) {
            System.err
                    .println("Failed to get rate limit status of " + args[5] + " because: " + te.getMessage());
        } else {
            System.err.println("Failed to get rate limit status because: " + te.getMessage());
        }
    }
    return StaticVars;
}

From source file:com.avishkar.NewGetFollowersIDs.java

License:Apache License

private static int checkRateLimit(String uri, Twitter twitter) throws TwitterException, InterruptedException {
    System.out.println("Checking rate limit for URI:" + uri);
    Map<String, RateLimitStatus> rateMap = twitter.getRateLimitStatus();
    RateLimitStatus rateLimit = rateMap.get(uri);
    RateLimitStatus apprateLimit = rateMap.get("/application/rate_limit_status");
    if (apprateLimit.getRemaining() <= 1) {
        consoleTimerWaitForRate("/application/rate_limit_status", apprateLimit);
    }/*from   w  w w .j  a v  a2  s .com*/
    if (apprateLimit.getRemaining() <= 1) {
        System.out.println("Rate Limit URI fetch exhausted. Check cancelled. Operation may fail.");
    }
    System.out.println("Rate Limit remaining:" + rateLimit.getRemaining());
    if (rateLimit.getRemaining() == 0) {
        consoleTimerWaitForRate(uri, rateLimit);
    }
    return rateLimit.getRemaining();
}

From source file:com.mobilesolutionworks.android.twitter.TwitterPluginFragment.java

License:Apache License

protected Task<Map<String, RateLimitStatus>> doCheckStatus(final Twitter instance) {
    final Task<Map<String, RateLimitStatus>>.TaskCompletionSource source = Task.create();

    new AsyncTask<Void, Void, Object>() {

        @Override/*from   ww w  . jav  a 2 s  .  c  o  m*/
        protected Object doInBackground(Void... params) {
            try {
                return instance.getRateLimitStatus();
            } catch (TwitterException e) {
                return e;
            }
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if (o instanceof Map) {
                source.trySetResult((Map<String, RateLimitStatus>) o);
            } else {
                source.trySetError((Exception) o);
            }

        }
    }.execute();
    return source.getTask();
}

From source file:examples.GetRateLimitStatus.java

License:Apache License

public static void main(String[] args) {
    try {//from ww  w .  j ava  2s . c  om

        Twitter twitter = CommonUtils.getTwitterInstance();

        Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus();
        for (String endpoint : rateLimitStatus.keySet()) {
            RateLimitStatus status = rateLimitStatus.get(endpoint);
            if (endpoint.equals("/users/lookup")) {
                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());
            }
        }
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get rate limit status: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:mineTwit.Main.java

License:Open Source License

private void updateStatus(Twitter twitter, String newMessage) {
    if (twitter != null) {
        // Check newMessage
        try {/*from  w w  w .j a  va  2s . c  o m*/
            // Debug code to check twitter rate limits
            Map<String, RateLimitStatus> rateLimit = twitter.getRateLimitStatus();
            for (String endpoint : rateLimit.keySet()) {
                RateLimitStatus status = rateLimit.get(endpoint);
                //Test line to remove later
                //getLogger().info("Got rateLimits.endpoints");
                //Omit any endpoints that haven't moved from default limit
                if (status.getRemaining() != status.getLimit()) {
                    getLogger().info("Endpoint: " + endpoint);
                    getLogger().info(" Limit: " + status.getLimit());
                    getLogger().info(" Remaining: " + status.getRemaining());
                    getLogger().info(" ResetTimeInSeconds: " + status.getResetTimeInSeconds());
                    getLogger().info(" SecondsUntilReset: " + status.getSecondsUntilReset());
                }
            }
            boolean rateLimited = false;
            //Test line for debugging
            getLogger().info(" Duplicate Array value is : " + myNotifications[8].status);
            // Check if rateLimited by any particular endpoint.
            if (!rateLimited) {
                //Tweet if duplicates are off AND not duplicate AND not rate limited
                if (myNotifications[8].status) {
                    getLogger().info("Duplicates are true.\n Who cares what the new message is.");
                    twitter.updateStatus(newMessage + "\n" + new Date());
                    // Tweet anyway if duplicates are on AND not ratelimited
                } else if (!myNotifications[8].status && !newMessage.equals(getCurrentStatus(twitter))) {
                    getLogger().info("Duplicates are false.");
                    getLogger().info("Latest is ''" + newMessage + "''");
                    getLogger().info("Last was ''" + getCurrentStatus(twitter) + "''");
                    twitter.updateStatus(newMessage + "\n" + new Date());
                } else {
                    getLogger().info("Duplicates are false and message is duplicate");
                }
            } else {
                getLogger().info("Twitter is rate limited, not tweeting");
            }
        } catch (TwitterException e) {
            getLogger().info("Twitter is broken because of " + e);
            throw new RuntimeException(e);
        }
    }
}

From source file:twitter4j.examples.account.GetRateLimitStatus.java

License:Apache License

/**
 * Usage: java twitter4j.examples.account.GetRateLimitStatus
 *
 * @param args message//from  www. ja v a 2s.c  o  m
 */
public static void main(String[] args) {
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        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());
        }
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get rate limit status: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:uk.ac.susx.tag.method51.webapp.handler.TwitterPinAuthHandler.java

License:Apache License

private void getRateLimitInfo(String target, Request baseRequest, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    final Params params = new Params();
    params.addValidator("id", new SystemStringValidator(true));

    new DoSomethingAfterValidatingMyParams(params, request, response) {
        @Override/*  w w w  .j ava  2s  .c o  m*/
        public void something() throws IOException {
            final String id = request.getParameter("id");
            final Twitter twitter = newTwitterInstance(ApiKeyStore.getKey(id));

            try {
                final Map<String, RateLimitStatus> status = twitter.getRateLimitStatus();
                okHereIsYourJson(response, entriesToArray(status.entrySet()));
            } catch (TwitterException e) {
                LOG.warn("Failed to retrieve rate limit information.", e);
                error(e.getMessage());
            }
        }
    };
}