Search in sources :

Example 1 with XmlReader

use of com.rometools.rome.io.XmlReader in project KaellyBot by Kaysoro.

the class RSS method getRSSFeeds.

public static List<RSS> getRSSFeeds(Language lg) {
    List<RSS> rss = new ArrayList<>();
    try {
        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = input.build(new XmlReader(new URL(Translator.getLabel(lg, "game.url") + Translator.getLabel(lg, "feed.url"))));
        for (SyndEntry entry : feed.getEntries()) {
            Matcher m = Pattern.compile("<img.+src=\"(.*\\.jpg)\".+>").matcher(entry.getDescription().getValue());
            rss.add(new RSS(entry.getTitle(), entry.getLink(), (m.find() ? m.group(1) : null), entry.getPublishedDate().getTime()));
        }
    } catch (FeedException e) {
        Reporter.report(e);
        LOG.error("getRSSFeeds", e);
    } catch (IOException e) {
        ExceptionManager.manageSilentlyIOException(e);
    } catch (Exception e) {
        ExceptionManager.manageSilentlyException(e);
    }
    Collections.sort(rss);
    return rss;
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) Matcher(java.util.regex.Matcher) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FeedException(com.rometools.rome.io.FeedException) XmlReader(com.rometools.rome.io.XmlReader) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) FeedException(com.rometools.rome.io.FeedException)

Example 2 with XmlReader

use of com.rometools.rome.io.XmlReader in project muikku by otavanopisto.

the class FeedSynchronizer method updateFeeds.

@Schedule(second = "0", minute = "0", hour = "*", persistent = false)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateFeeds() {
    Client client = ClientBuilder.newClient();
    feedItemDao.deleteAll();
    List<Feed> feeds = feedDao.listAll();
    for (Feed feed : feeds) {
        WebTarget target = client.target(feed.getUrl());
        try (InputStream stream = target.request("*").get(InputStream.class)) {
            SyndFeedInput input = new SyndFeedInput();
            SyndFeed syndFeed = input.build(new XmlReader(stream));
            List<SyndEntry> entries = syndFeed.getEntries();
            for (SyndEntry entry : entries) {
                feedItemDao.create(entry.getTitle(), entry.getLink(), entry.getAuthor(), entry.getDescription() == null ? null : clean(entry.getDescription().getValue()), entry.getPublishedDate(), (String) null, feed);
            }
        } catch (IOException | IllegalArgumentException | FeedException e) {
            logger.warning(String.format("Error while synchronizing feeds: %s", e.getMessage()));
            ejbContext.setRollbackOnly();
        }
    }
}
Also used : InputStream(java.io.InputStream) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FeedException(com.rometools.rome.io.FeedException) XmlReader(com.rometools.rome.io.XmlReader) IOException(java.io.IOException) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) Feed(fi.otavanopisto.muikku.plugins.feed.model.Feed) TransactionAttribute(javax.ejb.TransactionAttribute) Schedule(javax.ejb.Schedule)

Example 3 with XmlReader

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

the class RssToJsonTest method shouldNotHaveRawContent.

@Test
public void shouldNotHaveRawContent() 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, false);
        assertThat(xcb, notNullValue());
        assertThat(xcb.string(), new SubstringMatcher("<p>") {

            @Override
            protected boolean evalSubstringOf(String s) {
                return s.indexOf(substring) < 0;
            }

            @Override
            protected String relationship() {
                return "not containing";
            }
        });
        logger.info(xcb.string());
    }
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SubstringMatcher(org.hamcrest.core.SubstringMatcher) 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)

Example 4 with XmlReader

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

the class RomeFeedFetcher method fetchSyndFeed.

/**
 * Fetches the SyndFeed of an URL.
 * @param feedURL
 * @return
 */
protected SyndFeed fetchSyndFeed(String feedURL) {
    SyndFeed syndFeed = null;
    try (Reader xmlReader = new XmlReader(new URL(feedURL))) {
        syndFeed = syndFeedInput.build(xmlReader);
        log.info("Read external feed: " + feedURL);
    } catch (Exception e) {
        log.warn("Cannot read external feed: : " + feedURL);
    }
    return syndFeed;
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) Reader(java.io.Reader) XmlReader(com.rometools.rome.io.XmlReader) XmlReader(com.rometools.rome.io.XmlReader) URL(java.net.URL) FileNotFoundException(java.io.FileNotFoundException) ParsingFeedException(com.rometools.rome.io.ParsingFeedException)

Example 5 with XmlReader

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

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)

Aggregations

XmlReader (com.rometools.rome.io.XmlReader)16 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)15 SyndFeedInput (com.rometools.rome.io.SyndFeedInput)13 URL (java.net.URL)8 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)5 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 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 InputStream (java.io.InputStream)1