Advertisement
Guest User

Untitled

a guest
Mar 14th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.09 KB | None | 0 0
  1. // Example Scraper 1
  2.  
  3. val result: Future[Boolean] = scrape { implicit context =>
  4.  
  5.   get(virgin.loginPage) andThen { implicit context =>
  6.  
  7.       postForm(virgin.loginForm(username, password)) asHTML { implicit context =>
  8.         doc =>
  9.  
  10.           val samlResponse = doc.$("#acsForm input[name=SAMLResponse]").value;
  11.           val relayState = doc.$("#acsForm input[name=RelayState]").value;
  12.  
  13.           postForm(virgin.acsForm(samlResponse, relayState)) andThen { implicit context =>
  14.  
  15.               self ! UpdateState(context)
  16.               complete(true)
  17.           }
  18.       }
  19.   }
  20. }
  21.  
  22. // Example Scraper 2
  23.  
  24. val result: Future[Option[BillDetails]] = scrapeWithContext(scrapingContext) { implicit context =>
  25.  
  26.   get(virgin.home) asHTML { implicit context =>
  27.     doc =>
  28.  
  29.       val totals = doc.$(".total").map(_.text)
  30.       totals match {
  31.         case billAmount :: balanceAmount :: Nil => complete(Some(BillDetails(billAmount, balanceAmount)))
  32.         case _ =>
  33.  
  34.           after(2 seconds) {
  35.  
  36.             get(virgin.billingSnippet) asHTML { implicit context =>
  37.               doc =>
  38.  
  39.                 val totals = doc.$(".total").map(_.text)
  40.  
  41.                 totals match {
  42.                   case billAmount :: balanceAmount :: Nil => complete(Some(BillDetails(billAmount, balanceAmount)))
  43.                   case _                                  => complete(None)
  44.                 }
  45.             }
  46.           }
  47.       }
  48.   }
  49. }
  50.  
  51. // Example Collector
  52.  
  53. spawn[VirginMediaScraper] { scraper =>
  54.  
  55.   scraper ? Login("mathewsonkt@hotmail.com", "Ovoaccess1") map {
  56.     case LoggedIn =>
  57.       log.info(s"Logged In")
  58.  
  59.       collect(scraper ? GetBillDetails, scraper ? GetPersonalDetails) {
  60.         case (billDetails: BillDetails, personalDetails: PersonalDetails) =>
  61.           log.info(s"${billDetails.toString} ${personalDetails.toString}")
  62.           scraper ! PoisonPill
  63.          
  64.         case _ => log.info("Unable to get the bill and personal details")
  65.       }
  66.  
  67.     case NotLoggedIn => log.info(s"Not Logged In")
  68.   } recover {
  69.     case ex: Exception => log.error(ex, "Unable to login")
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement