Created
March 5, 2009 23:03
-
-
Save agentcoops/74628 to your computer and use it in GitHub Desktop.
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
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