Created
March 13, 2014 11:06
-
-
Save glenn-kim/9526425 to your computer and use it in GitHub Desktop.
Crawling web and Response Sample on Playframework2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("*/*")) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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