#!/usr/bin/perl
#
# Fetch the Amazon Web Services XML data
#
# written by Jackie Hamilton (kira@cgi101.com)
# for the cgi101.com "books" pages.
#
# This program runs nightly to retrieve XML data from Amazon
# on the specified keywords. The XML is saved to separate files
# for later parsing.
#
# see http://www.cgi101.com/books/howto/ for more on
# how this all works together.
#

use LWP::UserAgent;
$ua = LWP::UserAgent->new;

my $path = '/home/www/books/xml';

@keywords = qw(Perl PHP Java Javascript MySQL XML CSS);
push(@keywords, "Flash MX");

# Info about forming an AWS/ECS request can be found on Amazon's site:
# http://www.amazon.com/gp/aws/sdk/102-9033949-1328920?v=2005%2d01%2d19

# In this case I'm doing an ItemSearch on the Books SearchIndex
# For a keyword that I specify ("Title=Perl", for example).
# The ResponseGroup is Medium (returns info and pricing about each
# item), with Reviews (so I can get the average customer rating)
# and OfferFull (all offers). By specifying MerchantID=Amazon the 
# offers returned are only those from Amazon, and that's how I get
# the "Our Price" data. 

$url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=YOURIDHERE&Operation=ItemSearch&SearchIndex=Books&ItemPage=1&ResponseGroup=Medium,Reviews,OfferFull&MerchantId=Amazon&Sort=salesrank&AssociateTag=lightsphere-20&Title';

foreach my $term (@keywords) {

    # Create a request
    my $req = HTTP::Request->new(GET => "$url=$term");
    
    # Pass request to the user agent and get a response back
    my $res = $ua->request($req);
    
    # Check the outcome of the response
    if ($res->is_success) {
        my $outfile = lc($term);
        $outfile =~ s/\s+/_/g;
        open(OUT, ">$path/$outfile.xml");
        print OUT $res->content;
        close(OUT);
    }
    else {
        print "Error fetching xml for keyword $term: ",$res->status_line,"\n";
    }
}