Search in sources :

Example 11 with XmlReader

use of com.rometools.rome.io.XmlReader in project structr by structr.

the class DataFeed method updateFeed.

static void updateFeed(final DataFeed thisFeed, final boolean cleanUp) {
    final String remoteUrl = thisFeed.getUrl();
    if (StringUtils.isNotBlank(remoteUrl)) {
        final SecurityContext securityContext = thisFeed.getSecurityContext();
        final App app = StructrApp.getInstance(securityContext);
        try {
            final PropertyKey<Date> dateKey = StructrApp.key(FeedItem.class, "pubDate");
            final PropertyKey<String> urlKey = StructrApp.key(FeedItem.class, "url");
            final URL remote = new URL(remoteUrl);
            final SyndFeedInput input = new SyndFeedInput();
            try (final Reader reader = new XmlReader(remote)) {
                final SyndFeed feed = input.build(reader);
                final List<SyndEntry> entries = feed.getEntries();
                thisFeed.setProperty(StructrApp.key(DataFeed.class, "feedType"), feed.getFeedType());
                thisFeed.setProperty(StructrApp.key(DataFeed.class, "description"), feed.getDescription());
                final List<FeedItem> newItems = Iterables.toList(thisFeed.getItems());
                for (final SyndEntry entry : entries) {
                    final PropertyMap props = new PropertyMap();
                    final String link = entry.getLink();
                    // Check if item with this link already exists
                    if (app.nodeQuery(FeedItem.class).and(urlKey, link).getFirst() == null) {
                        props.put(urlKey, entry.getLink());
                        props.put(StructrApp.key(FeedItem.class, "name"), entry.getTitle());
                        props.put(StructrApp.key(FeedItem.class, "author"), entry.getAuthor());
                        props.put(StructrApp.key(FeedItem.class, "comments"), entry.getComments());
                        props.put(StructrApp.key(FeedItem.class, "description"), entry.getDescription().getValue());
                        final FeedItem item = app.create(FeedItem.class, props);
                        item.setProperty(dateKey, entry.getPublishedDate());
                        final List<FeedItemContent> itemContents = new LinkedList<>();
                        final List<FeedItemEnclosure> itemEnclosures = new LinkedList<>();
                        // Get and add all contents
                        final List<SyndContent> contents = entry.getContents();
                        for (final SyndContent content : contents) {
                            final FeedItemContent itemContent = app.create(FeedItemContent.class);
                            itemContent.setValue(content.getValue());
                            itemContents.add(itemContent);
                        }
                        // Get and add all enclosures
                        final List<SyndEnclosure> enclosures = entry.getEnclosures();
                        for (final SyndEnclosure enclosure : enclosures) {
                            final FeedItemEnclosure itemEnclosure = app.create(FeedItemEnclosure.class);
                            itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "url"), enclosure.getUrl());
                            itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "enclosureLength"), enclosure.getLength());
                            itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "enclosureType"), enclosure.getType());
                            itemEnclosures.add(itemEnclosure);
                        }
                        item.setProperty(StructrApp.key(FeedItem.class, "contents"), itemContents);
                        item.setProperty(StructrApp.key(FeedItem.class, "enclosures"), itemEnclosures);
                        newItems.add(item);
                        logger.debug("Created new item: {} ({}) ", item.getProperty(FeedItem.name), item.getProperty(dateKey));
                    }
                }
                thisFeed.setProperty(StructrApp.key(DataFeed.class, "items"), newItems);
                thisFeed.setProperty(StructrApp.key(DataFeed.class, "lastUpdated"), new Date());
            }
        } catch (IllegalArgumentException | IOException | FeedException | FrameworkException ex) {
            logger.error("Error while updating feed", ex);
        }
    }
    if (cleanUp) {
        thisFeed.cleanUp();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) XmlReader(com.rometools.rome.io.XmlReader) Reader(java.io.Reader) URL(java.net.URL) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEnclosure(com.rometools.rome.feed.synd.SyndEnclosure) FrameworkException(org.structr.common.error.FrameworkException) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FeedException(com.rometools.rome.io.FeedException) XmlReader(com.rometools.rome.io.XmlReader) IOException(java.io.IOException) Date(java.util.Date) LinkedList(java.util.LinkedList) PropertyMap(org.structr.core.property.PropertyMap) SyndContent(com.rometools.rome.feed.synd.SyndContent) SecurityContext(org.structr.common.SecurityContext)

Example 12 with XmlReader

use of com.rometools.rome.io.XmlReader in project rssriver by dadoonet.

