Exemplo n.º 1
0
  private void syncRss() {
    // get the feed list
    ArrayList<JDFeedMeFeed> feeds = gui.getFeeds();

    // go over all the feeds
    for (JDFeedMeFeed feed : feeds) {
      if (!feed.isEnabled()) continue;
      String feed_address = feed.getAddress();
      if (feed_address.length() == 0) continue;

      // sync each feed
      String response = "not downloaded yet";
      try {
        // get the feed last update timestamp
        String timestamp = null;
        if ((feed.getTimestamp() != null) && (feed.getTimestamp().length() > 0))
          timestamp = feed.getTimestamp();

        logger.info("JDFeedMe syncing feed: " + feed_address + " [" + timestamp + "]");
        gui.setFeedStatus(feed, JDFeedMeFeed.STATUS_RUNNING);

        // get the feed content from the web
        Browser browser = new Browser();
        browser.setFollowRedirects(true); // support redirects since some feeds have them
        response = browser.getPage(feed_address);

        // parse the feed
        String new_timestamp = parseFeed(feed, timestamp, response);

        // set the new timestamp if needed
        if ((new_timestamp != null) && (new_timestamp != timestamp)) {
          gui.setFeedTimestamp(feed, new_timestamp);
        }

        // all ok
        gui.setFeedStatus(feed, JDFeedMeFeed.STATUS_OK);
      } catch (Exception e) {
        // shorten the response so we can show it in the log
        int max_reponse_length = 1000;
        String response_short = response;
        if (response_short != null) {
          if (response_short.length() > max_reponse_length) {
            response_short =
                response_short.substring(0, max_reponse_length / 2)
                    + "..."
                    + response_short.substring(response_short.length() - max_reponse_length / 2);
          }
        }

        e.printStackTrace();
        logger.severe(
            "JDFeedMe cannot sync feed: "
                + feed_address
                + " ("
                + e.toString()
                + "), feed response: "
                + response_short);
        gui.setFeedStatus(feed, JDFeedMeFeed.STATUS_ERROR);
      }
    }

    // save our xml with any updates from what we just downloaded
    gui.saveFeeds(); // maybe do this only if we had updates
    gui.savePosts(); // maybe do this only if we had updates
  }