Example usage for twitter4j TwitterException getErrorCode

List of usage examples for twitter4j TwitterException getErrorCode

Introduction

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

Prototype

public int getErrorCode() 

Source Link

Usage

From source file:com.jeanchampemont.wtfdyum.service.impl.TwitterServiceImpl.java

License:Apache License

@Override
public List<User> getUsers(final Principal principal, final long... ids) throws WTFDYUMException {
    final List<User> result = new ArrayList<>();
    if (ids.length == 0) {
        return result;
    }/*from   w ww .  ja  va  2  s.  c o m*/
    try {
        final List<twitter4j.User> users = new ArrayList<>();
        for (int i = 0; i <= (ids.length - 1) / 100; i++) {
            final ResponseList<twitter4j.User> lookupUsers = twitter(principal).users()
                    .lookupUsers(Arrays.copyOfRange(ids, i * 100, Math.min((i + 1) * 100, ids.length)));

            users.addAll(lookupUsers);
        }

        for (final twitter4j.User u : users) {
            result.add(mapper.map(u, User.class));
        }

    } catch (final TwitterException e) {
        if (e.getErrorCode() == 17) {
            log.debug("Error while getUsers for ids: " + Arrays.toString(ids)
                    + ". Seems like those users are not on twitter anymore.");
        } else {
            log.debug("Error while getUsers for ids: " + Arrays.toString(ids), e);
            throw new WTFDYUMException(e, WTFDYUMExceptionType.TWITTER_ERROR);
        }
    }
    return result;
}

From source file:com.yfiton.notifiers.twitter.TwitterNotifier.java

License:Apache License

@Override
protected void notify(Parameters parameters) throws NotificationException {
    try {/*from  w  w w.j  av  a 2s  .  c  om*/
        StatusUpdate statusUpdate = new StatusUpdate(status);
        Status status = twitter.updateStatus(statusUpdate);

        log.info("https://twitter.com/" + status.getUser().getScreenName() + "/status/" + status.getId());
    } catch (TwitterException e) {
        throw new NotificationException(new String[] { e.getErrorMessage(),
                "Error code " + e.getErrorCode() + ": https://dev.twitter.com/overview/api/response-codes" },
                e);
    }
}

From source file:crawler.DataSearch.java

License:Apache License

public void collectNewTweets() {

    while (keywords.size() > 0) {
        String hash = keywords.get(0);
        Query query = new Query(hash);
        query.setCount(Settings.searchTweetNo);
        QueryResult result;/*from   ww  w.ja  v a 2 s  . c  o m*/

        try {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            if (tweets.size() == 0)
                TwitterCrawler.TFFreq.remove(hash);
            for (Status status : tweets) {
                //System.out.println("{"+hash+"}["+status.getCreatedAt().toString()+"]- [USER: " + status.getUser().getScreenName() + "] - " + status.getText());
                String temp;
                if (trash(status.getText())) {
                    if (Settings.limitTime) {
                        long currentTime = System.currentTimeMillis();
                        long tweetTime = status.getCreatedAt().getTime();

                        if (currentTime - tweetTime > Settings.timeLimit)
                            continue;
                    }
                    if (hashtext.containsKey(hash))
                        temp = hashtext.get(hash) + status.getText();
                    else
                        temp = status.getText();
                    hashtext.put(hash, temp);
                }
            }
            //System.out.println("{"+hash+"} with "+tweets.size()+"tweets");
            keywords.remove(0);
        } catch (TwitterException e) {
            if (e.getErrorCode() == 88) {
                System.out.println("Using other keys???");

                String ConsumerKey_temp = Settings.ConsumerKey.get(0);
                String ConsumerSecret_temp = Settings.ConsumerSecret.get(0);
                String AccessToken_temp = Settings.AccessToken.get(0);
                String AccessSecret_temp = Settings.AccessSecret.get(0);

                Settings.ConsumerKey.remove(ConsumerKey_temp);
                Settings.ConsumerSecret.remove(ConsumerSecret_temp);
                Settings.AccessToken.remove(AccessToken_temp);
                Settings.AccessSecret.remove(AccessSecret_temp);

                twitter = new TwitterFactory(
                        OA.build(Settings.ConsumerKey.get(0), Settings.ConsumerSecret.get(0),
                                Settings.AccessToken.get(0), Settings.AccessSecret.get(0))).getInstance();

                Settings.ConsumerKey.add(Settings.ConsumerKey.size(), ConsumerKey_temp);
                Settings.ConsumerSecret.add(Settings.ConsumerSecret.size(), ConsumerSecret_temp);
                Settings.AccessToken.add(Settings.AccessToken.size(), AccessToken_temp);
                Settings.AccessSecret.add(Settings.AccessSecret.size(), AccessSecret_temp);

                System.out.println("**************************************************************");
                System.out.println("[" + ConsumerKey_temp + "] to [" + Settings.ConsumerKey.get(0) + "]");
                System.out.println("[" + ConsumerSecret_temp + "] to [" + Settings.ConsumerSecret.get(0) + "]");
                System.out.println("[" + AccessToken_temp + "] to [" + Settings.AccessToken.get(0) + "]");
                System.out.println("[" + AccessSecret_temp + "] to [" + Settings.AccessSecret.get(0) + "]");
                System.out.println("**************************************************************");

                continue;
            }
            e.printStackTrace();
        }
    }

}

