Example usage for twitter4j JSONObject toString

List of usage examples for twitter4j JSONObject toString

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Encodes this object as a compact JSON string, such as:
{"query":"Pizza","locations":[94043,90210]}

Usage

From source file:com.marpies.ane.twitter.utils.StatusUtils.java

License:Apache License

public static void dispatchStatus(Status status, int callbackID) {
    try {//w  w w  .j  a  v  a  2  s .c om
        JSONObject statusJSON = StatusUtils.getJSON(status);
        statusJSON.put("listenerID", callbackID);
        statusJSON.put("success", "true");
        AIR.dispatchEvent(AIRTwitterEvent.STATUS_QUERY_SUCCESS, statusJSON.toString());
    } catch (JSONException e) {
        e.printStackTrace();
        AIR.dispatchEvent(AIRTwitterEvent.STATUS_QUERY_SUCCESS,
                StringUtils.getEventErrorJSON(callbackID, e.getMessage()));
    }
}

From source file:com.marpies.ane.twitter.utils.UserUtils.java

License:Apache License

/**
 * Creates JSON from given response list and dispatches generic event.
 * Helper method for queries like getFollowers and getFriends.
 * @param users// w ww.  ja  v  a  2s . co m
 * @param callbackID
 */
public static void dispatchUsers(PagableResponseList<User> users, final int callbackID) {
    try {
        AIR.log("Got response with " + users.size() + " users");
        /* Create array of JSON users */
        JSONArray usersJSON = new JSONArray();
        for (User user : users) {
            /* Create JSON for each user and put it to the array */
            JSONObject userJSON = getJSON(user);
            usersJSON.put(userJSON.toString());
        }
        JSONObject result = new JSONObject();
        result.put("users", usersJSON);
        if (users.hasNext()) {
            result.put("nextCursor", users.getNextCursor());
        }
        if (users.hasPrevious()) {
            result.put("previousCursor", users.getPreviousCursor());
        }
        result.put("listenerID", callbackID);
        AIR.dispatchEvent(AIRTwitterEvent.USERS_QUERY_SUCCESS, result.toString());
    } catch (JSONException e) {
        AIR.dispatchEvent(AIRTwitterEvent.USERS_QUERY_ERROR,
                StringUtils.getEventErrorJSON(callbackID, "Error creating result JSON: " + e.getMessage()));
    }
}

From source file:StringMatching.GetTweet.java

/**
 * @param args the command line arguments
 *///  w ww  . ja  va2s . c  o m
public static void main(String[] args) throws JSONException, IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Gkr9iZwYDALZ16OdxMp5rubBH")
            .setOAuthConsumerSecret("nhEwYFfiX5qp90sLLwO2eeYMxLwb3WC120lgihrocZDPWRNcUK")
            .setOAuthAccessToken("94107100-572UpcOkkz9kMWGaJS8YFsIGdlmJAd2cDw8y9rOnA")
            .setOAuthAccessTokenSecret("ST0XtXUjYgYWKHryL2feNM0VcDQQAgrov2V7nB7hq1xBC")
            .setHttpProxyHost("cache.itb.ac.id").setHttpProxyPort(8080).setHttpProxyUser("jonathan.benedict")
            .setHttpProxyPassword("rollingonthefloor");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    JSONObject obj = new JSONObject();
    int counterTweet = 0;
    FileWriter file = new FileWriter("C:\\Users\\user\\IdeaProjects\\TwitterStringMatching\\input.txt");
    file.flush();
    try {
        Query query = new Query("barca".toLowerCase());
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                counterTweet++;
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                obj.put("user", tweet.getUser().getScreenName());
                obj.put("tweets", tweet.getText());

                //Tulis file ke dalam txt

                try {
                    file.write(obj.toString());
                    System.out.println("Successfully Copied JSON Object to File...");
                    System.out.println("\nJSON Object: " + obj);

                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        } while (counterTweet < 1000);

        file.close();
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    System.exit(0);
}

From source file:tiofortwitter.TioForTwitter.java

/**
 * @param args the command line arguments
 *//*from  w  ww  .j  a  v  a 2 s .c  o  m*/
public static void main(String[] args) throws JSONException, IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Gkr9iZwYDALZ16OdxMp5rubBH")
            .setOAuthConsumerSecret("nhEwYFfiX5qp90sLLwO2eeYMxLwb3WC120lgihrocZDPWRNcUK")
            .setOAuthAccessToken("94107100-572UpcOkkz9kMWGaJS8YFsIGdlmJAd2cDw8y9rOnA")
            .setOAuthAccessTokenSecret("ST0XtXUjYgYWKHryL2feNM0VcDQQAgrov2V7nB7hq1xBC");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    JSONObject obj = new JSONObject();
    int counterTweet = 0;
    FileWriter file = new FileWriter("Users\\user\\IdeaProjects\\TwitterStringMatching\\input.txt");
    file.flush();
    try {
        Query query = new Query("Satria");
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                counterTweet++;
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                obj.put("user", tweet.getUser().getScreenName());
                obj.put("tweets", tweet.getText());

                //Tulis file ke dalam txt

                try {
                    file.write(obj.toString());
                    System.out.println("Successfully Copied JSON Object to File...");
                    System.out.println("\nJSON Object: " + obj);

                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        } while (counterTweet < 1000);

        file.close();
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    System.exit(0);
}

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("\"", "");

                }//from  w w  w. j  ava2  s. co m

            }
            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);

        }
    }

}