Example usage for twitter4j TwitterException getRateLimitStatus

List of usage examples for twitter4j TwitterException getRateLimitStatus

Introduction

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

Prototype

@Override
    public RateLimitStatus getRateLimitStatus() 

Source Link

Usage

From source file:net.awairo.favdler.favlist.FavoriteListService.java

License:MIT License

@Override
protected Task<FavoriteListItems> newTask() {

    return new MyTask<FavoriteListItems>() {

        @Override//w ww. j a va 2 s  .  c o  m
        protected FavoriteListItems execute() throws Exception {

            // TODO: 
            // TODO: ?

            changeState(TaskState.GET_FAVOLITES);

            val lastValue = setupAndGetCurrentList();

            ResponseList<Status> res;
            try {
                res = twitter.favorites().getFavorites(screenName, paging());
            } catch (TwitterException e) {
                val rateLimitStatus = e.getRateLimitStatus();

                if (e.getErrorCode() != RATE_LIMIT_EXCEEDED || rateLimitStatus == null)
                    throw e;

                log.warn("?????????????? {}",
                        rateLimitStatus);

                changeState(TaskState.UPDATE_VALUE);

                val ret = new FavoriteListItems(e.getRateLimitStatus());

                changeState(TaskState.FINISH);
                return ret;
            }

            changeState(TaskState.UPDATE_VALUE);

            if (!lastValue.isPresent()) {
                val ret = new FavoriteListItems(res);
                changeState(TaskState.FINISH);
                return ret;
            }

            lastValue.ifPresent(v -> v.update(res));
            changeState(TaskState.FINISH);
            return lastValue.get();
        }

        private void changeState(TaskState state) {
            updateProgress(state.ordinal(), TaskState.SIZE);
            updateTitle(RB.labelOf(state.titleKey));
            updateMessage(RB.messageOf(state.messageKey));
        }
    };
}

From source file:tweetcrawling.TweetCrawler.java

public void getTweets(TwitterConfiguration tc_) throws IOException, InterruptedException {

    try {/*from   w w  w .  j  av  a  2 s.c  o m*/

        for (String query_ : Queries) {

            // Ngambil tweet dari tiap page lalu disimpan di Statuses
            int maxTweetCrawled = 3240; // This is the number of the latest tweets that we can crawl, specified by Twitter

            Query query = new Query(query_);
            query.setLang("id");
            QueryResult result;
            do {
                rateLimitHandler(tc_, "/search/tweets"); // Check rate limit first
                //System.out.println("kanya sini");
                result = tc_.getTwitter().search(query);
                List<Status> tweets = result.getTweets();
                for (Status tweet : tweets) {
                    ArrayList<String> ValToWrite = getValueToWrite(tweet);
                    writeValue(ValToWrite, OutputFile);
                    System.out.println(
                            "@" + tweet.getUser().getScreenName() + " - " + tweet.getText().replace("\n", " "));
                }
                addStatuses(tweets);

            } while ((query = result.nextQuery()) != null);

            //printTweets(OutputFile); // Printing out crawling result per page of this keywords
            //emptyStatuses(); // Empty out the current attribute Statuses so that it can be used for other keywords    

        }

    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        if (te.exceededRateLimitation()) {
            System.out.println("Rate limit status: " + te.getRateLimitStatus());
        }
        System.exit(-1);
    }

}

From source file:twitter4j.examples.lambda.RateLimitLambda.java

License:Apache License

public static void main(String... args) {
    Twitter twitter = TwitterFactory.getSingleton();
    twitter.onRateLimitStatus(//from  w  w w .  ja va2s .  c om
            e -> System.out.println("rate limit remaining: " + e.getRateLimitStatus().getRemaining()));
    for (int i = 0; i < 20; i++) {
        try {
            System.out.println(twitter.getHomeTimeline());
        } catch (TwitterException e) {
            e.printStackTrace();
        }
    }
}

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  .j  av a  2  s.  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;
}