fijimf / playfij

Experimenting with play framework
2 stars 0 forks source link

ncaa game scraping #34

Closed fijimf closed 11 years ago

fijimf commented 11 years ago

package junk import scala.util.control.Exception. import play.api.libs.json.

object Stuff { def hello() { println("Hello") }

def loadFile(fileName:String):String = { scala.io.Source.fromFile(fileName).mkString }

def ripJson[T](json:String, f:%28JsValue=>T%29):List[T] = { val games = ((Json.parse(stripCallbackWrapper(json)) \ "scoreboard") (0) \ "games").validate[JsArray] match { case JsSuccess(g,) => g.asInstanceOf[JsArray].value.toList case => { println("Error") List.empty } } games.map(f)
}

def ripGames(json:String):List[(String, String, Option[(Int, Int)])] = { ripJson(json, ripGameResult).flatten }

val ripTwitters:((String, JsValue)=>Option[(String, String)]) = {case (ha, j) => val t = (j \ ha \ "nameSeo").asOpt[String] val bb = (j \ ha \ "social" \ "twitter" \ "accounts" \ "sport").asOpt[String] val ad = (j \ ha \ "social" \ "twitter" \ "accounts" \ "athleticDept").asOpt[String] val cf = (j \ ha \ "social" \ "twitter" \ "accounts" \ "conference").asOpt[String](t, bb.orElse%28ad%29.orElse%28cf%29) match { case (Some(team), Some(twitter)) => Some(team->twitter) case _ => None } }

def ripTwitterMap(json:String):Map[String,String] = { (ripJson(json, ripTwitters("home",)).flatten ++ ripJson(json, ripTwitters("home",)).flatten).toMap }

def ripColors(json:String):Map[String,String] = { Map.empty[String, String] }

val ripGameResult:(JsValue=>Option[(String, String, Option[(Int, Int)])]) = {j=> val result = ( teamData(j,"home","currentScore").flatMap(x=>catching(classOf[NumberFormatException]) opt x.toInt), teamData(j,"away","currentScore").flatMap(x=>catching(classOf[NumberFormatException]) opt x.toInt) ) match { case (Some(h),Some(a)) => Some(h.asInstanceOf[Int],a.asInstanceOf[Int]) case _ => None }

 val teams = (teamData(j,"home","nameSeo"), teamData(j,"away","nameSeo")) match {
    case (Some(h),Some(a)) => Some( h.toString, a.toString, result)
    case _ => None
 }
 teams   

}

def teamData(game:JsValue, homeOrAway:String, item:String):Option[String] = { (game \ homeOrAway \ item).asOpt[String] }

def stripCallbackWrapper(json:String):String = { json.replaceFirst( """^callbackWrapper({""", "{").replaceFirst( """});$""", "}") } }