Skip to content

Instantly share code, notes, and snippets.

@fernandoacorreia
Created November 20, 2012 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandoacorreia/4117974 to your computer and use it in GitHub Desktop.
Save fernandoacorreia/4117974 to your computer and use it in GitHub Desktop.
Improved XWiki RSS Aggregator Macro
{{groovy}}
import java.util.TreeMap;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import org.apache.commons.codec.binary.Base64
def urls = [];
def entries = new TreeMap();
xcontext.macro.content.eachLine() { urls.add(it.trim()); }
urls.each() { rssUrl ->
try {
URL url = new URL(rssUrl);
URLConnection urlConnection = url.openConnection();
if (url.getUserInfo() != null) {
String basicAuth = "Basic " + new String(new Base64().encode(url.getUserInfo().getBytes()));
urlConnection.setRequestProperty("Authorization", basicAuth);
}
InputStream inputStream = urlConnection.getInputStream();
def feed = new SyndFeedInput().build(new XmlReader(inputStream));
feed.entries.each() { entries.put(it.publishedDate, ["entry":it, "feed": feed]); }
} catch(Exception e) {
println "There was an error while trying to retrieve RSS feed."
// Enable for debugging:
// println e.getMessage()
}
}
def mysize = entries.keySet().size()
if (mysize >= 1) {
println """|=Title|=Date|=Author|=Feed"""
(entries.keySet() as List).reverse()[0..mysize-1].each() {
def e = entries.get(it).entry, f = entries.get(it).feed;
print """|[[{{{${e.title}}}}>>${e.link}]]|{{{${xwiki.formatDate(e.publishedDate)}}}}|"""
println """{{{${e.author}}}}|[[{{{${f.title}}}}>>${f.link}]]"""
}
}
{{/groovy}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment