Example usage for twitter4j TwitterException exceededRateLimitation

List of usage examples for twitter4j TwitterException exceededRateLimitation

Introduction

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

Prototype

public boolean exceededRateLimitation() 

Source Link

Document

Tests if the exception is caused by rate limitation exceed

Usage

From source file:net.lacolaco.smileessence.twitter.task.DirectMessagesTask.java

License:Open Source License

@Override
protected DirectMessage[] doInBackground(Void... params) {
    ResponseList<DirectMessage> responseList;
    try {/*from w w w . j a  v a  2  s  .  c o m*/
        if (paging == null) {
            responseList = twitter.directMessages().getDirectMessages();
        } else {
            responseList = twitter.directMessages().getDirectMessages(paging);
        }
    } catch (TwitterException e) {
        e.printStackTrace();
        Logger.error(e.toString());
        if (e.exceededRateLimitation()) {
            Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT);
        } else {
            Notificator.publish(activity, R.string.notice_error_get_messages, NotificationType.ALERT);
        }
        return new DirectMessage[0];
    }
    return responseList.toArray(new DirectMessage[responseList.size()]);
}

From source file:net.lacolaco.smileessence.twitter.task.HomeTimelineTask.java

License:Open Source License

@Override
protected twitter4j.Status[] doInBackground(Void... params) {
    ResponseList<twitter4j.Status> responseList;
    try {//from w  w w .  j  a  v a  2  s . c om
        if (paging == null) {
            responseList = twitter.timelines().getHomeTimeline();
        } else {
            responseList = twitter.timelines().getHomeTimeline(paging);
        }
    } catch (TwitterException e) {
        e.printStackTrace();
        Logger.error(e.toString());
        if (e.exceededRateLimitation()) {
            Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT);
        } else {
            Notificator.publish(activity, R.string.notice_error_get_home, NotificationType.ALERT);
        }
        return new twitter4j.Status[0];
    }
    return responseList.toArray(new twitter4j.Status[responseList.size()]);
}

From source file:net.lacolaco.smileessence.twitter.task.MentionsTimelineTask.java

License:Open Source License

@Override
protected twitter4j.Status[] doInBackground(Void... params) {
    ResponseList<twitter4j.Status> responseList;
    try {/*from  ww  w . j  a va2s.  c  o m*/
        if (paging == null) {
            responseList = twitter.timelines().getMentionsTimeline();
        } else {
            responseList = twitter.timelines().getMentionsTimeline(paging);
        }
    } catch (TwitterException e) {
        e.printStackTrace();
        Logger.error(e.toString());
        if (e.exceededRateLimitation()) {
            Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT);
        } else {
            Notificator.publish(activity, R.string.notice_error_get_mentions, NotificationType.ALERT);
        }
        return new twitter4j.Status[0];
    }
    return responseList.toArray(new twitter4j.Status[responseList.size()]);
}

From source file:net.lacolaco.smileessence.twitter.task.SearchTask.java

License:Open Source License

@Override
protected QueryResult doInBackground(Void... params) {
    try {/*from   w  ww . j  a  va2  s  .  c om*/
        return twitter.search(query);
    } catch (TwitterException e) {
        e.printStackTrace();
        Logger.debug(e);
        if (e.exceededRateLimitation()) {
            Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT);
        } else {
            Notificator.publish(activity, R.string.notice_error_search, NotificationType.ALERT);
        }
        return null;
    }
}

From source file:net.lacolaco.smileessence.twitter.task.SentDirectMessagesTask.java

License:Open Source License

@Override
protected DirectMessage[] doInBackground(Void... params) {
    ResponseList<DirectMessage> responseList;
    try {/*from   ww  w  . jav a2s.  c o m*/
        if (paging == null) {
            responseList = twitter.directMessages().getSentDirectMessages();
        } else {
            responseList = twitter.directMessages().getSentDirectMessages(paging);
        }
    } catch (TwitterException e) {
        e.printStackTrace();
        Logger.error(e.toString());
        if (e.exceededRateLimitation()) {
            Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT);
        } else {
            Notificator.publish(activity, R.string.notice_error_get_messages, NotificationType.ALERT);
        }
        return new DirectMessage[0];
    }
    return responseList.toArray(new DirectMessage[responseList.size()]);
}

From source file:org.anc.lapps.datasource.twitter.TwitterDatasource.java

/** Contacts the Twitter API and gets any number of tweets corresponding to a certain query. The main
 * purpose of this function is to avoid the limit of 100 tweets that can be extracted at once.
 *
 * @param numberOfTweets the number of tweets to be printed
 * @param query the query to be searched by the twitter client
 * @param twitter the twitter client//  w ww  .  j av  a2 s .  com
 *
 * @return A JSON string containing a Data object with either a list containing the tweets as a payload
 * (when successful) or a String payload (for errors).
 */
