/** * A helper function to print out the contents of an entry to log.trace * * @param entry */ public static void printEntry(SyndEntry entry) { if (!log.isTraceEnabled()) { return; } StringBuffer pr = new StringBuffer( "URI: " + entry.getUri() + "\n" + "Title: " + entry.getTitle() + "\n" + "\n" + "Date: " + entry.getPublishedDate() + "\n" + "Modified: " + entry.getUpdatedDate() + "\n"); pr.append("Creators: \n"); for (SyndPerson author : FeedHelper.getAuthors(entry)) { pr.append(" - " + author.getName() + "\n"); } pr.append("Links: \n"); for (SyndLink link : FeedHelper.getLinks(entry)) { pr.append(" - " + link.getTitle() + ": " + link.getHref() + "\n"); } SyndContent description = entry.getDescription(); if (description != null) { pr.append("\nDescription(" + description.getType() + "): " + description.getValue()); } pr.append("Contents: \n"); for (SyndContent content : FeedHelper.getContents(entry)) { pr.append(" Type: " + content.getType()); pr.append(" Body: " + content.getValue()); try { pr.append( " Body (Plain text): " + PlainTextExtractor.getPlainText(content.getType(), content.getValue())); } catch (ParserException e) { pr.append("Failed to parse content"); } } pr.append("Categories: \n"); for (SyndCategory category : FeedHelper.getCategories(entry)) { pr.append(category.getName() + "(" + category.getTaxonomyUri() + ")"); } log.trace(pr.toString()); }
protected Item createRSSItem(SyndEntry sEntry) { Item item = super.createRSSItem(sEntry); SyndContent sContent = sEntry.getDescription(); if (sContent != null) { item.setDescription(createItemDescription(sContent)); } List contents = sEntry.getContents(); if (contents != null && contents.size() > 0) { SyndContent syndContent = (SyndContent) contents.get(0); Content cont = new Content(); cont.setValue(syndContent.getValue()); cont.setType(syndContent.getType()); item.setContent(cont); } return item; }
protected Description createItemDescription(SyndContent sContent) { Description desc = new Description(); desc.setValue(sContent.getValue()); desc.setType(sContent.getType()); return desc; }
/** * Generate an ebook from an RSS DOM Document. * * @param url The URL from where the Document was fetched (used only to set the author metadata) * @param doc The DOM Document of the feed. * @return An ebook. * @throws IllegalArgumentException * @throws FeedException * @throws IOException */ private static Book createBookFromFeed(URL url, Document doc, List<Keyword> keywords) throws IllegalArgumentException, FeedException, IOException { Book book = new Book(); // start parsing our feed and have the above onItem methods called SyndFeedInput input = new SyndFeedInput(); SyndFeed feed = input.build(doc); System.out.println(feed); // Set the title book.getMetadata().addTitle(feed.getTitle()); // Add an Author String author = feed.getAuthor(); if (author == null || "".equals(author.trim())) { author = url.getHost(); } book.getMetadata().addAuthor(new Author(author)); if (feed.getPublishedDate() != null) { book.getMetadata().addDate(new nl.siegmann.epublib.domain.Date(feed.getPublishedDate())); } if (feed.getDescription() != null) { book.getMetadata().addDescription(feed.getDescription()); } if (feed.getCopyright() != null) { book.getMetadata().getRights().add(feed.getCopyright()); } // Set cover image - This has never worked. // if (feed.getImage() != null) { // System.out.println("There is an image for the feed"); // Promise<HttpResponse> futureImgResponse = // WS.url(feed.getImage().getUrl()).getAsync(); // HttpResponse imgResponse = await(futureImgResponse); // System.out.println("Content-type: " + imgResponse.getContentType()); // if (imgResponse.getContentType().startsWith("image/")) { // String extension = // imgResponse.getContentType().substring("image/".length()); // InputStream imageStream = imgResponse.getStream(); // book.getMetadata().setCoverImage(new Resource(imageStream, "cover." + // extension)); // System.out.println("Using default cover"); // imageStream = // VirtualFile.fromRelativePath("assets/cover.png").inputstream(); // if (imageStream != null) { // System.out.println("Using default cover"); // book.getMetadata().setCoverImage(new Resource(imageStream, // "cover.png")); // } else { // System.out.println("Could not load default cover"); // } // } // } int entryNumber = 0; List<SyndEntry> entries = feed.getEntries(); for (SyndEntry entry : entries) { if (matchesKeyword(entry, keywords)) { StringBuilder title = new StringBuilder(100); if (entry.getTitle() != null) { title.append(entry.getTitle()); } if (entry.getAuthor() != null) { title.append(" - ").append(entry.getAuthor()); } StringBuilder content = new StringBuilder(); // Add title inside text content.append("<h2>").append(title).append("</h2>"); if (entry.getDescription() != null) { SyndContent syndContent = (SyndContent) entry.getDescription(); if (!syndContent.getType().contains("html")) { content.append("<pre>\n"); } content.append(syndContent.getValue()); if (!syndContent.getType().contains("html")) { content.append("\n</pre>"); } content.append("<hr/>"); } if (entry.getContents().size() > 0) { SyndContent syndContent = (SyndContent) entry.getContents().get(0); if (!syndContent.getType().contains("html")) { content.append("<pre>\n"); } content.append(syndContent.getValue()); if (!syndContent.getType().contains("html")) { content.append("\n</pre>"); } } String strContent = clean(content.toString()); // Add Chapter try { entryNumber++; book.addSection( title.toString(), new Resource(new StringReader(strContent), "entry" + entryNumber + ".xhtml")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return book; }
// build a SubscriptionEntry from Rome SyndEntry and SyndFeed private SubscriptionEntry buildEntry(SyndEntry romeEntry) { // if we don't have a permalink then we can't continue if (romeEntry.getLink() == null) { return null; } SubscriptionEntry newEntry = new SubscriptionEntry(); newEntry.setTitle(romeEntry.getTitle()); newEntry.setPermalink(romeEntry.getLink()); // Play some games to get the author DCModule entrydc = (DCModule) romeEntry.getModule(DCModule.URI); if (romeEntry.getAuthor() != null) { newEntry.setAuthor(romeEntry.getAuthor()); } else { newEntry.setAuthor(entrydc.getCreator()); // use <dc:creator> } // Play some games to get the updated date if (romeEntry.getUpdatedDate() != null) { newEntry.setUpdateTime(new Timestamp(romeEntry.getUpdatedDate().getTime())); } // TODO: should we set a default update time here? // And more games getting publish date if (romeEntry.getPublishedDate() != null) { newEntry.setPubTime(new Timestamp(romeEntry.getPublishedDate().getTime())); // use <pubDate> } else if (entrydc != null && entrydc.getDate() != null) { newEntry.setPubTime(new Timestamp(entrydc.getDate().getTime())); // use <dc:date> } else { newEntry.setPubTime(newEntry.getUpdateTime()); } // get content and unescape if it is 'text/plain' if (romeEntry.getContents().size() > 0) { SyndContent content = (SyndContent) romeEntry.getContents().get(0); if (content != null && content.getType().equals("text/plain")) { newEntry.setText(StringEscapeUtils.unescapeHtml(content.getValue())); } else if (content != null) { newEntry.setText(content.getValue()); } } // no content, try summary if (newEntry.getText() == null || newEntry.getText().trim().length() == 0) { if (romeEntry.getDescription() != null) { newEntry.setText(romeEntry.getDescription().getValue()); } } // copy categories if (romeEntry.getCategories().size() > 0) { List list = new ArrayList(); Iterator cats = romeEntry.getCategories().iterator(); while (cats.hasNext()) { SyndCategory cat = (SyndCategory) cats.next(); list.add(cat.getName()); } newEntry.setCategoriesString(list); } return newEntry; }