Skip to content

Instantly share code, notes, and snippets.

@agentcoops
Created March 5, 2009 23:03
Show Gist options
  • Save agentcoops/74628 to your computer and use it in GitHub Desktop.
Save agentcoops/74628 to your computer and use it in GitHub Desktop.
import scala.xml._
import java.net.{URLConnection, URL}
class RSSRead {
def getFeed(feedUrl: String) = {
val url = new URL(feedUrl)
val connect = url.openConnection
val feed = XML.load(connect.getInputStream)
feed \\ "channel" \\ "item"
}
def handleItem(item: List[scala.xml.Node], accum: Map[String, String]): Map[String, String] = item match {
case List() => accum
case hd::tail => hd match {
case <title>{title}</title> => handleItem(tail, accum.+(("title", title.toString)))
case <link>{link}</link> => handleItem(tail, accum.+(("link", link.toString)))
case <pubDate>{date}</pubDate> => handleItem(tail, accum.+(("date", date.toString)))
case <description>{desc}</description> => handleItem(tail, accum.+(("desc", desc.toString)))
case _ => handleItem(tail, accum)
}
}
def handleFeed = {
val curried = getFeed("http://tvrss.net/feed/combined").foldLeft(List[Map[String,String]]())_
curried((y: List[Map[String,String]], x:scala.xml.Node) =>
handleItem(x.child.toList, Map[String,String]())::y)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment