Example usage for twitter4j StatusUpdate getStatus

List of usage examples for twitter4j StatusUpdate getStatus

Introduction

In this page you can find the example usage for twitter4j StatusUpdate getStatus.

Prototype

public String getStatus() 

Source Link

Usage

From source file:com.rhymestore.twitter.mock.MockTwitter.java

License:Open Source License

@Override
public Status updateStatus(final StatusUpdate latestStatus) throws TwitterException {
    lastUpdatedStatus = latestStatus.getStatus();
    return null;/* w  w  w.  jav a  2  s .c  om*/
}

From source file:kerguelenpetrel.BotherSomeoneServlet.java

License:Apache License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    StringBuilder builder = new StringBuilder();
    long[] friendIDs, victimIDs;

    resp.setContentType("text/plain; charset=UTF-8");
    try {//from   w w  w .jav  a  2s.  c om
        //Get the Twitter object
        Twitter twit = TwitterFactory.getSingleton();

        //Find a friend of a follower to bother
        friendIDs = twit.getFollowersIDs(twit.getId(), cursor).getIDs();

        if (friendIDs.length == 0) {
            resp.getWriter().println("Cannot find any followers to bother \n");
            return;
        }

        //Load the potential victim IDs
        victimIDs = twit.getFollowersIDs(friendIDs[r.nextInt(friendIDs.length)], cursor).getIDs();

        if (victimIDs.length == 0) {
            resp.getWriter().println("Cannot find any followers to bother \n");
            return;
        }

        //Write to our victim
        String victim = twit.showUser(victimIDs[r.nextInt(victimIDs.length)]).getScreenName();

        //Get a global trend
        Trends t = twit.getPlaceTrends(1);
        String trend = t.getTrends()[r.nextInt(t.getTrends().length)].getName();
        builder.append(getWordnikContent(victim, trend, resp));

        if (builder.length() > 280)
            builder.setLength(280); //Tweets are limited to 280 characters

        //Set the status
        StatusUpdate status = new StatusUpdate(builder.toString());

        //Post the status
        twit.updateStatus(status);
        resp.getWriter().println("Tweet posted: " + status.getStatus());
    }

    catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter \n");
        e.printStackTrace(resp.getWriter());
    }
}

From source file:kerguelenpetrel.FriendSomeoneServlet.java

License:Apache License

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    User friend = null;//w  w  w .jav a 2  s .com
    resp.setContentType("text/plain; charset=UTF-8");
    try {
        //Get the Twitter object
        Twitter twit = TwitterFactory.getSingleton();

        //Find a friend of a follower to bother
        long[] followerIDs = twit.getFollowersIDs(twit.getId(), cursor, 30).getIDs();

        if (followerIDs.length == 0) {
            resp.getWriter().println("Cannot find any followers \n");
            return;
        }

        //Load the potential victim IDs
        long[] friendIDs = twit.getFollowersIDs(followerIDs[r.nextInt(followerIDs.length)], cursor).getIDs();

        if (friendIDs.length == 0) {
            resp.getWriter().println("Cannot find any followers to bother \n");
            return;
        }

        //Get a new friend
        friend = twit.showUser(friendIDs[r.nextInt(friendIDs.length)]);
        twit.createFriendship(friend.getId());
        resp.getWriter().println("Made a new friend with @" + friend.getScreenName());

        //Write to our new friend
        StatusUpdate status = new StatusUpdate(writeToFriend(friend.getScreenName(), resp));
        twit.updateStatus(status);
        resp.getWriter().println("Tweet posted: " + status.getStatus());
    } catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter \n");
        e.printStackTrace(resp.getWriter());
    }
}

From source file:kerguelenpetrel.RespondServlet.java

