O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Amazon Hacks
By Paul Bausch
August 2003
More Info

HACK
#71
Add an Amazon Box to Your Site
Add a quick list of products to your site based on a category or keyword
The Code
[Discuss (1) | Link to this hack]

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;
$ua->agent('www.oreillynet.com/~rael/lang/perl/amazonbox/0+1i');
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;
print 
    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]);


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.