Example usage for twitter4j User getStatusesCount

List of usage examples for twitter4j User getStatusesCount

Introduction

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

Prototype

int getStatusesCount();

Source Link

Usage

From source file:org.apache.asterix.external.util.TweetProcessor.java

License:Apache License

public AMutableRecord processNextTweet(Status tweet) {
    User user = tweet.getUser();

    // Tweet user data
    ((AMutableString) mutableUserFields[userFieldNameMap.get(Tweet.SCREEN_NAME)])
            .setValue(JObjectUtil.getNormalizedString(user.getScreenName()));
    ((AMutableString) mutableUserFields[userFieldNameMap.get(Tweet.LANGUAGE)])
            .setValue(JObjectUtil.getNormalizedString(user.getLang()));
    ((AMutableInt32) mutableUserFields[userFieldNameMap.get(Tweet.FRIENDS_COUNT)])
            .setValue(user.getFriendsCount());
    ((AMutableInt32) mutableUserFields[userFieldNameMap.get(Tweet.STATUS_COUNT)])
            .setValue(user.getStatusesCount());
    ((AMutableString) mutableUserFields[userFieldNameMap.get(Tweet.NAME)])
            .setValue(JObjectUtil.getNormalizedString(user.getName()));
    ((AMutableInt32) mutableUserFields[userFieldNameMap.get(Tweet.FOLLOWERS_COUNT)])
            .setValue(user.getFollowersCount());

    // Tweet data
    ((AMutableString) mutableTweetFields[tweetFieldNameMap.get(Tweet.ID)])
            .setValue(String.valueOf(tweet.getId()));

    int userPos = tweetFieldNameMap.get(Tweet.USER);
    for (int i = 0; i < mutableUserFields.length; i++) {
        ((AMutableRecord) mutableTweetFields[userPos]).setValueAtPos(i, mutableUserFields[i]);
    }/* ww  w . j  a  va2  s .c o m*/
    if (tweet.getGeoLocation() != null) {
        ((AMutableDouble) mutableTweetFields[tweetFieldNameMap.get(Tweet.LATITUDE)])
                .setValue(tweet.getGeoLocation().getLatitude());
        ((AMutableDouble) mutableTweetFields[tweetFieldNameMap.get(Tweet.LONGITUDE)])
                .setValue(tweet.getGeoLocation().getLongitude());
    } else {
        ((AMutableDouble) mutableTweetFields[tweetFieldNameMap.get(Tweet.LATITUDE)]).setValue(0);
        ((AMutableDouble) mutableTweetFields[tweetFieldNameMap.get(Tweet.LONGITUDE)]).setValue(0);
    }
    ((AMutableString) mutableTweetFields[tweetFieldNameMap.get(Tweet.CREATED_AT)])
            .setValue(JObjectUtil.getNormalizedString(tweet.getCreatedAt().toString()));
    ((AMutableString) mutableTweetFields[tweetFieldNameMap.get(Tweet.MESSAGE)])
            .setValue(JObjectUtil.getNormalizedString(tweet.getText()));

    for (int i = 0; i < mutableTweetFields.length; i++) {
        mutableRecord.setValueAtPos(i, mutableTweetFields[i]);
    }

    return mutableRecord;

}

From source file:org.apache.druid.examples.twitter.TwitterSpritzerFirehoseFactory.java

License:Apache License

