Thursday, January 25, 2007

PowerShell and Amazon Web Services (AWS) - part 2

In part 1 I blogged about getting the current sale price for a book on Amazon.com.

I've slightly extended the script to now get me the Amazon price, as well as any external parties selling them based on classifications of "new", "used", and "collectible".  I'm not sure how the "new" category can be used, but it must be external parties who've actually received books directly from the publisher.

I've consulted the Amazon E-Commerce Service (ECS) SDK to get the XML tags I need.  Its cool that I can directly get the lowest price instead of having to iterate through entries.

I'm still looking up Microsoft Windows PowerShell Programing for the Absolute Beginner.

Here's my code for lookup_prices.ps1:

$asin=$args[0]

$url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService"
$url += "&AWSAccessKeyId=enter_your_aws_key_here"
$url += "&Operation=ItemLookup"
$url += "&ItemId=$asin"
$url += "&ResponseGroup=Offers"

$rxml = [xml](new-object Net.WebClient).DownloadString("$url")
$amazon_price = $rxml.ItemLookupResponse.Items.Item.Offers.Offer.OfferListing.Price.Amount
$amazon_price = $amazon_price/100
write-host "Amazon price: $amazon_price"
$lowest_new_price = $rxml.ItemLookupResponse.Items.Item.OfferSummary.LowestNewPrice.Amount
$lowest_new_price = $lowest_new_price/100
write-host "Lowest new price: $lowest_new_price"
$lowest_used_price = $rxml.ItemLookupResponse.Items.Item.OfferSummary.LowestNewPrice.Amount
$lowest_used_price = $lowest_used_price/100
write-host "Lowest used price: $lowest_used_price"
$lowest_collectible_price = $rxml.ItemLookupResponse.Items.Item.OfferSummary.LowestNewPrice.Amount
$lowest_collectible_price = $lowest_collectible_price/100
write-host "Lowest collectible price: $lowest_collectible_price"

[end lookup_prices.ps1]

So, you simply call it as .\lookup_prices.ps1 some_ISBN.

PSH> .\lookup_prices.ps1 1598633546
Amazon price: 19.79
Lowest new price: 18.65
Lowest used price: 18.65
Lowest collectible price: 18.65

There, I now have the lowest available price for the book.

I'll need that to determine my asking price...