Example usage for twitter4j TwitterException getMessage

List of usage examples for twitter4j TwitterException getMessage

Introduction

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

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:Jums.SearchTweets.java

License:Apache License

/**
 * Usage: java twitter4j.examples.search.SearchTweets [query]
 *
 * @param args search query//from   ww w.  j a va 2  s  . co m
 */
public void main(String args) {
    System.out.println("java twitter4j.examples.search.SearchTweets [query]");
    System.exit(-1);

    Twitter twitter = new TwitterFactory().getInstance();
    try {
        Query query = new Query(args);
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
            }
        } while ((query = result.nextQuery()) != null);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:mashup.MashTweetManager.java

public static ArrayList<String> getTweets(String topic) throws IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthAccessToken(Common.AccessToken)
            .setOAuthAccessTokenSecret(Common.AccessTokenSecret).setOAuthConsumerKey(Common.ConsumerKey)
            .setOAuthConsumerSecret(Common.ConsumerSecret);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();//from  w  w w  .  ja  v a  2  s . co m

    //Twitter twitter = new TwitterFactory().getInstance();

    //  twitter.setOAuthAccessToken(null);

    ArrayList<String> tweetList = new ArrayList<>();

    try {
        Query query = new Query(topic.toLowerCase().trim());

        query.setCount(100);
        query.setLocale("en");
        query.setLang("en");

        QueryResult result = null;

        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();

            for (Status tweet : tweets) {
                String data = tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", "");

                HinghlishStopWords.removeStopWords(data.trim());
                // Remove Special... Characters 
                // Remove stop words 
                tweetList.add(tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", ""));

            }

        } while ((query = result.nextQuery()) != null);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
    return tweetList;
}

From source file:net.chrissearle.spring.twitter.spring.Twitter4jDirectMessageService.java

License:Apache License

private void sendMessage(String friend, String text) {
    try {/*from  w  ww . j  a v  a2 s. co  m*/
        if (canSendTo(friend)) {
            twitter.sendDirectMessage(friend, text);
        } else {
            final String message = new StringBuilder().append("Unable to DM ").append(friend)
                    .append(" - not friends").toString();

            if (logger.isLoggable(Level.WARNING)) {
                logger.warning(message);
            }

            throw new TwitterServiceException(message);
        }
    } catch (TwitterException e) {
        final String message = new StringBuilder().append("Unable to DM ").append(friend)
                .append(" with message ").append(text).append(" due to ").append(e.getMessage()).toString();

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(message);
        }

        throw new TwitterServiceException(message, e);
    }
}

From source file:net.chrissearle.spring.twitter.spring.Twitter4jFollowService.java

License:Apache License

private List<String> retrieveFollowing() {
    List<String> usernames = new ArrayList<String>();

    try {//from   www.j  a v a2 s.  co m

        usernames = populateUsernameList(twitter.getFriendsIDs());
    } catch (TwitterException e) {
        final String message = new StringBuilder().append("Unable to retrieve user details due to ")
                .append(e.getMessage()).toString();

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(message);
        }

        throw new TwitterServiceException(message, e);
    }

    return usernames;
}

From source file:net.chrissearle.spring.twitter.spring.Twitter4jFollowService.java

License:Apache License

private List<String> retrieveFollowers() {
    List<String> usernames = new ArrayList<String>();

    try {/*from ww w. j  a  v  a2s  .c o m*/

        usernames = populateUsernameList(twitter.getFollowersIDs());
    } catch (TwitterException e) {
        final String message = new StringBuilder().append("Unable to retrieve user details due to ")
                .append(e.getMessage()).toString();

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(message);
        }

        throw new TwitterServiceException(message, e);
    }

    return usernames;
}

From source file:net.chrissearle.spring.twitter.spring.Twitter4jFollowService.java

License:Apache License

private boolean checkForFriendship(String twitterId) {
    try {//from www .  j a v a 2 s.  c om
        return alreadyFriends(twitterId);
    } catch (TwitterException e) {
        final String message = new StringBuilder().append("Unable to check isFriend ").append(twitterId)
                .append(" due to ").append(e.getMessage()).toString();

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(message);
        }

        throw new TwitterServiceException(message, e);
    }
}

From source file:net.chrissearle.spring.twitter.spring.Twitter4jFollowService.java

License:Apache License

private void addTwitterFriendshipAndNotification(String twitterId) {
    try {/* w w w.  ja v  a 2 s  .  co  m*/
        establishFriendshipWithNotification(twitterId);
    } catch (TwitterException e) {
        final String message = new StringBuilder().append("Unable to follow ").append(twitterId)
                .append(" due to ").append(e.getMessage()).toString();

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(message);
        }

        throw new TwitterServiceException(message, e);
    }
}

From source file:net.chrissearle.spring.twitter.spring.Twitter4jFollowService.java

License:Apache License

private void removeTwitterFriendshipAndNotification(String twitterId) {
    try {/*  w w w.jav a 2 s  .co m*/
        removeFriendshipWithNotification(twitterId);
    } catch (TwitterException e) {
        final String message = new StringBuilder().append("Unable to stop following ").append(twitterId)
                .append(" due to ").append(e.getMessage()).toString();

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(message);
        }

        throw new TwitterServiceException(message, e);
    }
}

From source file:net.chrissearle.spring.twitter.spring.Twitter4jTweetService.java

License:Apache License

private void updateTwitterStatus(String text) {
    try {/*from   www  .j a  va 2 s . co m*/
        twitter.updateStatus(text);
    } catch (TwitterException e) {
        if (e.getMessage().indexOf("Status is a duplicate") >= 0) {
            // A duplicate post means that the tweet wanted is the latest on the timeline.
            if (logger.isLoggable(Level.INFO)) {
                logger.info("Tweet was a duplicate");
            }

            return;
        }

        final String message = new StringBuilder().append("Unable to tweet: ").append(text).append(" due to ")
                .append(e.getMessage()).toString();

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(message);
        }

        throw new TwitterServiceException(message, e);
    }
}

From source file:net.chrissearle.spring.twitter.spring.Twitter4jUserExistanceService.java

License:Apache License

private boolean askTwitterForUser(String twitterId) {
    try {/*from  ww  w  . j  a va2  s  .  c  o  m*/
        twitter.showUser(twitterId);

        return true;
    } catch (TwitterException e) {
        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(new StringBuilder().append("Twitter failed: ").append(e.getMessage()).toString());
        }

        return false;
    }
}