| blib/lib/WWW/Scraper/ISBN/Waterstones_Driver.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 68 | 68 | 100.0 |
| branch | 11 | 20 | 55.0 |
| condition | 8 | 18 | 44.4 |
| subroutine | 8 | 8 | 100.0 |
| pod | 1 | 1 | 100.0 |
| total | 96 | 115 | 83.4 |
| line | stmt | bran | cond | sub | pod | time | code | |
|---|---|---|---|---|---|---|---|---|
| 1 | package WWW::Scraper::ISBN::Waterstones_Driver; | |||||||
| 2 | ||||||||
| 3 | 6 | 6 | 155773 | use strict; | ||||
| 6 | 18 | |||||||
| 6 | 291 | |||||||
| 4 | 6 | 6 | 42 | use warnings; | ||||
| 6 | 12 | |||||||
| 6 | 349 | |||||||
| 5 | ||||||||
| 6 | 6 | 6 | 34 | use vars qw($VERSION @ISA); | ||||
| 6 | 17 | |||||||
| 6 | 662 | |||||||
| 7 | $VERSION = '0.07'; | |||||||
| 8 | ||||||||
| 9 | #-------------------------------------------------------------------------- | |||||||
| 10 | ||||||||
| 11 | =head1 NAME | |||||||
| 12 | ||||||||
| 13 | WWW::Scraper::ISBN::Waterstones_Driver - Search driver for the Waterstones 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 Waterstones online book catalog. | |||||||
| 22 | ||||||||
| 23 | =cut | |||||||
| 24 | ||||||||
| 25 | #-------------------------------------------------------------------------- | |||||||
| 26 | ||||||||
| 27 | ########################################################################### | |||||||
| 28 | # Inheritence | |||||||
| 29 | ||||||||
| 30 | 6 | 6 | 37 | use base qw(WWW::Scraper::ISBN::Driver); | ||||
| 6 | 11 | |||||||
| 6 | 4208 | |||||||
| 31 | ||||||||
| 32 | ########################################################################### | |||||||
| 33 | # Modules | |||||||
| 34 | ||||||||
| 35 | 6 | 6 | 13258 | use WWW::Mechanize; | ||||
| 6 | 1086701 | |||||||
| 6 | 394 | |||||||
| 36 | ||||||||
| 37 | ########################################################################### | |||||||
| 38 | # Constants | |||||||
| 39 | ||||||||
| 40 | 6 | 6 | 112 | use constant REFERER => 'http://www.waterstones.com'; | ||||
| 6 | 11 | |||||||
| 6 | 615 | |||||||
| 41 | 6 | 6 | 41 | use constant SEARCH => 'http://www.waterstones.com/waterstonesweb/simpleSearch.do?typeAheadFormSubmit.x=15&typeAheadFormSubmit.y=17&simpleSearchString='; | ||||
| 6 | 12 | |||||||
| 6 | 7162 | |||||||
| 42 | my ($URL1,$URL2) = ('http://www.waterstones.com/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 Waterstones | |||||||
| 78 | website. | |||||||
| 79 | ||||||||
| 80 | =back | |||||||
| 81 | ||||||||
| 82 | =cut | |||||||
| 83 | ||||||||
| 84 | sub search { | |||||||
| 85 | 3 | 3 | 1 | 13507 | my $self = shift; | |||
| 86 | 3 | 8 | my $isbn = shift; | |||||
| 87 | 3 | 14 | $self->found(0); | |||||
| 88 | 3 | 52 | $self->book(undef); | |||||
| 89 | ||||||||
| 90 | # validate and convert into EAN13 format | |||||||
| 91 | 3 | 76 | my $ean = $self->convert_to_ean13($isbn); | |||||
| 92 | 3 | 50 | 66 | 234 | return $self->handler("Invalid ISBN specified") | |||
| 33 | ||||||||
| 66 | ||||||||
| 33 | ||||||||
| 93 | if(!$ean || (length $isbn == 13 && $isbn ne $ean) | |||||||
| 94 | || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean))); | |||||||
| 95 | ||||||||
| 96 | 3 | 73 | my $mech = WWW::Mechanize->new(); | |||||
| 97 | 3 | 27958 | $mech->agent_alias( 'Windows IE 6' ); | |||||
| 98 | 3 | 208 | $mech->add_header( 'Accept-Encoding' => undef ); | |||||
| 99 | 3 | 44 | $mech->add_header( 'Referer' => REFERER ); | |||||
| 100 | ||||||||
| 101 | 3 | 27 | eval { $mech->get( SEARCH . $ean ) }; | |||||
| 3 | 18 | |||||||
| 102 | 3 | 50 | 33 | 804156 | return $self->handler("The Waterstones website appears to be unavailable.") | |||
| 33 | ||||||||
| 103 | if($@ || !$mech->success() || !$mech->content()); | |||||||
| 104 | ||||||||
| 105 | #print STDERR "\n# search=[".SEARCH."$ean]\n"; | |||||||
| 106 | #print STDERR "\n# is_html=".$mech->is_html().", content type=".$mech->content_type()."\n"; | |||||||
| 107 | #print STDERR "\n# dump headers=".$mech->dump_headers."\n"; | |||||||
| 108 | ||||||||
| 109 | # The Book page | |||||||
| 110 | 3 | 334 | my $html = $mech->content(); | |||||
| 111 | ||||||||
| 112 | 3 | 100 | 7742 | return $self->handler("Failed to find that book on the Waterstones website. [$isbn]") | ||||
| 113 | if($html =~ m|Sorry! We did not find any results for|si); | |||||||
| 114 | ||||||||
| 115 | 2 | 50 | 6514 | return $self->handler("Waterstones website has crashed. [$isbn]") | ||||
| 116 | if($html =~ m|Exception was UseCaseError: \d+|si); | |||||||
| 117 | ||||||||
| 118 | 2 | 767 | $html =~ s/&/&/g; | |||||
| 119 | #print STDERR "\n# content2=[\n$html\n]\n"; | |||||||
| 120 | ||||||||
| 121 | 2 | 7 | my $data; | |||||
| 122 | 2 | 3805 | ($data->{title}) = $html =~ m! \s* ([^<]+)!si; |
|||||
| 123 | 2 | 5357 | ($data->{author}) = $html =~ m! \s*by\s*]+>([^<]+)!si; |
|||||
| 124 | 2 | 3838 | ($data->{binding},$data->{pages}) | |||||
| 125 | = $html =~ m! | ([^<]+)\s+(\d+)\s+pages | !si;||||||
| 126 | 2 | 4191 | ($data->{description}) = $html =~ m! (.*?) !si; |
|||||
| 127 | 2 | 4309 | ($data->{pubdate}) = $html =~ m! Published |
|||||
| 128 | 2 | 4326 | ($data->{publisher}) = $html =~ m! Publisher |
|||||
| 129 | 2 | 4052 | ($data->{isbn13}) = $html =~ m! ISBN |
|||||
| 130 | 2 | 5458 | ($data->{image}) = $html =~ m! | |||||
| 131 | ||||||||
| 132 | #use Data::Dumper; | |||||||
| 133 | #print STDERR "\n# data=" . Dumper($data); | |||||||
| 134 | ||||||||
| 135 | 2 | 50 | 18 | return $self->handler("Could not extract data from the Waterstones result page. [$isbn]") | ||||
| 136 | unless(defined $data); | |||||||
| 137 | ||||||||
| 138 | 2 | 10 | for(qw(author publisher description title)) { | |||||
| 139 | 8 | 50 | 65 | $data->{$_} =~ s/?39;/'/g if($data->{$_}); | ||||
| 140 | } | |||||||
| 141 | ||||||||
| 142 | 2 | 21 | $data->{isbn10} = $self->convert_to_isbn10($ean); | |||||
| 143 | 2 | 50 | 200 | $data->{title} =~ s!\s*\($data->{binding}\)\s*!! if($data->{title}); | ||||
| 144 | 2 | 50 | 38 | $data->{description} =~ s!<[^>]+>!! if($data->{description}); | ||||
| 145 | ||||||||
| 146 | 2 | 50 | 9 | if($data->{image}) { | ||||
| 147 | 2 | 7 | $data->{thumb} = $data->{image}; | |||||
| 148 | 2 | 18 | $data->{thumb} =~ s!/images/nbd/[lms]/!/images/nbd/s/!; | |||||
| 149 | 2 | 38 | $data->{image} =~ s!/images/nbd/[lms]/!/images/nbd/l/!; | |||||
| 150 | } | |||||||
| 151 | ||||||||
| 152 | #use Data::Dumper; | |||||||
| 153 | #print STDERR "\n# data=" . Dumper($data); | |||||||
| 154 | ||||||||
| 155 | # trim top and tail | |||||||
| 156 | 2 | 16 | foreach (keys %$data) { | |||||
| 157 | 22 | 50 | 41 | next unless(defined $data->{$_}); | ||||
| 158 | 22 | 48 | $data->{$_} =~ s! ! !g; | |||||
| 159 | 22 | 54 | $data->{$_} =~ s/^\s+//; | |||||
| 160 | 22 | 125 | $data->{$_} =~ s/\s+$//; | |||||
| 161 | } | |||||||
| 162 | ||||||||
| 163 | 2 | 19 | my $url = $mech->uri(); | |||||
| 164 | 2 | 95 | $url =~ s/\?.*//; | |||||
| 165 | ||||||||
| 166 | 2 | 19 | my $bk = { | |||||
| 167 | 'ean13' => $data->{isbn13}, | |||||||
| 168 | 'isbn13' => $data->{isbn13}, | |||||||
| 169 | 'isbn10' => $data->{isbn10}, | |||||||
| 170 | 'isbn' => $data->{isbn13}, | |||||||
| 171 | 'author' => $data->{author}, | |||||||
| 172 | 'title' => $data->{title}, | |||||||
| 173 | 'book_link' => "$url", | |||||||
| 174 | 'image_link' => $data->{image}, | |||||||
| 175 | 'thumb_link' => $data->{thumb}, | |||||||
| 176 | 'description' => $data->{description}, | |||||||
| 177 | 'pubdate' => $data->{pubdate}, | |||||||
| 178 | 'publisher' => $data->{publisher}, | |||||||
| 179 | 'binding' => $data->{binding}, | |||||||
| 180 | 'pages' => $data->{pages}, | |||||||
| 181 | 'html' => $html | |||||||
| 182 | }; | |||||||
| 183 | ||||||||
| 184 | #use Data::Dumper; | |||||||
| 185 | #print STDERR "\n# book=".Dumper($bk); | |||||||
| 186 | ||||||||
| 187 | 2 | 48 | $self->book($bk); | |||||
| 188 | 2 | 32 | $self->found(1); | |||||
| 189 | 2 | 25 | return $self->book; | |||||
| 190 | } | |||||||
| 191 | ||||||||
| 192 | 1; | |||||||
| 193 | ||||||||
| 194 | __END__ |