| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Scraper::ISBN::Blackwell_Driver; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 85248 | use strict; | 
|  | 5 |  |  |  |  | 18 |  | 
|  | 5 |  |  |  |  | 152 |  | 
| 4 | 5 |  |  | 5 |  | 33 | use warnings; | 
|  | 5 |  |  |  |  | 12 |  | 
|  | 5 |  |  |  |  | 182 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 5 |  |  | 5 |  | 31 | use vars qw($VERSION @ISA); | 
|  | 5 |  |  |  |  | 15 |  | 
|  | 5 |  |  |  |  | 413 |  | 
| 7 |  |  |  |  |  |  | $VERSION = '0.10'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | WWW::Scraper::ISBN::Blackwell_Driver - Search driver for Blackwell's 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 Blackwell's online book catalog. | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =cut | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | ########################################################################### | 
| 28 |  |  |  |  |  |  | # Inheritence | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 5 |  |  | 5 |  | 34 | use base qw(WWW::Scraper::ISBN::Driver); | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 2756 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | ########################################################################### | 
| 33 |  |  |  |  |  |  | # Modules | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 5 |  |  | 5 |  | 8237 | use WWW::Mechanize; | 
|  | 5 |  |  |  |  | 640748 |  | 
|  | 5 |  |  |  |  | 207 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | ########################################################################### | 
| 38 |  |  |  |  |  |  | # Constants | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 5 |  |  | 5 |  | 51 | use constant	REFERER	=> 'http://bookshop.blackwell.co.uk'; | 
|  | 5 |  |  |  |  | 14 |  | 
|  | 5 |  |  |  |  | 357 |  | 
| 41 | 5 |  |  | 5 |  | 31 | use constant	SEARCH	=> 'http://bookshop.blackwell.co.uk/jsp/search_results.jsp?wcp=1&quicksearch=1&cntType=&searchType=keywords&searchData=%s&x=10&y=10'; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 3100 |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | ########################################################################### | 
| 46 |  |  |  |  |  |  | # Public Interface | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head1 METHODS | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =over 4 | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =item C | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Creates a query string, then passes the appropriate form fields to the | 
| 55 |  |  |  |  |  |  | Blackwell server. | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | The returned page should be the correct catalog page for that ISBN. If not the | 
| 58 |  |  |  |  |  |  | function returns zero and allows the next driver in the chain to have a go. If | 
| 59 |  |  |  |  |  |  | a valid page is returned, the following fields are returned via the book hash: | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | isbn          (now returns isbn13) | 
| 62 |  |  |  |  |  |  | isbn10 | 
| 63 |  |  |  |  |  |  | isbn13 | 
| 64 |  |  |  |  |  |  | ean13         (industry name) | 
| 65 |  |  |  |  |  |  | author | 
| 66 |  |  |  |  |  |  | title | 
| 67 |  |  |  |  |  |  | book_link | 
| 68 |  |  |  |  |  |  | image_link | 
| 69 |  |  |  |  |  |  | thumb_link | 
| 70 |  |  |  |  |  |  | description | 
| 71 |  |  |  |  |  |  | pubdate | 
| 72 |  |  |  |  |  |  | publisher | 
| 73 |  |  |  |  |  |  | binding       (if known) | 
| 74 |  |  |  |  |  |  | weight        (if known) (in grammes) | 
| 75 |  |  |  |  |  |  | width         (if known) (in millimetres) | 
| 76 |  |  |  |  |  |  | height        (if known) (in millimetres) | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | The book_link, image_link and thumb_link all refer back to the Blackwell website. | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =back | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =cut | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub search { | 
| 85 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 86 | 0 |  |  |  |  |  | my $isbn = shift; | 
| 87 | 0 |  |  |  |  |  | $self->found(0); | 
| 88 | 0 |  |  |  |  |  | $self->book(undef); | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | # validate and convert into EAN13 format | 
| 91 | 0 |  |  |  |  |  | my $ean = $self->convert_to_ean13($isbn); | 
| 92 | 0 | 0 | 0 |  |  |  | return $self->handler("Invalid ISBN specified") | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 93 |  |  |  |  |  |  | if(!$ean || (length $isbn == 13 && $isbn ne $ean) | 
| 94 |  |  |  |  |  |  | || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean))); | 
| 95 | 0 |  |  |  |  |  | my $isbn10 = $self->convert_to_isbn10($ean); | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 |  |  |  |  |  | my $mech = WWW::Mechanize->new(); | 
| 98 | 0 |  |  |  |  |  | $mech->agent_alias( 'Windows IE 6' ); | 
| 99 | 0 |  |  |  |  |  | $mech->add_header( 'Accept-Encoding' => undef ); | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 |  |  |  |  |  | my $search = sprintf SEARCH , $ean; | 
| 102 |  |  |  |  |  |  | #print STDERR "\n# search=[$search]\n"; | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | eval { $mech->get( $search ) }; | 
|  | 0 |  |  |  |  |  |  | 
| 105 | 0 | 0 | 0 |  |  |  | return $self->handler("Blackwell's website appears to be unavailable.") | 
|  |  |  | 0 |  |  |  |  | 
| 106 |  |  |  |  |  |  | if($@ || !$mech->success() || !$mech->content()); | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | # The Book page | 
| 109 | 0 |  |  |  |  |  | my $html = $mech->content(); | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 | 0 |  |  |  |  | return $self->handler("Failed to find that book on the Blackwell website. [$isbn]") | 
| 112 |  |  |  |  |  |  | if($html =~ m!Sorry, there are no results for!si); | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | $html =~ s/&/&/g; | 
| 115 | 0 |  |  |  |  |  | $html =~ s/ / /g; | 
| 116 |  |  |  |  |  |  | #print STDERR "\n# html=[\n$html\n]\n"; | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 0 |  |  |  |  |  | my $data; | 
| 119 | 0 |  |  |  |  |  | ($data->{isbn13})       = $html =~ m!!si; | 
| 120 | 0 |  |  |  |  |  | ($data->{title})        = $html =~ m!!si; | 
| 121 | 0 |  |  |  |  |  | ($data->{author})       = $html =~ m! \s*]+>([^<]+)!si;  | 
| 122 | 0 |  |  |  |  |  | ($data->{binding})      = $html =~ m! \s*([^<\(]+)!si;  | 
| 123 | 0 |  |  |  |  |  | ($data->{publisher})    = $html =~ m! | Publisher:\s* | \s*]+>([^<]+)!si; | 
| 124 | 0 |  |  |  |  |  | ($data->{pubdate})      = $html =~ m! | Pub date:\s* | !si; | 
| 125 | 0 |  |  |  |  |  | ($data->{description})  = $html =~ m!!si; | 
| 126 | 0 |  |  |  |  |  | ($data->{pages})        = $html =~ m! | Number of pages:\s* | (\d+)!si; | 
| 127 | 0 |  |  |  |  |  | ($data->{weight})       = $html =~ m! | Weight:\s* | (\d+)g!si; | 
| 128 | 0 |  |  |  |  |  | ($data->{height})       = $html =~ m! | Height:\s* | (\d+)mm!si; | 
| 129 | 0 |  |  |  |  |  | ($data->{width})        = $html =~ m! | Width:\s* | (\d+)mm!si; | 
| 130 | 0 |  |  |  |  |  | ($data->{image})        = $html =~ m!!si; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 0 |  |  |  |  |  | $data->{image}     =~ s!https:///!https://bookshop.blackwell.co.uk/!; | 
| 133 | 0 |  |  |  |  |  | $data->{thumb}     = $data->{image}; | 
| 134 | 0 |  |  |  |  |  | $data->{isbn10}    = $isbn10; | 
| 135 | 0 |  |  |  |  |  | $data->{publisher} =~ s/?39;/'/g; | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | #use Data::Dumper; | 
| 138 |  |  |  |  |  |  | #print STDERR "\n# " . Dumper($data); | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 0 | 0 |  |  |  |  | return $self->handler("Could not extract data from the Blackwell result page.") | 
| 141 |  |  |  |  |  |  | unless(defined $data); | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | # trim top and tail | 
| 144 | 0 |  |  |  |  |  | foreach (keys %$data) { | 
| 145 | 0 | 0 |  |  |  |  | next unless(defined $data->{$_}); | 
| 146 | 0 |  |  |  |  |  | $data->{$_} =~ s! ! !g; | 
| 147 | 0 |  |  |  |  |  | $data->{$_} =~ s/^\s+//; | 
| 148 | 0 |  |  |  |  |  | $data->{$_} =~ s/\s+$//; | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 0 |  |  |  |  |  | my $url = $mech->uri(); | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | my $bk = { | 
| 154 |  |  |  |  |  |  | 'ean13'		    => $data->{isbn13}, | 
| 155 |  |  |  |  |  |  | 'isbn13'		=> $data->{isbn13}, | 
| 156 |  |  |  |  |  |  | 'isbn10'		=> $data->{isbn10}, | 
| 157 |  |  |  |  |  |  | 'isbn'			=> $data->{isbn13}, | 
| 158 |  |  |  |  |  |  | 'author'		=> $data->{author}, | 
| 159 |  |  |  |  |  |  | 'title'			=> $data->{title}, | 
| 160 |  |  |  |  |  |  | 'book_link'		=> $url, | 
| 161 |  |  |  |  |  |  | 'image_link'	=> $data->{image}, | 
| 162 |  |  |  |  |  |  | 'thumb_link'	=> $data->{thumb}, | 
| 163 |  |  |  |  |  |  | 'description'	=> $data->{description}, | 
| 164 |  |  |  |  |  |  | 'pubdate'		=> $data->{pubdate}, | 
| 165 |  |  |  |  |  |  | 'publisher'		=> $data->{publisher}, | 
| 166 |  |  |  |  |  |  | 'binding'	    => $data->{binding}, | 
| 167 |  |  |  |  |  |  | 'pages'		    => $data->{pages}, | 
| 168 |  |  |  |  |  |  | 'weight'		=> $data->{weight}, | 
| 169 |  |  |  |  |  |  | 'height'		=> $data->{height}, | 
| 170 |  |  |  |  |  |  | 'width'		    => $data->{width}, | 
| 171 | 0 |  |  |  |  |  | }; | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | #use Data::Dumper; | 
| 174 |  |  |  |  |  |  | #print STDERR "\n# book=".Dumper($bk); | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 0 |  |  |  |  |  | $self->book($bk); | 
| 177 | 0 |  |  |  |  |  | $self->found(1); | 
| 178 | 0 |  |  |  |  |  | return $self->book; | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | 1; | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | __END__ |