public
Created

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import net.liftweb.json._
 
object Challenge117 {
case class RawPost(title: String, permalink: String)
case class Post(difficulty: String, id: String, title: String, permalink: String) {
override def toString = "[" + difficulty + "]" + " #" + id + ": " + "\"" + title + "\"" + " " + permalink
}
val dailyJson = io.Source.fromURL("http://www.reddit.com/r/dailyprogrammer/.json?limit=30").mkString
implicit val formats = DefaultFormats
val mapped = (parse(dailyJson) \ "data" \ "children" \ "data")
.extract[List[RawPost]]
.filterNot(_.title.contains("MOD POST"))
val difficulties = List("Easy", "Intermediate", "Difficult")
val posts = mapped.map { post =>
val splitted = post.title.split("Challenge #").tail.mkString
val id = splitted.split(" ").head
val difficulty = difficulties.collectFirst { case(d) if splitted contains(d) => d }.getOrElse("None")
val title = splitted.split("""\[""" + difficulty + """\] """).last
Post(difficulty, id, title, post.permalink)
}
def main(args: Array[String]): Unit = {
val listOne = posts.sortBy(_.id).mkString("\n")
val listTwo = posts.sortBy(_.difficulty).mkString("\n")
println(listOne)
println()
println(listTwo)
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.