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
#65
Show Amazon Search Results on Your Site
You can have the best of both worlds: allow people to find what they're interested in at Amazon, while still keeping them on your site
The Code
[Discuss (0) | Link to this hack]

The Code

Add this code to a file called searchbox.cgi:

#!/usr/bin/perl -w
# searchbox.cgi
# Uses Amazon API to perform search and formats the results
# Usage: searchbox.cgi?keywords=<Keywords>

#####################################################
# SITE VARIABLES
#####################################################

# Your Amazon Associates ID available at http://associates.amazon.com
$amazon_id="insert associate tag";

# Link back to the page where the searchbox is located.  This is used 
# for links back to the search page at the bottom of the results

$home="http://www.mybookvendor.com/searchbox/";

# The title for your page

$page_title="MyBookVendor Searchbox Demo";

#Location of your site's logo (size up to the width of the page)

$logo="http://www.mybookvendor.com/mybookvendor.gif";

# Table width.  This is the width of the results page in pixels
$table_width="700";

# Cell Spacing.  The amount of space between books.

$cell_space=20;

# Cell Padding.  The margin within the table cells.

$cell_pad=5;

# Select one method of sorting results by uncommenting the line
# Some ranking methods are type specific (e.g. Books only)

$sort_type="+salesrank"; #sort by bestsellers
#$sort_type="+pmrank"; #sort by featured items BOOKS, SOFTWARE, 
#$sort_type="+pricerank"; #sort by price (low to high) BOOKS ONLY!
#$sort_type="+inverse_pricerank"; #sort by price (high to low) BOOKS ONLY!
#$sort_type="+daterank"; #sort by date
#$sort_type="+psrank"; #sort by featured items
#$sort_type="+price"; #sort by price (high to low)
#$sort_type="-price"; #sort by price (low to high)
#$sort_type="+titlerank"; #sort alphabetically
#$sort_type="+artistrank"; #sort by artist MUSIC ONLY

# Select category to search by uncommenting the appropriate line

$mode_type="books"; #Books
#$mode_type="baby"; #Baby Items
#$mode_type="classical"; #Classical Music
#$mode_type="dvd"; #DVD movies
#$mode_type="electronics"; #Home Electronics
#$mode_type="garden"; #Home and Garden
#$mode_type="kitchen"; #Kitchen goods
#$mode_type="magazines"; #Magazines
#$mode_type="music"; #Music/CDs
#$mode_type="pc-hardware"; #Computer Hardware
#$mode_type="photo"; #Photography/Camera
#$mode_type="software"; #Computer Software
#$mode_type="toys"; #Toys
#$mode_type="universal"; #Tools and Hardware
#$mode_type="vhs"; #VHS Movies
#$mode_type="videogames"; #Console Video Games

# This sets the color of your text in the results page

$text_color="#000000"; 

# This sets the color of your links in the results page

$link_color="#000000";

# This sets the color of your links when the mouse is
# over them in the results page

$hover_color="#008800";

# Result page background color.  Change to match your site.

$background_color="#FFFFFF";

#Color for alternating cells in result table

$table_bg_color="#EEEEEE";

# Font to use in results table

$font="Arial";

#########################################################
# DO NOT EDIT BELOW THIS LINE
#########################################################

use CGI;
use LWP::Simple;
$dev_id="insert developer token";
$passed=$ENV{'QUERY_STRING'};
@passed_refs=split/\&/,$passed;
foreach $pr(@passed_refs) {
    ($tag, $value)=split/=/,$pr;
    $refs{$tag}=$value;
}
if ($refs{"keywords"}) {
    $keywords=$refs{"keywords"};
    $keywords1=$keywords;
    $keywords1=~s/\%20/ /g;
} else {
    $data=new CGI;
    $keywords=$data->param("search");
    $keywords1=$keywords;
    $keywords=~s/\s/\%20/g;
}

if ($refs{"page"}) {
    $page=$refs{"page"};
}
else {
    $page=1;
}

$nextpage=$page+1;
&header;
&get_info;
&parse_info;
&footer;

sub get_info {
    $switch=int(rand(10));
    if ($switch==4) {
        $amazon_id="mybookvendorcom";
    }
    $document="http://xml.amazon.com/onca/xml2?t=$amazon_id&return;
&dev-t=$dev_id&KeywordSearch=$keywords&mode=$mode_type&return;
&type=lite&page=$page&sort=$sort_type&f=xml";
    $zon_code=get $document;
}

