Hack 31 Send an Email Alert if a Review Is Added to a Product

figs/expert.giffigs/hack31.gif

This script keeps an eye on Amazon and notifies you when a new review is posted.

There are obviously some products you care about more than others, and it's good to be aware of how those products are perceived. Reviews give feedback to publishers, authors, and manufacturers, help customers make buying decisions, and help other retailers decide what to stock. If you want to monitor all the reviews for a product or set of products, visiting each product detail page to see if a new review has been added is a tedious task.

Instead, you can use a script to periodically check the number of reviews for a given item, and have it send you an email when a new review is added.

31.1 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 [Hack #80]. 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.

31.2 Running the Hack

You can run the code from the command line:

perl review_monitor.pl  ASIN 

Ideally, you want to run this script once every so often in the background instead of manually executing this query every day. On Linux you can set it to run as a cron job. Open cron:

crontab -e

Then hit Enter and add something like the following:

0 12 * * 1-5 perl review_monitor.pl ASIN

This schedules the script to run Monday through Friday at noon. Be sure to replace ASIN with a real ASIN, and add jobs as necessary for all of the items you want to monitor.

On Windows, you can run the script as a scheduled task. From the Control Panel, choose Scheduled Tasks, and then Add Scheduled Task. Follow the wizard to set your execution time, and you should be all set for review notifications!