Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created October 23, 2009 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuroyoro/216843 to your computer and use it in GitHub Desktop.
Save yuroyoro/216843 to your computer and use it in GitHub Desktop.
import scala.xml._
import scala.io.Source
object LDRFeedSummary {
val opmlUrl = "http://reader.livedoor.com/user/%s/opml"
val r = """http\:\/\/([^\/]+)\/.*$""".r
// まとめるドメイン
val topLevelDomain = List("fc2.com",
"blogspot.com",
"g.hatena.ne.jp",
"tumblr.com",
"exblog.jp",
"justblog.jp",
"blog.so-net.ne.jp",
"ameba.jp",
"sakura.ne.jp",
"seesaa.net",
"cocolog-nifty.com")
def getDomain( domain:String) =
topLevelDomain.find( domain.endsWith( _ )).getOrElse( domain )
def main( args:Array[String] ) = {
val url = opmlUrl.format( args.first )
val source = Source.fromURL( url )
val xml = XML.loadString( source.getLines.mkString )// XML取得
val feeds = xml \ "body" \ "outline" \ "outline" \ "outline"
val totalCount:Double = feeds.size
(Map.empty[String, Int]/: feeds)(
(m:Map[String, Int] , feed:NodeSeq) => {
(feed \ "@xmlUrl" text ) match {
case r( domain ) =>
val d = getDomain( domain )
m + ( d -> (m.getOrElse( d , 0) + 1 ))
case _ => m
}
}
).toList.partition{
case (k, v)=> v > 1
} match {
case (l1, l2 ) =>
l1.sort{
case ((k1, v1), (k2, v2)) => v1 > v2
}.foreach{
case (k, v) =>
println( "%-30s : %3d ( %2.2f )".format( k, v , (v / totalCount * 100) ))
}
println( "%-30s : %3d ( %2.2f )".format( "Other", l2.size , (l2.size / totalCount * 100) ))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment