The Code
You'll need two
nonstandard Perl modules to run
this code: LWP::Simple to make the AWS (Amazon Web Services) request,
and XML::Simple to parse the results. The combination provides a
simple way to access AWS XML over HTTP with Perl . Create a file called
review_monitor.pl with the
following code:
#!/usr/bin/perl
# review_monitor.pl
#
# Monitors products, sending email when a new review is added
# Usage: perl review_monitor.pl <asin>
use warnings;
use strict;
use LWP::Simple;
use XML::Simple;
# Your Amazon developer's token.
my $dev_token='insert developer token';
# Your Amazon affiliate code.
my $af_code='insert affiliate code';
# Location of sendmail and your email.
my $sendmailpath = 'insert sendmail path';
my $emailAddress = 'insert email address';
# Take the asin from the command-line.
my $asin = shift @ARGV or die "Usage: perl review_monitor.pl <asin>\n";
# Get the number of reviews the last time this script ran.
open (ReviewCountDB, "<reviewCount_$asin.db");
my $lastReviewCount = <ReviewCountDB> || 0;
close(ReviewCountDB); # errors?! bah!
# Assemble the query URL (RESTian).
my $url = "http://xml.amazon.com/onca/xml2?t=$af_code" .
"&dev-t=$dev_token&type=heavy&f=xml" .
"&AsinSearch=$asin";
# Grab the content...
my $content = get($url);
die "Could not retrieve $url" unless $content;
# And parse it with XML::Simple.
my $response = XMLin($content);
# Send email if a review has been added
my $currentReviewCount = $response->{Details}->{Reviews}->&return;
{TotalCustomerReviews};
my $productName = $response->{Details}->{ProductName};
if ($currentReviewCount > $lastReviewCount) {
open (MAIL, "|$sendmailpath -t") || die "Can't open mail program!\n";
print MAIL "To: $emailAddress\n";
print MAIL "From: Amazon Review Monitor\n";
print MAIL "Subject: A Review Has Been Added!\n\n";
print MAIL "The total review count for $productName is &return;
$currentReviewCount.\n";
close (MAIL);
# Write the current review count to a file
open(ReviewCountDB, ">reviewCount_$asin.db");
print ReviewCountDB $currentReviewCount;
close(ReviewCountDB);
}
This code performs a standard Web Services ASIN query looking for one
bit of data: the total number of customer reviews
(TotalCustomerReviews). The script saves the
number of reviews in a text file
ASIN.db, and if the
number is different than the last time the script was run, it sends
an email to let you know.
Be sure to include your local path to sendmail,
a program that sends email from the server. Most ISPs have
sendmail installed in some form or another,
(often at /usr/bin/sendmail); check with your
local administrator or Internet service provider (ISP) if you're not
sure where it's located.