Chat with some Perl code to get book prices via AOL Instant Messenger.. AOL Instant Messenger isn't the most likely place you'll need Amazon book data, but that doesn't mean the applicat
Trang 1Chat with some Perl code to get book prices via AOL
Instant Messenger.
AOL Instant Messenger isn't the most likely place you'll need Amazon book data, but that doesn't mean the applications
aren't fun to connect With Perl and the Net::AIM module, you can have your own chattering book-bot requesting Amazon
information for you
95.1 What You Need
First you'll need the Net::AIM library, which provides all the
functions for logging into AIM and sending or receiving
messages You can find it at activestate.com
(
http://aspn.activestate.com/ASPN/CodeDoc/Net-AIM/AIM.html) To get a jumpstart on coding, check out the tutorials at Wired Bots
(http://www.wiredbots.com/tutorial.html) They have some
fully functional sample bots and lots of example code for
working with Net::AIM
You'll also need an AIM screen name and password for your new virtual assistant, along with a screen name for yourself if you don't have one; sign up at http://www.aim.com
95.2 The Code
Create a file called asin_bot.pl and include the following code.
Trang 2previous hack ([Hack #80]), though the AWS request is made inside the on_im subroutine, when a message comes in Instead
of printing out to the console, it saves the results in a variable,
$detail, and sends it as an instant message back to the person sending the message
#!/usr/bin/perl
# asin_bot.pl
#
# An AIM bot that given an ASIN will
# return the product title and price
# Usage: perl asin_bot.pl
use warnings;
use strict;
use Net::AIM;
use LWP::Simple;
use XML::Simple;
# fill in your relevants
my $aim_un = 'insert AIM username';
my $aim_pw = 'insert AIM password';
my $dev_key = 'insert developer token';
my $af_code = 'insert affiliate tag';
# create an AIM connection
# and return it for usage
my $aim = new Net::AIM;
$aim->newconn(Screenname=>$aim_un,Password=>$aim_pw)
or die "Cannot connect to AIM.";
my $conn = $aim->getconn();
# Set up a handler for messages
$conn->set_handler('im_in', on_im);
$conn->set_handler('error', on_error);
print "Logged on to AIM!\n\n";
Trang 3# incoming
sub on_im {
my ($aim, $evt, $from, $to) = @_;
my $args = $evt->args();
($from, my $friend, my $msg) = @$args;
# cheaply remote html
$msg =~ s/<(.|\n)+?>//g;
# if this isn't an ASIN sized string,
# send back an error message stating such
$aim->send_im($from, "I only accept ASINs.") unless length($msg) eq 10;
# create our final URL
my $url = "http://xml.amazon.com/onca/xml3?t=$af_code"
"&dev-t=$dev_key&type=lite&f=xml&"
"AsinSearch=$msg";
my $content = get($url);
my $response = XMLin($content);
my $detail = $response->{Details}->{ProductName}||"no title";
$detail .= " $response->{Details}->{OurPrice}";
$aim->send_im($from, $detail);
}
# oops!
sub on_error {
my ($self, $evt) = @_;
my ($error, @stuff) = @{$evt->args()};
# Translate error number into English
# then filter and print to STDERR
my $errstr = $evt->trans($error);
$errstr =~ s/\$(\d+)/$stuff[$1]/ge;
Trang 4}
Notice that inside the on_im subroutine, the script checks to make sure the incoming message is exactly 10 characters, the length of an ASIN Otherwise it sends back the message, "I only accept ASINs." It's a good idea to set up rules like this for any kind of queries you allow Bots should always send a message about success or failure
95.3 Running the Hack
Start up AOL Instant Messenger and add the virtual screen
name you gave your bot to your buddy list When you run
asin_bot.pl, you should see the bot appear among your online
buddies Send a message consisting of only an ASIN, and you should get the book title and Amazon price back This
conversation is shown in Figure 6-9
Figure 6-9 Talking ASINs with an AIM bot
Not exactly stimulating conversation, but expanding its
vocabulary is simply a matter of adding Amazon requests and responses to the script
Trang 5A lightweight XML parser is all you need to work with
Amazon's data in Perl scripts.
Even without wrapper functions, retrieving data directly from Amazon with XML/HTTP is straightforward You just need the ability to grab a file from the Web and parse the results
80.1 What You Need
This hack requires two common Perl modules: one to handle the HTTP request and another to parse the XML Once the Amazon request URL is built, LWP::Simple handles sending the request and receiving the XML with a get( ) function You can find out more about LWP::Simple at CPAN
(http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm)
XML::Simple (
http://search.cpan.org/author/GRANTM/XML-Simple-2.04/lib/XML/Simple.pm) is a lightweight XML parser It provides a quick, simple interface for working with XML
Many ISPs have both of these modules installed already If not, you can install them with CPAN:
perl -MCPAN -e shell
cpan> install XML::Simple
If you have a Win32 system, you can install them from the
command line with the package manager like this:
ppm install XML::Simple
Trang 6This code accepts a command-line argument and builds an
Amazon URL with the argument as the keyword Create the file
amazon_http.pl with the following code:
#!/usr/bin/perl
# amazon_http.pl
# A typical Amazon Web API Perl script using the XML/HTTP interface
# Usage: amazon_http.pl <keyword>
#Your Amazon developer's token
my $dev_key='insert developer token';
#Your Amazon affiliate code
my $af_tag='insert associate tag';
#Take the keyword from the command-line
my $keyword =shift @ARGV or die "Usage:perl amazon_http.pl <keyword>\n";
#Assemble the URL
my $url = "http://xml.amazon.com/onca/xml3?t=" $af_tag
"&dev-t=" $dev_key
"&type=lite&f=xml&mode=books&"
"KeywordSearch=" $keyword;
use strict;
#Use the XML::Parser and LWP::Simple Perl modules
use XML::Simple;
use LWP::Simple;
my $content = get($url);
die "Could not retrieve $url" unless $content;
my $xmlsimple = XML::Simple->new( );
Trang 7foreach my $result (@{$response->{Details}}){
#Print out the main bits of each result
join "\n",
$result->{ProductName}||"no title",
"ASIN: " $result->{Asin} ", "
$result->{OurPrice} "\n\n";
}
The foreach at the end of the code loops through the results from Amazon and prints them out By changing the variable names, you can change the information that is displayed For example, changing OurPrice on the last line to ListPrice would display that price instead of Amazon's price
80.3 Running the Hack
From the command line, call the script like so:
perl amazon_http.pl hacks
Be sure to enclose phrases or multiple keywords in quotes, like so:
perl amazon.http.pl "google hacks"