Created
November 20, 2012 13:37
-
-
Save fernandoacorreia/4117974 to your computer and use it in GitHub Desktop.
Improved XWiki RSS Aggregator Macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{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