| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Scraper::ISBN::PickABook_Driver; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 279375 | use strict; | 
|  | 5 |  |  |  |  | 36 |  | 
|  | 5 |  |  |  |  | 148 |  | 
| 4 | 5 |  |  | 5 |  | 26 | use warnings; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 188 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 5 |  |  | 5 |  | 32 | use vars qw($VERSION @ISA); | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 411 |  | 
| 7 |  |  |  |  |  |  | $VERSION = '0.08'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | WWW::Scraper::ISBN::PickABook_Driver - Search driver for the Pick-A-Book 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 Pick-A-Book online book catalog. | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =cut | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | ########################################################################### | 
| 28 |  |  |  |  |  |  | # Inheritence | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 5 |  |  | 5 |  | 35 | use base qw(WWW::Scraper::ISBN::Driver); | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 2563 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | ########################################################################### | 
| 33 |  |  |  |  |  |  | # Modules | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 5 |  |  | 5 |  | 8957 | use WWW::Mechanize; | 
|  | 5 |  |  |  |  | 758222 |  | 
|  | 5 |  |  |  |  | 230 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | ########################################################################### | 
| 38 |  |  |  |  |  |  | # Constants | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 5 |  |  | 5 |  | 80 | use constant    REFERER => 'http://www.pickabook.co.uk/'; | 
|  | 5 |  |  |  |  | 12 |  | 
|  | 5 |  |  |  |  | 320 |  | 
| 41 | 5 |  |  | 5 |  | 34 | use constant    SEARCH  => 'http://www.pickabook.co.uk/%s.aspx?ToSearch=TRUE'; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 4838 |  | 
| 42 |  |  |  |  |  |  | my ($URL1,$URL2) = ('http://www.PickABook.co.uk/book/','/[^?]+\?b=\-3\&t=\-26\#Bibliographicdata\-26'); | 
| 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 |  |  |  |  |  |  | Book Depository 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 |  |  |  |  |  |  | thumb_link | 
| 71 |  |  |  |  |  |  | description | 
| 72 |  |  |  |  |  |  | pubdate | 
| 73 |  |  |  |  |  |  | publisher | 
| 74 |  |  |  |  |  |  | binding       (if known) | 
| 75 |  |  |  |  |  |  | pages         (if known) | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | The book_link, image_link and thumb_link all refer back to the Pick-A-Book | 
| 78 |  |  |  |  |  |  | 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 |  |  |  |  |  |  |  | 
| 96 | 0 |  |  |  |  |  | my $mech = WWW::Mechanize->new(); | 
| 97 | 0 |  |  |  |  |  | $mech->agent_alias( 'Windows IE 6' ); | 
| 98 | 0 |  |  |  |  |  | $mech->add_header( 'Accept-Encoding' => undef ); | 
| 99 | 0 |  |  |  |  |  | $mech->add_header( 'Referer' => REFERER ); | 
| 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("The Pick-A-Book 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 | 0 |  |  |  | return $self->handler("Failed to find that book on the Pick-A-Book website. [$isbn]") | 
| 112 |  |  |  |  |  |  | if(!$html || $html =~ m!Your search for "ISBN = \d+"\s+has produced 0 results.!si); | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | $html =~ s/&/&/g; | 
| 115 |  |  |  |  |  |  | #print STDERR "\n# content2=[\n$html\n]\n"; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 0 |  |  |  |  |  | my $data; | 
| 118 |  |  |  |  |  |  | ($data->{author},$data->{title}) | 
| 119 | 0 |  |  |  |  |  | = $html =~ m!]*>([^<]+)!si; | 
| 120 | 0 |  |  |  |  |  | ($data->{binding})      = $html =~ m!]*>([^<]+)!si; | 
| 121 | 0 |  |  |  |  |  | ($data->{isbn13})       = $html =~ m!]*>([^<]+)!si; | 
| 122 | 0 |  |  |  |  |  | ($data->{isbn10})       = $html =~ m!]*>([^<]+)!si; | 
| 123 | 0 |  |  |  |  |  | ($data->{publisher})    = $html =~ m!]*>([^<]+)!si; | 
| 124 | 0 |  |  |  |  |  | ($data->{pubdate})      = $html =~ m!]*>([^<]+)!si; | 
| 125 | 0 |  |  |  |  |  | ($data->{pages})        = $html =~ m!]*>([^<]+)!si; | 
| 126 | 0 |  |  |  |  |  | ($data->{description})  = $html =~ m!]*>([^<]+)!si; | 
| 127 | 0 |  |  |  |  |  | ($data->{isbn10})       = $html =~ m!]*>([^<]+)!si; | 
| 128 | 0 |  |  |  |  |  | ($data->{image})        = $html =~ m!]*>!si; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 0 | 0 |  |  |  |  | if($data->{image}) { | 
| 131 | 0 |  |  |  |  |  | $data->{image} = REFERER . $data->{image}; | 
| 132 | 0 |  |  |  |  |  | $data->{thumb} = $data->{image}; | 
| 133 |  |  |  |  |  |  | } | 
| 134 | 0 | 0 |  |  |  |  | $data->{author} =~ s/.*?\s+by\s+//      if($data->{author}); | 
| 135 | 0 | 0 |  |  |  |  | $data->{publisher} =~ s/?39;/'/g     if($data->{publisher}); | 
| 136 | 0 |  |  |  |  |  | for(qw(title author publisher)) { | 
| 137 | 0 | 0 |  |  |  |  | next    unless($data->{$_}); | 
| 138 | 0 |  |  |  |  |  | $data->{$_} =~ s/([\w']+)/\u\L$1/g; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | #use Data::Dumper; | 
| 142 |  |  |  |  |  |  | #print STDERR "\n# " . Dumper($data); | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 0 | 0 |  |  |  |  | return $self->handler("Could not extract data from the Pick-A-Book result page.") | 
| 145 |  |  |  |  |  |  | unless(defined $data); | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | # trim top and tail | 
| 148 | 0 |  |  |  |  |  | foreach (keys %$data) { | 
| 149 | 0 | 0 |  |  |  |  | next unless(defined $data->{$_}); | 
| 150 | 0 |  |  |  |  |  | $data->{$_} =~ s! ! !g; | 
| 151 | 0 |  |  |  |  |  | $data->{$_} =~ s/^\s+//; | 
| 152 | 0 |  |  |  |  |  | $data->{$_} =~ s/\s+$//; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 0 |  |  |  |  |  | my $url = $mech->uri(); | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | my $bk = { | 
| 158 |  |  |  |  |  |  | 'ean13'         => $data->{isbn13}, | 
| 159 |  |  |  |  |  |  | 'isbn13'        => $data->{isbn13}, | 
| 160 |  |  |  |  |  |  | 'isbn10'        => $data->{isbn10}, | 
| 161 |  |  |  |  |  |  | 'isbn'          => $data->{isbn13}, | 
| 162 |  |  |  |  |  |  | 'author'        => $data->{author}, | 
| 163 |  |  |  |  |  |  | 'title'         => $data->{title}, | 
| 164 |  |  |  |  |  |  | 'book_link'     => "$url", | 
| 165 |  |  |  |  |  |  | 'image_link'    => $data->{image}, | 
| 166 |  |  |  |  |  |  | 'thumb_link'    => $data->{thumb}, | 
| 167 |  |  |  |  |  |  | 'description'   => $data->{description}, | 
| 168 |  |  |  |  |  |  | 'pubdate'       => $data->{pubdate}, | 
| 169 |  |  |  |  |  |  | 'publisher'     => $data->{publisher}, | 
| 170 |  |  |  |  |  |  | 'binding'       => $data->{binding}, | 
| 171 |  |  |  |  |  |  | 'pages'         => $data->{pages}, | 
| 172 | 0 |  |  |  |  |  | 'html'          => $html | 
| 173 |  |  |  |  |  |  | }; | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | #use Data::Dumper; | 
| 176 |  |  |  |  |  |  | #print STDERR "\n# book=".Dumper($bk); | 
| 177 |  |  |  |  |  |  |  | 
| 178 | 0 |  |  |  |  |  | $self->book($bk); | 
| 179 | 0 |  |  |  |  |  | $self->found(1); | 
| 180 | 0 |  |  |  |  |  | return $self->book; | 
| 181 |  |  |  |  |  |  | } | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | 1; | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | __END__ |