License:Apache License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    StringBuilder builder = new StringBuilder();
    resp.setContentType("text/plain; charset=UTF-8");

    try {/*  w  ww.  j av  a  2 s  . com*/
        //Get the Twitter object
        Twitter twit = TwitterFactory.getSingleton();
        ResponseList<Status> mentions = twit.getMentionsTimeline();

        lastPostIdEntity = datastore.get(KeyFactory.createKey("lastPostIDEntity", "ID"));
        lastPostId = Long.parseLong(lastPostIdEntity.getProperty("lastPostID").toString());

        if (mentions.size() == 0) {
            resp.getWriter().println("No mentions so far...\n");
            return;
        }

        for (Status mention : mentions) {
            if (lastPostId < mention.getId()) {
                if (mention.getUser().getId() == twit.getId())
                    ; //don't respond to myself

                else if (mention.isRetweeted())
                    mention = twit.createFavorite(mention.getId()); //mark the retweet as a favourite
                else if (mention.getText().toLowerCase().contains("bye")) {
                    builder.setLength(0);
                    builder.append("@");
                    builder.append(mention.getUser().getScreenName());
                    builder.append(" Bye");
                } else {
                    builder.setLength(0);
                    //Add the screen name of the person we are responding to
                    builder.append("@");
                    builder.append(mention.getUser().getScreenName() + " ");

                    //Get feed title as content
                    builder.append(getFeedTitle(resp));

                    //Get a Wordnik trend
                    builder.append(getWordnikTrend(resp));

                    /* Tweets are maximum 280 characters */
                    if (builder.length() > 280) {
                        builder.setLength(builder.lastIndexOf(" ", 270));
                        builder.append(end[(r.nextInt(end.length))]);
                    }
                }
                //Set the status
                StatusUpdate status = new StatusUpdate(builder.toString());

                //Post the status
                twit.updateStatus(status);
                resp.getWriter().println("Tweet posted: " + status.getStatus());
            }
        }
        //Save last post ID
        lastPostIdEntity.setProperty("lastPostID", (Long.toString(mentions.get(0).getId())));
        datastore.put(lastPostIdEntity);
    } catch (EntityNotFoundException e) {
        // Make new ResponseIDentity
        lastPostIdEntity = new Entity("lastPostIDEntity", "ID");
        lastPostIdEntity.setProperty("lastPostID", "0");
        datastore.put(lastPostIdEntity);
        resp.getWriter()
                .println("Made new lastPostId " + lastPostIdEntity.getProperty("lastPostID").toString());
    } catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter \n");
        e.printStackTrace(resp.getWriter());
    }
}

From source file:kerguelenpetrel.UpdateStatusServlet.java

License:Apache License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    StringBuilder builder = new StringBuilder();

    resp.setContentType("text/plain; charset=UTF-8");
    resp.getWriter().println("Updating status...");

    try {/*from w w  w  . j a va  2 s .  c  om*/
        //Append feed title
        builder.append(getFeedTitle(resp));

        //Append Wordnik example sentence
        builder.append(getWordnikSentence(resp));

        /* Tweets are maximum 280 characters, so trim our sentence appropriately */
        if (builder.length() > 280) {
            if (builder.lastIndexOf(";", 220) > 0)
                builder.setLength(builder.lastIndexOf(";", 220));
            else if (builder.lastIndexOf(":", 220) > 0)
                builder.setLength(builder.lastIndexOf(":", 220));
            else if (builder.lastIndexOf(",", 220) > 0)
                builder.setLength(builder.lastIndexOf(",", 220));
            else
                builder.setLength(220);
        }

        //Append a Global trend
        Twitter twit = TwitterFactory.getSingleton();
        builder.append(
                " " + twit.getPlaceTrends(1).getTrends()[r.nextInt(twit.getPlaceTrends(1).getTrends().length)]
                        .getName());

        // Append a Wordnik trend
        builder.append(getWordnikTrend(resp));

        if (builder.length() > 280)
            builder.setLength(280); //Tweets are limited to 280 characters

        //Set the status
        StatusUpdate status = new StatusUpdate(builder.toString());

        /* Add an image from Flickr for small status */
        if (builder.length() < 180)
            status.setMediaIds(addFlickrImg(twit, resp));

        twit.updateStatus(status);
        resp.getWriter().println("Tweet posted: " + status.getStatus());
    }

    catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter \n");
        e.printStackTrace(resp.getWriter());
    }
}

From source file:org.komusubi.feeder.sns.twitter.Twitter4j.java

License:Apache License

/**
 * tweet./*from  ww w  .j a  v a  2  s .c  o  m*/
 * @param message
 */
public void tweet(Message message) {
    Script current = new ScriptLine("");
    try {
        Status result = null;
        for (Script script : message) {
            current = script; // mark current script for when exception occurred.
            if (outputConsole) {
                System.out.printf("tweet(length:%d): %s%n", TweetScript.lengthAfterTweeted(script.trimedLine()),
                        script.trimedLine());
            } else {
                StatusUpdate status = new StatusUpdate(script.trimedLine());
                if (result != null) {
                    status.inReplyToStatusId(result.getId());
                }
                logger.info("tweet(length:{}): {}", TweetScript.lengthAfterTweeted(status.getStatus()),
                        status.getStatus());
                result = twitter.updateStatus(status);
            }
        }
    } catch (TwitterException e) {
        throw new Twitter4jException(String.format("tweet(length:%d): %s",
                TweetScript.lengthAfterTweeted(current.trimedLine()), current.trimedLine()), e);
    }
}