Created
February 16, 2011 04:05
-
-
Save KirinDave/828846 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 com.swipr.lib | |
| import dispatch._ | |
| import net.liftweb.json._ | |
| import java.net.URL | |
| package Google.ImageSearch { | |
| case class Result(url: String) | |
| case class SearchResponse(results: List[Result]) | |
| case class SearchEnvelope(responseData: SearchResponse, | |
| responseDetails: Option[Map[String,Any]], | |
| responseStatus: Int) | |
| } | |
| object GoogleImageFetcher { | |
| import Google.ImageSearch._ | |
| lazy val http = new Http | |
| val endpoint = :/("ajax.googleapis.com") / | |
| "ajax" / | |
| "services" / | |
| "search" / | |
| "images" | |
| def requestForImages(q: String) = | |
| endpoint <<? Map("v" -> "1.0", | |
| "q" -> q) secure | |
| def queryResults(q: String): Option[SearchEnvelope] = { | |
| implicit val formats = DefaultFormats | |
| http(requestForImages(q) >- | |
| { (body) => | |
| JsonParser.parse(body).extractOpt[Google.ImageSearch.SearchEnvelope] | |
| }) | |
| } | |
| def apply(q: String): List[URL] = { | |
| queryResults(q) match { | |
| case None => List() | |
| case Some(x) => x.responseData.results.map( (r) => new URL(r.url) ) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment