Search in sources :

Example 41 with SyndEntry

use of com.rometools.rome.feed.synd.SyndEntry in project MtgDesktopCompanion by nicho92.

the class RSSNewsProvider method listNews.

@Override
public List<MagicNewsContent> listNews(MagicNews rssBean) throws IOException {
    InputStream is = null;
    SyndFeed feed;
    List<MagicNewsContent> ret = new ArrayList<>();
    try {
        HttpURLConnection openConnection = (HttpURLConnection) new URL(rssBean.getUrl()).openConnection();
        logger.debug("reading " + rssBean.getUrl());
        openConnection.setRequestProperty("User-Agent", MTGConstants.USER_AGENT);
        openConnection.setInstanceFollowRedirects(true);
        is = openConnection.getInputStream();
        InputSource source = new InputSource(is);
        feed = input.build(source);
        String baseURI = feed.getLink();
        for (SyndEntry s : feed.getEntries()) {
            MagicNewsContent content = new MagicNewsContent();
            content.setTitle(s.getTitle());
            content.setAuthor(s.getAuthor());
            content.setDate(s.getPublishedDate());
            URL link;
            if (!s.getLink().startsWith(baseURI))
                link = new URL(baseURI + s.getLink());
            else
                link = new URL(s.getLink());
            content.setLink(link);
            ret.add(content);
        }
        return ret;
    } catch (IllegalArgumentException | FeedException e) {
        throw new IOException(e);
    } finally {
        if (is != null)
            is.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) MagicNewsContent(org.magic.api.beans.MagicNewsContent) FeedException(com.rometools.rome.io.FeedException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) HttpURLConnection(java.net.HttpURLConnection)

Example 42 with SyndEntry

use of com.rometools.rome.feed.synd.SyndEntry in project mycore by MyCoRe-Org.

the class MCRRSSFeedImporter method importPublications.

public void importPublications(String projectID) throws Exception {
    LOGGER.info("Getting new publications from {} RSS feed...", sourceSystemID);
    SyndFeed feed = retrieveFeed();
    List<MCRObject> importedObjects = new ArrayList<>();
    for (SyndEntry entry : feed.getEntries()) {
        MCRObject importedObject = handleFeedEntry(entry, projectID);
        if (importedObject != null) {
            importedObjects.add(importedObject);
        }
    }
    int numPublicationsImported = importedObjects.size();
    LOGGER.info("imported {} publications.", numPublicationsImported);
    if ((numPublicationsImported > 0) && (xsl2BuildNotificationMail != null)) {
        sendNotificationMail(importedObjects);
    }
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) MCRObject(org.mycore.datamodel.metadata.MCRObject) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) ArrayList(java.util.ArrayList)

Example 43 with SyndEntry

use of com.rometools.rome.feed.synd.SyndEntry in project openolat by klemens.

the class RomeFeedFetcher method validateFeedUrl.

@Override
public ValidatedURL validateFeedUrl(String url, boolean enclosuresExpected) {
    SyndFeedInput input = new SyndFeedInput();
    boolean modifiedProtocol = false;
    try {
        if (url != null) {
            url = url.trim();
        }
        if (url.startsWith("feed") || url.startsWith("itpc")) {
            // accept feed(s) urls like generated in safari browser
            url = "http" + url.substring(4);
            modifiedProtocol = true;
        }
        URL realUrl = new URL(url);
        SyndFeed feed = input.build(new XmlReader(realUrl));
        if (!feed.getEntries().isEmpty()) {
            if (enclosuresExpected) {
                SyndEntry entry = feed.getEntries().get(0);
                if (entry.getEnclosures().isEmpty()) {
                    return new ValidatedURL(url, ValidatedURL.State.NO_ENCLOSURE);
                }
            }
            return new ValidatedURL(url, ValidatedURL.State.VALID);
        }
        // The feed was read successfully
        return new ValidatedURL(url, ValidatedURL.State.VALID);
    } catch (ParsingFeedException e) {
        if (modifiedProtocol) {
            // fallback for SWITCHcast itpc -> http -> https
            url = "https" + url.substring(4);
            return validateFeedUrl(url, enclosuresExpected);
        }
        String message = String.format("Validation of the feed url %s failed. %s: %s ", url, e.getClass(), e.getMessage());
        log.debug(message);
        return new ValidatedURL(url, ValidatedURL.State.NOT_FOUND);
    } catch (FileNotFoundException e) {
        String message = String.format("Validation of the feed url %s failed. %s: %s ", url, e.getClass(), e.getMessage());
        log.debug(message);
        return new ValidatedURL(url, ValidatedURL.State.NOT_FOUND);
    } catch (Exception e) {
        String message = String.format("Validation of the feed url %s failed. %s: %s ", url, e.getClass(), e.getMessage());
        log.debug(message);
    }
    return new ValidatedURL(url, ValidatedURL.State.MALFORMED);
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) ParsingFeedException(com.rometools.rome.io.ParsingFeedException) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FileNotFoundException(java.io.FileNotFoundException) XmlReader(com.rometools.rome.io.XmlReader) URL(java.net.URL) FileNotFoundException(java.io.FileNotFoundException) ParsingFeedException(com.rometools.rome.io.ParsingFeedException)

Example 44 with SyndEntry

use of com.rometools.rome.feed.synd.SyndEntry in project onebusaway-application-modules by camsys.

the class StatusUpdateAction method setAgencyMessages.

private void setAgencyMessages(List<SyndEntry> entries, String baseUrl) {
    // Add Agency Messages
    SyndEntry agencyMsgEntry = new SyndEntryImpl();
    agencyMsgEntry.setTitle("General Notices");
    agencyMsgEntry.setLink(baseUrl + "/rss/agency-messages-update");
    entries.add(agencyMsgEntry);
    StatusGroup agencyMsgGroup = _statusProvider.getAgencyMetadataStatus();
    if (agencyMsgGroup.getItems().size() == 0) {
        agencyMsgEntry = new SyndEntryImpl();
        agencyMsgEntry.setTitle("No Agency Messages");
        entries.add(agencyMsgEntry);
    } else {
        for (StatusItem agencyMsgItem : agencyMsgGroup.getItems()) {
            agencyMsgEntry = new SyndEntryImpl();
            agencyMsgEntry.setTitle(agencyMsgItem.getTitle());
            entries.add(agencyMsgEntry);
        }
    }
}
Also used : StatusItem(org.onebusaway.enterprise.webapp.actions.status.model.StatusItem) StatusGroup(org.onebusaway.enterprise.webapp.actions.status.model.StatusGroup) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl)

Example 45 with SyndEntry

use of com.rometools.rome.feed.synd.SyndEntry 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)

Aggregations

SyndEntry (com.rometools.rome.feed.synd.SyndEntry)45 ArrayList (java.util.ArrayList)23 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)22 SyndEntryImpl (com.rometools.rome.feed.synd.SyndEntryImpl)18 SyndContent (com.rometools.rome.feed.synd.SyndContent)15 SyndFeedImpl (com.rometools.rome.feed.synd.SyndFeedImpl)14 SyndContentImpl (com.rometools.rome.feed.synd.SyndContentImpl)13 SyndFeedInput (com.rometools.rome.io.SyndFeedInput)9 FeedException (com.rometools.rome.io.FeedException)7 Test (org.junit.Test)7 XmlReader (com.rometools.rome.io.XmlReader)6 URL (java.net.URL)6 Date (java.util.Date)5 StatusGroup (org.onebusaway.enterprise.webapp.actions.status.model.StatusGroup)5 StatusItem (org.onebusaway.enterprise.webapp.actions.status.model.StatusItem)5 IOException (java.io.IOException)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 SyndFeedOutput (com.rometools.rome.io.SyndFeedOutput)3 Post (it.vige.rubia.model.Post)3