private Data getTweetsByCount(int numberOfTweets, Query query, Twitter twitter) {
    ArrayList<Status> tweets = new ArrayList<>();
    if (!(numberOfTweets > 0)) {
        // Default of 15 tweets
        numberOfTweets = 15;
    }
    // Set the last ID to the maximum possible value as a default
    long lastID = Long.MAX_VALUE;
    int original;
    try {
        while (tweets.size() < numberOfTweets) {

            // Keep number of original to avoid infinite looping when not getting enough tweets
            original = tweets.size();
            // If there are more than 100 tweets left to be extracted, extract
            // 100 during the next query, since 100 is the limit to retrieve at once
            if (numberOfTweets - tweets.size() > 100)
                query.setCount(100);
            else
                query.setCount(numberOfTweets - tweets.size());
            // Extract tweets corresponding to the query then add them to the list
            QueryResult result = twitter.search(query);
            tweets.addAll(result.getTweets());
            // Iterate through the list and get the lastID to know where to start from
            // if there are more tweets to be extracted
            for (Status status : tweets)
                if (status.getId() < lastID)
                    lastID = status.getId();
            query.setMaxId(lastID - 1);
            // Break the loop if the tweet count didn't change. This would prevent an infinite loop when
            // tweets for the specified query are not available
            if (tweets.size() == original)
                break;
        }
    }

    catch (TwitterException te) {
        // Put the list of tweets in Data format then output as JSon String.
        // Since we checked earlier for errors, we assume that an error occuring at this point due
        // to Rate Limits is caused by a too high request. Thus, we output the retrieved tweets and log
        // the error
        String errorDataJson = generateError(te.getMessage());
        logger.error(errorDataJson);
        if (te.exceededRateLimitation() && tweets.size() > 0) {
            Data<ArrayList<Status>> tweetsData = new Data<>();
            tweetsData.setDiscriminator(Discriminators.Uri.LIST);
            tweetsData.setPayload(tweets);
            return tweetsData;
        } else {
            return Serializer.parse(errorDataJson, Data.class);
        }
    }

    // Return a special error message if no tweets are found
    if (tweets.size() == 0) {
        String noTweetsMessage = "No tweets found for the following query. "
                + "Note: Twitter's REST API only retrieves tweets from the past week.";
        String errorDataJson = generateError(noTweetsMessage);
        return Serializer.parse(errorDataJson, Data.class);
    }

    else {
        // Put the list of tweets in Data format then output as JSon String.
        Data<ArrayList<Status>> tweetsData = new Data<>();
        tweetsData.setDiscriminator(Discriminators.Uri.LIST);
        tweetsData.setPayload(tweets);
        return tweetsData;
    }
}

From source file:org.apache.streams.twitter.processor.FetchAndReplaceTwitterProcessor.java

License:Apache License

protected void fetchAndReplace(Activity doc, String originalId) {
    try {/*from  w  ww.  j  a v a2  s  .  com*/
        String json = fetch(doc);
        replace(doc, json);
        doc.setId(originalId);
        retryCount = 0;
    } catch (TwitterException tw) {
        if (tw.exceededRateLimitation()) {
            sleepAndTryAgain(doc, originalId);
        }
    } catch (Exception e) {
        LOGGER.warn("Error fetching and replacing tweet for activity {}", doc.getId());
    }
}

From source file:org.apache.streams.twitter.provider.TwitterErrorHandler.java

License:Apache License

public static int handleTwitterError(Twitter twitter, Exception exception) {
    if (exception instanceof TwitterException) {
        TwitterException e = (TwitterException) exception;
        if (e.exceededRateLimitation()) {
            LOGGER.warn("Rate Limit Exceeded");
            try {
                Thread.sleep(retry);
            } catch (InterruptedException e1) {
                Thread.currentThread().interrupt();
            }//from   w w w.j  av  a2  s .co  m
            return 1;
        } else if (e.isCausedByNetworkIssue()) {
            LOGGER.info("Twitter Network Issues Detected. Backing off...");
            LOGGER.info("{} - {}", e.getExceptionCode(), e.getLocalizedMessage());
            try {
                Thread.sleep(retry);
            } catch (InterruptedException e1) {
                Thread.currentThread().interrupt();
            }
            return 1;
        } else if (e.isErrorMessageAvailable()) {
            if (e.getMessage().toLowerCase().contains("does not exist")) {
                LOGGER.warn("User does not exist...");
                return 100;
            } else {
                return 1;
            }
        } else {
            if (e.getExceptionCode().equals("ced778ef-0c669ac0")) {
                // This is a known weird issue, not exactly sure the cause, but you'll never be able to get the data.
                return 5;
            } else {
                LOGGER.warn("Unknown Twitter Exception...");
                LOGGER.warn("  Account: {}", twitter);
                LOGGER.warn("   Access: {}", e.getAccessLevel());
                LOGGER.warn("     Code: {}", e.getExceptionCode());
                LOGGER.warn("  Message: {}", e.getLocalizedMessage());
                return 1;
            }
        }
    } else if (exception instanceof RuntimeException) {
        LOGGER.warn("TwitterGrabber: Unknown Runtime Error", exception.getMessage());
        return 1;
    } else {
        LOGGER.info("Completely Unknown Exception: {}", exception);
        return 1;
    }
}

From source file:tweetcrawling.TweetCrawler.java

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

    try {//from ww w.  j  a v  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:TwitterDownload.TwitterHandler.java

public List<Status> getSearchTweets(String searchPhrase, int pageSize) {
    if (pageSize > 18000)
        pageSize = 18000;/*  www.jav a2 s.  c  om*/

    ArrayList<Status> tweets = new ArrayList<Status>();
    //boolean last = false;

    try {
        Query query = new Query(searchPhrase);

        int limit = getRemainingSearchRateLimit();

        while (tweets.size() < pageSize && query != null && limit > 0) {
            if (pageSize - tweets.size() > 100)
                query.setCount(100);
            else
                query.setCount(pageSize - tweets.size());

            QueryResult result = twitter.search(query);

            tweets.addAll(result.getTweets());

            query = result.nextQuery();

            limit = getRemainingSearchRateLimit();
        }

        return (List<Status>) tweets;
    } catch (TwitterException ex) {
        String s = ex.toString();
        if (ex.exceededRateLimitation())
            return null;
    } catch (IllegalStateException ex) {
        String s = ex.toString();
    }
    return null;
}