PowerShell and Amazon Web Service

I got some experiences with Amazon Web Service when I created my Anime Reviews site. It’s pretty neat that you can write a query to look up very detailed product information. Recently, I am thinking to buy a Garmin StreetPilot i3 from Amazon.

i3.jpg

The regular price is around $308.00, but I heard that sometimes Amazon would lower the price to as low as $199.00. However, the low price is only available for a short time. I don’t want to sit in front of my computer and manually check the price myself. Wouldn’t it be nice if I can have a script to check the price automatically? It would be nicer if I write the script using PowerShell!

Ok, enough rambling. Let’s get started. To use Amazon Web Service, the first thing to do is to create an account and obtain your own Access Key ID. You can create a free account here.

After the account has been created successfully, you can log on to the site with your new account. Move the mouse cursor on top of the button that says “Your Web Services Account”. A drop down menu would appear, click “View Access Key Identifiers”. There are two keys, one is called “Access Key ID”, the other is called “Secret Access Key”. Make a note of the Access Key ID, this is the key you used for the web service. The next step is sending the Secret Access Key to me. ๐Ÿ™‚ Ok, just kidding. You should NOT share Secret Access Key with anyone, it’s only used to create signature. You should keep it confidential and not reveal it to anyone.

Amazon Web Service offers several ways to find product information. ItemSearch is used to search products by using keywords. Since we already know the product that we are interested in, we can just use ItemLookup. To use ItemLookup, you need two pieces of information, AWSAccessKeyId and ItemId. You already have the Access Key ID, all you need to know is the ItemId for the product. Each product offered on Amazon has a unique ID. If you look at a product page, it’s called ASIN. You use ASIN as ItemId for the query. For books query, use ISBN as ItemId in your query. This is the query string for looking up information for the Garmin i3.

$url =  "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService"

$url += "&AWSAccessKeyId=Your_AccessKeyID_Here"

$url += "&Operation=ItemLookup"

$url += "&ItemId=B000ACHVVE"

$url += "&ResponseGroup=Offers"

Note that this is for North America www.amazon.com. If you want to query product information for other Amazon sites. You need to change the first line accordingly. You can check here for information on how to modify the query string. Another point to note is that if you don’t specify ResponseGroup in your query, the default response is “Request” and “Small” which don’t have the price information. To get the current price, we use “Offers” as response group. Finally, we use .Net’s Net.WebClient class to get the response from Amazon Web Service. The response from the server is an xml string. We can cast it to XMLDocument for easy accessing the data. The amount returned from the query is in cents, so divide it by 100 to get the dollar amount.

$rxml = [xml](new-object Net.WebClient).DownloadString("$url")

$price =  $rxml.ItemLookupResponse.Items.Item.Offers.Offer.OfferListing.Price.Amount

$price = $price/100

echo $price

You might ask how did you come up with the offer price? You can check the documentation in Amazon Web Service web site, or you can save the response as an XML file and use your favorite XML editor/reader to examine the response. FireFox is just fine to parse the XML file.

$sxml = (new-object Net.WebClient).DownloadString("$url")

echo $sxml > "c:\\response.xml"

I tested the script under Vista Beta 2 and it works well.


This post may contain affiliated links. When you click on the link and purchase a product, we receive a small commision to keep us running. Thanks.


Be the first to comment

Leave a Reply