Example usage for twitter4j User getScreenName

List of usage examples for twitter4j User getScreenName

Introduction

In this page you can find the example usage for twitter4j User getScreenName.

Prototype

String getScreenName();

Source Link

Document

Returns the screen name of the user

Usage

From source file:DataCollections.UserHelper.java

public User_dbo convertUserToUser_dbo(User user) {

    User_dbo u = new User_dbo();
    u.values[User_dbo.map.get("user_id")].setValue(String.valueOf(user.getId()));
    u.values[User_dbo.map.get("name")].setValue(removeEscapeCharacters(user.getName()));
    u.values[User_dbo.map.get("screename")].setValue(removeEscapeCharacters(user.getScreenName()));
    u.values[User_dbo.map.get("lang")].setValue(user.getLang());
    u.values[User_dbo.map.get("location")].setValue(removeEscapeCharacters(user.getLocation()));
    u.values[User_dbo.map.get("geoenabled")].setValue(String.valueOf(user.isGeoEnabled()));
    u.values[User_dbo.map.get("timezone")].setValue(user.getTimeZone());
    u.values[User_dbo.map.get("profileurl")].setValue(user.getURL());
    u.values[User_dbo.map.get("protected")].setValue(String.valueOf(user.isProtected()));
    u.values[User_dbo.map.get("verified")].setValue(String.valueOf(user.isVerified()));

    u.values[User_dbo.map.get("description")].setValue(removeEscapeCharacters(user.getDescription()));

    if (geoinfoavailable) {
        double[] geocoor = geohelper.searchGeoLocCoor(user.getLocation());
        u.values[User_dbo.map.get("probased_geoinfo")].setValue(String.valueOf("true"));
        //u.values[User_dbo.map.get("descbased_geoinfo")].setValue(String.valueOf("false"));
        u.values[User_dbo.map.get("probased_lat")].setValue(String.valueOf(geocoor[0]));
        u.values[User_dbo.map.get("probased_lon")].setValue(String.valueOf(geocoor[1]));
    }/* w  w  w .  j  a v  a2s.c  om*/
    u.values[User_dbo.map.get("udetails_processed")].setValue(String.valueOf(true));
    u.values[User_dbo.map.get("totaltweets")].setValue(String.valueOf(user.getStatusesCount()));
    return u;
}

From source file:de.binfalse.jatter.processors.JabberMessageProcessor.java

License:Open Source License

/**
 * Translate user./*from   w  w w. j a  v a  2 s  .c  o m*/
 *
 * @param user the user
 * @param profile the profile
 * @return the string
 */
public static String translateUser(User user, String profile) {
    String ret = "profile of *" + profile + "*\n" + "name: " + user.getName() + "\n" + "screen name: "
            + user.getScreenName() + "\n" + "id: " + user.getId() + "\n";

    if (user.getDescription() != null)
        ret += "description: " + user.getDescription() + "\n";
    if (user.getURL() != null)
        ret += "url: " + JatterTools.expandUrl(user.getURL()) + "\n";
    if (user.getLang() != null)
        ret += "language: " + user.getLang() + "\n";
    if (user.getLocation() != null)
        ret += "location: " + user.getLocation() + "\n";
    if (user.getTimeZone() != null)
        ret += "time zone: " + user.getTimeZone() + "\n";

    ret += "tweets: " + user.getStatusesCount() + "\n";
    ret += "favourites: " + user.getFavouritesCount() + "\n";
    ret += "followers: " + user.getFollowersCount() + "\n";
    ret += "friends: " + user.getFriendsCount() + "\n";

    if (user.getStatus() != null)
        ret += "last status: " + TwitterStatusProcessor.translateTwitterStatus(user.getStatus());
    return ret;
}

From source file:de.jetsli.twitter.TwitterSearch.java

License:Apache License

/**
 * API COSTS: 1/*w ww .j av  a2  s .com*/
 *
 * @param users should be maximal 100 users
 * @return the latest tweets of the users
 */
public Collection<? extends Status> updateUserInfo(List<? extends User> users) {
    int counter = 0;
    String arr[] = new String[users.size()];

    // responseList of twitter.lookup has not the same order as arr has!!
    Map<String, User> userMap = new LinkedHashMap<String, User>();

    for (User u : users) {
        arr[counter++] = u.getScreenName();
        userMap.put(u.getScreenName(), u);
    }

    int maxRetries = 5;
    for (int retry = 0; retry < maxRetries; retry++) {
        try {
            ResponseList<User> res = twitter.lookupUsers(arr);
            rateLimit--;
            List<Status> tweets = new ArrayList<Status>();
            for (int ii = 0; ii < res.size(); ii++) {
                User user = res.get(ii);
                User yUser = userMap.get(user.getScreenName().toLowerCase());
                if (yUser == null)
                    continue;

                //                    Status stat = yUser.updateFieldsBy(user);
                //                    if (stat == null)
                //                        continue;
                //                    Status tw = toTweet(stat, res.get(ii));
                tweets.add(user.getStatus());
            }
            return tweets;
        } catch (TwitterException ex) {
            logger.warn("Couldn't lookup users. Retry:" + retry + " of " + maxRetries, ex);
            if (retry < 1)
                continue;
            else
                break;
        }
    }

    return Collections.EMPTY_LIST;
}

From source file:de.jetsli.twitter.TwitterSearch.java

License:Apache License

public void getFriendsOrFollowers(String userName, AnyExecutor<User> executor, boolean friends,
        boolean waitIfNoApiPoints) {
    long cursor = -1;
    resetRateLimitCache();/* w  ww  .j  a  v  a  2s.c o m*/
    MAIN: while (true) {
        if (waitIfNoApiPoints) {
            checkAndWaitIfRateLimited("getFriendsOrFollowers 1");
        }

        ResponseList<User> res = null;
        IDs ids = null;
        try {
            if (friends)
                ids = twitter.getFriendsIDs(userName, cursor);
            else
                ids = twitter.getFollowersIDs(userName, cursor);

            rateLimit--;
        } catch (TwitterException ex) {
            if (waitIfNoApiPoints && checkAndWaitIfRateLimited("getFriendsOrFollowers 2", ex)) {
                // nothing to do
            } else
                logger.warn(ex.getMessage());
            break;
        }
        if (ids.getIDs().length == 0)
            break;

        long[] intids = ids.getIDs();

        // split into max 100 batch            
        for (int offset = 0; offset < intids.length; offset += 100) {
            long[] limitedIds = new long[100];
            for (int ii = 0; ii + offset < intids.length && ii < limitedIds.length; ii++) {
                limitedIds[ii] = intids[ii + offset];
            }

            // retry at max N times for every id bunch
            for (int trial = 0; trial < 5; trial++) {
                try {
                    res = twitter.lookupUsers(limitedIds);
                } catch (TwitterException ex) {
                    if (waitIfNoApiPoints
                            && checkAndWaitIfRateLimited("getFriendsOrFollowers 3 (trial " + trial + ")", ex)) {
                        // nothing to do
                    } else {
                        // now hoping that twitter recovers some seconds later
                        logger.error("(trial " + trial + ") error while lookupUsers: " + getMessage(ex));
                        myWait(10);
                    }
                    continue;
                } catch (Exception ex) {
                    logger.error("(trial " + trial + ") error while lookupUsers: " + getMessage(ex));
                    myWait(5);
                    continue;
                }

                rateLimit--;
                for (User user : res) {
                    // strange that this was necessary for ibood
                    if (user.getScreenName().trim().isEmpty())
                        continue;

                    if (executor.execute(user) == null)
                        break MAIN;
                }
                break;
            }

            if (res == null) {
                logger.error("giving up");
                break;
            }
        }

        if (!ids.hasNext())
            break;

        cursor = ids.getNextCursor();
    }
}

From source file:de.jetsli.twitter.TwitterSearch.java

License:Apache License

public void follow(User user) {
    try {/*from  www .j  a  v  a 2 s  .c om*/
        twitter.createFriendship(user.getScreenName());
    } catch (TwitterException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.jetwick.data.JUser.java

License:Apache License

public JUser(User u) {
    this(u.getScreenName());
    updateFieldsBy(u);
}

From source file:de.jetwick.tw.TwitterSearch.java

License:Apache License

public static Twitter4JTweet toTweet(Status st, User user) {
    if (user == null)
        throw new IllegalArgumentException("User mustn't be null!");
    if (st == null)
        throw new IllegalArgumentException("Status mustn't be null!");

    Twitter4JTweet tw = new Twitter4JTweet(st.getId(), st.getText(), user.getScreenName());
    tw.setCreatedAt(st.getCreatedAt());//from   w  w w  .  jav a 2  s  .c om
    tw.setFromUser(user.getScreenName());

    if (user.getProfileImageURL() != null)
        tw.setProfileImageUrl(user.getProfileImageURL().toString());

    tw.setSource(st.getSource());
    tw.setToUser(st.getInReplyToUserId(), st.getInReplyToScreenName());
    tw.setInReplyToStatusId(st.getInReplyToStatusId());

    if (st.getGeoLocation() != null) {
        tw.setGeoLocation(st.getGeoLocation());
        tw.setLocation(st.getGeoLocation().getLatitude() + ", " + st.getGeoLocation().getLongitude());
    } else if (st.getPlace() != null)
        tw.setLocation(st.getPlace().getCountryCode());
    else if (user.getLocation() != null)
        tw.setLocation(toStandardLocation(user.getLocation()));

    return tw;
}

From source file:de.jetwick.tw.TwitterSearch.java

License:Apache License

/**
 * API COSTS: 1/*from   w w w .java  2 s.c  o m*/
 *
 * @param users should be maximal 100 users
 * @return the latest tweets of the users
 */
public Collection<? extends Tweet> updateUserInfo(List<? extends JUser> users) {
    int counter = 0;
    String arr[] = new String[users.size()];

    // responseList of twitter.lookup has not the same order as arr has!!
    Map<String, JUser> userMap = new LinkedHashMap<String, JUser>();

    for (JUser u : users) {
        arr[counter++] = u.getScreenName();
        userMap.put(u.getScreenName(), u);
    }

    int maxRetries = 5;
    for (int retry = 0; retry < maxRetries; retry++) {
        try {
            ResponseList res = twitter.lookupUsers(arr);
            rateLimit--;
            List<Tweet> tweets = new ArrayList<Tweet>();
            for (int ii = 0; ii < res.size(); ii++) {
                User user = (User) res.get(ii);
                JUser yUser = userMap.get(user.getScreenName().toLowerCase());
                if (yUser == null)
                    continue;

                Status stat = yUser.updateFieldsBy(user);
                if (stat == null)
                    continue;

                Twitter4JTweet tw = toTweet(stat, user);
                tweets.add(tw);
            }
            return tweets;
        } catch (TwitterException ex) {
            logger.warn("Couldn't lookup users. Retry:" + retry + " of " + maxRetries, ex);
            if (retry < 1)
                continue;
            else
                break;
        }
    }

    return Collections.EMPTY_LIST;
}

From source file:de.jetwick.tw.TwitterSearch.java

License:Apache License

public void getFriendsOrFollowers(String userName, AnyExecutor<JUser> executor, boolean friends) {
    long cursor = -1;
    resetRateLimitCache();//  w w  w .ja  va 2 s  .  c  o  m
    MAIN: while (true) {
        while (getRateLimitFromCache() < LIMIT) {
            int reset = getSecondsUntilReset();
            if (reset != 0) {
                logger.info("no api points left while getFriendsOrFollowers! Skipping ...");
                return;
            }
            resetRateLimitCache();
            myWait(0.5f);
        }

        ResponseList res = null;
        IDs ids = null;
        try {
            if (friends)
                ids = twitter.getFriendsIDs(userName, cursor);
            else
                ids = twitter.getFollowersIDs(userName, cursor);

            rateLimit--;
        } catch (TwitterException ex) {
            logger.warn(ex.getMessage());
            break;
        }
        if (ids.getIDs().length == 0)
            break;

        long[] intids = ids.getIDs();

        // split into max 100 batch            
        for (int offset = 0; offset < intids.length; offset += 100) {
            long[] limitedIds = new long[100];
            for (int ii = 0; ii + offset < intids.length && ii < limitedIds.length; ii++) {
                limitedIds[ii] = intids[ii + offset];
            }

            // retry at max N times for every id bunch
            for (int i = 0; i < 5; i++) {
                try {
                    res = twitter.lookupUsers(limitedIds);
                    rateLimit--;
                    for (Object o : res) {
                        User user = (User) o;
                        // strange that this was necessary for ibood
                        if (user.getScreenName().trim().isEmpty())
                            continue;

                        JUser jUser = new JUser(user);
                        if (executor.execute(jUser) == null)
                            break MAIN;
                    }
                    break;
                } catch (Exception ex) {
                    ex.printStackTrace();
                    myWait(5);
                    continue;
                }
            }

            if (res == null) {
                logger.error("giving up");
                break;
            }
        }

        if (!ids.hasNext())
            break;

        cursor = ids.getNextCursor();
    }
}

From source file:de.jetwick.ui.MySession.java

License:Apache License

public Cookie afterLogin(AccessToken token, ElasticUserSearch uSearch, WebResponse response)
        throws TwitterException {
    if (email == null)
        throw new IllegalArgumentException("Email not available. Please login again.");

    twitterSearch.initTwitter4JInstance(token.getToken(), token.getTokenSecret(), true);
    twitterSearchInitialized = true;/* w  ww  .j  a  va2  s. co  m*/
    //            logger.info("TOKEN:" + token);
    Cookie cookie = new Cookie(TwitterSearch.COOKIE, token.getToken());
    // LATER: use https: cookie.setSecure(true);
    cookie.setComment("Supply autologin for " + Helper.JETSLIDE_URL);
    // four weeks
    cookie.setMaxAge(4 * 7 * 24 * 60 * 60);
    // set path because the cookie should be sent for / and /slide* and /login*
    cookie.setPath("/");
    response.addCookie(cookie);

    // get current infos
    User twitterUser = twitterSearch.getTwitterUser();

    // get current saved searches and update with current twitter infos
    JUser tmpUser = uSearch.findById(token.getUserId());
    if (tmpUser == null) {
        tmpUser = uSearch.findByScreenName(twitterUser.getScreenName());
        if (tmpUser == null)
            tmpUser = new JUser(twitterUser.getScreenName());
    }

    tmpUser.updateFieldsBy(twitterUser);
    // now set email entered on form one page before twitter redirect
    tmpUser.setEmail(email);
    tmpUser.setTwitterToken(token.getToken());
    tmpUser.setTwitterTokenSecret(token.getTokenSecret());
    tmpUser.setLastVisit(new Date());

    // save user into user index
    uSearch.save(tmpUser, false);

    // save user into session
    setUser(tmpUser);

    logger.info("[stats] user login:" + twitterUser.getScreenName() + " " + tmpUser.getScreenName());
    return cookie;
}