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
#51
List Several Items for Sale at Once
If you're ready to do serious business through Amazon, a Pro-Merchant subscription allows you to list hundreds or even thousands of items in one fell swoop
The Code
[Discuss (0) | Link to this hack]

The Code

Save the following code to a text file called create_bulk.pl. You'll need a Web Services developer's token and affiliate tag , so be sure to include them in the script.

#!/usr/bin/perl
# create_bulk.pl
# A script to create an Amazon Marketplace Bulk Load file.
# Usage: perl create_bulk.pl <asin file>

#Your Amazon developer's token
my $dev_token='insert developer token';

#Your Amazon affiliate tag
my $af_tag='insert affiliate tag';

#A Random Sku Suffix
my $sku = "SKU";

#Take the query from the command-line
my $asinfile =shift @ARGV or die "Usage:perl create_bulk.pl <asin file>\n";

#Use the XML::Parser Perl module
use strict;
use XML::Simple;
use LWP::Simple;

#Open output file for writing, and set column headers
open(BULKFILE, ">bulkfile.tab");
print BULKFILE 
"product-id\tauthor\ttitle\tpublisher\tpub-date\tbinding\tsku\tprice\&return;
titem-condition\n";

#Loop through the ASINs, Looking up details
open(ASINFILE, "<".$asinfile);
while(<ASINFILE>) {
    my($asin) = $_;
    chomp($asin);
   
    #Assemble the URL
    my $url = "http://xml.amazon.com/onca/xml3?" . 
           "t=" . $af_tag . 
           "&dev-t=" . $dev_token .
           "&type=heavy" .
           "&f=xml" .
           "&AsinSearch=" . $asin,
           "&offer=used";
    
    my $content = get($url);
    die "Amazon service unavailable." unless $content;
    
    my $xmlsimple = XML::Simple->new('forcearray' => 1);
    my $response = $xmlsimple->XMLin($content);
    foreach my $result (@{$response->{Details}}) {
      #Print out the main bits of each result
      print BULKFILE
      $result->{Asin}[0] . "\t",
      $result->{Authors}[0]->{Author}[0]||$result->{Artists}[0]->&return;
{Artist}[0]||"n/a",
      "\t" . $result->{ProductName}[0],
      "\t" . $result->{Manufacturer}[0],
      "\t" . $result->{ReleaseDate}[0],
      "\t" . $result->{Media}[0],
      "\t" . $asin . "-" . $sku;
      print "\nSet Price ",
      "(around ".$result->{UsedPrice}[0].")",
      " for\n",
      $result->{ProductName}[0].": ";
      chomp(my $price = <STDIN>);
      print BULKFILE "\t".$price."\t2\n";
    }
}
close(BULKFILE);

Note that the Web Services call uses the offer=used option so the product includes information related to used items, in this case <UsedPrice>. This information helps you determine how to price your item. This script could be combined with finding average prices to provide even more pricing information.


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.