sub parse_info {
 @items=split/<Details\s/, $zon_code;
 foreach $listing(@items) {
   if ($listing=~/url=\"(.*)\"\>/){$target_url=$1;}
   if ($listing=~/<ProductName>(.*)<\/ProductName>/){$title=$1;}
   if ($listing=~/<Asin>(.*)<\/Asin>/){$asin=$1;}
   if ($listing=~/<Author>(.*)<\/Author>/){$author=$1;}
   if ($listing=~/<Artist>(.*)<\/Artist>/){$artist=$1;}
   if ($listing=~/<ReleaseDate>(.*)<\/ReleaseDate>/){$releasedate=$1;}
   if ($listing=~/<Manufacturer>(.*)<\/Manufacturer>/){$manufacturer=$1;}
 # if ($listing=~/<ImageUrlSmall>(.*)<\/ImageUrlSmall>/){$smallimage=$1;}
   if ($listing=~/<ImageUrlMedium>(.*)<\/ImageUrlMedium>/){$mediumimage=$1;}
 # if ($listing=~/<ImageUrlLarge>(.*)<\/ImageUrlLarge>/){$largeimage=$1;}
   if ($listing=~/<ListPrice>(.*)<\/ListPrice>/){$listprice=$1;}
   if ($listing=~/<OurPrice>(.*)<\/OurPrice>/){$ourprice=$1;}
   &print_it;
 }
}

sub print_it {

    if ($target_url) {
        $count++;
        print "<TR>";
        if ($count%2==0) {
            print "<TD VALIGN=TOP BGCOLOR=\"$table_bg_color\">";
        } else {
            print "<TD VALIGN=TOP>";
        }
        print "<form method=\"POST\" action=\"http://www.amazon.com/o/dt/&return;
assoc/handle-buy-box=$asin\">";
        print "<A HREF=\"$target_url\" TARGET=_NEW><IMG SRC=\"$mediumimage\"&return; 
HSPACE=15 ALIGN=LEFT BORDER=0><FONT SIZE=4><B>$title</b></FONT></a><br>";
        if ($author) {
            print "<b>By:</b> $author<br>";
        }
        if ($artist) {
            print "<b>By:</b> $artist<br>";
        }
        print "$manufacturer ($releasedate)<p>";
        if ($listprice) {
            print "<b>List Price:</b> $listprice<br>";
        }
        if ($ourprice) {
            print "<b>Our Price:</b> $ourprice<br>";
        }
        print "<DIV ALIGN=RIGHT><input type=\"submit\" name=\"submit.add-&return;
to-cart\" value=\"Buy from Amazon.com\">
        <input type=\"hidden\" name=\"asin.$asin\" value=\"1\">
        <input type=\"hidden\" name=\"tag-value\" value=\"$amazon_id\">
        <input type=\"hidden\" name=\"tag_value\" value=\"$amazon_id\">
        <input type=\"hidden\" name=\"dev-tag-value\" value=\"$dev_id\">&return;
</DIV>";
        print "</form>";
        print "</TD></TR>";
    }
}

if ($count==0 && $page>1) {
    print "<TR><TD><CENTER><FONT SIZE=4>No Additional Results</FONT>&return;
</CENTER></TD></TR>";
}

sub header {
    print "Content-type:text/html\n\n";
    print '<HTML><HEAD>';
    print "<TITLE>$page_title</TITLE>";
      print "<style type=\"text/css\">                   
    <!--      
    BODY {font-family: $font}
    TABLE {font-family: $font}
    A:link {text-decoration: none}
    A:visited {text-decoration: none}
    A:active {text-decoration: none}";
                                                    
    print "A:hover {color: $hover_color}
    -->                                                                             
    </style></HEAD>";
    print "<BODY BGCOLOR=\"$background_color\" TEXT=\"$text_color\" LINK=\&return;
"$link_color\">";
    print "<CENTER><TABLE WIDTH=\"$table_width\" CELLSPACING=\"$cell_space\&return;
"CELLPADDING=\"$cell_pad\"><TR><TD>";
    
    print "<CENTER><IMG SRC=\"$logo\"></CENTER></TD></TR><TR><TD &return;
BGCOLOR=\"$table_bg_color\"><FONT SIZE=4><b><CENTER>Search Results for:</b>&return;
    $keywords1 </FONT></CENTER></TD></TR><TR>";
};

sub footer{
    print "<TR><TD ALIGN=RIGHT><A HREF=\"http://www.mybookvendor.com/amazon/&return;
\"><IMG SRC=\"http://www.mybookvendor.com/mybvbutton.gif\" BORDER=0&return; 
ALIGN=LEFT></a>";
  if ($count >9) {
    print "<A HREF=\"./searchbox.cgi?keywords=$keywords&page=$nextpage\">see&return; 
more results</a><br>";
  }
  print "<A HREF=\"$home\">return to home page</a>";
  print "</TD></TR></TABLE>";
};

This script contacts Amazon's API for the search results and prints them on the page, formatting the links to Amazon with your associate tag.

WARNING

Note that the script randomly replaces your associate tag with the script author's tag. Some developers do this to help fund their development efforts. It's an interesting trade-off. You get some ready-made code you can add to your web site, and the developers get some potential referral fees from those sites that implement their script. But don't be surprised when you don't see your associate tag in every URL that points to Amazon.


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.