| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Scraper::ISBN::WHSmith_Driver; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 287982 | use strict; | 
|  | 5 |  |  |  |  | 42 |  | 
|  | 5 |  |  |  |  | 150 |  | 
| 4 | 5 |  |  | 5 |  | 30 | use warnings; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 178 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 5 |  |  | 5 |  | 37 | use vars qw($VERSION @ISA); | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 467 |  | 
| 7 |  |  |  |  |  |  | $VERSION = '0.09'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | WWW::Scraper::ISBN::WHSmith_Driver - Search driver for the WHSmith online book catalog. | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | See parent class documentation (L) | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | Searches for book information from the WHSmith online book catalog | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =cut | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | ########################################################################### | 
| 28 |  |  |  |  |  |  | # Inheritence | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 5 |  |  | 5 |  | 38 | use base qw(WWW::Scraper::ISBN::Driver); | 
|  | 5 |  |  |  |  | 19 |  | 
|  | 5 |  |  |  |  | 2683 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | ########################################################################### | 
| 33 |  |  |  |  |  |  | # Modules | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 5 |  |  | 5 |  | 9469 | use WWW::Mechanize; | 
|  | 5 |  |  |  |  | 768030 |  | 
|  | 5 |  |  |  |  | 268 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | ########################################################################### | 
| 38 |  |  |  |  |  |  | # Constants | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 5 |  |  | 5 |  | 51 | use constant REFERER => 'http://www.whsmith.co.uk'; | 
|  | 5 |  |  |  |  | 14 |  | 
|  | 5 |  |  |  |  | 385 |  | 
| 41 | 5 |  |  | 5 |  | 33 | use constant SEARCH  => 'http://www.whsmith.co.uk/pws/ProductDetails.ice?ProductID=%s&keywords=%s&redirect=true'; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 284 |  | 
| 42 | 5 |  |  | 5 |  | 33 | use constant PRODUCT => '/products/[^/]+/product/'; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 5212 |  | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | ########################################################################### | 
| 47 |  |  |  |  |  |  | # Public Interface | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =head1 METHODS | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =over 4 | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =item C | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | Creates a query string, then passes the appropriate form fields to the | 
| 56 |  |  |  |  |  |  | WHSmith server. | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | The returned page should be the correct catalog page for that ISBN. If not the | 
| 59 |  |  |  |  |  |  | function returns zero and allows the next driver in the chain to have a go. If | 
| 60 |  |  |  |  |  |  | a valid page is returned, the following fields are returned via the book hash: | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | isbn          (now returns isbn13) | 
| 63 |  |  |  |  |  |  | isbn10 | 
| 64 |  |  |  |  |  |  | isbn13 | 
| 65 |  |  |  |  |  |  | ean13         (industry name) | 
| 66 |  |  |  |  |  |  | author | 
| 67 |  |  |  |  |  |  | title | 
| 68 |  |  |  |  |  |  | book_link | 
| 69 |  |  |  |  |  |  | image_link | 
| 70 |  |  |  |  |  |  | description | 
| 71 |  |  |  |  |  |  | pubdate | 
| 72 |  |  |  |  |  |  | publisher | 
| 73 |  |  |  |  |  |  | binding       (if known) | 
| 74 |  |  |  |  |  |  | pages         (if known) | 
| 75 |  |  |  |  |  |  | weight        (if known) (in grammes) | 
| 76 |  |  |  |  |  |  | width         (if known) (in millimetres) | 
| 77 |  |  |  |  |  |  | height        (if known) (in millimetres) | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | The book_link and image_link refer back to the WHSmith website. | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =back | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =cut | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub search { | 
| 86 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 87 | 0 |  |  |  |  |  | my $isbn = shift; | 
| 88 | 0 |  |  |  |  |  | $self->found(0); | 
| 89 | 0 |  |  |  |  |  | $self->book(undef); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | # validate and convert into EAN13 format | 
| 92 | 0 |  |  |  |  |  | my $ean = $self->convert_to_ean13($isbn); | 
| 93 | 0 | 0 | 0 |  |  |  | return $self->handler("Invalid ISBN specified [$isbn]") | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 94 |  |  |  |  |  |  | if(!$ean || (length $isbn == 13 && $isbn ne $ean) | 
| 95 |  |  |  |  |  |  | || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean))); | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 |  |  |  |  |  | $isbn = $ean; | 
| 98 |  |  |  |  |  |  | #print STDERR "\n# isbn=[\n$isbn\n]\n"; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 |  |  |  |  |  | my $mech = WWW::Mechanize->new(); | 
| 101 | 0 |  |  |  |  |  | $mech->agent_alias( 'Linux Mozilla' ); | 
| 102 | 0 |  |  |  |  |  | $mech->add_header( 'Accept-Encoding' => undef ); | 
| 103 | 0 |  |  |  |  |  | $mech->add_header( 'Referer' => REFERER ); | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 0 |  |  |  |  |  | my $search = sprintf SEARCH, $isbn, $isbn; | 
| 106 |  |  |  |  |  |  | #print STDERR "\n# search=[$search]\n"; | 
| 107 | 0 |  |  |  |  |  | eval { $mech->get( $search ) }; | 
|  | 0 |  |  |  |  |  |  | 
| 108 | 0 | 0 | 0 |  |  |  | return $self->handler("the WHSmith website appears to be unavailable.") | 
|  |  |  | 0 |  |  |  |  | 
| 109 |  |  |  |  |  |  | if($@ || !$mech->success() || !$mech->content()); | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | # The Book page | 
| 112 | 0 |  |  |  |  |  | my $html = $mech->content(); | 
| 113 | 0 | 0 | 0 |  |  |  | return $self->handler("Failed to find that book on the WHSmith website. [$isbn]") | 
| 114 |  |  |  |  |  |  | if($html =~ m!Sorry, no products were found!si || | 
| 115 |  |  |  |  |  |  | $html !~ m! |