Skip to content

Instantly share code, notes, and snippets.

@shomah4a
Created June 26, 2011 14:49
Show Gist options
  • Save shomah4a/1047664 to your computer and use it in GitHub Desktop.
Save shomah4a/1047664 to your computer and use it in GitHub Desktop.
rss 読んでみた
import scala.io
import scala.xml
import scala.xml.parsing
import scala.xml.parsing.NoBindingFactoryAdapter
import nu.validator.htmlparser
import org.xml.sax
/// HTML からノードに変換
def to_node(str: String): xml.Node = {
val hp = new htmlparser.sax.HtmlParser
hp.setNamePolicy(htmlparser.common.XmlViolationPolicy.ALLOW)
val saxer = new parsing.NoBindingFactoryAdapter
hp.setContentHandler(saxer)
hp.parse(new sax.InputSource(new java.io.StringReader(str)))
saxer.rootElem
}
/// ストーリの情報を持っておく
case class Story(val title: String, url: String, section: String, description: String, date: String, comments: Int)
{}
/// 属性を取ってくる。もしかしたらエラー出るかもねーな感じ
def get_attribute(node: xml.Node, attr: String): String = {
node attribute attr map {_(0).text} get
}
/// 子ノードからテキストを取ってくる
def get_child_text(node: xml.Node, tag: String) =
node \ tag text
/// 子ノードから数値を取ってくる。空文字列なら 0
def get_child_int(node: xml.Node, tag: String) = {
val x: String = node \\ tag text;
if (x isEmpty)
{
0
}
else
{
x toInt
}
}
/// rss を読んでみる
def load_rss(url: String): Seq[Story] = {
val node = xml.XML.loadString(get_url(url)).toList(0)
node \\ "item" map (n=>{
Story(title=get_child_text(n, "title"),
url=get_child_text(n, "link"),
section=get_child_text(n, "slash:section"),
description=get_child_text(n, "description"),
date=get_child_text(n, "dc:date"),
comments=get_child_int(n, "slash:comments"))
})
}
load_rss("http://rss.rssad.jp/rss/slashdot/slashdot.rss") foreach println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment