Example usage for twitter4j Status getText

List of usage examples for twitter4j Status getText

Introduction

In this page you can find the example usage for twitter4j Status getText.

Prototype

String getText();

Source Link

Document

Returns the text of the status

Usage

From source file:com.ijuru.tweetgrab.web.GrabServlet.java

License:Open Source License

/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *///  w  w  w  . j  av  a2  s. com
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    // Get random account
    List<String> accounts = Context.getOptions().getAccounts();
    String randAccount = accounts.get(rand.nextInt(accounts.size()));

    try {
        Twitter twitter = Context.getTwitterFactory().getInstance();
        ResponseList<Status> tweets = twitter.getUserTimeline(randAccount);

        if (tweets.size() == 0) {
            log.warn("No tweets for " + randAccount);
            out.write(ERROR_MESSAGE);
            return;
        }

        // Pick random tweet
        Status randomTweet = tweets.get(rand.nextInt(tweets.size()));
        //long tweetId = randomTweet.getId();
        String tweetText = randomTweet.getText();

        out.write(tweetText);
    } catch (TwitterException ex) {
        log.error(ex);
        out.write(ERROR_MESSAGE);
        return;
    }
}

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

License:Apache License

/**
 * Usage: java twitter4j.examples.timeline.GetHomeTimeline
 *
 * @param args String[]/*from  w  w w.j  av a  2 s .  c o  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[]//  w w  w .ja  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[]/*  www. j  a  v a 2  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 static void main(String[] args) throws InterruptedException {
    //        System.out.println("Hello, welcome to my first java application.");
    //        System.out.println("");
    //        String s = new String();
    //        while (!"exit".equals(s)) {
    //            try {
    //                BufferedReader bufferReader = new BufferedReader(new InputStreamReader(System.in));
    //                s = bufferReader.readLine();
    //                System.out.println(": " + s);
    //                twitterCmd cmd = new twitterCmd();
    //                String ip = cmd.getRemoteIpAddress();
    //                System.out.println("IP Address: " + ip);
    //            } catch (IOException e) {
    //                e.printStackTrace();
    //            }
    //        }//from   w ww.  jav  a 2  s.c  o m
    twitterCmd getDM = new twitterCmd();
    if (args == null) {
        System.out.println(" ========== Start application. ");
        System.out.println("Showing @isongsee11's mentions.");

        if (!getDM.isOldMessageId()) {
            System.out.println("#################### Not Old Message. ");
            String command = new String();
            while (!"exit".equals(command)) {
                Thread.sleep(15000);
                Status s = new twitterCmd().getLastestMessage();
                command = s.getText();
                command = command.replace("@isongsee11", "");
                command = command.replaceAll("\\s+", "");
                System.out.println(" ##### command[" + command + "]");
                if ("ipaddress".equals(command)) {
                    String ipAdd = getDM.getRemoteIpAddress();
                    System.out.println("IP Address: " + ipAdd);
                    String replied = "@isongsee11 >> my ipaddress " + ipAdd;
                    getDM.updateStatus(replied);
                }

                if ("hello".equals(command)) {
                    String msg = "Hi, how are you ?";
                    String replied = "@" + s.getUser().getScreenName() + " - " + msg;
                    System.out.println(">" + replied);
                    getDM.updateStatus(replied);
                }
            }
        }
        System.out.println("  ========== End application. ");
    } else {
        if (args[0] == "cron") {
            System.out.println("Command crom:");
            Date date = new Date();
            String dateStr = String.format(" Date/Time : %tc", date);
            String ipAdd = getDM.getRemoteIpAddress();
            System.out.println("IP Address: " + ipAdd);
            String replied = "@isongsee11 >> my ipaddress " + ipAdd + " at " + dateStr;
            System.out.println("msg: " + replied);
            getDM.updateStatus(replied);
            System.out.println("Done.");
        }
    }
}

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

public Status getLastestMessage() {
    String msg = new String();
    Status s = null;
    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    try {/*from   ww  w  .j  av a 2s . com*/
        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 w  w  w .  j  ava2  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);
    }
}

From source file:com.infine.android.devoxx.service.TwitterService.java