From source file:de.vanita5.twittnuker.util.Utils.java

License:Open Source License

public static String getTwitterErrorMessage(final Context context, final CharSequence action,
        final TwitterException te) {
    if (context == null)
        return null;
    if (te == null)
        return context.getString(R.string.error_unknown_error);
    if (te.exceededRateLimitation()) {
        final RateLimitStatus status = te.getRateLimitStatus();
        final long secUntilReset = status.getSecondsUntilReset() * 1000;
        final String nextResetTime = ParseUtils
                .parseString(getRelativeTimeSpanString(System.currentTimeMillis() + secUntilReset));
        if (isEmpty(action))
            return context.getString(R.string.error_message_rate_limit, nextResetTime.trim());
        return context.getString(R.string.error_message_rate_limit_with_action, action, nextResetTime.trim());
    } else if (te.getErrorCode() > 0) {
        final String msg = StatusCodeMessageUtils.getTwitterErrorMessage(context, te.getErrorCode());
        return getErrorMessage(context, action, msg != null ? msg : trimLineBreak(te.getMessage()));
    } else if (te.getCause() instanceof SSLException) {
        final String msg = te.getCause().getMessage();
        if (msg != null && msg.contains("!="))
            return getErrorMessage(context, action, context.getString(R.string.ssl_error));
        else/*from w  w w.ja va 2s  .c om*/
            return getErrorMessage(context, action, context.getString(R.string.network_error));
    } else if (te.getCause() instanceof IOException)
        return getErrorMessage(context, action, context.getString(R.string.network_error));
    else if (te.getCause() instanceof JSONException)
        return getErrorMessage(context, action, context.getString(R.string.api_data_corrupted));
    else
        return getErrorMessage(context, action, trimLineBreak(te.getMessage()));
}

From source file:de.vanita5.twittnuker.util.Utils.java

License:Open Source License

public static String getTwitterErrorMessage(final Context context, final TwitterException te) {
    if (te == null)
        return null;
    if (StatusCodeMessageUtils.containsTwitterError(te.getErrorCode()))
        return StatusCodeMessageUtils.getTwitterErrorMessage(context, te.getErrorCode());
    else if (StatusCodeMessageUtils.containsHttpStatus(te.getStatusCode()))
        return StatusCodeMessageUtils.getHttpStatusMessage(context, te.getStatusCode());
    else/*from www  . ja v a 2 s.com*/
        return te.getMessage();
}

From source file:de.vanita5.twittnuker.util.Utils.java

License:Open Source License

public static void showTwitterErrorMessage(final Context context, final CharSequence action,
        final TwitterException te, final boolean long_message) {
    if (context == null)
        return;//from  www  .  j ava 2  s  .c om
    final String message;
    if (te != null) {
        if (action != null) {
            if (te.exceededRateLimitation()) {
                final RateLimitStatus status = te.getRateLimitStatus();
                final long sec_until_reset = status.getSecondsUntilReset() * 1000;
                final String next_reset_time = ParseUtils
                        .parseString(getRelativeTimeSpanString(System.currentTimeMillis() + sec_until_reset));
                message = context.getString(R.string.error_message_rate_limit_with_action, action,
                        next_reset_time.trim());
            } else if (isErrorCodeMessageSupported(te)) {
                final String msg = StatusCodeMessageUtils.getMessage(context, te.getStatusCode(),
                        te.getErrorCode());
                message = context.getString(R.string.error_message_with_action, action,
                        msg != null ? msg : trimLineBreak(te.getMessage()));
            } else if (te.getCause() instanceof SSLException) {
                final String msg = te.getCause().getMessage();
                if (msg != null && msg.contains("!=")) {
                    message = context.getString(R.string.error_message_with_action, action,
                            context.getString(R.string.ssl_error));
                } else {
                    message = context.getString(R.string.error_message_with_action, action,
                            context.getString(R.string.network_error));
                }
            } else if (te.getCause() instanceof IOException) {
                message = context.getString(R.string.error_message_with_action, action,
                        context.getString(R.string.network_error));
            } else {
                message = context.getString(R.string.error_message_with_action, action,
                        trimLineBreak(te.getMessage()));
            }
        } else {
            message = context.getString(R.string.error_message, trimLineBreak(te.getMessage()));
        }
    } else {
        message = context.getString(R.string.error_unknown_error);
    }
    showErrorMessage(context, message, long_message);
}

