fijimf / fijbook2

Other
0 stars 0 forks source link

Yikes #2

Open fijimf opened 9 years ago

fijimf commented 9 years ago
package com.jpmorgan.spg.spray.fijimf

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.event.Logging
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import spray.can.Http
import spray.http.HttpMethods._
import spray.http.MediaTypes._
import spray.http.{HttpRequest, HttpResponse, Uri}
import spray.routing._

import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
;

object Main2 {

  def main(args: Array[String]): Unit = {

    System.setProperty("http.proxyHost", "proxy.jpmchase.net")
    System.setProperty("http.proxyPort", "8080")

    implicit val system = ActorSystem("on-spray-can")
    //        val recorder = system.actorOf(Props[Recorder], "recorder-actor")
    val service = system.actorOf(Props(classOf[MainServiceActor]), "demo-service")
    implicit val timeout = Timeout(5.seconds)
    // start a new HTTP server on port 8080 with our service actor as the handler
    IO(Http) ? Http.Bind(service, interface = "localhost", port = 8080)
    val disc = system.actorOf(Props(classOf[Discovery2Actor]), "loadTeamNames")

    disc ! new Character('g')

    // val discoveryActor = system.actorOf(Props(classOf[DiscoveryActor], recorder), "discovery-actor")
    //  system.scheduler.schedule(5.minutes, Duration(2, TimeUnit.MINUTES), discoveryActor, DiscoveryService("http://mcc-bond-drapp43.svr.us.jpmchase.net:1948/details?section=Core+Platform+Services%3A+Security+Server+v3j"))
  }
}

class MainServiceActor2() extends Actor with HttpService {
  def actorRefFactory = context

  def receive = runRoute(rte)

  private val staticAssets: Route = pathPrefix("assets") {
    path("css" / PathMatchers.Segment) { filename =>
      get {
        respondWithMediaType(`text/css`) {
          getFromResource("css/" + filename, `text/css`)
        }
      }
    } ~ path("js" / PathMatchers.Segment) { filename =>
      get {
        respondWithMediaType(`application/javascript`) {
          getFromResource("js/" + filename, `application/javascript`)
        }
      }
    } ~ path("img" / PathMatchers.Segment) { filename =>
      get {
        respondWithMediaType(`image/png`) {
          getFromResource("img/" + filename, `image/png`)
        }
      }
    }
  }

  val rte =
    path("fijimf") {
      get {
        respondWithMediaType(`text/html`) {
          // XML is marshalled to `text/xml` by default, so we simply override here
          complete {
            <html>
              <body>
                <h1>Say hello to
                  <i>spray-routing</i>
                  on
                  <i>spray-can</i>
                  !</h1>
              </body>
            </html>
          }
        }
      }
    } ~ staticAssets
}

import org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl
import org.xml.sax.InputSource

import scala.xml._
import scala.xml.parsing.NoBindingFactoryAdapter

class Discovery2Actor() extends Actor {
  val log = Logging(context.system, this)
  var instanceActors = Map.empty[String, ActorRef]

  val handleResponse: (Try[HttpResponse]) => Unit = {
    case Success(resp) => Try {
      HTML.loadString(resp.entity.data.asString)

    } match {
      case Success(htmlNode) =>
        val seq: NodeSeq = htmlNode \\ "li" \ "a"
        println(seq.size)
        val filter: NodeSeq = seq.filter(_.attribute("href").flatMap(_.headOption).exists(_.text.startsWith("/schools/")))
        val confMap = filter.map(n=>{
          val id = n.attribute("href").flatMap(_.headOption).map(_.text.replace( "/schools/",""))
          val name: String = (n \ "span").text
          id -> name
        }).filter(tup=> tup._1.isDefined && tup._1.get.length>1).map(tup=>tup._1.get->tup._2)
        confMap.foreach(n => println(n))

      case Failure(ex) =>
        log.warning("Failed parsing discovery service response")
        log.warning("Response was: " + resp.entity.data.toString)
    }
    case Failure(ex) => log.error("Failed to retrieve instances from Discovery Service")
  }

  override def receive: Receive = {
    case c:Character =>
      implicit val timeout: Timeout = Timeout(15.seconds)
      implicit val system: ActorSystem = context.system
      import scala.concurrent.ExecutionContext.Implicits.global
      val response = (IO(Http) ? HttpRequest(GET, Uri("http://www.ncaa.com/schools/"+c))).mapTo[HttpResponse]
      response.onComplete(handleResponse)
    case _ => log.warning("Unrecognized message")
  }

}
fijimf commented 9 years ago

import org.joda.time.DateTime

case class Ncaa(teams: List[Team] = List.empty, conferences: Set[Conference] = List.empty, schedules: List[Schedule] = List.empty)

case class Team(id: Long, keyName: String, name: String, nickname: String, extras: TeamExtras)

case class TeamExtras(longName: Option[String], color1: Option[String], color2: Option[String], logoURLBig: Option[String], logURLSmall: Option[String], websit: Option[String], twitter: Option[String], instagram: Option[String], facebook: Option[String])

case class Conference(id: Long, keyName: String, name: String)

case class Schedule(id: Long, academicYear: Int, name: String, conferenceMap: Map[Team, Conference], games: List[Game])

object Schedule {
  def apply(year: Int) = Schedule(year, year, year + "-" + (year + 1), Map.empty[Team, Conference], List.empty[Game])
}

case class Game(datetime: DateTime, home: Team, away: Team, isNeutral: Boolean, result: Option[Result], extras: GameExtras)

case class Result(homeScore: Int, awayScore: Int, ots: Int = 0)

case class GameExtras(isConfTournament: Option[Boolean], isNcaaTournament: Option[Boolean], location: Option[String], attendence: Option[String], boxScore: Option[BoxScore])

case class BoxScore(x: String = "TODO")

// TODO

object Ncaa {
  def main(args: Array[String]) {
    List(2012, 2013, 2014, 2015).foreach(yr => {
      val s = Schedule(yr)
      val confs = loadConferences(yr)
      confs.foreach(c=>{
        val teams = loadTeams(yr,c)
        teams.map(_->c)
      })

    })
  }
}