Example usage for twitter4j TwitterObjectFactory getRawJSON

List of usage examples for twitter4j TwitterObjectFactory getRawJSON

Introduction

In this page you can find the example usage for twitter4j TwitterObjectFactory getRawJSON.

Prototype

public static String getRawJSON(Object obj) 

Source Link

Document

Returns a raw JSON form of the provided object.
Note that raw JSON forms can be retrieved only from the same thread invoked the last method call and will become inaccessible once another method call

Usage

From source file:uniandes.edu.twitterreader.TwitterApp.java

/**
 * Reads all twetts accoding to TWITTER_QUERIES with JSON format and inserts
 * in MongoDB collection//from w  ww. j  a  v  a  2s  .  co m
 *
 * @param writesFile writes an additional file in TWEETS_PATH if true
 */
public void readColombiaTopics(boolean writesFile) {

    PrintWriter writer = null;

    try {
        if (writesFile) {
            writer = new PrintWriter(TWEETS_PATH, "UTF-8");
        }

        for (String queryString : TWITTER_QUERIES) {
            Query query = new Query(queryString);
            query.setSince(QUERY_SINCE_DATE);
            query.setUntil(QUERY_LAST_DATE);
            QueryResult result;
            do {
                result = twitter.search(query);
                List<Status> tweets = result.getTweets();
                for (Status tweet : tweets) {
                    String jsonTweet = TwitterObjectFactory.getRawJSON(tweet);
                    if (writesFile && writer != null) {
                        writer.println(jsonTweet);
                    }
                    mongoCollection.insertOne(Document.parse(jsonTweet));
                }
            } while ((query = result.nextQuery()) != null);
        }

    } catch (FileNotFoundException | UnsupportedEncodingException | TwitterException ex) {
        Logger.getLogger(TwitterApp.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (writesFile && writer != null) {
            writer.close();
        }
    }

}