Created
July 19, 2014 02:14
-
-
Save piyo7/7797d99f99fd9ed3f9ff to your computer and use it in GitHub Desktop.
Simple Hatebu Parser for Atom (XML)
This file contains hidden or 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
sbt.version=0.13.2 |
This file contains hidden or 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
name := "hatebuParser" | |
version := "1.0" | |
scalaVersion := "2.11.1" | |
libraryDependencies ++= Seq( | |
"org.scala-lang.modules" %% "scala-xml" % "1.0.2" | |
) |
This file contains hidden or 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 java.text.SimpleDateFormat | |
import java.util.Date | |
import scala.xml.Node | |
import scala.xml.XML | |
object HatebuParser { | |
def main(args: Array[String]) { | |
if (args.isEmpty) println("Needed: filePath") | |
val filePath = args(0) | |
val xml = XML.load(filePath) | |
val bookmarks = (xml \ "entry").map(Bookmark.parseAtom(_)) | |
bookmarks.take(10).foreach(println(_)) | |
} | |
} | |
case class Bookmark(title: String, link: String, date: Date) | |
object Bookmark{ | |
def parseAtom(node: Node): Bookmark = { | |
Bookmark( | |
(node \ "title").head.text, | |
((node \ "link"). | |
filter(node => (node \ "@rel").head.text == "related"). | |
head \ "@href").head.text, | |
iso8601Format.parse((node \ "issued").head.text)) | |
} | |
private val iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX") | |
} |
This file contains hidden or 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
<entry> | |
<id>tag:hatena.ne.jp,2005:bookmark-dummyuser-123456789</id> | |
<title>gist.github.com</title> | |
<link type="text/html" rel="related" href="https://gist.github.com/"/> | |
<link type="text/html" rel="alternate" href="http://b.hatena.ne.jp/dummyuser/20050101#bookmark-123456789"/> | |
<link type="application/x.atom+xml" rel="service.edit" title="gist.github.com" href="http://b.hatena.ne.jp/atom/edit/123456789"/> | |
<summary></summary> | |
<issued>2005-01-01T01:01:01+09:00</issued> | |
<author> | |
<name>dummyuser</name> | |
</author> | |
</entry> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment