List of usage examples for twitter4j JSONArray get
public Object get(int index) throws JSONException
From source file:com.adobe.ibm.watson.traits.impl.TwitterServiceClient.java
License:Apache License
/** * Fetches all of the Tweets for a given screen name. * @param screenName//from ww w . ja va 2s. c om * @param maxTweets * @return ArrayList of the user's timeline tweets. * @throws IOException */ public ArrayList<String> fetchTimelineTweets(String screenName, int maxTweets, Resource pageResource) throws IOException { HttpsURLConnection connection = null; String bearerToken = requestBearerToken("https://api.twitter.com/oauth2/token", pageResource); String endPointUrl = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" + screenName + "&count=" + maxTweets; try { URL url = new URL(endPointUrl); connection = (HttpsURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setRequestProperty("Host", "api.twitter.com"); connection.setRequestProperty("User-Agent", "Your Program Name"); connection.setRequestProperty("Authorization", "Bearer " + bearerToken); connection.setUseCaches(false); // Parse the JSON response into a JSON mapped object to fetch fields from. JSONArray obj = new JSONArray(readResponse(connection)); ArrayList<String> tweets = new ArrayList<String>(); if (obj != null) { for (int j = 0; j < obj.length(); j++) { if (obj.getJSONObject(j).has("retweeted_status") == false) { tweets.add(obj.getJSONObject(j).get("text").toString()); } } return tweets; } else { return null; } } catch (MalformedURLException e) { throw new IOException("Invalid endpoint URL specified.", e); } catch (JSONException e) { throw new IOException("Unable to process JSON content.", e); } finally { if (connection != null) { connection.disconnect(); } } }
From source file:com.isi.master.meaningcloudAPI.topicsextraction.TopicsClient.java
License:Open Source License
private static List<String> entidadTopics(JSONObject jsonObj) { List<String> provincia = new ArrayList<String>(); try {/*from w w w. j a va 2s . c o m*/ JSONArray array = jsonObj.getJSONArray("entity_list"); for (int i = 0; i < array.length(); i++) { try { JSONObject doc = (JSONObject) array.getJSONObject(i); JSONObject doc1 = (JSONObject) doc.get("sementity"); if (doc1.getString("id").equals("ODENTITY_CITY") && doc1.getString("type").equals("Top>Location>GeoPoliticalEntity>City")) { JSONArray doc2 = (JSONArray) doc.get("semgeo_list"); JSONObject doc21 = (JSONObject) doc2.get(0); if (((JSONObject) doc21.get("country")).getString("form").equals("Espaa")) { try { provincia.add(((JSONObject) doc21.get("adm2")).getString("form")); } catch (JSONException e) { provincia.add(((JSONObject) doc21.get("adm1")).getString("form")); } } else { // System.err.println(((JSONObject)array.get(i)).get("form")+" en el texto se refiere a un lugar de "+((JSONObject)doc21.get("country")).getString("form")); } } else if (doc1.getString("id").equals("ODENTITY_ADM2") && doc1.getString("type").equals("Top>Location>GeoPoliticalEntity>Adm2")) { JSONArray doc2 = (JSONArray) doc.get("semgeo_list"); JSONObject doc21 = (JSONObject) doc2.get(0); if (((JSONObject) doc21.get("country")).getString("form").equals("Espaa")) {//insertamos la entidad provincia.add(doc.getString("form")); } else { // System.err.println(((JSONObject)array.get(i)).get("form")+" en el texto se refiere a un lugar de "+((JSONObject)doc21.get("country")).getString("form")); } } else { // System.err.println(((JSONObject)array.get(i)).get("form")+" no es una ciudad\n"); } } catch (JSONException e) { // System.err.println(((JSONObject)array.get(i)).get("form")+" no es una ciudad\n"); } } } catch (JSONException e1) { // TODO Auto-generated catch block // e1.printStackTrace(); } return provincia; }
From source file:com.isi.master.meaningcloudAPI.topicsextraction.TopicsClient.java
License:Open Source License
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException { // We define the variables needed to call the API String api = "http://api.meaningcloud.com/topics-2.0"; String key = "67d2d31e37c2ba1d032188b1233f19bf"; String txt = "Los vehculos AUDI con tres ocupantes podrn circular por Real Madrid, Alcobendas y Miranda de Ebro los das de contaminacin porque Carmena no les deja debido al aire sucio"; // String txt = "Madrid est con altos niveles de contaminacin, como el NO2"; // String txt = "La ciudad Madrid est con altos niveles de contaminacin, como el NO2"; // String txt = "Avils"; String lang = "es"; // es/en/fr/it/pt/ca Post post = new Post(api); post.addParameter("key", key); post.addParameter("txt", txt); post.addParameter("lang", lang); post.addParameter("tt", "ec"); post.addParameter("uw", "y"); post.addParameter("cont", "City"); post.addParameter("of", "json"); // String response = post.getResponse(); JSONObject jsonObj = null;/* w ww .j ava 2 s .com*/ try { jsonObj = new JSONObject(post.getResponse()); System.out.println("AAAAAAAAAAAAAAAAAAAAAAAA"); System.out.println(jsonObj.toString()); JSONArray array2 = jsonObj.getJSONArray("concept_list"); System.out.println(array2); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Show response System.out.println("Response"); System.out.println("============"); try { System.out.println(jsonObj); JSONArray array = jsonObj.getJSONArray("entity_list"); System.out.println(array); for (int i = 0; i < array.length(); i++) { try { // System.out.println("_--------------------_"); JSONObject doc = (JSONObject) array.getJSONObject(i); System.out.println(doc); // System.out.println("_---------------------_"); JSONObject doc1 = (JSONObject) doc.get("sementity"); System.out.println(doc1); // System.out.println("_A---------------------_"); if (doc1.getString("id").equals("ODENTITY_CITY") && doc1.getString("type").equals("Top>Location>GeoPoliticalEntity>City")) { JSONArray doc2 = (JSONArray) doc.get("semgeo_list"); JSONObject doc21 = (JSONObject) doc2.get(0); if (((JSONObject) doc21.get("country")).getString("form").equals("Espaa")) { // System.out.println("Entidad_: "+((JSONObject)array.get(i)).get("form")); // System.out.println("IDENTIFICADORES DE ENTIDAD CIUDAD_: "+doc1.getString("id")+" - "+doc1.getString("type")); // System.out.println("PAIS_: "+((JSONObject)doc21.get("country")).get("form")); try { System.out.println( "PROVINCIA_: " + ((JSONObject) doc21.get("adm2")).get("form") + "\n"); } catch (JSONException e) { System.out.println( "PROVINCIA_: " + ((JSONObject) doc21.get("adm1")).get("form") + "\n"); } } else { System.err.println(((JSONObject) array.get(i)).get("form") + " en el texto se refiere a un lugar de " + ((JSONObject) doc21.get("country")).getString("form")); } } else if (doc1.getString("id").equals("ODENTITY_ADM2") && doc1.getString("type").equals("Top>Location>GeoPoliticalEntity>Adm1")) { System.out.println(doc.get("form")); JSONArray doc2 = (JSONArray) doc.get("semgeo_list"); JSONObject doc21 = (JSONObject) doc2.get(0); if (((JSONObject) doc21.get("country")).getString("form").equals("Espaa")) { System.out.println("PAIS_: " + ((JSONObject) doc21.get("country")).get("form")); } } else { System.err.println(((JSONObject) array.get(i)).get("form") + " no es una ciudad\n"); } } catch (JSONException e) { System.err.println(((JSONObject) array.get(i)).get("form") + " no es una ciudad\n"); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Prints the specific fields in the response (topics) // DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); // DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); // Document doc = docBuilder.parse(new ByteArrayInputStream(response.getBytes("UTF-8"))); // doc.getDocumentElement().normalize(); // Element response_node = doc.getDocumentElement(); // System.out.println("\nInformation:"); // System.out.println("----------------\n"); // try { // NodeList status_list = response_node.getElementsByTagName("status"); // Node status = status_list.item(0); // NamedNodeMap attributes = status.getAttributes(); // Node code = attributes.item(0); // if(!code.getTextContent().equals("0")) { // System.out.println("Not found"); // } else { // String output = ""; // output += "Entities:\n"; // output += "=============\n"; // output += printInfoEntityConcept(response_node, "entity"); //// output += "\n"; //// output += "Concepts:\n"; //// output += "============\n"; //// output += printInfoEntityConcept(response_node, "concept"); //// output += "\n"; //// output += "Time expressions:\n"; //// output += "==========\n"; //// output += printInfoGeneral(response_node, "time_expression"); //// output += "\n"; //// output += "Money expressions:\n"; //// output += "===========\n"; //// output += printInfoGeneral(response_node, "money_expression"); //// output += "\n"; //// output += "Quantity expressions:\n"; //// output += "======================\n"; //// output += printInfoGeneral(response_node, "quantity_expression"); //// output += "\n"; //// output += "Other expressions:\n"; //// output += "====================\n"; //// output += printInfoGeneral(response_node, "other_expression"); //// output += "\n"; //// output += "Quotations:\n"; //// output += "====================\n"; //// output += printInfoQuotes(response_node); //// output += "\n"; //// output += "Relations:\n"; //// output += "====================\n"; //// output += printInfoRelation(response_node); // output += "\n"; // if(output.isEmpty()) // System.out.println("Not found"); // else // System.out.print(output); // } // } catch (Exception e) { // System.out.println("Not found"); // } }