License:Apache License

private ContentProviderOperation buildCPOperation(Status tweet) {

    final ContentProviderOperation.Builder builder = ContentProviderOperation
            .newInsert(ScheduleContract.Tweets.CONTENT_URI);

    // affecte les valeurs des colonnes
    builder.withValue(ScheduleContract.Tweets.TWEET_ID, tweet.getId());
    builder.withValue(ScheduleContract.Tweets.CREATION_DATE, tweet.getCreatedAt().getTime());
    builder.withValue(ScheduleContract.Tweets.IMAGE_URL, tweet.getUser().getProfileImageURL());
    builder.withValue(ScheduleContract.Tweets.TEXT, tweet.getText());
    builder.withValue(ScheduleContract.Tweets.USER, tweet.getUser().getName());
    builder.withValue(ScheduleContract.Tweets.USER_NAME, tweet.getUser().getScreenName());

    return builder.build();
}

From source file:com.isdp.twitterposter.TwitterManager.java

License:Open Source License

public void tweet(String message) {
    TwitterFactory tf = new TwitterFactory(configuration);
    Twitter twitter = tf.getInstance();/*from   w  w  w  . j av  a 2 s  .com*/
    try {
        Status status = twitter.updateStatus(message);
        System.out.println("Tweet status: Successfully updated the status to [" + status.getText() + "].");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.isdp.twitterposter.TwitterManager.java

License:Open Source License

public void startTwitterStream() {
    TwitterStream twitterStream = new TwitterStreamFactory(configuration).getInstance();

    StatusListener listener = new StatusListener() {
        @Override//from w w w  .j  ava 2s  . c  o m
        public void onStatus(Status status) {
            try {
                System.out.println("Text recieved: @" + status.getUser().getScreenName() + " - "
                        + status.getText() + "\n");

                StringTokenizer st = new StringTokenizer(status.getText(), " ");

                //first token indicates search engine
                String searchEngine = st.nextToken();

                //burn the next random token
                String randToken = st.nextToken();

                //next token indicates max number of results to return
                int maxResults = Integer.parseInt(st.nextToken());

                String searchString = "";
                while (st.hasMoreTokens()) {
                    searchString += st.nextToken() + " ";
                }

                if (searchEngine.equals(GoogleManager.TAG_YELP)) {
                    String[] results = GoogleManager.getInstance().trySearch(searchString,
                            GoogleManager.SEARCH_YELP);

                    if (results != null) {
                        for (int i = 0; i < results.length && i < maxResults; ++i) {
                            String tweetMsg = Util.truncateString(results[i], TWITTER_CHARACTER_LIMIT);

                            System.out.println("Tweeting" + tweetMsg);

                            tweet(tweetMsg);
                        }
                    } else if (results == null || results.length == 0) {
                        tweet(Util.generateRandomString(7) + "\n" + "No results found!");
                    }
                } else if (searchEngine.equals(GoogleManager.TAG_WIKI)) {
                    String[] results = GoogleManager.getInstance().trySearch(searchString,
                            GoogleManager.SEARCH_WIKI);

                    if (results != null) {
                        for (int i = 0; i < results.length && i < maxResults; ++i) {
                            String tweetMsg = Util.generateRandomString(3) + "\n"
                                    + Util.shortenText(results[i]);
                            tweetMsg = Util.truncateString(tweetMsg, TWITTER_CHARACTER_LIMIT);

                            System.out.println("Tweeting " + tweetMsg);

                            tweet(tweetMsg);
                        }
                    } else if (results == null || results.length == 0) {
                        tweet(Util.generateRandomString(7) + "\n" + "No results found!");
                    }
                }
            } catch (Exception e) {
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println(
                    "onDeletionNotice: Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out
                    .println("onTrackLimitationNotice: Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println(
                    "onScrubGeo: Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("onStallWarning: Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            System.out.println("onException" + ex.toString());
        }
    };

    twitterStream.addListener(listener);

    FilterQuery tweetFilterQuery = new FilterQuery();

    tweetFilterQuery.follow(new long[] { FOLLOW_ID });

    twitterStream.filter(tweetFilterQuery);
}