Hack 21 Organize Your Wish List by Priority

figs/expert.giffigs/hack21.gif

Amazon provides a few ways to sort your wish list, but missed an important one: sorting by which items you want most.

When I sat down to create my Christmas list in November of 2002, I decided to use my Amazon wish list. I added all the books, CDs, and DVDs that I had been craving to my list. Next, I went to the "Your Wish List" page, where I planned to rank the items on my list. To my surprise, I found that Amazon doesn't allow you to rank your wish list.

Instead, all you can do is sort the list by (a) date added, (b) last updated, or (c) price. Even worse, the default sorting is by date added, and, since the items I wanted the most were the first ones I added, they ended up at the bottom of my list. (Amazon does provide a "Comment" field for each wish list item, so I initially thought I could store my ranks as comments, but Amazon doesn't let you sort by comment either.)

In search of a solution, I took a look at Amazon Web Services (http://www.amazon.com/webservices) and found that I could build my own simple application that adds ranking ability for Amazon wish lists.

21.1 Ranking Your Wish List

Using the Wish List Ranking application is simple. First, fill out the registration form (http://www.yoon.org/amazon/register). The only tricky part is that you have to enter your Wish List ID manually [Hack #18]. The FAQ (http://www.yoon.org/amazon/faq) includes instructions on how to find your Wish List ID. Then, as you can see in Figure 2-8, the application fetches your wish list from Amazon.

Figure 2-8. A Wish list pulled from Amazon
figs/amzh_0208.gif

As with any third-party service that requires registration, always use a different email and password combination than you use for your Amazon account. It's easy to use the same combination to save space in your memory, but you may be giving away the key to your account.

Click the "Change rankings" link at the top to go to a page where you can rank the items on your list, as shown in Figure 2-9.

Figure 2-9. Changing the order of items in a wish list
figs/amzh_0209.gif

Use the arrows to move your wish list items into the order you want, click the "Save Changes" button, and you're done. Use the "Share your Ranked Wish List with your family and friends" link so that they'll know what you really want.

21.2 How It Works

Amazon's Web Services API provides a WishlistSearchRequest method, which returns information about each item in a specific wish list. Since each item that Amazon sells has a unique identifier (the ASIN), all I needed to add was a way to store the rank of each ASIN in a given wish list.

A simple MySQL database (http://www.mysql.com) did the trick. In addition to standard user account information (email address, password, etc.) stored in a table named users, the core of the data model is just two tables:

CREATE TABLE wishlists (
 wishlist_id INTEGER NOT NULL PRIMARY KEY,
 user_id INTEGER NOT NULL REFERENCES users(user_id),
 amazon_wishlist_id VARCHAR(100) NOT NULL,
 UNIQUE (user_id, amazon_wishlist_id)
);

CREATE TABLE wishlist_items (
    item_id INTEGER NOT NULL PRIMARY KEY,
    wishlist_id INTEGER NOT NULL REFERENCES wishlists(wishlist_id)
    asin VARCHAR(100) NOT NULL,
    rank INTEGER NOT NULL,
    UNIQUE (wishlist_id, asin)
);

In the wishlists table, I store the Wish List ID assigned by Amazon (which is not simply a number) in the amazon_wishlist_id column. I don't use amazon_wishlist_id as my primary key because doing so would create a minor security hole: if I wanted to prevent you from ranking your wish list, I could search for it on Amazon (the Wish List ID appears in the URL) and then register it with the Wish List Ranking application. Then, when you came to rank your wish list, you'd find that it had already been registered by me. (If the Amazon API provided some kind of access control for wish lists, that would alleviate this issue.)

The wishlist_items table is where the ranks of each item, identified by ASIN, are stored. Whenever someone views a wish list, the application calls the WishlistSearchRequest method to load the product information of each wish list item (name, authors, price, image URL, etc.) into memory, fetches the rankings from the database, and then re-sorts the Amazon data in rank order.

21.3 Handling Deletions and Additions

The Amazon API doesn't provide any way to find out if a wish list has been modified, so we have to use a brute-force method to handle additions and deletions. Fetch the wish list periodically (whenever someone views the ranked version of the list) and then compare it to the rankings stored in our database. Any ASINs that are in the database but not in the list returned by Amazon are deleted from the database, and any ASINs that are in the Amazon search results but not in the database are inserted into the database at the bottom of the list.

21.4 Running It Yourself

I wrote this application in PHP (http://www.php.net), using the NuSOAP library (http://dietrich.ganx4.com/nusoap) to invoke the Amazon Web Services API and the Smarty template engine (http://smarty.php.net) to separate presentation from application logic. (Of course, I could have written the application with any number of tools: Java, ASP.NET, Perl, Python, what have you. PHP just happened to be installed by my hosting service.)

If you want to run the application on your own server, download the code (http://www.yoon.org/amazon/download/amazon-wish-list-ranking-1.0.tar.gz) and follow the instructions in the INSTALL file.

?Michael Yoon