#!/usr/bin/perl
######################################################################
# amazon -- Mike Schilli, 2001 (m@perlmeister.com)
######################################################################
my $URLPATH = "http://www.amazon.com/exec/obidos/ASIN";
my $VERSION = "1.0";
our $CVSVERSION = '$Revision: 1.5 $';
use LWP::UserAgent;
use HTTP::Request::Common;
use Text::Wrap;
use Pod::Usage;
use Getopt::Std;
use File::Basename;
use constant DEBUG => 0;
getopts( 'hdv', \ my %opts );
$URLPATH =~ s/\.com/.de/g if $opts{d};
pod2usage(-verbose => 2) if $opts{h};
die basename($0) . " $VERSION\n" if $opts{v};
pod2usage() unless @ARGV;
my $ua = LWP::UserAgent->new();
foreach my $isbn (@ARGV) {
my $r = $ua->request(GET "$URLPATH/$isbn");
if($r->is_error()) {
die "Request for isbn $isbn failed";
}
my $data = $r->content();
$data =~ s/\n//g;
my($author, $price, $title) = map { "Unknown" } (1..4);
if($data =~ /
.*:\s*(.*?)\s*<\/title>/) {
$title = $1;
print "Title: [$1]\n" if DEBUG;
}
if($data =~ /field-author.*?>(.*?)) {
$author = $1;
print "Author: [$1]\n" if DEBUG;
}
if($opts{d}) {
print "Looking for /images-eu.amazon.com/images" .
"/P/$isbn.*?EUR\s*(\d+,\d+)/\n" if DEBUG;
if($data =~
m#images-eu.amazon.com/images/P/$isbn.*?EUR\s*(\d+,\d+)#) {
$price = "EUR $1";
}
} else {
if($data =~ /$author.*?\$\s*(\d+\.\d+)/) {
$price = "\$$1";
}
}
# if($data =~ /ISBN.*?(\d{5,})/) {
# $isbn = $1;
# }
print " * ";
print Text::Wrap::fill("", " ",
"$author, '$title', $price, ISBN $isbn\n");
print "\n";
}
__END__
=head1 NAME
amazon - Look up books on amazon by ISBN
=head1 DOWNLOAD
_SRC_HERE_
=head1 SYNOPSIS
amazon [options] ISBN
Options:
-h get help
-d use amazon.de instead of amazon.com
=head1 OPTIONS
=over 8
=item B<-h>
Prints this manual page in text format.
=item B<-d>
Instead of going to C, use C
=back
=head1 DESCRIPTION
B takes a book's ISBN number, goes to Amazon,
fetches autor, title, publisher, year and price and
prints it in text format, ideal for ASCII wishlists.
=head1 EXAMPLES
$ amazon 0812932870
* Robert K. Cooper, 'The Other 90%: How to Unlock
Your Vast Untapped Potential for Leadership and
Life', Unknown, ISBN 0812932870
=head1 LEGALESE
Copyright 2002 by Mike Schilli, all rights reserved.
This program is free software, you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 AUTHOR
2001, Mike Schilli