List of usage examples for twitter4j JSONArray JSONArray
public JSONArray(Object array) throws JSONException
From source file:ac.simons.tweetarchive.web.ArchiveHandlingController.java
License:Apache License
/** * As you can see, it get's nasty here... * <br>//from www . j av a 2s. 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 .j av a 2 s . co 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.epsi.twitterdashboard.utils.JsonFile.java
/** * Add dashboard bookmark to json file/*from www . j a va 2 s . c om*/ * @param user */ public static void AddUser(User user) { List<User> users = ReadUsers(); if (users == null) { users = new ArrayList<User>(); } users.add(user); JSONArray jsonUsers = new JSONArray(users); Write(JsonFile.UsersPath, jsonUsers.toString()); }
From source file:com.epsi.twitterdashboard.utils.JsonFile.java
/** * Add dashboard bookmark to json file/* www . ja v a 2 s . c o m*/ * @param id */ public static void DeleteUser(int id) { List<User> users = ReadUsers(); if (users == null) { users.remove(ListFinder.FindUserById(users, id)); JSONArray jsonUsers = new JSONArray(users); Write(JsonFile.UsersPath, jsonUsers.toString()); } }
From source file:com.epsi.twitterdashboard.utils.JsonFile.java
/** * Add dashboard bookmark to json file/* www . j a v a 2s . c o m*/ * @param id */ public static void AddBookmark(String username, int id) { List<Tweet> database = ReadDatabase(username); List<Tweet> bookmarks = ReadBookmarks(username); if (bookmarks == null) { bookmarks = new ArrayList<Tweet>(); } bookmarks.add(ListFinder.FindTweetById(database, id)); JSONArray jsonBookmark = new JSONArray(bookmarks); Write(String.format(JsonFile.BookmarkPath, username), jsonBookmark.toString()); }
From source file:com.epsi.twitterdashboard.utils.JsonFile.java
/** * Delete dashboard bookmark from json file * @param id //from w w w .java2 s .co m */ public static void DeleteBookmark(String username, int id) { List<Tweet> bookmarks = ReadBookmarks(username); if (bookmarks == null) { bookmarks.remove(ListFinder.FindTweetById(bookmarks, id)); JSONArray jsonBookmark = new JSONArray(bookmarks); Write(String.format(JsonFile.BookmarkPath, username), jsonBookmark.toString()); } }
From source file:edu.smc.mediacommons.panels.WeatherPanel.java
License:Open Source License
public WeatherPanel() { setLayout(null);//from w w w.ja va2 s . c om 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(); } } }); }