Amazon Hacks Free Open Book

Amazon Hacks

Previous Section Next Section

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!

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Credits
    Foreword
    Preface
    Chapter 1. Browsing and Searching
    Chapter 2. Controlling Your Information
    Chapter 3. Participating in the Amazon Community
    3.1 Hacks #27-48
    3.2 Community Features
    3.3 Accessing Community Features
    Hack 27 Write a Review
    Hack 28 Link Directly to Reviews of a Product
    Hack 29 Post a Review from a Remote Site
    Hack 30 Add Pop-up Amazon Reviews to Your Web Site
    Hack 31 Send an Email Alert if a Review Is Added to a Product
    Hack 32 Sort Books by Average Customer Rating
    Hack 33 Sort Your Recommendations by Average Customer Rating
    Hack 34 Scrape Product Reviews
    Hack 35 Publish Your Amazon Reviews on Your Site
    Hack 36 Share the Love (and Savings!) with Your Friends
    Hack 37 Create a Guide
    Hack 38 Post a Guide Remotely
    Hack 39 Add Product Advice Remotely
    Hack 40 Scrape Customer Advice
    Hack 41 Create a Listmania! List
    Hack 42 Gather Your Friends on Amazon
    Hack 43 Gather Your Friends' Amazon IDs
    Hack 44 Get Purchase Circle Products with Screen Scraping
    Hack 45 Find Purchase Circles by Zip Code
    Hack 46 Track the Ranks of Books Over Time
    Hack 47 Group Conversations About Books
    Hack 48 Add a 'Currently Reading' List to Your Web Site
    Chapter 4. Selling Through Amazon
    Chapter 5. Associates Program
    Chapter 6. Amazon Web Services
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele