Example usage for twitter4j Status getUser

List of usage examples for twitter4j Status getUser

Introduction

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

Prototype

User getUser();

Source Link

Document

Return the user associated with the status.
This can be null if the instance is from User.getStatus().

Usage

From source file:com.michaelfitzmaurice.clocktwerk.TweetResponder.java

License:Apache License

public Collection<Status> getNewMentions() throws TweetException {

    long sinceId = lastSeenMention();
    try {/*w  w  w  .  j  a  v  a 2  s  . c  o m*/
        LOG.info("Checking for new Twitter mentions of {} since Tweet {}", myScreenName, sinceId);
        Paging paging = new Paging(sinceId);
        ResponseList<Status> mentions = twitterClient.getMentionsTimeline(paging);
        int numberOfMentions = mentions.size();
        LOG.info("Found {} new mentions", numberOfMentions);
        for (Status status : mentions) {
            User user = status.getUser();
            String mentionSummary = format("Date:%s, ID:%s, From:%s (%s), Text:'%s'", status.getCreatedAt(),
                    status.getId(), user.getScreenName(), user.getName(), status.getText());
            LOG.debug("New mention: [{}]", mentionSummary);
        }
        return mentions;
    } catch (TwitterException e) {
        throw new TweetException("Error getting Twitter mentions", e);
    }

    // TODO handle paging
}

From source file:com.michaelfitzmaurice.clocktwerk.TweetResponder.java

License:Apache License

private String replyBody(String myScreenName, Status mention, String messageBody) {

    String replyMessage = "@" + mention.getUser().getScreenName() + " ";
    for (UserMentionEntity userMentionEntity : mention.getUserMentionEntities()) {
        String mentionScreenName = userMentionEntity.getScreenName();
        if (mentionScreenName.equalsIgnoreCase(myScreenName) == false) {
            replyMessage += "@" + mentionScreenName + " ";
        }/*from w w w .j a va2 s.c om*/
    }
    replyMessage += messageBody;

    return replyMessage;
}

From source file:com.mothsoft.alexis.engine.retrieval.TwitterRetrievalTaskImpl.java

License:Apache License

private void handleSourceImpl(final TwitterSource twitterSource) {
    final SocialConnection socialConnection = twitterSource.getSocialConnection();
    final AccessToken accessToken = new AccessToken(socialConnection.getOauthToken(),
            socialConnection.getOauthTokenSecret());

    final List<Status> statuses = this.twitterService.getHomeTimeline(accessToken,
            twitterSource.getLastTweetId(), (short) 800);

    if (statuses != null && statuses.size() > 0) {
        logger.info("Twitter retrieval found " + statuses.size() + " Tweets for user: "
                + socialConnection.getRemoteUsername());

        // the newest tweet in the timeline will be our starting point for
        // the next fetch
        twitterSource.setLastTweetId(statuses.get(0).getId());

        // import these in reverse order to ensure newest ones have the
        // highest document IDs
        Collections.reverse(statuses);

        final Long userId = twitterSource.getUserId();
        final User user = this.userDao.get(userId);

        for (final Status status : statuses) {
            final Long tweetId = status.getId();

            Tweet tweet = this.tweetDao.getTweetByRemoteTweetId(tweetId);
            final boolean isAdd = (tweet == null);

            if (isAdd) {
                // TODO - is this right?
                // Twitter allows 2 different styles of retweets. The
                // ones that are actually retweets show as tweeted by the
                // original user. Others may show
                // "RT @original thing original said" tweeted
                // by the new person
                final boolean retweet = status.isRetweet();

                final twitter4j.User tweeter;
                final String text;
                twitter4j.User retweeter = null;

                final List<TweetLink> links;
                final List<TweetMention> mentions;
                final List<TweetHashtag> hashtags;

                if (retweet) {
                    tweeter = status.getRetweetedStatus().getUser();
                    text = status.getRetweetedStatus().getText();
                    retweeter = status.getUser();
                    links = readLinks(status.getRetweetedStatus());
                    mentions = readMentions(status.getRetweetedStatus());
                    hashtags = readHashtags(status.getRetweetedStatus());
                } else {
                    tweeter = status.getUser();
                    text = status.getText();
                    links = readLinks(status);
                    mentions = readMentions(status);
                    hashtags = readHashtags(status);
                }//  w  w w  . j a  va 2 s  .c om

                final URL profileImageUrl = tweeter.getProfileImageUrlHttps();
                final Date createdAt = status.getCreatedAt();

                tweet = new Tweet(tweetId, createdAt, tweeter.getScreenName(), tweeter.getName(),
                        profileImageUrl, text, links, mentions, hashtags, retweet,
                        retweet ? retweeter.getScreenName() : null);
                this.documentDao.add(tweet);
            }

            final DocumentUser documentUser = new DocumentUser(tweet, user);

            if (isAdd || !tweet.getDocumentUsers().contains(documentUser)) {
                tweet.getDocumentUsers().add(new DocumentUser(tweet, user));
                this.documentDao.update(tweet);
            }
        }
    } else {
        logger.info("Twitter retrieval found no Tweets for user: " + socialConnection.getRemoteUsername());
    }

    twitterSource.setRetrievalDate(new Date());
    this.sourceDao.update(twitterSource);
}

From source file:com.mycompany.dovetune.PostTweet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//ww  w . ja va  2  s.c o  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");
    String tweetItem = request.getParameter("tweet");
    String songName = request.getParameter("song");

    songName = songName.replace(" ", "_");
    String tweet = tweetItem + " #" + songName + " #DoveTune #SoundCloud";

    String user = "";
    try {
        Status status = twitter.updateStatus(tweet);
        user = "<img class='profileImg' src='" + status.getUser().getProfileImageURL()
                + "' title='Profile Image' alt='Profile Image'><strong>" + status.getUser().getName() + " @"
                + status.getUser().getScreenName() + "</strong> - " + status.getText();

        user = user.replace("#" + songName, "<strong class='hashtags'>#" + songName + "</strong>");
        user = user.replace("#DoveTune", "<strong class='hashtags'>#DoveTune</strong>");
        user = user.replace("#SoundCloud", "<strong class='hashtags'>#SoundCloud</strong>");
        String songL = songName;
        songL = songL.toLowerCase();
        user = user.replace("#" + songL, "<strong class='hashtags'>#" + songL + "</strong>");

    } catch (TwitterException ex) {
        Logger.getLogger(PostTweet.class.getName()).log(Level.SEVERE, null, ex);

    }

    //        request.setAttribute("tweet", tweet);

    response.setContentType("text/HTML");
    response.getWriter().write(user);

    //        request.getRequestDispatcher("details.jsp").forward(request, response);
}

From source file:com.mycompany.dovetune.SearchTwitter.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  ww w  .j  av  a 2s  .  co m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, TwitterException {

    //            HttpSession session = request.getSession(false);

    //            if(session != null){

    Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");

    if (twitter != null) {
        String soundcloudUrl = request.getParameter("soundcloudUrl");
        String songName = request.getParameter("songName");
        out.println(twitter.toString());

        int i = songName.indexOf("- ");
        String song = songName.substring(i + 2);
        try {
            Query query = new Query("(#" + song + ") OR ( " + songName + ") OR (#DoveTune)");

            QueryResult result = twitter.search(query);

            List<Status> tweets = result.getTweets();
            //                String list = "<ulid='tweetList'>";
            String list = "";
            for (Status tweet : tweets) {
                list += "<li><img class='profileImg' src='" + tweet.getUser().getProfileImageURL()
                        + "' title='Profile Image' alt='Profile Image'><strong>" + tweet.getUser().getName()
                        + " @" + tweet.getUser().getScreenName() + "</strong> - " + tweet.getText() + "</li>";
            }

            list = list.replace("#" + song, "<strong class='hashtags'>#" + song + "</strong>");
            list = list.replace("#DoveTune", "<strong class='hashtags'>#DoveTune</strong>");
            list = list.replace("#SoundCloud", "<strong class='hashtags'>#SoundCloud</strong>");
            String songL = song;
            songL = songL.toLowerCase();
            list = list.replace("#" + songL, "<strong class='hashtags'>#" + songL + "</strong>");
            if (list.equals("")) {
                list = "<li>There are no tweets to display. Tweet your own!</li>";
            }

            request.setAttribute("soundcloudUrl", soundcloudUrl);
            request.setAttribute("songName", songName);
            request.setAttribute("song", songName);
            request.setAttribute("list", list);

        } catch (TwitterException te) {
            te.printStackTrace();
            out.println("Failed to search tweets: " + te.getMessage());
            System.exit(-1);
        }
    } else {
        String list = "<ul><li>In order to view tweets, please sign in to <a href=\"SignIn\" title=\"Sign in to Twitter\"><strong class=\"notSignedIn\">Twitter</strong></a></li></ul>";
        request.setAttribute("list", list);
    }

    request.getRequestDispatcher("details.jsp").forward(request, response);

}

From source file:com.mycompany.omnomtweets.Search.java

public static boolean writeToStdErr(Status tweet) {
    //System.out.println("Writing " + tweets.size() + " tweets");
    boolean success = true;
    if (tweet != null) {
        String tweetText;/*w w w.j  av  a  2 s  . c  om*/
        String idOfRetweetee = "";
        if (tweet.getRetweetedStatus() != null) {
            tweetText = "RT " + tweet.getRetweetedStatus().getText();
            idOfRetweetee = "" + tweet.getRetweetedStatus().getUser().getScreenName();
            //System.out.println("retweeted" + tweetText);
        } else {
            tweetText = tweet.getText();
        }
        String urlText = "";
        if (tweet.getURLEntities().length > 0) {
            for (URLEntity url : tweet.getURLEntities()) {
                if (url.getExpandedURL() != null) {
                    urlText += url.getExpandedURL() + " ";
                    tweetText = tweetText.replace(url.getURL(), url.getExpandedURL());
                    //System.out.println("Expanded URL " + url.getExpandedURL());
                } else {
                    urlText += url.getURL() + " ";
                    //System.out.println("URL " + url.getURL());
                }
            }
        }
        if (tweet.getMediaEntities().length > 0) {
            for (MediaEntity media : tweet.getMediaEntities()) {
                if (media.getExpandedURL() != null) {
                    urlText += media.getExpandedURL() + " ";
                    tweetText = tweetText.replace(media.getMediaURL(), media.getExpandedURL());
                    //System.out.println("Expanded URL " + media.getExpandedURL());
                } else {
                    urlText += media.getMediaURL() + " ";
                    //System.out.println("URL " + media.getMediaURL());
                }
            }
        }
        String encodedText = tweetText.replaceAll("\"", "\"\"");
        encodedText = encodedText.replaceAll("\\s", " ");
        String writeMe = "\"" + encodedText + "\"," + urlText + "," + tweet.getUser().getId() + ","
                + tweet.getId() + "," + candidate.name + "," + sdf.format(tweet.getCreatedAt()) + ","
                + idOfRetweetee + "\n";
        System.err.print(writeMe);
        //writeTweets.write(writeMe);
    }
    return success;
}

From source file:com.mycompany.omnomtweets.Search.java

/**
 * Method to write the tweets to file, base 64 encoded tweet text.
 * @param tweets the tweets to be written
 * @param filename the file to write the tweets into
 * @return true unless something bad happens
 *//*from  w  ww.java 2 s  . c  o  m*/
public static boolean writeTweetsToFile(List<Status> tweets, String filename) {
    //System.out.println("Writing " + tweets.size() + " tweets");
    boolean success = true;
    try {
        FileWriter addTweets = new FileWriter(new File(filename), true);
        if (tweets != null && tweets.size() > 0) {
            for (Status tweet : tweets) {
                String tweetText;
                String idOfRetweetee = "";
                if (tweet.getRetweetedStatus() != null) {
                    tweetText = "RT " + tweet.getRetweetedStatus().getText();
                    idOfRetweetee = "" + tweet.getRetweetedStatus().getUser().getScreenName();
                    //System.out.println("retweeted" + tweetText);
                } else {
                    tweetText = tweet.getText();
                }
                String urlText = "";
                if (tweet.getURLEntities().length > 0) {
                    for (URLEntity url : tweet.getURLEntities()) {
                        if (url.getExpandedURL() != null) {
                            urlText += url.getExpandedURL() + " ";
                            tweetText = tweetText.replace(url.getURL(), url.getExpandedURL());
                            //System.out.println("Expanded URL " + url.getExpandedURL());
                        } else {
                            urlText += url.getURL() + " ";
                            //System.out.println("URL " + url.getURL());    
                        }
                    }
                }
                if (tweet.getMediaEntities().length > 0) {
                    for (MediaEntity media : tweet.getMediaEntities()) {
                        if (media.getExpandedURL() != null) {
                            urlText += media.getExpandedURL() + " ";
                            tweetText = tweetText.replace(media.getMediaURL(), media.getExpandedURL());
                            //System.out.println("Expanded URL " + media.getExpandedURL());
                        } else {
                            urlText += media.getMediaURL() + " ";
                            //System.out.println("URL " + media.getMediaURL());    
                        }
                    }
                }
                String encodedText = tweetText.replaceAll("\"", "\"\"");
                encodedText = encodedText.replaceAll("\n", " ");
                String writeMe = "\"" + encodedText + "\"," + urlText + "," + tweet.getUser().getId() + ","
                        + tweet.getId() + "," + candidate.name + "," + tweet.getCreatedAt() + ","
                        + idOfRetweetee + "\n";
                //System.out.println(writeMe);
                addTweets.write(writeMe);
            }
        }
        addTweets.close();
    } catch (IOException ex) {
        //System.out.println("Something broke lol");
        success = false;
    }
    return success;
}

