Hack 53 Receive an Email Alert for Newly Added Amazon.com Reviews

figs/moderate.gif figs/hack53.gif

This hack keeps an eye on Amazon.com and notifies you, via email, when a new product review is posted to items you're tracking .

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 Details 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.

The Code

This script requires you to have the XML::Simple Perl module installed, a Developer's Token (http://www.amazon.com/gp/aws/landing.html), and a product's unique ASIN (included in the details of every Amazon.com product).

Save the following script to a file called review_monitor.pl :

 #!/usr/bin/perl-w # review_monitor.pl # # Monitors products, sending email when a new review is added. # Usage: perl review_monitor.pl <asin> use strict; use LWP::Simple; use XML::Simple; # Your Amazon developer's token. my $dev_token='   insert developer token   '; # Your Amazon affiliate code. Optional. # See http://associates.amazon.com/. my $af_code='   insert affiliate tag   '; # Location of sendmail and your email. my $sendmailpath = "   insert sendmail location   "; my $emailAddress = "   insert your 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 "Review count for $productName is $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, sends an email to let you know.

In your local path to sendmail , be sure to include 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.

Running the Hack

Run the script from the command line, passing it an ASIN (to find an ASIN, see Figure 4-3 in [Hack #52] for guidance):

 %  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 [Hack #90], like so:

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

This schedules the script to run Monday through Friday at noon on each day. Be sure to replace ASIN with a real ASIN, and add jobs as necessary for all 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!

See Also

  • Amazon Hacks (http://oreilly.com/catalog/amazonhks/) by Paul Bausch

  • Section 5.3Scheduling Tasks Without cron" [Hack #91]

Paul Bausch



Spidering Hacks
Spidering Hacks
ISBN: 0596005776
EAN: 2147483647
Year: 2005
Pages: 157

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net