Wednesday, September 22, 2004

Back to the Good Ol' Days

Yes, it's 5 a.m., and I'm still up.

But success!

C:\Perl>perl MyFindBook.pl 0385504209
ISBN: 0385504209
Author: Dan Brown
Title: The Da Vinci Code
Binding: Hardcover
Number of Pages: 454
Publication Date: 2003-03-18
Publisher: Doubleday

A program that takes an ISBN and returns book details!
Only command line at the moment though, nothing fancy.
Major bitch setting up XML parsing in Perl.
But Amazon Web Services is cool!

[Code]
#!/usr/bin/perl -w

use strict;
use LWP::Simple qw /get/;
use XML::XPath;

# Defining parts of the REST request URL. These are parts of the URL

that are independent of the Operation being performed
my $BaseURL = "http://aws-beta.amazon.com";
my $Path = "/onca/xml";
my $Service = "AWSProductData";
my $SubscriptionId = "[INSERT YOUR SUBSCRIPTION ID]";

my $xp; # XPath object for parsing the xml response

main();

sub main {
# reading from the command line
my $asin = shift @ARGV or die "Usage: pricing_tool.pl \n";

if (@ARGV > 0) {
$BaseURL = shift @ARGV;
}

# Make the request
my $response = lookup_Asin($asin);

# Construct the XPath object
$xp = XML::XPath->new(xml => $response);

# Die if there is an error
die_if_error();


my $isbn =

$xp->findvalue("/ItemLookupResponse/Items/Item/ItemAttributes/ISBN");
print "ISBN: $isbn\n";

my $author =

$xp->findvalue("/ItemLookupResponse/Items/Item/ItemAttributes/Author")

;
print "Author: $author\n";

my $title =

$xp->findvalue("/ItemLookupResponse/Items/Item/ItemAttributes/Title");
print "Title: $title\n";

my $binding =

$xp->findvalue("/ItemLookupResponse/Items/Item/ItemAttributes/Binding"

);
print "Binding: $binding\n";

my $pages =

$xp->findvalue("/ItemLookupResponse/Items/Item/ItemAttributes/NumberOf

Pages");
print "Number of Pages: $pages\n";

my $date =

$xp->findvalue("/ItemLookupResponse/Items/Item/ItemAttributes/Publicat

ionDate");
print "Publication Date: $date\n";

my $publisher =

$xp->findvalue("/ItemLookupResponse/Items/Item/ItemAttributes/Publishe

r");
print "Publisher: $publisher\n";

}

# Function takes an ASIN as an argument and performs an ItemLookup on

it.
# It then returns a string containing the XML response received
sub lookup_Asin {
my $asin = shift; # shifts one element of default array @_.

Arguments passed are stored in @_.
my $Operation = "ItemLookup";
my $ResponseGroup = "Medium";

# Create the REST request to AWS
my $Request = "$BaseURL" . "$Path?" . "Service=$Service" .

"&Operation=$Operation" .
"&SubscriptionId=$SubscriptionId" . "&ItemId=$asin" .

"&ResponseGroup=$ResponseGroup";

# Use LWP to make an HTTP request to AWS
my $response = get($Request);
die "Attempt to make request failed!\n" unless defined($response);

return $response;
}


# Function checks if there is an error returned from the request.
# If there is an error then it outputs a message and dies
sub die_if_error {
if($xp->find("//Error")) {
print "Error recieved while making request:\n",
"Error Code: ", $xp->findvalue("//Error/Code"),
"\nError Message: ", $xp->findvalue("//Error/Message"),

"\n\n";

die;
}
}

7 Comments:

At 6:57 AM, Blogger ttw said...

This comment has been removed by a blog administrator.

 
At 6:59 AM, Blogger ttw said...

Oops. What have I done?

Anyway. You are a big geek...

... who collects stickers.

 
At 9:34 AM, Blogger bratsche said...

I agree! But then I collect scores and books and CDs and now DVDs. Geeks of the world, unite.

 
At 1:59 PM, Blogger tucque said...

Ok ok before people start openly accusing me of poor academic integrity, I must acknowledge M's sister for sharing with me the existence of AWS and her idea for the program. Also large parts of the code adapted from the pricing tool program sample from amazon.

 
At 5:25 PM, Anonymous Anonymous said...

Hey! Dunno what on earth you are talkin' about but am reading The Da Vinci Code now and absolutely loving it. I know it is dissed in some (mostly Catholic) circles as being factually inaccurate and simplistic but hey, I am enthralled.

Okok I must get back to "work". Ha ha!
-Despederata

 
At 12:32 AM, Blogger bean said...

Dude that's soooo nerdy/geeky (is there a diff?) :p

Anyway yeah da vinci code was a pretty good read. But too bad all this other novels follow the same formula/plot (heard from a raving fangirl), otherwise i might pick up his other novels. It was the most polished since it was (is?) the most recent.

 
At 12:56 AM, Blogger tucque said...

Yeah I just read Da Vinci Code recently myself and I really enjoyed it too. Reads like a Hollywood movie script, and I'm guesing that there will be a movie spinoff sometime. :)

Anyway, Despederata, all my post was trying to say was that the program I wrote takes in an ISBN number and then contacts the amazon.com database to query for the book details.

 

Post a Comment

<< Home