Example usage for twitter4j JSONObject getLong

List of usage examples for twitter4j JSONObject getLong

Introduction

In this page you can find the example usage for twitter4j JSONObject getLong.

Prototype

public long getLong(String name) throws JSONException 

Source Link

Document

Returns the value mapped by name if it exists and is a long or can be coerced to a long, or throws otherwise.

Usage

From source file:twitterapp.TweetsProcessing.java

public static void createSeparateEntities() throws JSONException {

    String[] collectionsTweets = { "myTweetCol", "myTweetCol2", "myTweetCol3", "myTweetCol4" };
    String[] colEntities = { "separateEntities", "separateEntities2", "separateEntities3",
            "separateEntities4" };
    for (int col = 0; col < collectionsTweets.length; col++) {
        MongoClient mongo = new MongoClient("localhost", 27017);
        MongoDatabase database = mongo.getDatabase("myTweetdb");
        MongoCollection<Document> collection = database.getCollection(collectionsTweets[col]);
        Iterator<Document> kati = collection.find().iterator();

        while (kati.hasNext()) {

            Document doc = kati.next();

            String user, url, hashtag, mentioned, id, timestamp;
            user = url = hashtag = mentioned = id = timestamp = "";

            JSONObject a = new JSONObject(doc);
            String temp = a.getString("user");
            String tokens[] = temp.split(",");
            for (int j = 0; j < tokens.length; j++) {
                if (tokens[j].contains("screen_name")) {

                    temp = tokens[j].replace("\"screen_name\":", "");
                    user = temp.replace("\"", "");

                }// w w  w. j  a  v  a  2 s.com

            }
            timestamp = String.valueOf(a.getLong("timestamp_ms"));
            JSONObject b = a.getJSONObject("entities");
            tokens = b.toString().split(",");
            for (int j = 0; j < tokens.length; j++) {
                if (tokens[j].contains("text")) {
                    String temp2 = tokens[j].replace("\"", "");
                    temp2 = temp2.replace(":", "");
                    temp2 = temp2.replace("}", "");
                    temp2 = temp2.replace("]", "");
                    temp2 = temp2.replace("text", "");
                    hashtag = hashtag.concat(temp2 + " ").trim();

                }
                if (tokens[j].contains("expanded_url")) {
                    String temp2 = tokens[j].replace("\":\"", "");
                    temp2 = temp2.replace("\"", "");
                    temp2 = temp2.replace("expanded_url", "");
                    url = url.concat(temp2 + " ");
                }
                if (tokens[j].contains("screen_name")) {
                    String temp2 = tokens[j].replace(":", "");
                    temp2 = temp2.replace("\"", "");
                    temp2 = temp2.replace("screen_name", "");
                    mentioned = mentioned.concat(temp2 + " ");
                }

            }

            if (a.toString().contains("retweeted_status")) {
                b = (JSONObject) a.getJSONObject("retweeted_status");
                id = b.getString("id_str");

            }

            Document object = new Document("user", user).append("timestamp", timestamp).append("hashtag",
                    hashtag);
            Document object1 = new Document("user", user).append("timestamp", timestamp).append("url", url);
            Document object2 = new Document("user", user).append("timestamp", timestamp)
                    .append("mentioned_users", mentioned);
            Document object3 = new Document("user", user).append("timestamp", timestamp)
                    .append("retweeted_tweet", id);

            MongoCollection<Document> collection2 = database.getCollection(colEntities[col]);

            collection2.insertOne(object);
            collection2.insertOne(object1);
            collection2.insertOne(object2);
            collection2.insertOne(object3);

        }
    }

}