Example usage for twitter4j JSONObject getString

List of usage examples for twitter4j JSONObject getString

Introduction

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

Prototype

public String getString(String name) throws JSONException 

Source Link

Document

Returns the value mapped by name if it exists, coercing it if necessary, or throws if no such mapping exists.

Usage

From source file:edu.smc.mediacommons.panels.WeatherPanel.java

License:Open Source License

public WeatherPanel() {
    setLayout(null);/*from w  w w .ja v a 2 s  . c o  m*/

    add(Utils.createLabel("Enter a ZIP Code or City, then Search", 35, 20, 300, 20, Resources.VERDANA_12_BOLD));

    final JTextField searchBox = Utils.createTextField(35, 40, 200, 20);
    add(searchBox);

    add(Utils.createLabel("Region", 30, 80, 200, 20, Resources.VERDANA_12_BOLD));
    add(Utils.createLabel("Country", 30, 105, 200, 20, Resources.VERDANA_12_BOLD));
    add(Utils.createLabel("City", 30, 130, 200, 20, Resources.VERDANA_12_BOLD));
    add(Utils.createLabel("Sunset", 30, 155, 200, 20, Resources.VERDANA_12_BOLD));
    add(Utils.createLabel("Sunrise", 30, 180, 200, 20, Resources.VERDANA_12_BOLD));
    add(Utils.createLabel("Forecast", 30, 205, 200, 20, Resources.VERDANA_12_BOLD));
    add(Utils.createLabel("High", 30, 230, 200, 20, Resources.VERDANA_12_BOLD));
    add(Utils.createLabel("Low", 30, 255, 200, 20, Resources.VERDANA_12_BOLD));

    final JTextField regionBox = Utils.createTextField(100, 80, 100, 20);
    add(regionBox);

    final JTextField countryBox = Utils.createTextField(100, 105, 100, 20);
    add(countryBox);

    final JTextField cityBox = Utils.createTextField(100, 130, 100, 20);
    add(cityBox);

    final JTextField sunsetBox = Utils.createTextField(100, 155, 100, 20);
    add(sunsetBox);

    final JTextField sunriseBox = Utils.createTextField(100, 180, 100, 20);
    add(sunriseBox);

    final JTextField forecastBox = Utils.createTextField(100, 205, 100, 20);
    add(forecastBox);

    final JTextField highBox = Utils.createTextField(100, 230, 100, 20);
    add(highBox);

    final JTextField lowBox = Utils.createTextField(100, 255, 100, 20);
    add(lowBox);

    cartoonRepresentation = new ImagePanel(Resources.IMAGE_SUN, 100, 100);
    cartoonRepresentation.setBounds(250, 70, 200, 200);
    add(cartoonRepresentation);

    JButton searchButton = new JButton("Search");
    searchButton.setBounds(235, 40, 150, 20);
    add(searchButton);

    searchButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (searchBox.getText().isEmpty()) {
                JOptionPane.showMessageDialog(getParent(), "Warning! Your input was blank.");
            } else {
                new Thread() {
                    @Override
                    public void run() {
                        JSONObject result = new WeatherModule(searchBox.getText()).getResponse();

                        try {
                            JSONObject query = new JSONObject(result.getString("query"));
                            JSONObject results = new JSONObject(query.getString("results"));
                            JSONObject channel = new JSONObject(results.getString("channel"));
                            JSONObject location = new JSONObject(channel.getString("location"));
                            JSONObject astronomy = new JSONObject(channel.getString("astronomy"));
                            JSONObject item = new JSONObject(channel.getString("item"));
                            JSONArray array = new JSONArray(item.getString("forecast"));
                            JSONObject forecast = array.getJSONObject(0);

                            regionBox.setText(location.getString("region"));
                            countryBox.setText(location.getString("country"));
                            cityBox.setText(location.getString("city"));
                            sunsetBox.setText(astronomy.getString("sunset"));
                            sunriseBox.setText(astronomy.getString("sunrise"));
                            forecastBox.setText(forecast.getString("text"));
                            highBox.setText(forecast.getString("high"));
                            lowBox.setText(forecast.getString("low"));

                            String partial = forecast.getString("text");

                            if (partial.contains("Cloud")) {
                                cartoonRepresentation.setScaled(Resources.IMAGE_CLOUDY);
                            } else if (partial.contains("Rain")) {
                                cartoonRepresentation.setScaled(Resources.IMAGE_RAINY);
                            }

                            cartoonRepresentation.repaint();
                        } catch (JSONException ex) {
                            ex.printStackTrace();
                        }
                    }
                }.start();
            }
        }
    });
}

From source file:EjemploObservador.BuscadorDeTweets.java

License:Apache License