From source file:de.vanita5.twittnuker.util.Utils.java

License:Open Source License

private static boolean isErrorCodeMessageSupported(final TwitterException te) {
    if (te == null)
        return false;
    return StatusCodeMessageUtils.containsHttpStatus(te.getStatusCode())
            || StatusCodeMessageUtils.containsTwitterError(te.getErrorCode());
}

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

License:MIT License

@Override
protected Task<FavoriteListItems> newTask() {

    return new MyTask<FavoriteListItems>() {

        @Override//from   w w  w. j  a  v a  2 s.  com
        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:TwitterLookUpUsers.LookupUsers.java

License:Apache License

public static void main(String[] args) throws InterruptedException {

    Postgresql.DBBaglan();//from  w w  w.  j a v  a  2 s . c o m
    Postgresql.DBSelect();
    Twitter twitter = new TwitterFactory().getInstance();

    for (int j = 0; j < sonuc.length; j++) {

        try {
            User user = twitter.showUser(sonuc[j]);
            if (user.getStatus() != null) {
                System.out.println((j + 1) + ". Sorgu___________________________________");
                System.out.println("@" + user.getScreenName());
                System.out.println(user.getTimeZone());
                System.out.println("@" + user.getName());
                System.out.println("@" + user.getURL());
                System.out.println("LOKASYONN  : " + user.getLocation());
                System.out.println("@" + user.getDescription());
                System.out.println("@" + user.getOriginalProfileImageURL());
                System.out.println("@" + user.getMiniProfileImageURL());
                System.out.println("@" + user.getProfileBackgroundImageURL());
                System.out.println("_______________________________________________");

                if (lokasyon[j].contains(user.getLocation()))

                {
                    System.out.println("KULLANICI BU OLABLR*************************");

                }
            } else {

                // the user is protected
                System.out.println((j + 1) + ". Sorgu______________________________________");
                System.out.println("@" + user.getScreenName() + "---- KULLANICI PUBLIC DEL");
                System.out.println("___________________________________________________");
            }

        }

        catch (TwitterException te)

        {
            te.printStackTrace();

            if ((te.getErrorCode() == 88)) {
                System.err.println("EXCEED..............");
                Thread.sleep(900000); //wait 15 min
            }
            System.out.println();

        }

    }

    System.exit(0);
}

From source file:TwitterUserTimelineTweets.timeliner.java

public static void main(String[] args) throws JSONException, ParseException, InterruptedException {

    Postgresql.DBBaglan();// ww w  . j av a  2 s.c  om
    Postgresql.DBSelect();
    Twitter twitter = new TwitterFactory().getInstance();
    int pageno = 1;
    //String user = "gasanyasan";
    List statuses = new ArrayList();

    for (int i = 0; i < 5; i++) {
        System.out.println("ARANAN KULLANICI  :  " + sonuc[i]);
        while (true) {
            try {
                int size = statuses.size();
                Paging page = new Paging(pageno++, 100);
                statuses.addAll(twitter.getUserTimeline(sonuc[i], page));
                if (statuses.size() == size)

                    break;
            } catch (TwitterException e) {

                if (e.getErrorCode() == 88) {
                    System.out.println("SORGU LMT AILDI.....UYKUYA GRYOR...");
                    Thread.sleep(900000);

                }

                //e.printStackTrace();
            }
        }
    }

    for (Object statuse : statuses) {
        Status a = (Status) statuse;
        System.out.println(a.getText());
        System.out.println(a.getCreatedAt());
        System.out.println(a.getUser().getScreenName());
        System.out.println(a.getId());
    }

    System.out.println("Total: " + statuses.size());
    // System.out.println(stats.get(0).getText());     

}