Skip to content

Instantly share code, notes, and snippets.

@KirinDave
Created February 16, 2011 04:05
Show Gist options
  • Select an option

  • Save KirinDave/828846 to your computer and use it in GitHub Desktop.

Select an option

Save KirinDave/828846 to your computer and use it in GitHub Desktop.
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