Skip to content

Instantly share code, notes, and snippets.

@nergal
Created January 3, 2012 13:33
Show Gist options
  • Save nergal/1554897 to your computer and use it in GitHub Desktop.
Save nergal/1554897 to your computer and use it in GitHub Desktop.
import java.net.{URLConnection,URL}
import scala.xml._
import scala.collection.mutable.Buffer
case class Feed(val url: String) {
var buff: Elem = {
val u = new URL(this.url)
val conn = u.openConnection()
conn.connect
XML.load(conn.getInputStream)
}
private def nice(str: Elem): String = {
var res: Buffer[String] = Buffer()
for (item <- str\"channel"\"item") {
val Array(cat, title) = (item\"title").last.text.split("/", 2).map(_ trim)
val link = (item\"link").last.text
var rec: String = "Category: %s\nTitle: %s\nLink: %s\n\n".format(cat, title, link)
res+= rec
}
res.mkString
}
override def toString(): String = {
this.nice(buff)
}
}
val xml = new Feed("http://habrahabr.ru/rss/best/")
println(xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment