/** Delete files that aren't needed anymore */
 private void cleanup(Feed feed) {
   if (feed.getFile_url() != null) {
     if (new File(feed.getFile_url()).delete())
       if (AppConfig.DEBUG) Log.d(TAG, "Successfully deleted cache file.");
       else Log.e(TAG, "Failed to delete cache file.");
     feed.setFile_url(null);
   } else if (AppConfig.DEBUG) {
     Log.d(TAG, "Didn't delete cache file: File url is not set.");
   }
 }
 /**
  * Checks if the FeedItems of this feed have images that point to the same URL. If two FeedItems
  * have an image that points to the same URL, the reference of the second item is removed, so
  * that every image reference is unique.
  */
 private void removeDuplicateImages(Feed feed) {
   for (int x = 0; x < feed.getItems().size(); x++) {
     for (int y = x + 1; y < feed.getItems().size(); y++) {
       FeedItem item1 = feed.getItems().get(x);
       FeedItem item2 = feed.getItems().get(y);
       if (item1.hasItemImage() && item2.hasItemImage()) {
         if (StringUtils.equals(
             item1.getImage().getDownload_url(), item2.getImage().getDownload_url())) {
           item2.setImage(null);
         }
       }
     }
   }
 }
示例#3
0
  /**
   * Takes a list of feeds and a writer and writes those into an OPML document.
   *
   * @throws IOException
   * @throws IllegalStateException
   * @throws IllegalArgumentException
   */
  public void writeDocument(List<Feed> feeds, Writer writer)
      throws IllegalArgumentException, IllegalStateException, IOException {
    if (AppConfig.DEBUG) Log.d(TAG, "Starting to write document");
    XmlSerializer xs = Xml.newSerializer();
    xs.setOutput(writer);

    xs.startDocument(ENCODING, false);
    xs.startTag(null, OpmlSymbols.OPML);
    xs.attribute(null, OpmlSymbols.VERSION, OPML_VERSION);

    xs.startTag(null, OpmlSymbols.HEAD);
    xs.startTag(null, OpmlSymbols.TITLE);
    xs.text(OPML_TITLE);
    xs.endTag(null, OpmlSymbols.TITLE);
    xs.endTag(null, OpmlSymbols.HEAD);

    xs.startTag(null, OpmlSymbols.BODY);
    for (Feed feed : feeds) {
      xs.startTag(null, OpmlSymbols.OUTLINE);
      xs.attribute(null, OpmlSymbols.TEXT, feed.getTitle());
      if (feed.getType() != null) {
        xs.attribute(null, OpmlSymbols.TYPE, feed.getType());
      }
      xs.attribute(null, OpmlSymbols.XMLURL, feed.getDownload_url());
      if (feed.getLink() != null) {
        xs.attribute(null, OpmlSymbols.HTMLURL, feed.getLink());
      }
      xs.endTag(null, OpmlSymbols.OUTLINE);
    }
    xs.endTag(null, OpmlSymbols.BODY);
    xs.endTag(null, OpmlSymbols.OPML);
    xs.endDocument();
    if (AppConfig.DEBUG) Log.d(TAG, "Finished writing document");
  }
 /** Checks if the feed was parsed correctly. */
 private boolean checkFeedData(Feed feed) {
   if (feed.getTitle() == null) {
     Log.e(TAG, "Feed has no title.");
     return false;
   }
   if (!hasValidFeedItems(feed)) {
     Log.e(TAG, "Feed has invalid items");
     return false;
   }
   return true;
 }
 /** Checks if the feed was parsed correctly. */
 private boolean checkFeedData(Feed feed) {
   if (feed.getTitle() == null) {
     Log.e(TAG, "Feed has no title.");
     return false;
   }
   if (!hasValidFeedItems(feed)) {
     Log.e(TAG, "Feed has invalid items");
     return false;
   }
   if (AppConfig.DEBUG) Log.d(TAG, "Feed appears to be valid.");
   return true;
 }
 private boolean hasValidFeedItems(Feed feed) {
   for (FeedItem item : feed.getItems()) {
     if (item.getTitle() == null) {
       Log.e(TAG, "Item has no title");
       return false;
     }
     if (item.getPubDate() == null) {
       Log.e(TAG, "Item has no pubDate. Using current time as pubDate");
       if (item.getTitle() != null) {
         Log.e(TAG, "Title of invalid item: " + item.getTitle());
       }
       item.setPubDate(new Date());
     }
   }
   return true;
 }
    public void run() {
      Feed savedFeed = null;

      Feed feed = new Feed(request.getSource(), new Date());
      feed.setFile_url(request.getDestination());
      feed.setDownloaded(true);

      reason = null;
      String reasonDetailed = null;
      successful = true;
      FeedHandler feedHandler = new FeedHandler();

      try {
        feed = feedHandler.parseFeed(feed);
        if (AppConfig.DEBUG) Log.d(TAG, feed.getTitle() + " parsed");
        if (checkFeedData(feed) == false) {
          throw new InvalidFeedException();
        }
        // Save information of feed in DB
        savedFeed = DBTasks.updateFeed(DownloadService.this, feed);
        // Download Feed Image if provided and not downloaded
        if (savedFeed.getImage() != null && savedFeed.getImage().isDownloaded() == false) {
          if (AppConfig.DEBUG) Log.d(TAG, "Feed has image; Downloading....");
          savedFeed.getImage().setFeed(savedFeed);
          final Feed savedFeedRef = savedFeed;
          try {
            requester.downloadImage(DownloadService.this, savedFeedRef.getImage());
          } catch (DownloadRequestException e) {
            e.printStackTrace();
            DBWriter.addDownloadStatus(
                DownloadService.this,
                new DownloadStatus(
                    savedFeedRef.getImage(),
                    savedFeedRef.getImage().getHumanReadableIdentifier(),
                    DownloadError.ERROR_REQUEST_ERROR,
                    false,
                    e.getMessage()));
          }
        }

      } catch (SAXException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (IOException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (ParserConfigurationException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (UnsupportedFeedtypeException e) {
        e.printStackTrace();
        successful = false;
        reason = DownloadError.ERROR_UNSUPPORTED_TYPE;
        reasonDetailed = e.getMessage();
      } catch (InvalidFeedException e) {
        e.printStackTrace();
        successful = false;
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      }

      // cleanup();
      if (savedFeed == null) {
        savedFeed = feed;
      }

      saveDownloadStatus(
          new DownloadStatus(
              savedFeed,
              savedFeed.getHumanReadableIdentifier(),
              reason,
              successful,
              reasonDetailed));
      sendDownloadHandledIntent();
      numberOfDownloads.decrementAndGet();
      queryDownloadsAsync();
    }
    private Feed parseFeed(DownloadRequest request) {
      Feed savedFeed = null;

      Feed feed = new Feed(request.getSource(), new Date());
      feed.setFile_url(request.getDestination());
      feed.setId(request.getFeedfileId());
      feed.setDownloaded(true);
      feed.setPreferences(
          new FeedPreferences(0, true, request.getUsername(), request.getPassword()));

      DownloadError reason = null;
      String reasonDetailed = null;
      boolean successful = true;
      FeedHandler feedHandler = new FeedHandler();

      try {
        feed = feedHandler.parseFeed(feed).feed;
        if (BuildConfig.DEBUG) Log.d(TAG, feed.getTitle() + " parsed");
        if (checkFeedData(feed) == false) {
          throw new InvalidFeedException();
        }

      } catch (SAXException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (IOException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (ParserConfigurationException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (UnsupportedFeedtypeException e) {
        e.printStackTrace();
        successful = false;
        reason = DownloadError.ERROR_UNSUPPORTED_TYPE;
        reasonDetailed = e.getMessage();
      } catch (InvalidFeedException e) {
        e.printStackTrace();
        successful = false;
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      }

      // cleanup();
      if (savedFeed == null) {
        savedFeed = feed;
      }

      if (successful) {
        return savedFeed;
      } else {
        saveDownloadStatus(
            new DownloadStatus(
                savedFeed,
                savedFeed.getHumanReadableIdentifier(),
                reason,
                successful,
                reasonDetailed));
        return null;
      }
    }