diff options
author | Michael Bien <[email protected]> | 2010-06-04 04:28:04 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-06-04 04:28:04 +0200 |
commit | 2ce7f690778db8f6a960a6ec1d086fb0b35f9c63 (patch) | |
tree | aa9d351bf4205a06aa003f04f166aa23e2d129d6 | |
parent | 2dbf7cb31149942d95b523ba7fc6dc5407ca48cb (diff) |
added maxEntries to config, refactored aggregator code.
-rw-r--r-- | src/main/java/com/jogamp/hungryharry/Config.java | 4 | ||||
-rw-r--r-- | src/main/java/com/jogamp/hungryharry/FeedAggregator.java | 141 | ||||
-rw-r--r-- | src/main/java/com/jogamp/hungryharry/config.xml | 4 |
3 files changed, 80 insertions, 69 deletions
diff --git a/src/main/java/com/jogamp/hungryharry/Config.java b/src/main/java/com/jogamp/hungryharry/Config.java index 33fcf52..515b878 100644 --- a/src/main/java/com/jogamp/hungryharry/Config.java +++ b/src/main/java/com/jogamp/hungryharry/Config.java @@ -81,6 +81,9 @@ public class Config { @XmlAttribute public final String link; + @XmlAttribute + public final int maxEntries; + @XmlElement(name="feed") public final List<PlanetFeed> feeds; @@ -94,6 +97,7 @@ public class Config { link = null; feeds = null; templatePath = null; + maxEntries = 0; } public String getLink() { diff --git a/src/main/java/com/jogamp/hungryharry/FeedAggregator.java b/src/main/java/com/jogamp/hungryharry/FeedAggregator.java index 872a598..29d8bd3 100644 --- a/src/main/java/com/jogamp/hungryharry/FeedAggregator.java +++ b/src/main/java/com/jogamp/hungryharry/FeedAggregator.java @@ -3,6 +3,7 @@ */ package com.jogamp.hungryharry; +import com.jogamp.hungryharry.Config.Feed; import com.jogamp.hungryharry.Config.Planet; import com.sun.syndication.io.SyndFeedOutput; import com.sun.syndication.feed.synd.SyndEntry; @@ -29,6 +30,7 @@ import java.io.FileWriter; import java.io.Writer; import java.util.Comparator; import java.util.HashMap; +import java.util.Map; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -55,78 +57,21 @@ public class FeedAggregator { Config config = null; try { - Unmarshaller unmarshaller = JAXBContext.newInstance(Config.class).createUnmarshaller(); - Object obj = unmarshaller.unmarshal(getClass().getResourceAsStream("config.xml")); - config = (Config) obj; + config = readConfiguration(); } catch (JAXBException ex) { throw new RuntimeException("can not read configuration", ex); } - List<Config.Feed> feeds = config.feed; - - List<URL> urls = new ArrayList<URL>(); - for (Config.Feed feed : feeds) { - urls.add(new URL(feed.url)); - } - - - List<SyndEntry> entries = new ArrayList<SyndEntry>(); - - FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); - FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache); - - for (int i = 0; i < urls.size(); i++) { - try { - SyndFeed inFeed = feedFetcher.retrieveFeed(urls.get(i)); - entries.addAll(inFeed.getEntries()); - } catch (IOException ex) { - LOG.log(WARNING, "skipping feed", ex); - } catch (FetcherException ex) { - LOG.log(WARNING, "skipping feed", ex); - } catch (FeedException ex) { - LOG.log(WARNING, "skipping feed", ex); - } - } - - sort(entries, new Comparator<SyndEntry>() { - @Override - public int compare(SyndEntry o1, SyndEntry o2) { - return o2.getPublishedDate().compareTo(o1.getPublishedDate()); - } - }); + List<SyndEntry> entries = loadFeeds(feeds); Planet planet = config.planet; - String path = cutoffTail(planet.templatePath, separatorChar); - - for (Planet.PlanetFeed planetFeed : planet.feeds) { - - try { - SyndFeed feed = new SyndFeedImpl(); - feed.setFeedType(planetFeed.type); - - feed.setTitle(planet.title); - feed.setDescription(planet.description); - feed.setAuthor(planet.author); - feed.setLink(planet.link+separatorChar+planetFeed.getFileName()); - feed.setEntries(entries); - - SyndFeedOutput output = new SyndFeedOutput(); - - output.output(feed, new File(path+separatorChar+planetFeed.getFileName())); - - } catch (IOException ex) { - LOG.log(SEVERE, null, ex); - } catch (FeedException ex) { - LOG.log(SEVERE, null, ex); - } - } + String path = createAggregatedFeed(planet, entries); StringBuilder content = new StringBuilder(); - int max = 10; int n = 0; for (SyndEntry entry : entries) { - if(n++>max) { + if(n++ >= planet.maxEntries) { break; } String link = entry.getLink(); @@ -141,14 +86,17 @@ public class FeedAggregator { } } + generatePage(content, planet, path); + } + + private void generatePage(StringBuilder content, Planet planet, String path) { - HashMap<String, Object> root = new HashMap<String, Object>(); + Map<String, Object> root = new HashMap<String, Object>(); root.put("content", content.toString()); root.put("planet", planet); root.put("feeds", planet.feeds); try { - Configuration cfg = new Configuration(); // Specify the data source where the template files come from. // Here I set a file directory for it: @@ -157,13 +105,9 @@ public class FeedAggregator { // but just use this: cfg.setObjectWrapper(new DefaultObjectWrapper()); Template temp = cfg.getTemplate("planet-template.html"); - - Writer writer = new FileWriter(new File(path+separator+"planet.html")); - - + Writer writer = new FileWriter(new File(path + separator + "planet.html")); temp.process(root, writer); writer.close(); - } catch (IOException ex) { LOG.log(SEVERE, null, ex); } catch (TemplateException ex) { @@ -171,6 +115,67 @@ public class FeedAggregator { } } + private String createAggregatedFeed(Planet planet, List<SyndEntry> entries) { + + String path = cutoffTail(planet.templatePath, separatorChar); + + for (Planet.PlanetFeed planetFeed : planet.feeds) { + try { + SyndFeed feed = new SyndFeedImpl(); + + feed.setFeedType(planetFeed.type); + feed.setTitle(planet.title); + feed.setDescription(planet.description); + feed.setAuthor(planet.author); + feed.setLink(planet.link + separatorChar + planetFeed.getFileName()); + feed.setEntries(entries); + + SyndFeedOutput output = new SyndFeedOutput(); + output.output(feed, new File(path + separatorChar + planetFeed.getFileName())); + + } catch (IOException ex) { + LOG.log(SEVERE, null, ex); + } catch (FeedException ex) { + LOG.log(SEVERE, null, ex); + } + } + return path; + } + + private List<SyndEntry> loadFeeds(List<Feed> feeds) throws IllegalArgumentException { + + FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); + FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache); + List<SyndEntry> entries = new ArrayList<SyndEntry>(); + + for (Config.Feed feed : feeds) { + try { + SyndFeed inFeed = feedFetcher.retrieveFeed(new URL(feed.url)); + entries.addAll(inFeed.getEntries()); + } catch (IOException ex) { + LOG.log(WARNING, "skipping feed", ex); + } catch (FetcherException ex) { + LOG.log(WARNING, "skipping feed", ex); + } catch (FeedException ex) { + LOG.log(WARNING, "skipping feed", ex); + } + } + + sort(entries, new Comparator<SyndEntry>() { + @Override + public int compare(SyndEntry o1, SyndEntry o2) { + return o2.getPublishedDate().compareTo(o1.getPublishedDate()); + } + }); + return entries; + } + + private Config readConfiguration() throws JAXBException { + Unmarshaller unmarshaller = JAXBContext.newInstance(Config.class).createUnmarshaller(); + Object obj = unmarshaller.unmarshal(getClass().getResourceAsStream("config.xml")); + return (Config) obj; + } + private String cutoffTail(String text, char cut) { return text.substring(0, text.lastIndexOf(cut)); } diff --git a/src/main/java/com/jogamp/hungryharry/config.xml b/src/main/java/com/jogamp/hungryharry/config.xml index 3b5294c..3868fd5 100644 --- a/src/main/java/com/jogamp/hungryharry/config.xml +++ b/src/main/java/com/jogamp/hungryharry/config.xml @@ -4,13 +4,15 @@ <planet title="JogAmp Streams" description="JogAmp Aggregated Feeds" author="Hungry Harry" + maxEntries="6" link="/home/mbien/streams"> <feed>atom_0.3</feed> <feed>rss_2.0</feed> <template>/home/mbien/streams/planet-template.html</template> </planet> - +<!-- <feed name="vimeo" url="http://vimeo.com/tag:jogl/rss"/> + --> <feed name="demoscenepassivist" url="http://gdata.youtube.com/feeds/base/users/DemoscenePassivist/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile"/> <template keyword="vimeo" idpattern="http://vimeo.com/([0-9]+)"> |