~statianzo/sevenlangs

864aab29fe6e6f9246b78fa8cb66359c5aa01767 — Jason Staten 2 years ago 4af35a5
anchor tag count
1 files changed, 12 insertions(+), 7 deletions(-)

M scala/src/main/scala/day3/LoadPages.scala
M scala/src/main/scala/day3/LoadPages.scala => scala/src/main/scala/day3/LoadPages.scala +12 -7
@@ 7,28 7,34 @@ import akka.util.Timeout;
import scala.io.Source
import scala.concurrent.duration._;
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.xml.XML;


case class Get(url : String)

object Fetch {
  def getPageSize(url: String) = Source.fromURL(url).mkString.length()

  def getLinks(url: String) = {
    val content = Source.fromURL(url).mkString;
    "<a".r.findAllMatchIn(content).length
  }
}

class PageLoader extends Actor {

  def receive: Actor.Receive = {
    case Get(url) => sender ! Fetch.getPageSize(url)
    case Get(url) => sender ! Fetch.getLinks(url)
  }
}

object LoadPages extends App {
  val system = ActorSystem("ActOne")
  val urls = List(
    "http://www.amazon.com",
    "http://jxs.me",
    "http://www.example.com",
    "http://sdf.org"
    "https://www.orbit.fm",
    "https://jxs.me",
    "https://www.example.com",
    "https://sdf.org"
  )




@@ 41,7 47,7 @@ object LoadPages extends App {

  def getPageSizeSequentially() = {
    for (url <- urls) {
      println("Size for " + url + ": " + Fetch.getPageSize(url))
      println("Links for " + url + ": " + Fetch.getLinks(url))
    }
  }



@@ 59,5 65,4 @@ object LoadPages extends App {

  timeMethod { getPageSizeSequentially }
  timeMethod { getPageSizeConcurrently }

}