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:br.com.porcelli.hornetq.integration.twitter.support.TweetMessageConverterSupport.java

License:Apache License

public static ServerMessage buildMessage(final String queueName, final Status status) {
    final ServerMessage msg = new ServerMessageImpl(status.getId(),
            InternalTwitterConstants.INITIAL_MESSAGE_BUFFER_SIZE);
    msg.setAddress(new SimpleString(queueName));
    msg.setDurable(true);/*  w  w w . j ava  2  s.c  o  m*/

    msg.putStringProperty(TwitterConstants.KEY_MSG_TYPE, MessageType.TWEET.toString());
    msg.putStringProperty(TwitterConstants.KEY_CREATED_AT, read(status.getCreatedAt()));
    msg.putStringProperty(TwitterConstants.KEY_ID, read(status.getId()));

    msg.putStringProperty(TwitterConstants.KEY_TEXT, read(status.getText()));
    msg.putStringProperty(TwitterConstants.KEY_SOURCE, read(status.getSource()));
    msg.putStringProperty(TwitterConstants.KEY_TRUNCATED, read(status.isTruncated()));
    msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID, read(status.getInReplyToStatusId()));
    msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_USER_ID, read(status.getInReplyToUserId()));
    msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_SCREEN_NAME, read(status.getInReplyToScreenName()));

    msg.putStringProperty(TwitterConstants.KEY_RETWEET, read(status.isRetweet()));
    msg.putStringProperty(TwitterConstants.KEY_FAVORITED, read(status.isFavorited()));

    msg.putStringProperty(TwitterConstants.KEY_ENTITIES_URLS_JSON, read(status.getURLEntities()));
    msg.putStringProperty(TwitterConstants.KEY_ENTITIES_HASHTAGS_JSON, read(status.getHashtagEntities()));
    msg.putStringProperty(TwitterConstants.KEY_ENTITIES_MENTIONS_JSON, read(status.getUserMentionEntities()));

    msg.putStringProperty(TwitterConstants.KEY_CONTRIBUTORS_JSON, read(status.getContributors()));

    if (status.getUser() != null) {
        buildUserData("", status.getUser(), msg);
    }

    GeoLocation gl;
    if ((gl = status.getGeoLocation()) != null) {
        msg.putStringProperty(TwitterConstants.KEY_GEO_LATITUDE, read(gl.getLatitude()));
        msg.putStringProperty(TwitterConstants.KEY_GEO_LONGITUDE, read(gl.getLongitude()));
    }

    Place place;
    if ((place = status.getPlace()) != null) {
        msg.putStringProperty(TwitterConstants.KEY_PLACE_ID, read(place.getId()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_URL, read(place.getURL()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_NAME, read(place.getName()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_FULL_NAME, read(place.getFullName()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_COUNTRY_CODE, read(place.getCountryCode()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_COUNTRY, read(place.getCountry()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_STREET_ADDRESS, read(place.getStreetAddress()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_TYPE, read(place.getPlaceType()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_GEO_TYPE, read(place.getGeometryType()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_TYPE, read(place.getBoundingBoxType()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_COORDINATES_JSON,
                read(place.getBoundingBoxCoordinates().toString()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_GEOMETRY_COORDINATES_JSON,
                read(place.getGeometryCoordinates().toString()));
    }

    msg.putStringProperty(TwitterConstants.KEY_RAW_JSON, status.toString());

    return msg;
}

From source file:br.unisal.twitter.bean.TwitterBean.java

public void consultarTweets() {
    TwitterFactory TwitterFactory = new TwitterFactory();
    Twitter twitter = TwitterFactory.getSingleton();
    List<Status> tweets = new ArrayList<>();
    Query query = new Query(getTwittQuery());
    try {//from w  ww .  jav  a2 s  .c o  m
        QueryResult result = twitter.search(query);
        tweets.addAll(result.getTweets());
        StringBuilder builder = new StringBuilder();
        double lon = 0;
        double lat = 0;
        for (Status s : tweets) {
            if ((s.getGeoLocation() != null)) {
                lon = s.getGeoLocation().getLongitude();
                lat = s.getGeoLocation().getLatitude();
            }
            Tweet t = new Tweet(s.getUser().getName(), s.getUser().getLocation(), s.getText(), s.getCreatedAt(),
                    lat, lon);
            dao.insert(t);
            builder.append(t.toString());
        }
        this.getUiResultQuery().setSubmittedValue(builder.toString());
    } catch (TwitterException te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
}

From source file:cats.twitter.collect.Collect.java

@Override
public boolean runCollect() {

    dateEnd = Calendar.getInstance();
    dateEnd.add(Calendar.DAY_OF_YEAR, duree);
    cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);/*from w w w  .ja  va  2  s .co  m*/
    cb.setOAuthConsumerKey(user.getConsumerKey());
    System.out.println("CONSUMER KEY " + user.getConsumerKey());
    cb.setOAuthConsumerSecret(user.getConsumerSecret());
    System.out.printf("CONSUMER SECRET " + user.getConsumerSecret());
    cb.setOAuthAccessToken(user.getToken());
    System.out.printf("TOKEN" + user.getToken());
    cb.setOAuthAccessTokenSecret(user.getTokenSecret());
    System.out.printf("TOKEN SECRET " + user.getTokenSecret());

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    setStatus(State.WAITING_FOR_CONNECTION);

    StatusListener listener = new StatusListener() {

        @Override
        public void onStatus(Status status) {

            if (!corpus.getState().equals(State.INPROGRESS)) {
                setStatus(State.INPROGRESS);
            }
            if (status.getCreatedAt().after(dateEnd.getTime())) {
                shutdown();

            } else if (corpus.getLang() == null || corpus.getLang().equals(status.getLang())) {
                Tweet t = new Tweet();
                t.setText(status.getText().replace("\r", "\n"));
                t.setAuthor(status.getUser().getId());
                t.setId(status.getId());
                t.setDate(status.getCreatedAt());
                if (status.getGeoLocation() != null)
                    t.setLocation(status.getGeoLocation().toString());
                t.setName(status.getUser().getName());
                t.setDescriptionAuthor(status.getUser().getDescription());
                t.setLang(status.getLang());
                t.setCorpus(corpus);
                if (tweetRepository != null)
                    tweetRepository.save(t);
            }

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice sdn) {
            System.out.println(sdn);
        }

        @Override
        public void onTrackLimitationNotice(int i) {
            corpus.setLimitationNotice(i);
            corpus = corpusRepository.save(corpus);

        }

        @Override
        public void onScrubGeo(long l, long l1) {
            System.out.println(l + "" + l1);
        }

        @Override
        public void onStallWarning(StallWarning sw) {
            System.out.println(sw);
        }

        @Override
        public void onException(Exception excptn) {
            corpus.setErrorMessage(excptn.getMessage());
            setStatus(State.ERROR);
            excptn.printStackTrace();
        }
    };
    twitterStream.addListener(listener);
    twitterStream.filter(filter);

    return false;
}

From source file:cc.twittertools.corpus.demo.ReadStatuses.java

License:Apache License

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input directory or file")
            .create(INPUT_OPTION));/*from  w  w  w  .ja va2s.  com*/
    options.addOption(VERBOSE_OPTION, false, "print logging output every 10000 tweets");
    options.addOption(DUMP_OPTION, false, "dump statuses");

    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }

    if (!cmdline.hasOption(INPUT_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(ReadStatuses.class.getName(), options);
        System.exit(-1);
    }

    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    StatusStream stream;
    // Figure out if we're reading from HTML SequenceFiles or JSON.
    File file = new File(cmdline.getOptionValue(INPUT_OPTION));
    if (!file.exists()) {
        System.err.println("Error: " + file + " does not exist!");
        System.exit(-1);
    }

    if (file.isDirectory()) {
        stream = new JsonStatusCorpusReader(file);
    } else {
        stream = new JsonStatusBlockReader(file);
    }

    int cnt = 0;
    Status status;
    while ((status = stream.next()) != null) {
        if (cmdline.hasOption(DUMP_OPTION)) {
            String text = status.getText();
            if (text != null) {
                text = text.replaceAll("\\s+", " ");
                text = text.replaceAll("\0", "");
            }
            out.println(String.format("%d\t%s\t%s\t%s", status.getId(), status.getUser().getScreenName(),
                    status.getCreatedAt(), text));
        }
        cnt++;
        if (cnt % 10000 == 0 && cmdline.hasOption(VERBOSE_OPTION)) {
            LOG.info(cnt + " statuses read");
        }
    }
    stream.close();
    LOG.info(String.format("Total of %s statuses read.", cnt));
}

From source file:ch.schrimpf.core.TwitterCrawler.java

License:Open Source License

/**
 * Performs a single crawl step according to the previous initialized query
 * until the specified limit is reached. Received tweets are stored in the
 * *.csv specified in the easyTwitterCrawler.properties file.
 * <p/>/*from ww w  .j  a va  2 s. c om*/
 * TODO make selecting values flexible
 *
 * @param limit to stop on
 */
public void crwal(int limit) {
    LOG.info("receiving tweets...");
    int i = 0;
    while (i < limit && running) {
        try {
            QueryResult res = twitter.search(query);
            if (res.getMaxId() > last) {
                for (Status status : res.getTweets()) {
                    String[] line = { String.valueOf(status.getId()), String.valueOf(status.getCreatedAt()),
                            status.getText(), String.valueOf(status.getUser()),
                            String.valueOf(status.getPlace()), status.getLang() };
                    csv.writeResult(Arrays.asList(line));
                    i++;
                }
                last = res.getMaxId();
            } else {
                break;
            }
        } catch (TwitterException e) {
            LOG.warning("could not process tweets");
        }
    }
    tweets += i;
    LOG.info(i + " tweets received in this crawl");
    LOG.info("totally " + tweets + " received");
}

From source file:co.cask.cdap.template.etl.realtime.source.TwitterSource.java

License:Apache License

private StructuredRecord convertTweet(Status tweet) {
    StructuredRecord.Builder recordBuilder = StructuredRecord.builder(this.schema);
    recordBuilder.set(ID, tweet.getId());
    recordBuilder.set(MSG, tweet.getText());
    recordBuilder.set(LANG, tweet.getLang());
    Date tweetDate = tweet.getCreatedAt();
    if (tweetDate != null) {
        recordBuilder.set(TIME, tweetDate.getTime());
    }//from  w  ww  . j a  va 2  s  .  c o m
    recordBuilder.set(FAVC, tweet.getFavoriteCount());
    recordBuilder.set(RTC, tweet.getRetweetCount());
    recordBuilder.set(SRC, tweet.getSource());
    if (tweet.getGeoLocation() != null) {
        recordBuilder.set(GLAT, tweet.getGeoLocation().getLatitude());
        recordBuilder.set(GLNG, tweet.getGeoLocation().getLongitude());
    }
    recordBuilder.set(ISRT, tweet.isRetweet());
    return recordBuilder.build();
}

From source file:co.cask.hydrator.plugin.realtime.source.TwitterSource.java

License:Apache License

private StructuredRecord convertTweet(Status tweet) {
    StructuredRecord.Builder recordBuilder = StructuredRecord.builder(SCHEMA);
    recordBuilder.set(ID, tweet.getId());
    recordBuilder.set(MSG, tweet.getText());
    recordBuilder.set(LANG, tweet.getLang());
    Date tweetDate = tweet.getCreatedAt();
    if (tweetDate != null) {
        recordBuilder.set(TIME, tweetDate.getTime());
    }/*from  w ww.java  2  s  .co  m*/
    recordBuilder.set(FAVC, tweet.getFavoriteCount());
    recordBuilder.set(RTC, tweet.getRetweetCount());
    recordBuilder.set(SRC, tweet.getSource());
    if (tweet.getGeoLocation() != null) {
        recordBuilder.set(GLAT, tweet.getGeoLocation().getLatitude());
        recordBuilder.set(GLNG, tweet.getGeoLocation().getLongitude());
    }
    recordBuilder.set(ISRT, tweet.isRetweet());
    return recordBuilder.build();
}

From source file:Collector.TweetCollector.java

public static List<Status> getTweets(final String q) {
    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {

        @Override//from  ww  w  . j a  va 2  s .c  o m
        public void run() {

            long amountOfTweets = 0;

            try {

                long maxID = -1;

                Query query = new Query(q);
                //printTimeLine(query);
                Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search");
                RateLimitStatus searchLimit = rateLimitStatus.get("/search/tweets");
                for (int batchNumber = 0; MAX_QUERIES < 10; batchNumber++) {

                    System.out.printf("\n\n!!! batch %d\n\n", batchNumber);

                    if (searchLimit.getRemaining() == 0) {
                        // so as to not get blocked by twitter
                        Thread.sleep(searchLimit.getSecondsUntilReset() + 3 * 1001);
                    }

                    query.setCount(TWEETS_PER_QUERY);// constant value of 100
                    query.setResultType(Query.ResultType.recent);
                    query.setLang("en");// only english tweets

                    if (maxID != -1) {
                        query.setMaxId(maxID - 1);// so the first querys not set to previous max
                    }
                    QueryResult result = twitter.search(query);
                    if (result.getTweets().size() == 0) {
                        break;
                    }

                    for (Status s : result.getTweets()) {
                        amountOfTweets++;
                        if (maxID == -1 || s.getId() < maxID) {
                            maxID = s.getId();
                        }
                        storeTweet(s);// where stored in db

                        System.out.printf("At%s : %s\n", // debugging purposes
                                s.getCreatedAt().toString(), s.getText());
                        searchLimit = result.getRateLimitStatus(); //resets
                        System.out.printf("\n\nA total of %d tweet retrieved\n", amountOfTweets);

                    }

                }

            } catch (TwitterException te) {

                System.out.println("Error Code :" + te.getErrorCode());
                System.out.println("Exception Code " + te.getExceptionCode());
                System.out.println("Status Code " + te.getStatusCode());

                if (te.getStatusCode() == 401) {
                    System.out.println("Twitter Error :\nAuthentication "
                            + "credentials (https://dev.twitter.com/auth) "
                            + " are either missing of incorrect, " + "\nplease check consumer key /secret");
                }
            } catch (InterruptedException ex) {

            }

        }
    };

    // schedule the task to run starting now and then every hour...
    timer.schedule(hourlyTask, 0l, 1000 * 60 * 60);
    return statuses;

}

From source file:Collector.TweetCollector.java

public static void storeTweet(Status tweet) {

    BasicDBObject basicOBJ = new BasicDBObject();
    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());
    basicOBJ.put("created_at", tweet.getCreatedAt());

    try {//from   w  ww  .  ja v  a  2 s .  co m
        items.insert(basicOBJ);

    } catch (Exception e) {
        System.out.println("Could not store Tweet please try again" + e.getMessage());

    }

}

From source file:collector.TwitterCollector.java

public LinkedHashSet<Tweet> search(String queryExpression, int maxResults) {
    Query query = new Query(queryExpression);
    int numberOfTweets = maxResults;//512;
    long lastID = Long.MAX_VALUE;
    query = query.lang("pt");
    List<Status> tweets = new ArrayList<>();
    boolean finish = false;
    while ((tweets.size() < numberOfTweets) && !finish) {
        System.out.print(".");
        if (numberOfTweets - tweets.size() > 100) {//100) {
            query.setCount(100);//100);
        } else {//from  ww  w  .ja  v a2 s  . c o  m
            query.setCount(numberOfTweets - tweets.size());
        }
        try {
            QueryResult result = twitter.search(query);
            List<Status> resultList = result.getTweets();
            if (resultList == null || resultList.isEmpty()) {
                finish = true;
                System.out.println("no foram encontrados mais tweets");
            } else {
                tweets.addAll(resultList);
                for (Status t : tweets) {
                    if (t.getId() < lastID) {
                        lastID = t.getId();
                    }
                }
            }
        } catch (TwitterException ex) {
            System.err.println(ex.getMessage());
        }
        query.setMaxId(lastID - 1);
    }
    LinkedHashSet<Tweet> out = new LinkedHashSet<>();
    for (Status status : tweets) {
        if (!status.getText().startsWith("RT")) {
            TwitterUser user;
            user = new TwitterUser().addID(status.getUser().getId()).addName(status.getUser().getName())
                    .addLocation(status.getUser().getLocation()).addDateSignin(status.getUser().getCreatedAt())
                    .addCountTweets(status.getUser().getStatusesCount())
                    .addCountFavorites(status.getUser().getFavouritesCount())
                    .addCountFriends(status.getUser().getFriendsCount())
                    .addCountFollowers(status.getUser().getFollowersCount());
            Tweet tweet = new Tweet().addUser(user).addText(status.getText()).addID(status.getId())
                    .addDate(status.getCreatedAt())
                    .addLatitude(status.getGeoLocation() != null ? status.getGeoLocation().getLatitude()
                            : Double.MAX_VALUE)
                    .addLongitude(status.getGeoLocation() != null ? status.getGeoLocation().getLongitude()
                            : Double.MAX_VALUE);
            out.add(tweet);
        }
    }
    return out;
}