Example usage for twitter4j Status getCreatedAt

List of usage examples for twitter4j Status getCreatedAt

Introduction

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

Prototype

Date getCreatedAt();

Source Link

Document

Return the created_at

Usage

From source file:crawling.PrintSampleStream.java

License:Apache License

private static void storeUserID(Status status) {
    // TODO Auto-generated method stub
    Long id = status.getUser().getId();
    String username = status.getUser().getScreenName();
    String realname = status.getUser().getName();
    String text = status.getText();
    Date date = status.getCreatedAt();

    //String line = id + "::" + username + "::" + realname + "::" + text + "::" + date + "::" + (new Date());
    String line = id + "," + username;
    //System.out.println(line);
    idOut.println(line);/*w ww  . j  a v  a 2s  .  c  o  m*/
    idOut.flush();
    System.out.print("\r" + ++count + ", " + tweetsCount);
}

From source file:crawling.SearchTweetsHashtag.java

License:Apache License

private static void storeATweet(Status status) {
    int len = status.getUserMentionEntities().length;
    /*//from   w w w  . ja v  a2  s  .c  om
     * We only need the interactions
     */
    if (len <= 0)
        return;

    String record = "";
    record += status.getId();
    record += "::" + status.getInReplyToStatusId();
    record += "::" + status.getInReplyToUserId();
    record += "::" + status.getRetweetCount();
    if (status.getRetweetedStatus() != null)
        record += "::" + status.getRetweetedStatus().getId();
    else
        record += "::" + "-1";
    //record += "::" + status.isRetweet();
    //int len = status.getUserMentionEntities().length;
    if (len > 0) {
        record += "::";
        for (int l = 0; l < len; l++) {
            UserMentionEntity ent = status.getUserMentionEntities()[l];
            record += "," + ent.getId();
        }
    } else {
        record += "::" + "-1";
    }
    len = status.getURLEntities().length;
    if (len > 0) {
        record += "::";
        for (int l = 0; l < len; l++) {
            URLEntity ent = status.getURLEntities()[l];
            record += "," + ent.getURL() + "|"
            //+ ent.getDisplayURL() + "|"
                    + ent.getExpandedURL();
        }
    } else
        record += "::" + "-1";
    record += "::" + cleanText(status.getText());
    // We just ignore the text content in this crawling
    //record += "::-1";
    record += "::" +
    // status.getCreatedAt();
            (status.getCreatedAt().getTime() - start.getTime()) / 1000;

    record += "::" + getSource(status.getSource());

    // Geo Location
    if (status.getGeoLocation() != null)
        record += "::" + status.getGeoLocation();
    else
        record += "::" + "-1";

    record += "::" + status.getUser().getId() + "::" + status.getUser().getScreenName() + "::"
            + status.getUser().getName() + "::" + status.getCreatedAt();

    //System.out.println(record);
    out.println(record);
    out.flush();
    count++;
}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(String keyword) {
    try {//w w w  . j  av a2s .com
        Query query = new Query(keyword);
        query.setCount(20000);
        QueryResult result;
        result = twitter.search(query);

        //System.out.println("Getting Tweets..by Key.");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {

                collection.insert(basicObj);
            } catch (Exception e) {
                //System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(GeoLocation myLoc, double radius) {
    try {// w  w  w . j av a 2s. co m
        Query query = new Query();
        Query.Unit unit = Query.KILOMETERS; // or Query.MILES;
        query.setGeoCode(myLoc, radius, unit);
        if (radius > 200)
            query.setCount(20000);
        else
            query.setCount(20000);

        QueryResult result;
        result = this.twitter.search(query);

        //System.out.println("Getting Tweets. by Geo..");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());
            //basicObj.put("isRetweet", tweet.getPlace().getGeometryCoordinates());                
            //basicObj.put("POS", tweet.getWithheldInCountries());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {
                //items.insert(basicObj);
                collection.insert(basicObj);
            } catch (Exception e) {
                //     System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}

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

License:Open Source License

/**
 * Translate twitter status./*ww w  .  j a va2s .  co m*/
 *
 * @param status
 *          the status
 * @return the string
 */
public static String translateTwitterStatus(Status status) {
    String msg = "";
    if (status.getUser() != null && status.getUser().getScreenName() != null)
        msg += "*" + status.getUser().getScreenName() + "*: ";
    if (!status.isRetweet())
        msg += JatterTools.processTwitterMessag(status.getText());
    msg += " [" + App.printDateFormat.format(status.getCreatedAt());
    if (status.getGeoLocation() != null)
        msg += " - " + status.getGeoLocation().getLatitude() + "," + status.getGeoLocation().getLongitude();
    msg += " - " + status.getId();
    msg += "]";

    if (status.isRetweet())
        msg += "\n *RT* > " + translateTwitterStatus(status.getRetweetedStatus());

    if (status.getQuotedStatus() != null)
        msg += "\n *QT* > " + translateTwitterStatus(status.getQuotedStatus());

    return msg;
}

From source file:de.fhm.bigdata.projekt.dataimport.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets.//from   w  ww.  j  a v a 2 s  .  c  o  m
 */
@Override
public void start() {
    // The channel is the piece of Flume that sits between the Source and Sink,
    // and is used to process events.
    final ChannelProcessor channel = getChannelProcessor();

    final Map<String, String> headers = new HashMap<String, String>();

    // The StatusListener is a twitter4j API, which can be added to a Twitter
    // stream, and will execute methods every time a message comes in through
    // the stream.
    StatusListener listener = new StatusListener() {
        // The onStatus method is executed every time a new tweet comes in.
        public void onStatus(Status status) {
            // The EventBuilder is used to build an event using the headers and
            // the raw JSON of a tweet
            // shouldn't log possibly sensitive customer data
            logger.debug("tweet arrived");

            headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime()));
            Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers);

            channel.processEvent(event);
        }

        // This listener will ignore everything except for new tweets
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onScrubGeo(long userId, long upToStatusId) {
        }

        public void onException(Exception ex) {
        }

        public void onStallWarning(StallWarning warning) {
        }
    };

    logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}",
            new String[] { consumerKey, accessToken });
    // Set up the stream's listener (defined above),
    twitterStream.addListener(listener);

    // Set up a filter to pull out industry-relevant tweets
    if (keywords.length == 0) {
        logger.debug("Starting up Twitter sampling...");
        twitterStream.sample();
    } else {
        logger.debug("Starting up Twitter filtering...");

        FilterQuery query = new FilterQuery().track(keywords);
        twitterStream.filter(query);
    }
    super.start();
}

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

License:Apache License

long search(String term, Collection<Status> result, Map<String, User> userMap, int tweets,
        long lastMaxCreateTime) throws TwitterException {
    long maxId = 0L;
    long maxMillis = 0L;
    // TODO it looks like only one page is possible with 4.0.0
    int maxPages = 1;
    int hitsPerPage = tweets;

    boolean breakPaging = false;
    for (int page = 0; page < maxPages; page++) {
        Query query = new Query(term);
        // RECENT or POPULAR
        query.setResultType(Query.MIXED);

        // avoid that more recent results disturb our paging!
        if (page > 0)
            query.setMaxId(maxId);/*from   www  .  j  a  v  a2 s  . co  m*/

        query.setCount(hitsPerPage);
        QueryResult res = twitter.search(query);

        // is res.getTweets() sorted?
        for (Status twe : res.getTweets()) {
            // determine maxId in the first page
            if (page == 0 && maxId < twe.getId())
                maxId = twe.getId();

            if (maxMillis < twe.getCreatedAt().getTime())
                maxMillis = twe.getCreatedAt().getTime();

            if (twe.getCreatedAt().getTime() + 1000 < lastMaxCreateTime)
                breakPaging = true;
            else {
                String userName = twe.getUser().getScreenName().toLowerCase();
                User user = userMap.get(userName);
                if (user == null)
                    userMap.put(userName, twe.getUser());

                result.add(twe);
            }
        }

        // minMillis could force us to leave earlier than defined by maxPages
        // or if resulting tweets are less then request (but -10 because of twitter strangeness)
        if (breakPaging || res.getTweets().size() < hitsPerPage - 10)
            break;
    }

    return maxMillis;
}

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());
    tw.setFromUser(user.getScreenName());

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

    tw.setSource(st.getSource());/* w  w  w .  ja v  a  2  s.co  m*/
    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.vanita5.twittnuker.model.ParcelableStatus.java

License:Open Source License

