Example usage for twitter4j RateLimitStatus getResetTimeInSeconds

List of usage examples for twitter4j RateLimitStatus getResetTimeInSeconds

Introduction

In this page you can find the example usage for twitter4j RateLimitStatus getResetTimeInSeconds.

Prototype

int getResetTimeInSeconds();

Source Link

Document

Returns the seconds the current rate limiting period ends.
This should be a same as getResetTime().getTime()/1000.

Usage

From source file:account.GetRateLimitStatus.java

License:Apache License

public static ConstVars getRateLimit(String[] args) {

    ConstVars StaticVars = new ConstVars();

    try {//from w w  w .ja va2 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.appdynamics.alerts.twitter.TwitterAlert.java

License:Apache License

/**
 * Check if rate limit has been reached/*  w w  w .j  av  a2s .  c o  m*/
 *
 * @return true if reached, false otherwise
 */
private static boolean isLimitReached() {
    RateLimitStatus status = readRateLimitStatus();
    if (status != null && status.getResetTimeInSeconds() > System.currentTimeMillis() / 1000) {
        return true;
    }
    return false;
}

From source file:demo.UserFollowers.java

License:Apache License

static List<Long> followers(Twitter twitter, String screenName) {
    List<Long> m_FollowersList = new ArrayList<Long>();

    long cursor = -1;
    //int count = 0;
    while (true) {
        IDs ids = null;/*from  ww  w .  j  av a 2  s . c o m*/
        try {
            //IDs ids = twitter.getFollowersIDs(user.getId(), cursor);
            ids = twitter.getFollowersIDs(screenName, cursor);
        } catch (TwitterException twitterException) {
            // Rate Limit ?????????
            // (memo) status code ?????????

            RateLimitStatus rateLimit = twitterException.getRateLimitStatus();
            int secondsUntilReset = rateLimit.getSecondsUntilReset();
            System.err.println("please wait for " + secondsUntilReset + " seconds");
            System.err.println("Reset Time : " + rateLimit.getResetTimeInSeconds());

            //() 120?getSecondsUntilReset() ?????????????
            // long waitTime = (long)(secondsUntilReset + 120) * 1000;
            long waitTime = (long) (300 * 1000); // 300
            try {
                Thread.sleep(waitTime);
            } catch (Exception e) {
                e.printStackTrace();
            }

            continue;
        } catch (Exception e) {
            e.printStackTrace();
        }

        long[] idArray = ids.getIDs();
        for (int i = 0; i < idArray.length; i++) {
            //System.out.println("["+(++count)+"]" + idArray[i]);
            m_FollowersList.add(new Long(idArray[i]));
        }

        if (ids.hasNext()) {
            cursor = ids.getNextCursor();
        } else {
            break;
        }
    }
    return m_FollowersList;
}

From source file:examples.GetRateLimitStatus.java

License:Apache License

public static void main(String[] args) {
    try {/*from w w w  . j  a v a  2  s  .co  m*/

        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   www .  ja v a2s . co  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:net.awairo.favdler.twitter.FavoriteListItems.java

License:MIT License

private void updateRateLimitStatus(RateLimitStatus status) {
    limit = status.getLimit();//from  ww  w.j av  a 2  s.  c  o  m
    remaining = status.getRemaining();
    resetTimeInSeconds = status.getResetTimeInSeconds();
    secondsUntilReset = status.getSecondsUntilReset();
}

From source file:tweetcrawling.Main.java

public static void getRateLimitStatuses(TwitterConfiguration tc_) {
    try {//from   ww  w .j av  a  2  s  .co m
        //          Twitter twitter = new TwitterFactory().getInstance();
        Map<String, RateLimitStatus> rateLimitStatus = tc_.getTwitter().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:twitter4j.examples.account.GetRateLimitStatus.java

License:Apache License

/**
 * Usage: java twitter4j.examples.account.GetRateLimitStatus
 *
 * @param args message/*  www . j  av a2  s. 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:twitterrest.Followersids.java

License:Apache License

static List<Long> followers(Twitter twitter, String screenName) {
    List<Long> m_FollowersList = new ArrayList<Long>();

    long cursor = -1;
    //int count = 0;
    while (true) {
        IDs ids = null;/*from  w  ww .jav  a 2s . co m*/
        try {
            //IDs ids = twitter.getFollowersIDs(user.getId(), cursor);
            ids = twitter.getFollowersIDs(screenName, cursor);
        } catch (TwitterException twitterException) {
            // Rate Limit ?????????
            // () status code ?????????

            RateLimitStatus rateLimit = twitterException.getRateLimitStatus();
            int secondsUntilReset = rateLimit.getSecondsUntilReset();
            System.err.println("please wait for " + secondsUntilReset + " seconds");
            System.err.println("Reset Time : " + rateLimit.getResetTimeInSeconds());

            //() 120?getSecondsUntilReset() ????
            //?????????
            long waitTime = (long) (secondsUntilReset + 120) * 1000;
            //long waitTime = (long)(300 * 1000); // 300?
            try {
                Thread.sleep(waitTime);
            } catch (Exception e) {
                e.printStackTrace();
            }

            continue;
        } catch (Exception e) {
            e.printStackTrace();
        }

        long[] idArray = ids.getIDs();
        for (int i = 0; i < idArray.length; i++) {
            //System.out.println("["+(++count)+"]" + idArray[i]);
            m_FollowersList.add(new Long(idArray[i]));
        }

        if (ids.hasNext()) {
            cursor = ids.getNextCursor();
        } else {
            break;
        }
    }
    return m_FollowersList;
}