Example usage for twitter4j JSONObject has

List of usage examples for twitter4j JSONObject has

Introduction

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

Prototype

public boolean has(String name) 

Source Link

Document

Returns true if this object has a mapping for name .

Usage

From source file:com.SentimentalResponse.java

public static String readJsonFromUrl(String text) throws IOException, JSONException {
    // System.out.println(text);
    // text=text.replaceAll(" ","%20");
    text = text.replaceAll("\n", " ");
    String[] str;/*from   www.j a  v  a 2 s  . c o  m*/
    str = text.split("http");
    text = str[0];
    text = text.replaceAll(" ", "%20").replaceAll(":", "%3A").replaceAll("/", "%2F").replaceAll(";", "%3B")
            .replaceAll("@", "%40").replaceAll("<", "%3C").replaceAll(">", "%3E").replaceAll("=", "%3D")
            .replaceAll("&", "%26").replaceAll("%", "%25").replaceAll("$", "%24").replaceAll("#", "%23")
            .replaceAll(",", "%2C");
    //.replaceAll("++","%2B").replaceAll("?","%3F");
    //System.out.println(text);
    url = sentimentalAnalysistUrl + "apikey=" + apiKey + "&text=" + text + "&outputMode=" + outputMode;
    System.out.println(url);
    InputStream is = new URL(url).openStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        if (json == null) {
            return "No tweets detection";
        } else {
            if (!json.has("docSentiment")) {
                return "language not detection";
            } else {
                JSONObject json2 = (JSONObject) json.get("docSentiment");
                return json2.get("type").toString();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.toString());
        return "error occur, retrying";
    } finally {
        is.close();
    }
}

From source file:com.tweettrends.pravar.FilterStreamExample.java

License:Apache License

public static void run(String consumerKey, String consumerSecret, String token, String secret,
        SimpleQueueService simpleQueueService) throws InterruptedException {
    BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
    StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
    // add some track terms
    endpoint.trackTerms(Lists.newArrayList("modi", "India", "Trump", "New York", "English", "London",
            "Tuesday Motivation", "Celtics", "GA06"));
    endpoint.languages(Lists.newArrayList("en"));

    Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
    // Authentication auth = new BasicAuth(username, password);

    // Create a new BasicClient. By default gzip is enabled.
    Client client = new ClientBuilder().hosts(Constants.STREAM_HOST).endpoint(endpoint).authentication(auth)
            .processor(new StringDelimitedProcessor(queue)).build();

    // Establish a connection
    client.connect();//from w w  w . ja  v a2  s  .  c o m

    while (true) {
        // Do whatever needs to be done with messages
        for (int msgRead = 0; msgRead < 1000; msgRead++) {
            String msg = queue.take();

            try {
                JSONObject tweet = new JSONObject(msg);
                if (tweet.has("coordinates")) {
                    String geoInfo = tweet.get("coordinates").toString();
                    if (!geoInfo.equals("null")) {
                        simpleQueueService.sendMessage(msg);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        //client.stop();

    }
}