public ParcelableStatus(final Status orig, final long account_id, final boolean is_gap) {
    this.is_gap = is_gap;
    this.account_id = account_id;
    id = orig.getId();/*from www. j  ava  2 s .com*/
    timestamp = getTime(orig.getCreatedAt());
    is_retweet = orig.isRetweet();
    final Status retweeted = orig.getRetweetedStatus();
    final User retweet_user = retweeted != null ? orig.getUser() : null;
    retweet_id = retweeted != null ? retweeted.getId() : -1;
    //NOTE getTime(orig.getCreatedAt())
    retweet_timestamp = retweeted != null ? getTime(retweeted.getCreatedAt()) : -1;
    retweeted_by_id = retweet_user != null ? retweet_user.getId() : -1;
    retweeted_by_name = retweet_user != null ? retweet_user.getName() : null;
    retweeted_by_screen_name = retweet_user != null ? retweet_user.getScreenName() : null;
    retweeted_by_profile_image = retweet_user != null
            ? ParseUtils.parseString(retweet_user.getProfileImageUrlHttps())
            : null;
    final Status status = retweeted != null ? retweeted : orig;
    final User user = status.getUser();
    user_id = user.getId();
    user_name = user.getName();
    user_screen_name = user.getScreenName();
    user_profile_image_url = ParseUtils.parseString(user.getProfileImageUrlHttps());
    user_is_protected = user.isProtected();
    user_is_verified = user.isVerified();
    user_is_following = user.isFollowing();
    text_html = formatStatusText(status);
    media = ParcelableMedia.fromEntities(status);
    text_plain = status.getText();
    retweet_count = status.getRetweetCount();
    favorite_count = status.getFavoriteCount();
    reply_count = status.getReplyCount();
    descendent_reply_count = status.getDescendentReplyCount();
    in_reply_to_name = getInReplyToName(status);
    in_reply_to_screen_name = status.getInReplyToScreenName();
    in_reply_to_status_id = status.getInReplyToStatusId();
    in_reply_to_user_id = status.getInReplyToUserId();
    source = status.getSource();
    location = new ParcelableLocation(status.getGeoLocation());
    is_favorite = status.isFavorited();
    text_unescaped = toPlainText(text_html);
    my_retweet_id = retweeted_by_id == account_id ? id : -1;
    is_possibly_sensitive = status.isPossiblySensitive();
    mentions = ParcelableUserMention.fromUserMentionEntities(status.getUserMentionEntities());
    first_media = media != null && media.length > 0 ? media[0].url : null;
}

From source file:de.vanita5.twittnuker.util.ContentValuesCreator.java

License:Open Source License

public static ContentValues makeStatusContentValues(final Status orig, final long accountId,
        final boolean largeProfileImage) {
    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());
    values.put(Statuses.MY_RETWEET_ID, orig.getCurrentUserRetweet());
    final boolean isRetweet = orig.isRetweet();
    final Status status;
    final Status retweetedStatus = isRetweet ? orig.getRetweetedStatus() : null;
    if (retweetedStatus != null) {
        final User retweetUser = orig.getUser();
        values.put(Statuses.RETWEET_ID, retweetedStatus.getId());
        values.put(Statuses.RETWEET_TIMESTAMP, retweetedStatus.getCreatedAt().getTime());
        values.put(Statuses.RETWEETED_BY_USER_ID, retweetUser.getId());
        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,
                ParseUtils.parseString(retweetUser.getProfileImageUrlHttps()));
        status = retweetedStatus;/*from  w w w. j a  v a 2 s.  co  m*/
    } else {
        status = orig;
    }
    final User user = status.getUser();
    if (user != null) {
        final long userId = user.getId();
        final String profileImageUrl = ParseUtils.parseString(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,
                largeProfileImage ? getBiggerTwitterProfileImage(profileImageUrl) : profileImageUrl);
        values.put(CachedUsers.IS_FOLLOWING, user.isFollowing());
    }
    final String text_html = Utils.formatStatusText(status);
    values.put(Statuses.TEXT_HTML, text_html);
    values.put(Statuses.TEXT_PLAIN, status.getText());
    values.put(Statuses.TEXT_UNESCAPED, toPlainText(text_html));
    values.put(Statuses.RETWEET_COUNT, status.getRetweetCount());
    values.put(Statuses.REPLY_COUNT, status.getReplyCount());
    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, Utils.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, location.getLatitude() + "," + location.getLongitude());
    }
    values.put(Statuses.IS_RETWEET, isRetweet);
    values.put(Statuses.IS_FAVORITE, status.isFavorited());
    final ParcelableMedia[] media = ParcelableMedia.fromEntities(status);
    if (media != null) {
        values.put(Statuses.MEDIA, JSONSerializer.toJSONArrayString(media));
        values.put(Statuses.FIRST_MEDIA, media[0].url);
    }
    final ParcelableUserMention[] mentions = ParcelableUserMention.fromStatus(status);
    if (mentions != null) {
        values.put(Statuses.MENTIONS, JSONSerializer.toJSONArrayString(mentions));
    }
    return values;
}