the class RomeTest method testGeoLoc.

@Test
public void testGeoLoc() throws Exception {
    SyndFeedInput input = new SyndFeedInput();
    input.setPreserveWireFeed(true);
    SyndFeed feed = input.build(new XmlReader(getClass().getResource("/reuters/rss.xml")));
    assertThat(feed, notNullValue());
    assertThat(feed.getEntries().isEmpty(), equalTo(false));
    for (Object o : feed.getEntries()) {
        assertThat(o, instanceOf(SyndEntryImpl.class));
        SyndEntryImpl entry = (SyndEntryImpl) o;
        GeoRSSModule geoRSSModule = GeoRSSUtils.getGeoRSS(entry);
        assertThat(geoRSSModule, notNullValue());
        Position position = geoRSSModule.getPosition();
        assertThat(position, notNullValue());
        assertThat(position.getLatitude(), equalTo(41.8947384616695));
        assertThat(position.getLongitude(), equalTo(12.4839019775391));
    }
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) Position(com.rometools.modules.georss.geometries.Position) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) GeoRSSModule(com.rometools.modules.georss.GeoRSSModule) XmlReader(com.rometools.rome.io.XmlReader) Test(org.junit.Test)

Example 13 with XmlReader

use of com.rometools.rome.io.XmlReader in project rssriver by dadoonet.

the class RomeTest method testLeMonde.

@Test
public void testLeMonde() throws Exception {
    SyndFeedInput input = new SyndFeedInput();
    input.setPreserveWireFeed(true);
    SyndFeed feed = input.build(new XmlReader(getClass().getResource("/lemonde/rss.xml")));
    assertThat(feed, notNullValue());
    assertThat(feed.getEntries().isEmpty(), equalTo(false));
    assertThat(feed.originalWireFeed(), notNullValue());
    assertThat(feed.originalWireFeed(), instanceOf(Channel.class));
    Channel channel = (Channel) feed.originalWireFeed();
    assertThat(channel.getTtl(), equalTo(15));
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) Channel(com.rometools.rome.feed.rss.Channel) XmlReader(com.rometools.rome.io.XmlReader) Test(org.junit.Test)

Example 14 with XmlReader

use of com.rometools.rome.io.XmlReader in project rssriver by dadoonet.

the class RssToJsonTest method buildEntry.

private SyndEntryImpl buildEntry() throws FeedException, IOException {
    SyndFeedInput input = new SyndFeedInput();
    SyndFeed feed = input.build(new XmlReader(getClass().getResource("/reuters/rss.xml")));
    return (SyndEntryImpl) feed.getEntries().get(0);
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) XmlReader(com.rometools.rome.io.XmlReader)

Example 15 with XmlReader

use of com.rometools.rome.io.XmlReader in project rssriver by dadoonet.

the class RssToJsonTest method shouldHaveRawContent.

@Test
public void shouldHaveRawContent() throws Exception {
    SyndFeedInput input = new SyndFeedInput();
    SyndFeed feed = input.build(new XmlReader(getClass().getResource("/dcrainmaker/rss.xml")));
    assertThat(feed.getEntries().size(), greaterThan(0));
    for (Object o : feed.getEntries()) {
        SyndEntryImpl message = (SyndEntryImpl) o;
        XContentBuilder xcb = toJson(message, null, null, true);
        assertThat(xcb, notNullValue());
        assertThat(xcb.string(), containsString("<p>"));
        logger.info(xcb.string());
    }
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) XmlReader(com.rometools.rome.io.XmlReader) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Test(org.junit.Test)

Aggregations

XmlReader (com.rometools.rome.io.XmlReader)17 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)16 SyndFeedInput (com.rometools.rome.io.SyndFeedInput)14 URL (java.net.URL)9 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)6 SyndEntryImpl (com.rometools.rome.feed.synd.SyndEntryImpl)5 Test (org.junit.Test)5 FeedException (com.rometools.rome.io.FeedException)4 ParsingFeedException (com.rometools.rome.io.ParsingFeedException)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 Reader (java.io.Reader)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)1 GeoRSSModule (com.rometools.modules.georss.GeoRSSModule)1 Position (com.rometools.modules.georss.geometries.Position)1 Channel (com.rometools.rome.feed.rss.Channel)1 SyndContent (com.rometools.rome.feed.synd.SyndContent)1 SyndEnclosure (com.rometools.rome.feed.synd.SyndEnclosure)1 Feed (fi.otavanopisto.muikku.plugins.feed.model.Feed)1