Skip to content

Instantly share code, notes, and snippets.

@alternegro
Last active December 15, 2015 14:49
Show Gist options
  • Save alternegro/5277247 to your computer and use it in GitHub Desktop.
Save alternegro/5277247 to your computer and use it in GitHub Desktop.
package com.alternegro
import java.util.Date
import java.util.Calendar
import java.util.GregorianCalendar
import scala.xml._
import scala.collection.JavaConversions._
object MoviesParser extends App {
case class CastMember(role: String, firstName: String, lastName: String)
case class Movie(title: String, description: String, rating: String, cast:List[CastMember]=Nil)
val identity = {x:String => x}
def createMovie(node: Node) = {
val tokenArray = Array(("title", identity), ("desc", identity), ("rating", identity), ("cast", identity)).map(t => nodeText(node, t._1, t._2))
Movie(tokenArray(0), tokenArray(1), tokenArray(2))
}
def nodeText(p: Node, key:String, valueTransformer:String => String): String = {
try {
valueTransformer(Option((p \\ key).head.text).getOrElse("missing"))
} catch {
case e: Exception => "missing"
}
}
def extractMovies(xml:scala.xml.Elem) = {
(xml \\ "program").filter { _ \\ "@ratingsBody" exists (_.text == "Motion Picture Association of America") }.map(createMovie(_))
}
val moviesXml = scala.xml.XML.loadFile(args.head)
val movies = extractMovies(moviesXml)
println(movies.size, movies.last.title, movies.last.description, movies.last.rating, movies.last.cast)
for( ln <- io.Source.stdin.getLines ) println( ln )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment