List of usage examples for twitter4j User getId
long getId();
From source file:org.getlantern.firetweet.util.ContentValuesCreator.java
License:Open Source License
public static ContentValues createDirectMessage(final DirectMessage message, final long accountId, final boolean isOutgoing) { if (message == null) return null; final ContentValues values = new ContentValues(); final User sender = message.getSender(), recipient = message.getRecipient(); if (sender == null || recipient == null) return null; final String sender_profile_image_url = sender.getProfileImageUrlHttps(); final String recipient_profile_image_url = recipient.getProfileImageUrlHttps(); values.put(DirectMessages.ACCOUNT_ID, accountId); values.put(DirectMessages.MESSAGE_ID, message.getId()); values.put(DirectMessages.MESSAGE_TIMESTAMP, message.getCreatedAt().getTime()); values.put(DirectMessages.SENDER_ID, sender.getId()); values.put(DirectMessages.RECIPIENT_ID, recipient.getId()); if (isOutgoing) { values.put(DirectMessages.CONVERSATION_ID, recipient.getId()); } else {// w ww . ja v a 2 s .c om values.put(DirectMessages.CONVERSATION_ID, sender.getId()); } final String text_html = TwitterContentUtils.formatDirectMessageText(message); values.put(DirectMessages.TEXT_HTML, text_html); values.put(DirectMessages.TEXT_PLAIN, message.getText()); values.put(DirectMessages.TEXT_UNESCAPED, toPlainText(text_html)); values.put(DirectMessages.IS_OUTGOING, isOutgoing); values.put(DirectMessages.SENDER_NAME, sender.getName()); values.put(DirectMessages.SENDER_SCREEN_NAME, sender.getScreenName()); values.put(DirectMessages.RECIPIENT_NAME, recipient.getName()); values.put(DirectMessages.RECIPIENT_SCREEN_NAME, recipient.getScreenName()); values.put(DirectMessages.SENDER_PROFILE_IMAGE_URL, sender_profile_image_url); values.put(DirectMessages.RECIPIENT_PROFILE_IMAGE_URL, recipient_profile_image_url); final ParcelableMedia[] mediaArray = ParcelableMedia.fromEntities(message); if (mediaArray != null) { values.put(DirectMessages.MEDIA_LIST, SimpleValueSerializer.toSerializedString(mediaArray)); } return values; }
From source file:org.getlantern.firetweet.util.ContentValuesCreator.java
License:Open Source License
public static ContentValues createStatus(final Status orig, final long accountId) { if (orig == null || orig.getId() <= 0) return null; final ContentValues values = new ContentValues(); values.put(Statuses.ACCOUNT_ID, accountId); values.put(Statuses.STATUS_ID, orig.getId()); values.put(Statuses.STATUS_TIMESTAMP, orig.getCreatedAt().getTime()); final Status status; if (orig.isRetweet()) { final Status retweetedStatus = orig.getRetweetedStatus(); final User retweetUser = orig.getUser(); final long retweetedById = retweetUser.getId(); values.put(Statuses.RETWEET_ID, retweetedStatus.getId()); values.put(Statuses.RETWEET_TIMESTAMP, retweetedStatus.getCreatedAt().getTime()); values.put(Statuses.RETWEETED_BY_USER_ID, retweetedById); values.put(Statuses.RETWEETED_BY_USER_NAME, retweetUser.getName()); values.put(Statuses.RETWEETED_BY_USER_SCREEN_NAME, retweetUser.getScreenName()); values.put(Statuses.RETWEETED_BY_USER_PROFILE_IMAGE, (retweetUser.getProfileImageUrlHttps())); values.put(Statuses.IS_RETWEET, true); if (retweetedById == accountId) { values.put(Statuses.MY_RETWEET_ID, orig.getId()); } else {//from w ww .j ava 2 s. com values.put(Statuses.MY_RETWEET_ID, orig.getCurrentUserRetweet()); } status = retweetedStatus; } else if (orig.isQuote()) { final Status quotedStatus = orig.getQuotedStatus(); final User quoteUser = orig.getUser(); final long quotedById = quoteUser.getId(); values.put(Statuses.QUOTE_ID, quotedStatus.getId()); final String textHtml = TwitterContentUtils.formatStatusText(orig); values.put(Statuses.QUOTE_TEXT_HTML, textHtml); values.put(Statuses.QUOTE_TEXT_PLAIN, orig.getText()); values.put(Statuses.QUOTE_TEXT_UNESCAPED, toPlainText(textHtml)); values.put(Statuses.QUOTE_TIMESTAMP, orig.getCreatedAt().getTime()); values.put(Statuses.QUOTE_SOURCE, orig.getSource()); values.put(Statuses.QUOTED_BY_USER_ID, quotedById); values.put(Statuses.QUOTED_BY_USER_NAME, quoteUser.getName()); values.put(Statuses.QUOTED_BY_USER_SCREEN_NAME, quoteUser.getScreenName()); values.put(Statuses.QUOTED_BY_USER_PROFILE_IMAGE, quoteUser.getProfileImageUrlHttps()); values.put(Statuses.QUOTED_BY_USER_IS_VERIFIED, quoteUser.isVerified()); values.put(Statuses.QUOTED_BY_USER_IS_PROTECTED, quoteUser.isProtected()); values.put(Statuses.IS_QUOTE, true); if (quotedById == accountId) { values.put(Statuses.MY_QUOTE_ID, orig.getId()); // } else { // values.put(Statuses.MY_QUOTE_ID, orig.getCurrentUserRetweet()); } status = quotedStatus; } else { values.put(Statuses.MY_RETWEET_ID, orig.getCurrentUserRetweet()); status = orig; } final User user = status.getUser(); final long userId = user.getId(); final String profileImageUrl = (user.getProfileImageUrlHttps()); final String name = user.getName(), screenName = user.getScreenName(); values.put(Statuses.USER_ID, userId); values.put(Statuses.USER_NAME, name); values.put(Statuses.USER_SCREEN_NAME, screenName); values.put(Statuses.IS_PROTECTED, user.isProtected()); values.put(Statuses.IS_VERIFIED, user.isVerified()); values.put(Statuses.USER_PROFILE_IMAGE_URL, profileImageUrl); values.put(CachedUsers.IS_FOLLOWING, user.isFollowing()); final String textHtml = TwitterContentUtils.formatStatusText(status); values.put(Statuses.TEXT_HTML, textHtml); values.put(Statuses.TEXT_PLAIN, status.getText()); values.put(Statuses.TEXT_UNESCAPED, toPlainText(textHtml)); values.put(Statuses.RETWEET_COUNT, status.getRetweetCount()); values.put(Statuses.REPLY_COUNT, status.getReplyCount()); values.put(Statuses.FAVORITE_COUNT, status.getFavoriteCount()); values.put(Statuses.DESCENDENT_REPLY_COUNT, status.getDescendentReplyCount()); values.put(Statuses.IN_REPLY_TO_STATUS_ID, status.getInReplyToStatusId()); values.put(Statuses.IN_REPLY_TO_USER_ID, status.getInReplyToUserId()); values.put(Statuses.IN_REPLY_TO_USER_NAME, TwitterContentUtils.getInReplyToName(status)); values.put(Statuses.IN_REPLY_TO_USER_SCREEN_NAME, status.getInReplyToScreenName()); values.put(Statuses.SOURCE, status.getSource()); values.put(Statuses.IS_POSSIBLY_SENSITIVE, status.isPossiblySensitive()); final GeoLocation location = status.getGeoLocation(); if (location != null) { values.put(Statuses.LOCATION, ParcelableLocation.toString(location.getLatitude(), location.getLongitude())); } final Place place = status.getPlace(); if (place != null) { values.put(Statuses.PLACE_FULL_NAME, place.getFullName()); } values.put(Statuses.IS_FAVORITE, status.isFavorited()); final ParcelableMedia[] media = ParcelableMedia.fromEntities(status); if (media != null) { values.put(Statuses.MEDIA_LIST, SimpleValueSerializer.toSerializedString(media)); } final ParcelableUserMention[] mentions = ParcelableUserMention.fromStatus(status); if (mentions != null) { values.put(Statuses.MENTIONS_LIST, SimpleValueSerializer.toSerializedString(mentions)); } final ParcelableCardEntity card = ParcelableCardEntity.fromCardEntity(status.getCard(), accountId); if (card != null) { values.put(Statuses.CARD_NAME, card.name); values.put(Statuses.CARD, JSONSerializer.toJSONObjectString(card)); } return values; }
From source file:org.getlantern.firetweet.util.TwitterWrapper.java
License:Open Source License
@NonNull public static User showUserAlternative(final Twitter twitter, final long id, final String screenName) throws TwitterException { final String searchScreenName; if (screenName != null) { searchScreenName = screenName;// w ww .j a v a 2s. co m } else if (id != -1) { searchScreenName = twitter.showFriendship(twitter.getId(), id).getTargetUserScreenName(); } else throw new IllegalArgumentException(); final Paging paging = new Paging(); paging.count(1); if (id != -1) { final ResponseList<Status> timeline = twitter.getUserTimeline(id, paging); for (final Status status : timeline) { final User user = status.getUser(); if (user.getId() == id) return user; } } else { final ResponseList<Status> timeline = twitter.getUserTimeline(screenName, paging); for (final Status status : timeline) { final User user = status.getUser(); if (searchScreenName.equalsIgnoreCase(user.getScreenName())) return user; } } for (final User user : twitter.searchUsers(searchScreenName, 1)) { if (user.getId() == id || searchScreenName.equalsIgnoreCase(user.getScreenName())) return user; } throw new TwitterException("can't find user"); }
From source file:org.getlantern.firetweet.util.Utils.java
License:Open Source License
public static String getUserName(final Context context, final User user) { if (context == null || user == null) return null; return UserColorNameUtils.getDisplayName(context, user.getId(), user.getName(), user.getScreenName()); }
From source file:org.graylog2.inputs.twitter.TwitterCodec.java
License:Open Source License
private Message createMessageFromStatus(final Status status) { final Message message = new Message(status.getText(), "twitter.com", new DateTime(status.getCreatedAt())); message.addField("facility", "Tweets"); message.addField("level", 6); message.addField("tweet_id", status.getId()); message.addField("tweet_is_retweet", Boolean.toString(status.isRetweet())); message.addField("tweet_favorite_count", status.getFavoriteCount()); message.addField("tweet_retweet_count", status.getRetweetCount()); message.addField("tweet_language", status.getLang()); final GeoLocation geoLocation = status.getGeoLocation(); if (geoLocation != null) { message.addField("tweet_geo_long", geoLocation.getLongitude()); message.addField("tweet_geo_lat", geoLocation.getLatitude()); }// w ww . j a va 2 s.co m final User user = status.getUser(); if (user != null) { message.addField("tweet_url", "https://twitter.com/" + user.getScreenName() + "/status/" + status.getId()); message.addField("user_id", user.getId()); message.addField("user_name", user.getScreenName()); message.addField("user_description", user.getDescription()); message.addField("user_timezone", user.getTimeZone()); message.addField("user_utc_offset", user.getUtcOffset()); message.addField("user_location", user.getLocation()); message.addField("user_language", user.getLang()); message.addField("user_url", user.getURL()); message.addField("user_followers", user.getFollowersCount()); message.addField("user_tweets", user.getStatusesCount()); message.addField("user_favorites", user.getFavouritesCount()); } return message; }
From source file:org.jwebsocket.plugins.twitter.TwitterPlugIn.java
License:Apache License
private void getUserData(WebSocketConnector aConnector, Token aToken) { TokenServer lServer = getServer();/*from w w w .j a v a 2s. c o m*/ // instantiate response token Token lResponse = lServer.createResponse(aToken); String lUsername = aToken.getString("username"); Integer lUserId = aToken.getInteger("userid"); try { User lUser; // if user id is given use this to get user data if (lUserId != null && lUserId != 0) { lUser = mTwitter.showUser(lUserId); // if user name is given use this to get user data } else if (lUsername != null && lUsername.length() > 0) { lUser = mTwitter.showUser(lUsername); // otherwise return user data of provider (ourselves) } else { lUser = mTwitter.verifyCredentials(); } if (lUser != null) { lResponse.setString("screenname", lUser.getScreenName()); lResponse.setLong("id", lUser.getId()); lResponse.setString("description", lUser.getDescription()); lResponse.setString("location", lUser.getLocation()); lResponse.setString("lang", lUser.getLang()); lResponse.setString("name", lUser.getName()); } else { lResponse.setInteger("code", -1); lResponse.setString("msg", "Neither UserId nor Username passed."); } } catch (Exception lEx) { String lMsg = lEx.getClass().getSimpleName() + ": " + lEx.getMessage(); mLog.error(lMsg); lResponse.setInteger("code", -1); lResponse.setString("msg", lMsg); } // send response to requester lServer.sendToken(aConnector, lResponse); }
From source file:org.keycloak.social.twitter.TwitterProvider.java
License:Open Source License
@Override public SocialUser processCallback(SocialProviderConfig config, AuthCallback callback) throws SocialProviderException { if (callback.getQueryParam("denied") != null) { throw new SocialAccessDeniedException(); }//from ww w . ja va2s. c o m try { Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(config.getKey(), config.getSecret()); String token = callback.getQueryParam("oauth_token"); String verifier = callback.getQueryParam("oauth_verifier"); RequestToken requestToken = new RequestToken((String) callback.getAttribute("token"), (String) callback.getAttribute("tokenSecret")); twitter.getOAuthAccessToken(requestToken, verifier); twitter4j.User twitterUser = twitter.verifyCredentials(); SocialUser user = new SocialUser(Long.toString(twitterUser.getId()), twitterUser.getScreenName()); user.setName(twitterUser.getName()); return user; } catch (Exception e) { throw new SocialProviderException(e); } }
From source file:org.loklak.harvester.TwitterAPI.java
License:Open Source License
/** * search for twitter user names by a given set of user id's * @param id_strs/* w w w .j a v a 2s . c o m*/ * @param lookupLocalUsersByUserId if this is true and successful, the resulting names may contain users without user info in the user dump * @return * @throws IOException * @throws TwitterException */ public static Map<String, Number> getScreenName(Set<Number> id_strs, final int maxFollowers, boolean lookupLocalUsersByUserId) throws IOException, TwitterException { // we have several sources to get this mapping: // - 1st / fastest: mapping from DAO.twitter_user_dump // - 2nd / fast : mapping from DAO.searchLocalUserByUserId(user_id) // - 3rd / slow : from twitter API with twitter.lookupUsers(String[] user_id) // first we check all fast solutions until trying the twitter api Map<String, Number> r = new HashMap<>(); Set<Number> id4api = new HashSet<>(); for (Number id_str : id_strs) { if (r.size() >= maxFollowers) break; JsonFactory mapcapsule = DAO.user_dump.get("id_str", id_str.toString()); if (mapcapsule != null) { JSONObject map = mapcapsule.getJSON(); String screen_name = map.has("screen_name") ? (String) map.get("screen_name") : null; if (screen_name != null) { r.put(screen_name, id_str); continue; } } if (lookupLocalUsersByUserId) { UserEntry ue = DAO.searchLocalUserByUserId(id_str.toString()); if (ue != null) { String screen_name = ue.getScreenName(); if (screen_name != null) { r.put(screen_name, id_str); continue; } } } id4api.add(id_str); } while (id4api.size() > 100 && id4api.size() + r.size() > maxFollowers) id4api.remove(id4api.iterator().next()); // resolve the remaining user_ids from the twitter api if (r.size() < maxFollowers && id4api.size() > 0) { TwitterFactory tf = getAppTwitterFactory(); if (tf == null) return new HashMap<>(); Twitter twitter = tf.getInstance(); collect: while (id4api.size() > 0) { // construct a query term with at most 100 id's int chunksize = Math.min(100, id4api.size()); long[] u = new long[chunksize]; Iterator<Number> ni = id4api.iterator(); for (int i = 0; i < chunksize; i++) { u[i] = ni.next().longValue(); } try { ResponseList<User> users = twitter.lookupUsers(u); for (User usr : users) { JSONObject map = user2json(usr); enrichLocation(map); DAO.user_dump.putUnique(map); r.put(usr.getScreenName(), usr.getId()); id4api.remove(usr.getId()); } } catch (TwitterException e) { if (r.size() == 0) throw e; break collect; } } } return r; }
From source file:org.loklak.scraper.TwitterRiver.java
License:Apache License
/** * Get users id of each list to stream them. * @param tUserlists List of user list. Should be a public list. * @return/*from w w w . j av a 2 s .c o m*/ */ private long[] getUsersListMembers(String[] tUserlists) { logger.debug("Fetching user id of given lists"); List<Long> listUserIdToFollow = new ArrayList<Long>(); Configuration cb = buildTwitterConfiguration(); Twitter twitterImpl = new TwitterFactory(cb).getInstance(); //For each list given in parameter for (String listId : tUserlists) { logger.debug("Adding users of list {} ", listId); String[] splitListId = listId.split("/"); try { long cursor = -1; PagableResponseList<User> itUserListMembers; do { itUserListMembers = twitterImpl.getUserListMembers(splitListId[0], splitListId[1], cursor); for (User member : itUserListMembers) { long userId = member.getId(); listUserIdToFollow.add(userId); } } while ((cursor = itUserListMembers.getNextCursor()) != 0); } catch (TwitterException te) { logger.error("Failed to get list members for : {}", listId, te); } } //Just casting from Long to long long ret[] = new long[listUserIdToFollow.size()]; int pos = 0; for (Long userId : listUserIdToFollow) { ret[pos] = userId; pos++; } return ret; }
From source file:org.mariotaku.twidere.loader.Twitter4JUsersLoader.java
License:Open Source License
@Override public List<ParcelableUser> loadInBackground() { final List<ParcelableUser> data = getData(); final List<User> users; try {//from www .j a v a 2 s.c o m users = getUsers(getTwitterInstance(mContext, mAccountId, true)); if (users == null) return data; } catch (final TwitterException e) { e.printStackTrace(); return data; } int pos = data.size(); for (final User user : users) { if (hasId(user.getId())) { continue; } data.add(new ParcelableUser(user, mAccountId, pos, mHiResProfileImage)); pos++; } Collections.sort(data); return data; }