@Override
public Firehose connect(InputRowParser parser, File temporaryDirectory) {
    final ConnectionLifeCycleListener connectionLifeCycleListener = new ConnectionLifeCycleListener() {
        @Override//from   w w  w. j ava  2s .  c o m
        public void onConnect() {
            log.info("Connected_to_Twitter");
        }

        @Override
        public void onDisconnect() {
            log.info("Disconnect_from_Twitter");
        }

        /**
         * called before thread gets cleaned up
         */
        @Override
        public void onCleanUp() {
            log.info("Cleanup_twitter_stream");
        }
    }; // ConnectionLifeCycleListener

    final TwitterStream twitterStream;
    final StatusListener statusListener;
    final int QUEUE_SIZE = 2000;
    /** This queue is used to move twitter events from the twitter4j thread to the druid ingest thread.   */
    final BlockingQueue<Status> queue = new ArrayBlockingQueue<Status>(QUEUE_SIZE);
    final long startMsec = System.currentTimeMillis();

    //
    //   set up Twitter Spritzer
    //
    twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addConnectionLifeCycleListener(connectionLifeCycleListener);
    statusListener = new StatusListener() { // This is what really gets called to deliver stuff from twitter4j
        @Override
        public void onStatus(Status status) {
            // time to stop?
            if (Thread.currentThread().isInterrupted()) {
                throw new RuntimeException("Interrupted, time to stop");
            }
            try {
                boolean success = queue.offer(status, 15L, TimeUnit.SECONDS);
                if (!success) {
                    log.warn("queue too slow!");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("InterruptedException", e);
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //log.info("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            // This notice will be sent each time a limited stream becomes unlimited.
            // If this number is high and or rapidly increasing, it is an indication that your predicate is too broad, and you should consider a predicate with higher selectivity.
            log.warn("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            //log.info("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onException(Exception ex) {
            log.error(ex, "Got exception");
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            log.warn("Got stall warning: %s", warning);
        }
    };

    twitterStream.addListener(statusListener);
    twitterStream.sample(); // creates a generic StatusStream
    log.info("returned from sample()");

    return new Firehose() {

        private final Runnable doNothingRunnable = new Runnable() {
            @Override
            public void run() {
            }
        };

        private long rowCount = 0L;
        private boolean waitIfmax = (getMaxEventCount() < 0L);
        private final Map<String, Object> theMap = new TreeMap<>();
        // DIY json parsing // private final ObjectMapper omapper = new ObjectMapper();

        private boolean maxTimeReached() {
            if (getMaxRunMinutes() <= 0) {
                return false;
            } else {
                return (System.currentTimeMillis() - startMsec) / 60000L >= getMaxRunMinutes();
            }
        }

        private boolean maxCountReached() {
            return getMaxEventCount() >= 0 && rowCount >= getMaxEventCount();
        }

        @Override
        public boolean hasMore() {
            if (maxCountReached() || maxTimeReached()) {
                return waitIfmax;
            } else {
                return true;
            }
        }

        @Nullable
        @Override
        public InputRow nextRow() {
            // Interrupted to stop?
            if (Thread.currentThread().isInterrupted()) {
                throw new RuntimeException("Interrupted, time to stop");
            }

            // all done?
            if (maxCountReached() || maxTimeReached()) {
                if (waitIfmax) {
                    // sleep a long time instead of terminating
                    try {
                        log.info("reached limit, sleeping a long time...");
                        Thread.sleep(2000000000L);
                    } catch (InterruptedException e) {
                        throw new RuntimeException("InterruptedException", e);
                    }
                } else {
                    // allow this event through, and the next hasMore() call will be false
                }
            }
            if (++rowCount % 1000 == 0) {
                log.info("nextRow() has returned %,d InputRows", rowCount);
            }

            Status status;
            try {
                status = queue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException("InterruptedException", e);
            }

            theMap.clear();

            HashtagEntity[] hts = status.getHashtagEntities();
            String text = status.getText();
            theMap.put("text", (null == text) ? "" : text);
            theMap.put("htags", (hts.length > 0)
                    ? Lists.transform(Arrays.asList(hts), new Function<HashtagEntity, String>() {
                        @Nullable
                        @Override
                        public String apply(HashtagEntity input) {
                            return input.getText();
                        }
                    })
                    : ImmutableList.<String>of());

            long[] lcontrobutors = status.getContributors();
            List<String> contributors = new ArrayList<>();
            for (long contrib : lcontrobutors) {
                contributors.add(StringUtils.format("%d", contrib));
            }
            theMap.put("contributors", contributors);

            GeoLocation geoLocation = status.getGeoLocation();
            if (null != geoLocation) {
                double lat = status.getGeoLocation().getLatitude();
                double lon = status.getGeoLocation().getLongitude();
                theMap.put("lat", lat);
                theMap.put("lon", lon);
            } else {
                theMap.put("lat", null);
                theMap.put("lon", null);
            }

            if (status.getSource() != null) {
                Matcher m = sourcePattern.matcher(status.getSource());
                theMap.put("source", m.find() ? m.group(1) : status.getSource());
            }

            theMap.put("retweet", status.isRetweet());

            if (status.isRetweet()) {
                Status original = status.getRetweetedStatus();
                theMap.put("retweet_count", original.getRetweetCount());

                User originator = original.getUser();
                theMap.put("originator_screen_name", originator != null ? originator.getScreenName() : "");
                theMap.put("originator_follower_count",
                        originator != null ? originator.getFollowersCount() : "");
                theMap.put("originator_friends_count", originator != null ? originator.getFriendsCount() : "");
                theMap.put("originator_verified", originator != null ? originator.isVerified() : "");
            }

            User user = status.getUser();
            final boolean hasUser = (null != user);
            theMap.put("follower_count", hasUser ? user.getFollowersCount() : 0);
            theMap.put("friends_count", hasUser ? user.getFriendsCount() : 0);
            theMap.put("lang", hasUser ? user.getLang() : "");
            theMap.put("utc_offset", hasUser ? user.getUtcOffset() : -1); // resolution in seconds, -1 if not available?
            theMap.put("statuses_count", hasUser ? user.getStatusesCount() : 0);
            theMap.put("user_id", hasUser ? StringUtils.format("%d", user.getId()) : "");
            theMap.put("screen_name", hasUser ? user.getScreenName() : "");
            theMap.put("location", hasUser ? user.getLocation() : "");
            theMap.put("verified", hasUser ? user.isVerified() : "");

            theMap.put("ts", status.getCreatedAt().getTime());

            List<String> dimensions = Lists.newArrayList(theMap.keySet());

            return new MapBasedInputRow(status.getCreatedAt().getTime(), dimensions, theMap);
        }

        @Override
        public Runnable commit() {
            // ephemera in, ephemera out.
            return doNothingRunnable; // reuse the same object each time
        }

        @Override
        public void close() {
            log.info("CLOSE twitterstream");
            twitterStream.shutdown(); // invokes twitterStream.cleanUp()
        }
    };
}

From source file:org.apache.flume.sink.solr.morphline.TwitterSource.java

License:Apache License

private Record extractRecord(String idPrefix, Schema avroSchema, Status status) {
    User user = status.getUser();
    Record doc = new Record(avroSchema);

    doc.put("id", idPrefix + status.getId());
    doc.put("created_at", formatterTo.format(status.getCreatedAt()));
    doc.put("retweet_count", status.getRetweetCount());
    doc.put("retweeted", status.isRetweet());
    doc.put("in_reply_to_user_id", status.getInReplyToUserId());
    doc.put("in_reply_to_status_id", status.getInReplyToStatusId());

    addString(doc, "source", status.getSource());
    addString(doc, "text", status.getText());

    MediaEntity[] mediaEntities = status.getMediaEntities();
    if (mediaEntities.length > 0) {
        addString(doc, "media_url_https", mediaEntities[0].getMediaURLHttps());
        addString(doc, "expanded_url", mediaEntities[0].getExpandedURL());
    }//w ww. j av a  2 s .co m

    doc.put("user_friends_count", user.getFriendsCount());
    doc.put("user_statuses_count", user.getStatusesCount());
    doc.put("user_followers_count", user.getFollowersCount());
    addString(doc, "user_location", user.getLocation());
    addString(doc, "user_description", user.getDescription());
    addString(doc, "user_screen_name", user.getScreenName());
    addString(doc, "user_name", user.getName());
    return doc;
}

From source file:org.getlantern.firetweet.loader.support.RetweetsOfMeLoader.java

License:Open Source License

@NonNull
@Override/*  ww  w. j  a  v a  2  s . c om*/
protected ResponseList<Status> getStatuses(@NonNull final Twitter twitter, final Paging paging)
        throws TwitterException {
    if (twitter == null)
        return null;
    final ResponseList<Status> statuses = twitter.getRetweetsOfMe(paging);
    if (mTotalItemsCount == -1 && !statuses.isEmpty()) {
        final User user = statuses.get(0).getUser();
        if (user != null) {
            mTotalItemsCount = user.getStatusesCount();
        }
    }
    return statuses;
}

From source file:org.getlantern.firetweet.model.ParcelableUser.java

License:Open Source License

public ParcelableUser(final User user, final long account_id, final long position) {
    this.position = position;
    this.account_id = account_id;
    final URLEntity[] urls_url_entities = user.getURLEntities();
    id = user.getId();//  w ww .j av a2s  .  com
    created_at = user.getCreatedAt().getTime();
    is_protected = user.isProtected();
    is_verified = user.isVerified();
    name = user.getName();
    screen_name = user.getScreenName();
    description_plain = user.getDescription();
    description_html = TwitterContentUtils.formatUserDescription(user);
    description_expanded = TwitterContentUtils.formatExpandedUserDescription(user);
    description_unescaped = HtmlEscapeHelper.toPlainText(description_html);
    location = user.getLocation();
    profile_image_url = user.getProfileImageUrlHttps();
    profile_banner_url = user.getProfileBannerImageUrl();
    url = user.getURL();
    url_expanded = url != null && urls_url_entities != null && urls_url_entities.length > 0
            ? urls_url_entities[0].getExpandedURL()
            : null;
    is_follow_request_sent = user.isFollowRequestSent();
    followers_count = user.getFollowersCount();
    friends_count = user.getFriendsCount();
    statuses_count = user.getStatusesCount();
    favorites_count = user.getFavouritesCount();
    listed_count = user.getListedCount();
    is_following = user.isFollowing();
    background_color = ParseUtils.parseColor("#" + user.getProfileBackgroundColor(), 0);
    link_color = ParseUtils.parseColor("#" + user.getProfileLinkColor(), 0);
    text_color = ParseUtils.parseColor("#" + user.getProfileTextColor(), 0);
    is_cache = false;
    is_basic = false;
}

From source file:org.getlantern.firetweet.util.ContentValuesCreator.java

License:Open Source License

public static ContentValues createCachedUser(final User user) {
    if (user == null || user.getId() <= 0)
        return null;
    final String profile_image_url = user.getProfileImageUrlHttps();
    final String url = user.getURL();
    final URLEntity[] urls = user.getURLEntities();
    final ContentValues values = new ContentValues();
    values.put(CachedUsers.USER_ID, user.getId());
    values.put(CachedUsers.NAME, user.getName());
    values.put(CachedUsers.SCREEN_NAME, user.getScreenName());
    values.put(CachedUsers.PROFILE_IMAGE_URL, profile_image_url);
    values.put(CachedUsers.PROFILE_BANNER_URL, user.getProfileBannerImageUrl());
    values.put(CachedUsers.CREATED_AT, user.getCreatedAt().getTime());
    values.put(CachedUsers.IS_PROTECTED, user.isProtected());
    values.put(CachedUsers.IS_VERIFIED, user.isVerified());
    values.put(CachedUsers.IS_FOLLOWING, user.isFollowing());
    values.put(CachedUsers.FAVORITES_COUNT, user.getFavouritesCount());
    values.put(CachedUsers.FOLLOWERS_COUNT, user.getFollowersCount());
    values.put(CachedUsers.FRIENDS_COUNT, user.getFriendsCount());
    values.put(CachedUsers.STATUSES_COUNT, user.getStatusesCount());
    values.put(CachedUsers.LISTED_COUNT, user.getListedCount());
    values.put(CachedUsers.LOCATION, user.getLocation());
    values.put(CachedUsers.DESCRIPTION_PLAIN, user.getDescription());
    values.put(CachedUsers.DESCRIPTION_HTML, TwitterContentUtils.formatUserDescription(user));
    values.put(CachedUsers.DESCRIPTION_EXPANDED, TwitterContentUtils.formatExpandedUserDescription(user));
    values.put(CachedUsers.URL, url);
    if (url != null && urls != null && urls.length > 0) {
        values.put(CachedUsers.URL_EXPANDED, urls[0].getExpandedURL());
    }/*w w w  . ja  v  a 2  s  .  c om*/
    values.put(CachedUsers.BACKGROUND_COLOR, ParseUtils.parseColor("#" + user.getProfileBackgroundColor(), 0));
    values.put(CachedUsers.LINK_COLOR, ParseUtils.parseColor("#" + user.getProfileLinkColor(), 0));
    values.put(CachedUsers.TEXT_COLOR, ParseUtils.parseColor("#" + user.getProfileTextColor(), 0));
    return values;
}

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());
    }//  ww w  . j av a 2s .  c  o 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.mariotaku.twidere.loader.UserTimelineLoader.java

License:Open Source License

@Override
protected ResponseList<Status> getStatuses(final Twitter twitter, final Paging paging) throws TwitterException {
    if (twitter == null)
        return null;
    final ResponseList<Status> statuses;
    if (mUserId != -1) {
        statuses = twitter.getUserTimeline(mUserId, paging);
    } else if (mUserScreenName != null) {
        statuses = twitter.getUserTimeline(mUserScreenName, paging);
    } else//w w  w. jav  a2s . c o m
        return null;
    if (mTotalItemsCount == -1 && !statuses.isEmpty()) {
        final User user = statuses.get(0).getUser();
        if (user != null) {
            mTotalItemsCount = user.getStatusesCount();
        }
    }
    return statuses;
}

From source file:org.mariotaku.twidere.model.ParcelableUser.java

License:Open Source License

public ParcelableUser(final User user, final long account_id, final long position) {
    this.position = position;
    this.account_id = account_id;
    final URLEntity[] urls_url_entities = user.getURLEntities();
    id = user.getId();//from w  w w  .j a v  a2 s .  c o  m
    created_at = user.getCreatedAt().getTime();
    is_protected = user.isProtected();
    is_verified = user.isVerified();
    name = user.getName();
    screen_name = user.getScreenName();
    description_plain = user.getDescription();
    description_html = TwitterContentUtils.formatUserDescription(user);
    description_expanded = TwitterContentUtils.formatExpandedUserDescription(user);
    description_unescaped = HtmlEscapeHelper.toPlainText(description_html);
    location = user.getLocation();
    profile_image_url = user.getProfileImageUrlHttps();
    profile_banner_url = user.getProfileBannerImageUrl();
    url = user.getURL();
    url_expanded = url != null && urls_url_entities != null && urls_url_entities.length > 0
            ? urls_url_entities[0].getExpandedURL()
            : null;
    is_follow_request_sent = user.isFollowRequestSent();
    followers_count = user.getFollowersCount();
    friends_count = user.getFriendsCount();
    statuses_count = user.getStatusesCount();
    favorites_count = user.getFavouritesCount();
    listed_count = user.getListedCount();
    media_count = user.getMediaCount();
    is_following = user.isFollowing();
    background_color = ParseUtils.parseColor("#" + user.getProfileBackgroundColor(), 0);
    link_color = ParseUtils.parseColor("#" + user.getProfileLinkColor(), 0);
    text_color = ParseUtils.parseColor("#" + user.getProfileTextColor(), 0);
    is_cache = false;
    is_basic = false;
}

From source file:org.mariotaku.twidere.util.ContentValuesCreator.java

License:Open Source License

public static ContentValues createCachedUser(final User user) {
    if (user == null || user.getId() <= 0)
        return null;
    final String profile_image_url = user.getProfileImageUrlHttps();
    final String url = user.getURL();
    final URLEntity[] urls = user.getURLEntities();
    final ContentValues values = new ContentValues();
    values.put(CachedUsers.USER_ID, user.getId());
    values.put(CachedUsers.NAME, user.getName());
    values.put(CachedUsers.SCREEN_NAME, user.getScreenName());
    values.put(CachedUsers.PROFILE_IMAGE_URL, profile_image_url);
    values.put(CachedUsers.PROFILE_BANNER_URL, user.getProfileBannerImageUrl());
    values.put(CachedUsers.CREATED_AT, user.getCreatedAt().getTime());
    values.put(CachedUsers.IS_PROTECTED, user.isProtected());
    values.put(CachedUsers.IS_VERIFIED, user.isVerified());
    values.put(CachedUsers.IS_FOLLOWING, user.isFollowing());
    values.put(CachedUsers.FAVORITES_COUNT, user.getFavouritesCount());
    values.put(CachedUsers.FOLLOWERS_COUNT, user.getFollowersCount());
    values.put(CachedUsers.FRIENDS_COUNT, user.getFriendsCount());
    values.put(CachedUsers.STATUSES_COUNT, user.getStatusesCount());
    values.put(CachedUsers.LISTED_COUNT, user.getListedCount());
    values.put(CachedUsers.MEDIA_COUNT, user.getMediaCount());
    values.put(CachedUsers.LOCATION, user.getLocation());
    values.put(CachedUsers.DESCRIPTION_PLAIN, user.getDescription());
    values.put(CachedUsers.DESCRIPTION_HTML, TwitterContentUtils.formatUserDescription(user));
    values.put(CachedUsers.DESCRIPTION_EXPANDED, TwitterContentUtils.formatExpandedUserDescription(user));
    values.put(CachedUsers.URL, url);
    if (url != null && urls != null && urls.length > 0) {
        values.put(CachedUsers.URL_EXPANDED, urls[0].getExpandedURL());
    }//from   w  ww.j a  va  2 s.  c o m
    values.put(CachedUsers.BACKGROUND_COLOR, ParseUtils.parseColor("#" + user.getProfileBackgroundColor(), 0));
    values.put(CachedUsers.LINK_COLOR, ParseUtils.parseColor("#" + user.getProfileLinkColor(), 0));
    values.put(CachedUsers.TEXT_COLOR, ParseUtils.parseColor("#" + user.getProfileTextColor(), 0));
    return values;
}