Example usage for twitter4j TwitterException getRetryAfter

List of usage examples for twitter4j TwitterException getRetryAfter

Introduction

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

Prototype

public int getRetryAfter() 

Source Link

Document

Returns int value of "Retry-After" response header (Search API) or seconds_until_reset (REST API).

Usage

From source file:de.jetsli.twitter.TwitterSearch.java

License:Apache License

/**
 * @return a message describing the problem with twitter or an empty string
 * if nothing related to twitter!//  w  w  w.  j  a  v  a 2 s .  co m
 */
public static String getMessage(Exception ex) {
    String msg = ex.getMessage() == null ? "" : ex.getMessage();
    if (msg.length() > 100)
        msg = msg.substring(0, 100);
    if (ex instanceof TwitterException) {
        TwitterException twExc = (TwitterException) ex;
        if (twExc.exceededRateLimitation())
            return "Couldn't process your request. You don't have enough twitter API points!" + " Please wait: "
                    + twExc.getRetryAfter() + " seconds and try again! " + msg;
        else if (twExc.isCausedByNetworkIssue())
            return "Couldn't process your request. Network issue." + msg;

        return "Couldn't process your request. Something went wrong while communicating with Twitter :-/ "
                + msg;
    } else if (ex != null)
        return msg;

    return "";
}

From source file:de.jetwick.tw.TwitterSearch.java

License:Apache License

/**
 * @return a message describing the problem with twitter or an empty string
 * if nothing related to twitter!// w w  w . j ava  2 s.  c o  m
 */
public static String getMessage(Exception ex) {
    if (ex instanceof TwitterException) {
        TwitterException twExc = (TwitterException) ex;
        if (twExc.exceededRateLimitation())
            return ("Couldn't process your request. You don't have enough twitter API points!"
                    + " Please wait: " + twExc.getRetryAfter() + " seconds and try again!");
        else if (twExc.isCausedByNetworkIssue())
            return ("Couldn't process your request. Network issue.");
        else
            return ("Couldn't process your request. Something went wrong while communicating with Twitter :-/");
    }

    return "";
}

From source file:org.gabrielebaldassarre.twitter.tweet.TalendFlowQueryResultBehaviour.java

License:Open Source License

/**
 * Perform a single twitter search and stores output in target {@link TalendFlow}
 * /*from w  ww  . ja v a2s  .co m*/
 * @throws RuntimeException if something went wrong during a search
 * @throws IllegalStateException if target flow was not visited before triggering the search
 * @throws IllegalStateException if no valid query object was passed to this visitor before triggering the search
 */
@SuppressWarnings("static-access")
public void search() throws IllegalStateException {
    ResourceBundle rb = ResourceBundle.getBundle("tTwitterInput", Locale.getDefault());

    if (valid == false)
        throw new IllegalStateException(rb.getString("exception.visitTargetBefore"));
    if (q == null)
        throw new IllegalStateException(rb.getString("exception.defineQueryBefore"));

    TalendRowFactory rowFactory = target.getModel().getRowFactory();
    TalendRow current;
    List<Status> statusSet = null;
    int howMany = 0;

    try {

        qr = client.search(q);

        howMany = getLimit() - alreadyRetrieved() > qr.getTweets().size() ? qr.getTweets().size()
                : getLimit() - alreadyRetrieved();
        current = rowFactory.newRow(target);

        Iterator<Entry<TalendColumn, QueryResultField>> i = associations.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry<TalendColumn, QueryResultField> row = (Map.Entry<TalendColumn, QueryResultField>) i
                    .next();

            if (target != null && !row.getKey().getFlow().equals(target)) {
                throw new IllegalArgumentException(String.format(rb.getString("exception.columnNotInFlow"),
                        row.getKey().getName(), target.getName()));
            }

            switch (row.getValue()) {
            case COMPLETED_IN:
                current.setValue(row.getKey(), qr.getCompletedIn());
                break;
            case COUNT:
                current.setValue(row.getKey(), qr.getCount());
                break;
            case HAS_NEXT:
                current.setValue(row.getKey(), qr.hasNext());
                break;
            case MAX_ID:
                current.setValue(row.getKey(), qr.getMaxId());
                break;
            case NEXT_QUERY:
                current.setValue(row.getKey(), qr.nextQuery());
                break;
            case QUERY:
                current.setValue(row.getKey(), qr.getQuery());
                break;
            case REFRESH_URL:
                current.setValue(row.getKey(), qr.getRefreshURL());
                break;
            case SINCE_ID:
                current.setValue(row.getKey(), qr.getSinceId());
                break;
            case STATUS_SET:
                statusSet = new ArrayList<Status>(getLimit() - alreadyRetrieved() < qr.getTweets().size()
                        ? qr.getTweets().subList(0, getLimit() - alreadyRetrieved())
                        : qr.getTweets());

                current.setValue(row.getKey(), statusSet);
                break;
            default:
                throw new IllegalArgumentException(
                        String.format(rb.getString("exception.unparseableColumn"), row.getKey().getName()));

            }
        }
    } catch (TwitterException te) {
        try {
            switch (te.getStatusCode()) {
            case 503:
                /* Four seconds courtesy delay because of a fail whale */
                setChanged();
                notifyObservers(new TwitterLogger("USER_DEF_LOG", Thread.currentThread().getId(), "WARNING",
                        String.format(Locale.getDefault(), rb.getString("log.failWhale"), 4)));

                Thread.currentThread().sleep(4000);

                break;
            case 420: /* Wait a safety-interval because of a rate limit */
            case 429:
                setChanged();
                notifyObservers(new TwitterLogger("USER_DEF_LOG", Thread.currentThread().getId(), "WARNING",
                        String.format(Locale.getDefault(), rb.getString("log.rateLimit"), te.getRetryAfter())));

                Thread.currentThread().sleep(te.getRetryAfter());
                break;
            default:
                throw new RuntimeException(te);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    this.retrieved += (statusSet != null ? statusSet.size() : 0);

    setChanged();
    notifyObservers(new TwitterLogger("USER_DEF_LOG", Thread.currentThread().getId(), "INFO",
            String.format(Locale.getDefault(), rb.getString("log.searchStatus"), qr.getCompletedIn(), howMany,
                    alreadyRetrieved(), getLimit() - alreadyRetrieved())));
    if (getLimit() - alreadyRetrieved() == 0 || qr.hasNext() == false) {
        setChanged();
        notifyObservers(new TwitterLogger("USER_DEF_LOG", Thread.currentThread().getId(), "INFO",
                String.format(rb.getString("log.searchEnd"), alreadyRetrieved())));

    }
}