Hack 71 Add an Amazon Box to Your Site


Add a quick list of products to your site based on a category or keyword.

If you run a weblog, you probably have a nice, long sidebar filled with links to your favorite sites. You could turn some of this web real estate into a money-maker with associate links. But who has time to find the top products in a given category, copy the URLs to the product detail pages, and place them all in order on your site? That's what an Amazon Box is for.

What's an Amazon Box? It's a simple HTML table that contains a list of products and that links to Amazon with your associate tag. You can use a Perl script by Rael Dornfest called Amazox to generate the box, making your life easier.

Amazox is run on-the-fly from the command line with the variables needed, so it's easy to quickly create several boxes with different configurations if you have more than one page you'd like to add a box to.

71.1 The Code

This Perl script queries Amazon based on command-line arguments you supply. It requires two standard Perl modules: XML::Simple (http://search.cpan.org/author/GRANTM/) and LWP::UserAgent (http://search.cpan.org/author/GAAS/). Many Perl installations have these modules already installed, or you can find them on CPAN, the Comprehensive Perl Archive Network (http://www.cpan.org/).

Be sure to include your associate tag and developer token in the right spots. You can configure which Amazon catalog to search by changing the $product_line variable.

#!/usr/bin/perl -w
# amazon_box.pl
# Author: Rael Dornfest <rael@oreilly.com>
# Version: 0+1i
# Home/Docs/Licensing: http://www.oreillynet.com/~rael/lang/perl/amazox/

use strict;

use CGI qw/:standard/;
use XML::Simple;
use LWP::UserAgent;

my $associate_id = "insert associate tag ";
my $dev_token = "insert developer token ";
my $product_line = "books" ;

# Amazon Associates XML
# https://associates.amazon.com/exec/panama/associates/tg/browse/-/567812/
# Consummate list of Amazon Product Modes and Browse IDs
# https://associates.amazon.com/exec/panama/associates/tg/trv/-/283099/

# Grab command-line arguments
die qq{Usage: perl amazon_box.pl (search|browse) "(query|browse id)" <number
of items>\n}
    unless @ARGV == 3 && $ARGV[0] =~ /^search|browse$/;

my($function, $query, $num_items) = @ARGV;

# Construct a search|browse URL
my $assoc_url = "http://xml.amazon.com/onca/xml3?t=" . $associate_id . 
    "&dev-t=" . $dev_token .
    "&type=lite" .
    "&f=xml" .
    "&mode=$product_line" .
    ($function eq 'search'
    ? "&KeywordSearch=$query"
    : "&BrowseNodeSearch=$query"

# Query Amazon
my $ua = new LWP::UserAgent;
my $http_request = new HTTP::Request('GET', $assoc_url);
my $http_response = $ua->request($http_request);
my $content = $http_response->{'_content'};
#print $content;

# Process the resulting XML
my $content_tree;
eval { $content_tree = XMLin($content) };
die "Couldn't understand Amazon's response\n"
    unless (!$@ and ref $content_tree and $content_tree->{Details});

# Output an Amazon box
my $ii = 1;
    table({-border=>0, -cellpadding=>0, -cellspacing=>0},
    map { 
      Tr({-valign=>"top"}, [
      td([ $ii++.'.&nbsp;', a({href=>$_->{url}}, $_->{ProductName}) ])
      ] ) . "\n"
    } @{$content_tree->{'Details'}}[0..$num_items-1]);

71.2 Running the Hack

You can run Amazox from the command line, supplying the necessary arguments, like this:

amazox.pl (search|browse) insert keyword/browse ID insert no. items

You can choose search or browse, depending on the type of query you'd like the results to be pulled from. If using search, you must supply a keyword; if using browse, you must supply a functioning browse ID [Hack #8]. Finally, add the number of items you'd like to display. Five items works well in a confined space; if you have a long sidebar, you can bump it up to fill even more space. Putting it all together, an Amazon Box with the top five keyword search results for hacks would be called like this:

perl amazox.pl search hacks 5

The result would be the properly formatted HTML to display a box like you see in Figure 5-15.

Figure 5-15. An Amazox Amazon box

The generated Amazon Box table doesn't set <table> attributes like height or width, so it's easy to integrate with an existing design. Otherwise, you can tweak those settings once you have the HTML.