From source file:com.mycompany.omnomtweets.TweetsAboutCandidates.java

/**
 * Method to write the tweets to file, base 64 encoded tweet text.
 * @param tweets the tweets to be written
 * @param filename the file to write the tweets into
 * @return true unless something bad happens
 *//*from w w w  .j av a 2  s  . co m*/
public boolean writeTweetsToFile(List<Status> tweets, String filename) {
    //System.out.println("Writing " + tweets.size() + " tweets");
    boolean success = true;
    try {
        FileWriter addTweets = new FileWriter(new File(filename), true);
        if (tweets != null && tweets.size() > 0) {
            for (Status tweet : tweets) {
                String tweetText;
                String idOfRetweetee = "";
                if (tweet.getRetweetedStatus() != null) {
                    tweetText = "RT " + tweet.getRetweetedStatus().getText();
                    idOfRetweetee = "" + tweet.getRetweetedStatus().getUser().getScreenName();
                    //System.out.println("retweeted" + tweetText);
                } else {
                    tweetText = tweet.getText();
                }
                String urlText = "";
                if (tweet.getURLEntities().length > 0) {
                    for (URLEntity url : tweet.getURLEntities()) {
                        if (url.getExpandedURL() != null) {
                            urlText += url.getExpandedURL() + " ";
                            //System.out.println("Expanded URL " + url.getExpandedURL());
                        } else {
                            urlText += url.getURL() + " ";
                            //System.out.println("URL " + url.getURL());    
                        }
                    }
                }
                if (tweet.getMediaEntities().length > 0) {
                    for (MediaEntity media : tweet.getMediaEntities()) {
                        if (media.getExpandedURL() != null) {
                            urlText += media.getExpandedURL() + " ";
                            //System.out.println("Expanded URL " + media.getExpandedURL());
                        } else {
                            urlText += media.getMediaURL() + " ";
                            //System.out.println("URL " + media.getMediaURL());    
                        }
                    }
                }
                String encodedText = tweet.getText().replaceAll("\"", "\"\"");
                String writeMe = "\"" + encodedText + "\"," + urlText + "," + tweet.getUser().getId() + ","
                        + tweet.getId() + "," + candidate.name + "," + tweet.getCreatedAt() + ","
                        + idOfRetweetee + "\n";
                //System.out.println(writeMe);
                addTweets.write(writeMe);
            }
        }
        addTweets.close();
    } catch (IOException ex) {
        System.out.println("Something broke lol");
        success = false;
    }
    return success;
}

From source file:com.mycompany.twitterapp.TwitterApp.java

public void getTimeline() {
    Set<String> keySet = listOfPeople.keySet();
    for (String key : keySet) {
        System.out.println("Debug Person - " + key);
        try {/*from  w  w w .  java 2s  .  c  om*/
            List<Status> statuses = null;
            for (int i = 1; i < 16; i++) {
                statuses = twitter.getUserTimeline(key, new Paging(i));
                System.out.println("Debug - filling statuses = " + i + " - Size: " + statuses.size());

                if (statuses.size() < 1 || statuses == null) {
                    System.out.println("List Empty.");
                    break;
                }
                //System.out.println("Showing @" + statuses.get(0).getUser().getScreenName() + "'s home timeline.");
                for (Status status : statuses) {
                    //                        System.out.println("Debug - text: " + status.getText());
                    saveToFile(status.getUser().getId(), status.getText().replaceAll("\n", "\\\\n"),
                            status.getUser().getScreenName(), listOfPeople.get(key));
                    //                        System.out.println("debug - truncated: " + status.isTruncated());
                }
            }
            if (statuses.size() < 1 || statuses == null) {
                continue;
            }
            Status temp = statuses.get(0);
            saveToFile(temp.getUser().getId(), temp.getUser().getScreenName(), listOfPeople.get(key));
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }
    }

}

From source file:com.mycompany.twitterapp.TwitterApp.java

public void getTweet() {

    Query query = new Query(tweetSearchWord);
    QueryResult result = null;/*  w w w .  j ava  2 s.c  om*/
    try {
        result = twitter.search(query);
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(TwitterApp.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (result == null) {
        System.out.println("nope");
        return;
    }
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }
}