Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

Amazon.com Wish Lists


Feb03: Amazon.com Wish Lists

Amazon.com Wish Lists

The Perl Journal February 2003

By brian d foy

brian is the founder of the first Perl Users Group, NY.pm, and Perl Mongers, the Perl advocacy organization. He has been teaching Perl through Stonehenge Consulting for the past five years, and can be contacted at [email protected].


Over the holiday, I wanted to check my Amazon.com wish list, which I use to keep track of the books that I would like to read, even if I do not intend to buy them. With Amazon.com web services, I can easily download my wish list in XML format, and with XML::Simple, I can easily parse and access the information. The Text::Template module gives me a flexible way to display the information once I get it.

Recently, Amazon.com opened its data, which it calls "Properties," to the public through their "Amazon.com Web Services" (http://soap.amazon.com/). Anyone can use these services by signing up for the program and getting a "Developer's Token" that allows them to access the web service. Once you have a token, you can access book, DVD, and author information as well as the results of the many sorts of searches available on the Amazon.com web site. I started using the web service for some publishing clients who wanted to check sales rank and price data for their books, and now I am finding it useful for my personal information as well.

A Web Service, despite its general name, typically applies to something on a web server that returns XML data. Sometimes this XML result is a Simple Object Access Protocol (SOAP) message, but it can also be an XML/HTTP response. Amazon.com web services allows me to use either. For my wish list, I use the XML/HTTP method, which only requires a URL with the right parameters. This is much easier to use than SOAP for simple tasks.

Before I can start, I need to find my wish list ID string, which Amazon.com hides in a lot of information in their URLs, including session and user identifiers. If I look at my wish list page, I see a link to "Share Your Wishlist," which has as part of its link "/wishlist/1VGWQEYUDRN9V/." The string after "wishlist" (1VGWQEYUDRN9V) is my wish list ID.

Once I have my Developer's Token and wish list ID, I can download my wish list information. If I use XML/HTTP, I need to create the URL. The base of the request URL is <URL:http://xml .amazon.com/onca/xml2>. After the base, I form a query string with the information that I have collected; see Table 1.

I create the URL with the URL module, using my Amazon.com Associates ID (theperlreview-20); my Developer's Token, which I have in my shell's environment variable AMAZON_DEV_T; the "lite" version of the output; and my wish list ID (1VGWQEYUDRN9V). Amazon.com returns up to ten items in the wish list for each request. The first ten are page one, the next ten are page two, and so on. In this request, I set page to one to get the first ten results. To get all the results, I need to make multiple requests, increasing the page number by one each time, until Amazon.com returns no more results.

my $url = do {
    require URI;
    my %values = (
        t		 => 'theperlreview-20',
        'dev-t'	 => $ENV{AMAZON_DEV_T},
        type		 => 'lite',
        f		 => 'xml',
        page		 => 1,
        WishlistSearch	 => '1VGWQEYUDRN9V'
        );
        
    my $u = URI->new( 'http://xml.amazon.com/onca/xml2' );  
    $u->query_form( %values );
    $u->as_string;
    };

I fetch this URL with LWP::Simple, and store the result in the scalar $xml.

use LWP::Simple qw(get);
my $xml = get( $url );

The result in $xml, for the "lite" output, is a ProductInfo node containing several Details nodes; see Example 1. In this example output, I have replaced long URLs with "..." to show the structure more clearly. The "heavy" output includes much more detailed information on the product, including category names, similar items, and sales rank.

I can parse this any way that I like, including using XML::Parser or simply using built-in pattern matching and text-manipulation functions. I use XML::Simple to get a hash, which makes the next step easier.

use XML::Simple;
my $hash = XMLin( $xml );

The anonymous hash in $hash has a Details key, which is an array of hashes because I have more than one item in my wish list. (If I have only one item in my wish list, the value of the Details key will be just the hash for the product information.)

{
'Details' => [
    {
    'OurPrice' => '$10.47',
    'ImageUrlLarge' => '.../2290319740.01.LZZZZZZZ.jpg',
    'ReleaseDate' => 'January, 2003',
    'ImageUrlMedium' => '.../2290319740.01.MZZZZZZZ.jpg',
    'Catalog'' => 'Book',
    'Asin' => '2290319740',
    'url' => '...',
    'Manufacturer' => 'J Ai Lu Editions',
    'ListPrice' => '$14.95',
    'ProductName' => 'Je Parler Francais',
    'ImageUrlSmall' => '.../2290319740.01.THUMBZZZ.jpg',
    'Authors' => {
        'Author' => 'David Sedaris'
        }
    },
    ]
}

The Text::Template module gives me a basic framework to hand off data to a presentation layer so I can change the way that I show the information without changing the logic code. I give Text::Template's fill_in_file() function the filename of the template that I want to use, and pass the data as the value for the HASH key.

use Text::Template qw(fill_in_file);

my $Template = 'wish_list-template.txt';

print fill_in_file( $Template, HASH => $wish_list );

Inside the template, Text::Template turns $wish_list into variables. The Details key turns into the array @Details because it has an anonymous array for a value. Each element of @Details is an anonymous hash with keys that are the names of the XML tags. I use a very simple template to print the titles of everything in my wish list. Text::Template evaluates the parts of the file between the braces {} as Perl code. Everything outside of the braces is literal text.

My Amazon.com wish list:

{
my $string = '';

foreach my $product ( @Details )
    {
    $string .= "Title: $product->{ProductName}\n";
    }

$string;
}

The template reformats the wish list information, which I print to the screen. In this example, I use plain text, but I can output any format, including HTML.

My Amazon.com wish list:

Je Parler Francais
The Rise of the Meritocracy
1421: The Year China Discovered America
The Declaration of Independence and Other Great
 	Documents of American History, 1775-1865 (Dover
 	Thrift Editions)
Common Sense (Dover Thrift Editions)
Democracy in America
The Path to Victory: America's Army and the
 	Revolution in Human Affairs
Boyd: The Fighter Pilot Who Changed the Art of War
I May Be Wrong but I Doubt It
My Losing Season

That's it—a couple of Perl modules and a little help from Amazon.com, and I can download my wish list data. With a little work, so can you.

TPJ


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.