Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created April 22, 2009 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandipert/99585 to your computer and use it in GitHub Desktop.
Save alandipert/99585 to your computer and use it in GitHub Desktop.
package com.thinkminimo.booksearch;
import java.net._;
import scala.io._;
import scala.xml._ ;
import scala.xml.parsing._ ;
class AmazonResultPage {
val awskey = "02KVYA5XBEP8PMG4HAR2"
var awsurl = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId="+awskey+"&Operation=ItemSearch&SearchIndex=Books"
var books = List[Book]()
private def encodeURL(url: String):String = {
URLEncoder.encode(url, "utf-8");
}
private def param(name:String, value:String):AmazonResultPage = {
awsurl += "&"+name+"="+encodeURL(value)
return this
}
def byTitle(t:String):AmazonResultPage = {
param("Title", t)
}
def fetch():List[Book] = {
(ConstructingParser.fromSource(Source.fromURL(awsurl), false)
.document()
.docElem \\ "Item")
.toList
.map((item:Node) => {
new Book(
(item \ "ItemAttributes" \ "Title").text,
(item \ "ASIN").text
)
});
}
def get():AmazonResultPage = {
books = fetch()
return this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment