Created
April 22, 2009 04:44
-
-
Save alandipert/99585 to your computer and use it in GitHub Desktop.
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 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