Skip to content

Instantly share code, notes, and snippets.

@glenn-kim
Created March 13, 2014 11:06
Show Gist options
  • Save glenn-kim/9526425 to your computer and use it in GitHub Desktop.
Save glenn-kim/9526425 to your computer and use it in GitHub Desktop.
Crawling web and Response Sample on Playframework2.
package controllers
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.mvc._
import play.api.libs.ws._
import play.api.templates.HtmlFormat
/**
* Created by infinitu on 2014. 3. 13..
*/
object Crawler extends Controller{
def crawl(url:String) = Action.async { request =>
WS.url("http://"+url).get().map(i=>
Ok(i.body) withCookies Cookie("reqUrl",url) as HTML) }
def resCrawl(a:Any)= Action.async { request =>
WS.url("http://"+request.cookies.get("reqUrl").get.value+request.path)
.withHeaders("Accept"->request.acceptedTypes(0).toString())
.get().map(i=>Ok(HtmlFormat.raw(i.body)) as i.header("content-type").getOrElse("*/*"))
}
}
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET /:url controllers.Crawler.crawl(url)
# Map static resources from the /public folder to the /assets URL path
GET /*a controllers.Crawler.resCrawl(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment