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:com.ibm.TwitterAsyncService.java

License:Open Source License

@Override
public void run() {
    String bookTitle = ac.getRequest().getParameter("title");
    String bookAuthor = ac.getRequest().getParameter("author");
    boolean translate = Boolean.parseBoolean(ac.getRequest().getParameter("enable"));

    logger.debug("Requested book title {} and author {}", bookTitle, bookAuthor);

    Query query = new Query(bookTitle + " " + bookAuthor);
    query.setResultType(Query.RECENT);

    Twitter twitter = TwitterFactory.getSingleton();

    try {//from  w  w w .j  a v a 2 s .c  om
        PrintWriter writer = ac.getResponse().getWriter();
        Locale locale = ac.getRequest().getLocale();
        WatsonTranslate watson = new WatsonTranslate(locale);

        // Just get the first page of results to avoid exceeding the Twitter rate limit
        QueryResult result = twitter.search(query);

        Alchemy alchemy = new Alchemy();

        List<Status> tweets = result.getTweets();

        logger.debug("Current tweets {}", tweets.toString());

        for (Status tweetMessage : tweets) {
            JSONObject json = new JSONObject();
            JSONObject tweet = new JSONObject();
            // We need to put the tweet and link into an inner object
            // so that we can use a special formatter in bootstrap table

            json.put("screenName", tweetMessage.getUser().getScreenName());

            if (translate) {
                String message = watson.translate(tweetMessage.getText());
                tweet.put("message", message);
                json.put("sentiment", alchemy.getSentiment(message));
            } else {
                tweet.put("message", tweetMessage.getText());
                json.put("sentiment", alchemy.getSentiment(tweetMessage.getText()));
            }

            String url = "https://twitter.com/" + tweetMessage.getUser().getScreenName() + "/status/"
                    + tweetMessage.getId();

            tweet.put("link", url);

            json.put("tweet", tweet);

            writer.write(("data: " + json.toString() + "\n\n"));
            writer.flush();
        }

        writer.write(("event: finished\n"));
        writer.write(("data: \n\n"));
        writer.flush();
        writer.close();
    } catch (TwitterException e) {
        logger.error("Twitter Error {}", e.getMessage());
    } catch (IOException e) {
        logger.error("could not write SSE {}", e.getMessage());
    } catch (NullPointerException e) {
        logger.error("Exception Twitter Async Service {}", e.getMessage());
    } finally {
        ac.complete();
    }
}

From source file:com.ikungolf.java.javatwitter.directmessage.DestroyDirectMessage.java

License:Apache License

/**
 * Usage: java twitter4j.examples.directmessages.DestroyDirectMessage [message id]
 *
 * @param args message// ww w. j av  a2s . c  om
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.directmessages.DestroyDirectMessage [message id]");
        System.exit(-1);
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.destroyDirectMessage(Long.parseLong(args[0]));
        System.out.println("Successfully deleted message [" + args[0] + "].");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to delete message: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.ikungolf.java.javatwitter.directmessage.GetDirectMessages.java

License:Apache License

private String getLastestMessage() {
    Twitter twitter = new TwitterFactory().getInstance();
    String msg = new String();
    try {/*from  w  ww . j  a  va 2 s. com*/
        Paging paging = new Paging(1);
        List<DirectMessage> messages;

        messages = twitter.getDirectMessages(paging);
        System.out.println(messages.size());
        DirectMessage dm = messages.get(0);

        System.out.println("Message: " + dm.getText());
        messageId = dm.getId();
        if (messageId != tempMsgId) {
            tempMsgId = messageId;
        }
        msg = dm.getText();

    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get messages: " + te.getMessage());
        System.exit(-1);
    }

    return msg;
}

From source file:com.ikungolf.java.javatwitter.directmessage.GetSentDirectMessages.java

License:Apache License

/**
 * Usage: java twitter4j.examples.directmessages.GetSentDirectMessages
 *
 * @param args message//from  w  w  w.  jav a  2s  . c  o m
 */
public static void main(String[] args) {
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        Paging page = new Paging(1);
        List<DirectMessage> directMessages;
        do {
            directMessages = twitter.getSentDirectMessages(page);
            for (DirectMessage message : directMessages) {
                System.out.println("To: @" + message.getRecipientScreenName() + " id:" + message.getId() + " - "
                        + message.getText());
            }
            page.setPage(page.getPage() + 1);
        } while (directMessages.size() > 0 && page.getPage() < 10);
        System.out.println("done.");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get sent messages: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.ikungolf.java.javatwitter.directmessage.ShowDirectMessage.java

License:Apache License

/**
 * Usage: java twitter4j.examples.directmessage.ShowDirectMessage [message id]
 *
 * @param args String[]/*from   w  w  w  .j  av  a 2 s.co  m*/
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.directmessage.ShowDirectMessage [message id]");
        System.exit(-1);
    }
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        DirectMessage message = twitter.showDirectMessage(Long.parseLong(args[0]));
        System.out.println("From: @" + message.getSenderScreenName() + " id:" + message.getId() + " - "
                + message.getText());
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get message: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.ikungolf.java.javatwitter.GetHomeTimeline.java

