WWW-Scraper-ISBN-Waterstones_Driver

 view release on metacpan or  search on metacpan

lib/WWW/Scraper/ISBN/Waterstones_Driver.pm  view on Meta::CPAN

package WWW::Scraper::ISBN::Waterstones_Driver;

use strict;
use warnings;

use vars qw($VERSION @ISA);
$VERSION = '0.09';

#--------------------------------------------------------------------------

=head1 NAME

WWW::Scraper::ISBN::Waterstones_Driver - Search driver for the Waterstones online book catalog.

=head1 SYNOPSIS

See parent class documentation (L<WWW::Scraper::ISBN::Driver>)

=head1 DESCRIPTION

Searches for book information from Waterstones online book catalog.

=cut

#--------------------------------------------------------------------------

###########################################################################
# Inheritence

use base qw(WWW::Scraper::ISBN::Driver);

###########################################################################
# Modules

use WWW::Mechanize;
use JSON::XS;

###########################################################################
# Constants

use constant REFERER => 'https://www.waterstones.com';
use constant SEARCH  => 'https://www.waterstones.com/index/search?term=';
my ($URL1,$URL2) = ('http://www.waterstones.com/book/','/[^?]+\?b=\-3\&amp;t=\-26\#Bibliographicdata\-26');

#--------------------------------------------------------------------------

###########################################################################
# Public Interface

=head1 METHODS

=over 4

=item C<search()>

Creates a query string, then passes the appropriate form fields to the 
Book Depository server.

The returned page should be the correct catalog page for that ISBN. If not the
function returns zero and allows the next driver in the chain to have a go. If
a valid page is returned, the following fields are returned via the book hash:

  isbn          (now returns isbn13)
  isbn10        
  isbn13
  ean13         (industry name)
  author
  title
  book_link
  image_link
  thumb_link
  description
  pubdate
  publisher
  binding       (if known)
  pages         (if known)

The book_link, image_link and thumb_link all refer back to the Waterstones
website.

=back

=cut

sub search {
    my $self = shift;
    my $isbn = shift;
    $self->found(0);
    $self->book(undef);

    # validate and convert into EAN13 format
    my $ean = $self->convert_to_ean13($isbn);
    return $self->handler("Invalid ISBN specified")   
        if(!$ean || (length $isbn == 13 && $isbn ne $ean)
                 || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));

    my $mech = WWW::Mechanize->new();
    $mech->agent_alias( 'Linux Mozilla' );
    $mech->add_header( 'Accept-Encoding' => undef );
    $mech->add_header( 'Referer' => REFERER );

    eval { $mech->get( SEARCH . $ean ) };
    return $self->handler("The Waterstones website appears to be unavailable.")



( run in 1.339 second using v1.01-cache-2.11-cpan-995e09ba956 )