Three Is It

Because two isn't enough and four is just too many

It is frightening how the actions of a single leader can have such drastic effects on the prestige of an entire nation.
Ramman Kenoun
Home Blogs Genealogy Brad's Bookshelf Subscriptions Contact Sign in
 

About the author

Brad Butts is a .NET developer and architect. He is married with children and enjoys reading, working out, and genealogy is his five minutes of spare time.
E-mail me Send mail
National Debt Clock

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

A Printable Amazon Wishlist

I maintain several Amazon wishlists broken out by the particular topics in which I’m interested.  These wishlists have become the central repository for most/all things (not just books) I’m contemplating purchasing in the future—already, you can buy a great many things from Amazon or their affiliates, but their cool Universal Wishlist Widget kind of seals the deal.

But I have a problem: whenever I visit my favorite book store, my wishlists out on the tubes are useless.  I need to be able to print my lists. 

As far as I can tell, there’s no easy way to do this.  Sure, I can print each page from my browser—I can navigate to page 1, hit the print button in my browser, then navigate to page 2, rinse and repeat.  But that’s lame and time-consuming.  Plus, it would print lots of information and pictures that I don’t need—and needlessly kill my ink cartridge.  Ideally, I’d like to just be able to print a list of the books and authors on my lists.  Even better, I’d like to sort my list alphabetically by the last name of the author since that’s how book stores order their books.  So, here’s how I solved my problem:

Requirements:
Microsoft PowerShell (free)
A free Amazon Web Service account
XML, XSLT, and your favorite browser (my script uses IE)

Step 1: Construct the AWS (Amazon Web Service) url and do my service calls

I’m using PowerShell as the primary engine to do all these operations.  I could have written C# code or the like to perform these tasks, but using PowerShell seemed a little slicker.

Figuring out how to construct the web service URL would have been tough were it not for this article on pulling back an XML representation of one’s wishlist.  I also should credit this article for letting me borrow the PowerShell script they used to call the AWS service.  (Alternatively, I could have scraped my wishlist to get the data I need.  Here’s a hilarious article on someone doing just that—to quasi-subversive ends.)  Here’s my URL syntax:

$urlTemplate =  "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService"
$urlTemplate += "&Version=2006-06-07"
$urlTemplate += "&AWSAccessKeyId=YourAWSIdHere"
$urlTemplate += "&Operation=ListLookup"
$urlTemplate += "&ListType=WishList"
$urlTemplate += "&ListId=YourWishlistIdHere"
$urlTemplate += "&Sort=DateAdded"
$urlTemplate += "&ResponseGroup=ItemAttributes,ListItems,ListInfo,Offers"
$urlTemplate += "&ProductPage={0}"

[Side note: I was glad to see Scott Hanselman’s post on how he highlights code in his blog posts.  This very post marks the first time I’m using Live Writer.  I guess we’ll see how this turns out.  After that, I intend to experiment with how I can use Live Writer to highlight my code.  I suspect, though, that most of these syntax highlighting plugins focus on highlighting the syntax of standard programming languages like C#.  I wonder how these tools deal with PowerShell script?]

Back to the topic at hand…notice the placeholder for the ProductPage querystring argument.  AWS will only send back a few entries per page.  So, I had to write a loop to call the service several times—once for each page—changing that ProductPage value with each call.  Also, with every iteration, I had to persist each service call result, so I created a second XML document that I used to stitch together the results of each call.

Step 2: Reformat the author names for sorting

For each book, AWS returns one or more author elements (or sometimes one or more editor elements).  The value is the full name of the individual (eg. John J. Doe).  Sometimes, the individual has a suffix like Jr., Sr., III, etc.  In order for me to be able to sort by author last name, I had to write script to parse the name and reformat it as last name, first name.  Here’s what I did:

$authors = $stitchedXml.StitchedXml.getElementsByTagName("Author")

foreach($author in $authors)
{
    $authorFullName = $author.get_InnerText().Split()
    $lnamePos = $authorFullName.length - 1
    $newAuthorName = $authorFullName[$lnamePos] + ", "
    if($authorFullName[$authorFullName.length - 1] -match "jr|sr|ii|iii")
    {
        $lnamePos--
        $newAuthorName = $authorFullName[$lnamePos] + " " + $authorFullName[$lnamePos + 1] + ", "
    }
    for($i = 0; $i -le $lnamePos - 1; $i++)
    {
        $newAuthorName += $authorFullName[$i] + " "
    }
    $author.set_InnerText($newAuthorName.Trim())
}

Clunky, yes, but it seems to get the job done.

Step 3: Use XSLT to generate my report

After reformatting all the author names in my stitched up xml file, all my script needs to do now is save the file to disk, then open it up in a browser.  When I originally created my conglomerated XML file, I assigned it a style sheet, as so:

$stitchedXml = [xml] '<?xml-stylesheet href="WishListReport.xslt" type="text/xsl"?><StitchedXml/>'

As soon as I load the XML file up in a browser, the browser will do the heavy lifting for me, using the associated stylesheet to transform the XML file into whatever…in this case, my sorted list I’m looking for.

And here are the final results:

wishlist1

Notice the two books at the top of the list with no authors: turns out these books have editors, not authors.  If I wanted to be really fancy, I probably could have wrote more sophisticated XSLT to treat editors like authors and alphabetize accordingly—I might save that for a rainy day.

Another interesting situation I encountered is that AWS returns all the entries I’ve ever made to my wishlist, even though I’ve deleted several entries over the years (clicked the Delete button in the Web UI).  There’s no “deleted” flag, but there is a QuantityReceived element that seems to serve as a deleted flag.  I had my XSLT script filter out entries where the QuantityReceived value was greater than 0.  If you’re interested in this stuff, I’ve posted both my PowerShell script and my XSLT file for your use.  Feel free to post back with suggestions for improving these operations.

Post Script: I printed my list and walked it into my favorite book store, and immediately encountered a problem…how can I move from section to section, thumbing through 6 pages of authors and books I want to purchase?  One improvement I need to make is to group my sorted list by category: fiction, non-fiction, science, philosophy, etc.  As far as I can see, the AWS service doesn’t offer such category information.  Hmm.  I wonder if I could take the ISBN number of each book (AWS does provide the ISBN) and call some other service to get the book’s category.  If I can’t do that, Amazon does provide a Comments field for each book.  I could type in a category in that field.  That would be a lot of work, but the results might be worth it.  The AWS service doesn’t appear to return this field, but I might be able to alter the call to get this field returned.  Looks like I have some more work ahead of me.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Technology Blog
Posted by Brad on Wednesday, December 24, 2008 6:44 PM
Permalink | Comments (2) | Post RSSRSS comment feed