List of usage examples for twitter4j JSONArray getJSONObject
public JSONObject getJSONObject(int index) throws JSONException
From source file:ac.simons.tweetarchive.web.ArchiveHandlingController.java
License:Apache License
/** * As you can see, it get's nasty here... * <br>// w w w .ja v a 2 s.c o m * Twitter4j doesn't offer an official way to parse Twitters JSON, so I * brute force my way into the twitter4j.StatusJSONImpl implementation of * Status. * <br> * And even if there was an official way, the JSON files inside the * official(!) Twitter archive differ from the API, even if they are said to * be identical. By the way, I'm not the only one, who * <a href="https://twittercommunity.com/t/why-does-twitter-json-archive-have-a-different-format-than-the-rest-api-1-1/35530">noticed * that</a>. * <br> * Furthermore, I didn't even bother to add error handling or tests. * * @param archive The uploaded archive * @return Redirect to the index * @throws java.io.IOException * @throws twitter4j.JSONException */ @PostMapping public String store(@NotNull final MultipartFile archive, final RedirectAttributes redirectAttributes) throws IOException, JSONException { try (final ZipInputStream archiv = new ZipInputStream(archive.getInputStream())) { ZipEntry entry; while ((entry = archiv.getNextEntry()) != null) { if (!entry.getName().startsWith("data/js/tweets/") || entry.isDirectory()) { continue; } log.debug("Reading archive entry {}...", entry.getName()); final BufferedReader buffer = new BufferedReader( new InputStreamReader(archiv, StandardCharsets.UTF_8)); final String content = buffer.lines().skip(1).map(l -> { Matcher m = PATTERN_CREATED_AT.matcher(l); String rv = l; if (m.find()) { try { rv = m.replaceFirst( "$1\"" + DATE_FORMAT_OUT.format(DATE_FORMAT_IN.parse(m.group(2))) + "\""); } catch (ParseException ex) { log.warn("Unexpected date format in twitter archive", ex); } } return rv; }).collect(Collectors.joining("")).replaceAll("\"sizes\" : \\[.+?\\],", "\"sizes\" : {},"); final JSONArray statuses = new JSONArray(content); for (int i = 0; i < statuses.length(); ++i) { final JSONObject rawJSON = statuses.getJSONObject(i); // https://twitter.com/lukaseder/status/772772372990586882 ;) final Status status = statusFactory.create(rawJSON).as(Status.class); this.tweetStorageService.store(status, rawJSON.toString()); } } } redirectAttributes.addFlashAttribute("message", "Done."); return "redirect:/upload"; }
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 v a 2 s . c o m*/ * @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.isdp.twitterposter.GoogleManager.java
License:Open Source License
public String[] parseWiki(JSONObject jsonObj) { try {/*www. j a v a2s . c om*/ ArrayList<String> results = new ArrayList<String>(); JSONArray itemsArray = jsonObj.getJSONArray("items"); for (int i = 0; i < itemsArray.length(); ++i) { JSONObject item = itemsArray.getJSONObject(i); String itemSnippet = item.getString("snippet"); results.add(itemSnippet); } String[] resultsArray = new String[results.size()]; return results.toArray(resultsArray); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.isdp.twitterposter.GoogleManager.java
License:Open Source License
public String[] parseYelp(JSONObject jsonObj) { try {/*from w w w. j a v a 2 s. co m*/ ArrayList<String> results = new ArrayList<String>(); JSONArray itemsArray = jsonObj.getJSONArray("items"); for (int i = 0; i < itemsArray.length(); ++i) { try { JSONObject item = itemsArray.getJSONObject(i); JSONObject localbusiness = item.getJSONObject("pagemap").getJSONArray("localbusiness") .getJSONObject(0); JSONObject postaladdress = item.getJSONObject("pagemap").getJSONArray("postaladdress") .getJSONObject(0); JSONObject aggregaterating = item.getJSONObject("pagemap").getJSONArray("aggregaterating") .getJSONObject(0); String textString = Util.generateRandomString(5) + "\n"; textString += Util.truncateString(localbusiness.getString("name"), 30) + "\n"; textString += localbusiness.getString("telephone") + "\n"; textString += postaladdress.getString("streetaddress") + "\n"; textString += postaladdress.getString("addresslocality") + " " + postaladdress.getString("addressregion") + "\n"; textString += postaladdress.getString("postalcode") + "\n"; textString += "R|" + aggregaterating.getString("ratingvalue") + "\n"; textString += "P|" + localbusiness.getString("pricerange") + "\n"; results.add(textString); } catch (Exception e) { } } String[] resultsArray = new String[results.size()]; return results.toArray(resultsArray); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.isi.master.meaningcloudAPI.topicsextraction.TopicsClient.java
License:Open Source License
/** * // w w w . j a va 2s . co m * @param jsonObj * @return */ private static boolean conceptTopics(JSONObject jsonObj) { try { JSONArray array = jsonObj.getJSONArray("concept_list"); for (int i = 0; i < array.length(); i++) { JSONObject doc = (JSONObject) array.getJSONObject(i); JSONObject doc1 = (JSONObject) doc.get("sementity"); if (doc1.getString("type").contains("Top>")) { return true; } } } catch (JSONException e) { // TODO Auto-generated catch block // e.printStackTrace(); } return false; }
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 www . j av a 2 s. co 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;/*from w w w .j a v a 2s .c o m*/ 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"); // } }
From source file:com.sampleapp.db.DBUtil.java
License:Open Source License
private DBUtil() { Map<String, String> env; env = System.getenv();//from w w w.ja v a 2 s. c o m String vcap = env.get("VCAP_SERVICES"); if (vcap == null) { System.out.println("No VCAP_SERVICES found"); return; } System.out.println("VCAP_SERVICES found"); try { JSONObject vcap_services = new JSONObject(vcap); @SuppressWarnings("rawtypes") Iterator iter = vcap_services.keys(); JSONArray cloudant = null; // find instance of cloudant bound to app while (iter.hasNext()) { String key = (String) iter.next(); if (key.startsWith("cloudantNoSQLDB")) { cloudant = vcap_services.getJSONArray(key); } } JSONObject instance = cloudant.getJSONObject(0); // Grab the first instance of mongoDB for this app (there is only one) JSONObject credentials = instance.getJSONObject("credentials"); // Get all VCAP_SERVICES credentials String host = credentials.getString("host"); String port = credentials.getString("port"); String username = credentials.getString("username"); String password = credentials.getString("password"); String url = credentials.getString("url"); System.out.println("Found all the params: " + username + " " + password + " " + url); // Create the client connection the the database and then create the database client = new CloudantClient(url, username, password); db = client.database(dbname, true); System.out.println("Connected to cloudant on " + host + ":" + port); } catch (Exception e) { e.printStackTrace(); } }
From source file:edu.smc.mediacommons.panels.WeatherPanel.java
License:Open Source License
public WeatherPanel() { setLayout(null);//from w ww . j av 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:org.csi.yucca.storage.datamanagementapi.model.metadata.ckan.MetadataCkanFactory.java
protected static String formatMessages(Locale locale, String element) { JSONObject messages = loadMessages(locale, element); StringBuffer sb = new StringBuffer(""); String loc = locale.getLanguage().substring(0, 1).toUpperCase() + locale.getLanguage().substring(1); String label1 = (element.equals("tags") ? "streamTags" : "streamDomains"); String label2 = (element.equals("tags") ? "tagCode" : "codDomain"); try {//w ww . ja v a 2 s. co m JSONObject streamTags = messages.getJSONObject(label1); JSONArray elements = streamTags.getJSONArray("element"); for (int i = 0; i < elements.length(); i++) { String tagCode = elements.getJSONObject(i).getString(label2); String langEl = elements.getJSONObject(i).getString("lang" + loc); sb.append(tagCode + " = " + langEl + "\n"); } } catch (JSONException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } return sb.toString(); }