#!/usr/bin/perl -s
##
## lookup-isbn-amazon
##
## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved.
## This code is free software; you can redistribute it and/or modify
## it under the same terms as Perl itself.
##
## $Id: lookup-isbn-amazon,v 1.3 2000/12/25 13:16:08 root Exp root $
use Data::Dumper;
use LWP::UserAgent;
use HTTP::Request::Common;
use HTML::Entities;
use URI::Escape;
my $POUND = "60";
my $DOLLAR = "43";
my $ISBN = shift @ARGV;
$ISBN =~ s/\-//g;
if ( $ISBN ) {
print &get_from_amazon( $ISBN ) ||
print &get_from_amazon_co_uk( $ISBN );
}
sub get_from_amazon {
my ( $isbn, $ashash ) = @_;
my $ua = new LWP::UserAgent;
my $URI ='http://www.amazon.com/exec/obidos/search-handle-form/ref=s_sf_b_ps/';
my $response = $ua->request( POST $URI, [ 'field-power' => "isbn: $isbn", index => 'books' ] );
return undef unless $response->headers->{location};
my $response2 = $ua->request( GET $response->headers->{location} );
if ( $response2->is_success ) {
return parse_amazon ( $response2->content, $ashash );
}
}
sub get_from_amazon_co_uk {
my ( $isbn, $ashash ) = @_;
my $ua = new LWP::UserAgent;
my $URI ='http://amazon.co.uk/exec/obidos/expert-query/';
my $response = $ua->request( POST $URI, [ 'expert-query' => "$isbn" ] );
return undef unless $response->headers->{location};
my $response2 = $ua->request( GET $response->headers->{location} );
if ($response2->is_success) {
return parse_amazon_co_uk ( $response2->content, $ashash );
}
}
sub parse_amazon {
my ( $text, $ashash ) = @_;
# open T, ">>amazon-raw"; print T "$text\n\n"; close T;
return undef if $text =~ /Books search: we were unable to find exact matches/i;
my ( $title ) = $text =~ m"
Amazon.com: buying info: (.*?)"i;
$title = HTML::Entities::decode ( $title );
my ( $pages ) = $text =~ m:(\d+) pages:;
my ( $price ) = $text =~ m:\$([\d\.]+):;
my ( $publisher ) = $text =~ m:\n\s+([^;\n]+);[\s\n]+ISBN:isg;
my ( $author ) = $text =~ m:\nby ]+>(.*?):s;
$author =~ s/<.*?>//sg; $author =~ s/\([^\)]+\)//g; chomp $author;
my @authors = split /\s*,\s*/,$author; $"=", "; chomp @authors;
return "$title\n by @authors\n $pages pages, \$$price\n $publisher\n" unless $ashash;
return { title => $title, author => $authors[0], author2 => $authors[1], author3 => $authors[2],
pages => $pages, price => $price*$DOLLAR, publisher => $publisher }
}
sub parse_amazon_co_uk {
my ( $text, $ashash ) = @_;
return undef if $text =~ /we were unable to find exact matches/i;
my ( $title ) = $text =~ m"Amazon.co.uk: A Glance: (.*?)"i;
my ( $pages ) = $text =~ m:(\d+) pages:;
my ( $price ) = $text =~ m:\£([\d\.]+):;
my ( $publisher ) = $text =~ m:\n\s+([^;\n]+);[\s\n]+ISBN:isg;
my ( $author ) = $text =~ m:Author=(.*?)/:isg; #-- we are picking up only one author.
$author = uri_unescape ( $author ); #-- fix later.
$author =~ s/(.*?),\s(.*?)$/\2 \1/g;
return "$title\n by $author\n $pages pages, \#$price\n $publisher\n" unless $ashash;
return { title => $title, author => $author, pages => $pages, price => $price*$POUND, publisher => $publisher }
}
1;
=head1 NAME
isbn-lookup-amazon - looks up the name, author, publisher and price of a book on its ISBN number from amazon.com.
=head1 VERSION
$Revision: 1.3 $
$Date: 2000/12/25 13:16:08 $
=head1 SYNOPSIS
isbn-lookup-amazon 0671578073
isbn-lookup-amazon 0-7515-0424-6
=head1 AUTHOR
Vipul Ved Prakash, Email@vipul.netE
=cut