@Override
public void run() {
    IniciaVariablesDeConeccion();//from   w  w w  .j  a  v a2s .co m
    client.connect();
    while (true) {
        try {
            /// Toma el JSON devuelto por el API de tweeter 
            /// Y extrae los datos en el TweetText

            String msg = queue.take();
            JSONObject Tweet = new JSONObject(msg);
            String Text = Tweet.getString("text");
            String User = Tweet.getJSONObject("user").getString("name");
            String location = Tweet.getJSONObject("user").getString("location");
            String TweetText = ">>>>>>>> " + location + "\n" + User + ": " + Text + "\n";

            /// Envia el TweetText al la Lista
            ListaDeTweets.agregaTweet(TweetText);

        } catch (InterruptedException ex) {
            Logger.getLogger(BuscadorDeTweets.class.getName()).log(Level.SEVERE, null, ex);

        } catch (JSONException ex) {
            Logger.getLogger(BuscadorDeTweets.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

From source file:tweetcrawling.TweetCrawler.java

private String getUserGender(String user) {
    String gender = "unknown";
    String apiUrl = "http://api.namsor.com/onomastics/api/json/gendre/";
    String namesPath = "";

    if (user.contains(" ")) {
        String[] firstTwoParts = user.split(" ");

        if (firstTwoParts.length == 1) {
            String name = firstTwoParts[0].replaceAll("[^A-Za-z0-9]", "");

            if (!name.isEmpty()) {
                namesPath = name + "/" + name;
            }/*from w  w w. java  2  s.  com*/
        } else {
            String name1 = firstTwoParts[0].replaceAll("[^A-Za-z0-9]", "");
            String name2 = firstTwoParts[1].replaceAll("[^A-Za-z0-9]", "");

            if (name1 != null && name2 != null && !name1.isEmpty() && !name2.isEmpty()) {
                namesPath = name1 + "/" + name2;
            } else if (name1 != null && !name1.isEmpty() && (name2 == null || name2.isEmpty())) {
                namesPath = name1 + "/" + name1;
            } else if ((name1 == null || name1.isEmpty()) && !name2.isEmpty() && name2 != null) {
                namesPath = name2 + "/" + name2;
            }
        }
    } else {
        String name = user.replaceAll("[^A-Za-z0-9]", "");
        namesPath = name + "/" + name;
    }

    namesPath = namesPath.trim();

    if (!namesPath.isEmpty() && namesPath != "/") {
        try {
            String data = getData(apiUrl + namesPath);
            JSONObject obj = new JSONObject(data);
            gender = obj.getString("gender");
        } catch (Exception e) {
            return "unknown";
        }
    }

    return gender;
}

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  va  2 s . c o 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);

        }
    }

}

From source file:twitterapp.TweetsSimilarity.java

/**
 * @param args the command line arguments
 *//*from  ww  w. j  a v  a 2  s  .  c  om*/
public static void main(String[] args) throws JSONException, Exception {
    System.out.println(intersection("unpresidented", "unpresidented", "unpresidented"));
    System.out.println(union("unpresidented", "unpresidented", "unpresidented"));
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("myTweetdb");

    Document temp;
    JSONObject b, c;
    Double sim[] = new Double[100];
    // String one = a.getString("hashtag");
    String two = "";
    int j = 0;
    int k = 0;
    int y = 0;
    String[] collections = { "hashtagAll", "mentionedAll", "urlAll", "retweetedAll" };
    String[] entities = { "hashtag", "mentioned_users", "url", "retweeted_tweet" };
    Double[][] hashtag = new Double[1000][1000];
    for (j = 0; j < hashtag.length; j++) {
        Arrays.fill(hashtag[j], 0.0);
    }
    j = 0;
    for (int p = 0; p < collections.length; p++) {

        String file = collections[p].concat(".csv");
        System.out.println(file);
        PrintWriter writer = new PrintWriter(file);
        writer.println("Source,Target,Weight,Type");
        MongoCursor<Document> manasou;
        MongoCollection collection2 = database.getCollection(collections[p]);
        manasou = collection2.find().iterator();
        HashMap<String, Integer> count = new HashMap<>();
        String common = "";

        while (manasou.hasNext() && y < 1000) {
            b = new JSONObject(manasou.next());
            String temp1 = b.getString(entities[p]);
            String tokens[] = temp1.split(" ");
            for (int i = 0; i < tokens.length; i++) {
                if (count.containsKey(tokens[i])) {
                    Integer temp2 = count.get(tokens[i]);
                    temp2++;
                    count.replace(tokens[i], temp2);
                } else {
                    count.put(tokens[i], 1);
                }
            }
            y++;
        }
        Iterator<Entry<String, Integer>> count_it = count.entrySet().iterator();
        while (count_it.hasNext()) {
            Entry entry = count_it.next();
            if ((Integer) entry.getValue() > 500) {
                common = common.concat(entry.getKey() + " ");
            }
        }
        System.out.println(common);
        manasou = collection2.find().iterator();
        j = 0;
        while (manasou.hasNext() && j < 1000) {
            b = new JSONObject(manasou.next());
            System.out.println(j);
            MongoCursor<Document> kati2 = collection2.find().iterator();
            k = 0;
            while (kati2.hasNext() && k < 1000) {
                c = new JSONObject(kati2.next());
                if (j < k) {
                    String temp1 = b.getString(entities[p]);
                    String temp2 = c.getString(entities[p]);
                    double temp3 = intersection(temp1, temp2, common) / union(temp1, temp2, common);
                    if (Double.isNaN(temp3)) {
                        temp3 = 1;
                    }
                    String lel = "tweet" + j + "," + "tweet" + k + "," + temp3 + ",Undirected";

                    hashtag[j][k] = hashtag[j][k] + temp3;

                    writer.flush();
                    if (temp3 > 0.2) {
                        writer.println(lel);
                    }

                } else {
                    k++;
                    continue;
                }
                k++;
            }

            j++;

        }

    }
    PrintWriter writer = new PrintWriter("all.csv");
    writer.println("Source,Target,Weight,Type");
    for (j = 0; j < hashtag.length; j++) {
        for (k = 0; k < hashtag.length; k++) {
            if (j < k) {
                double temp1 = hashtag[j][k] / 4.0;
                if (temp1 > 0.2) {
                    String lel = "tweet" + j + "," + "tweet" + k + "," + temp1 + ",Undirected";
                    writer.flush();
                    writer.println(lel);
                }
            }
        }
    }

}