License:Apache License

/**
 * Usage: java twitter4j.examples.timeline.GetHomeTimeline
 *
 * @param args String[]//w w  w  . j  a v a2  s  .co m
 */
public static void main(String[] args) {
    try {
        // gets Twitter instance with default credentials
        Twitter twitter = new TwitterFactory().getInstance();
        User user = twitter.verifyCredentials();
        List<Status> statuses = twitter.getHomeTimeline();
        System.out.println("Showing @" + user.getScreenName() + "'s home timeline.");
        for (Status status : statuses) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.ikungolf.java.javatwitter.GetMentions.java

License:Apache License

/**
 * Usage: java twitter4j.examples.timeline.GetMentions
 *
 * @param args String[]/*from  ww  w . j a  v a 2  s .  c  o m*/
 */
public static void main(String[] args) {
    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        User user = twitter.verifyCredentials();
        List<Status> statuses = twitter.getMentionsTimeline();
        System.out.println("Showing @" + user.getScreenName() + "'s mentions.");
        for (Status status : statuses) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.ikungolf.java.javatwitter.GetUserTimeline.java

/**
 * Usage: java twitter4j.examples.timeline.GetUserTimeline
 *
 * @param args String[]//from  ww  w.  j  a  v  a2  s.c om
 */
public static void main(String[] args) {
    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        List<Status> statuses;
        String user;
        //            if (args.length == 1) {
        //                user = args[0];
        user = "WebUpd8";
        statuses = twitter.getUserTimeline(user);
        //            } else {
        //                user = twitter.verifyCredentials().getScreenName();
        //                statuses = twitter.getUserTimeline();
        //            }
        System.out.println("Showing @" + user + "'s user timeline.");
        for (Status status : statuses) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.ikungolf.java.javatwitter.twitterCmd.java

public Status getLastestMessage() {
    String msg = new String();
    Status s = null;//from w  w  w  . j ava 2  s  . c  om
    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        User user = twitter.verifyCredentials();
        List<Status> statuses = twitter.getMentionsTimeline();

        s = statuses.get(0);
        msg = s.getText();
        System.out.println("Message: " + s.getText());
        //            for (Status status : statuses) {
        //                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        //            }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }

    return s;
}

From source file:com.ikungolf.java.javatwitter.twitterCmd.java

public void updateStatus(String msg) {
    if (msg == null) {
        System.out.println("Usage: java twitter4j.examples.tweets.UpdateStatus [text]");
        System.exit(-1);/*from   ww  w  .  j  av  a2  s .co  m*/
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        try {
            // get request token.
            // this will throw IllegalStateException if access token is already available
            RequestToken requestToken = twitter.getOAuthRequestToken();
            System.out.println("Got request token.");
            System.out.println("Request token: " + requestToken.getToken());
            System.out.println("Request token secret: " + requestToken.getTokenSecret());
            AccessToken accessToken = null;

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            while (null == accessToken) {
                System.out.println("Open the following URL and grant access to your account:");
                System.out.println(requestToken.getAuthorizationURL());
                System.out.print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:");
                String pin = br.readLine();
                try {
                    if (pin.length() > 0) {
                        accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                    } else {
                        accessToken = twitter.getOAuthAccessToken(requestToken);
                    }
                } catch (TwitterException te) {
                    if (401 == te.getStatusCode()) {
                        System.out.println("Unable to get the access token.");
                    } else {
                        te.printStackTrace();
                    }
                }
            }
            System.out.println("Got access token.");
            System.out.println("Access token: " + accessToken.getToken());
            System.out.println("Access token secret: " + accessToken.getTokenSecret());
        } catch (IllegalStateException ie) {
            // access token is already available, or consumer key/secret is not set.
            if (!twitter.getAuthorization().isEnabled()) {
                System.out.println("OAuth consumer key/secret is not set.");
                System.exit(-1);
            }
        }
        Status status = twitter.updateStatus(msg);
        System.out.println("Successfully updated the status to [" + status.getText() + "].");
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.out.println("Failed to read the system input.");
        System.exit(-1);
    }
}