| blib/lib/WWW/Scraper/ISBN/Wheelers_Driver.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % | 
| statement | 59 | 59 | 100.0 | 
| branch | 6 | 12 | 50.0 | 
| condition | 2 | 6 | 33.3 | 
| subroutine | 7 | 7 | 100.0 | 
| pod | 1 | 1 | 100.0 | 
| total | 75 | 85 | 88.2 | 
| line | stmt | bran | cond | sub | pod | time | code | ||
|---|---|---|---|---|---|---|---|---|---|
| 1 | package WWW::Scraper::ISBN::Wheelers_Driver; | ||||||||
| 2 | |||||||||
| 3 | 6 | 6 | 249834 | use strict; | |||||
| 6 | 20 | ||||||||
| 6 | 261 | ||||||||
| 4 | 6 | 6 | 34 | use warnings; | |||||
| 6 | 11 | ||||||||
| 6 | 276 | ||||||||
| 5 | |||||||||
| 6 | 6 | 6 | 39 | use vars qw($VERSION @ISA); | |||||
| 6 | 15 | ||||||||
| 6 | 544 | ||||||||
| 7 | $VERSION = '0.13'; | ||||||||
| 8 | |||||||||
| 9 | #-------------------------------------------------------------------------- | ||||||||
| 10 | |||||||||
| 11 | =head1 NAME | ||||||||
| 12 | |||||||||
| 13 | WWW::Scraper::ISBN::Wheelers_Driver - Search drivers for the Wheelers' 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 Wheelers' online book catalog. | ||||||||
| 22 | |||||||||
| 23 | =cut | ||||||||
| 24 | |||||||||
| 25 | #-------------------------------------------------------------------------- | ||||||||
| 26 | |||||||||
| 27 | ########################################################################### | ||||||||
| 28 | # Inheritence | ||||||||
| 29 | |||||||||
| 30 | 6 | 6 | 31 | use base qw(WWW::Scraper::ISBN::Driver); | |||||
| 6 | 13 | ||||||||
| 6 | 6393 | ||||||||
| 31 | |||||||||
| 32 | ########################################################################### | ||||||||
| 33 | # Modules | ||||||||
| 34 | |||||||||
| 35 | 6 | 6 | 18265 | use WWW::Mechanize; | |||||
| 6 | 1534218 | ||||||||
| 6 | 298 | ||||||||
| 36 | |||||||||
| 37 | ########################################################################### | ||||||||
| 38 | # Constants | ||||||||
| 39 | |||||||||
| 40 | 6 | 6 | 78 | use constant SEARCH => 'http://www.wheelers.co.nz/search/results/?query='; | |||||
| 6 | 15 | ||||||||
| 6 | 6741 | ||||||||
| 41 | |||||||||
| 42 | #-------------------------------------------------------------------------- | ||||||||
| 43 | |||||||||
| 44 | ########################################################################### | ||||||||
| 45 | # Public Interface | ||||||||
| 46 | |||||||||
| 47 | =head1 METHODS | ||||||||
| 48 | |||||||||
| 49 | =over 4 | ||||||||
| 50 | |||||||||
| 51 | =item C | ||||||||
| 52 | |||||||||
| 53 | Creates a query string, then passes the appropriate form fields to the | ||||||||
| 54 | Wheelers server. | ||||||||
| 55 | |||||||||
| 56 | The returned page should be the correct catalog page for that ISBN. If not the | ||||||||
| 57 | function returns zero and allows the next driver in the chain to have a go. If | ||||||||
| 58 | a valid page is returned, the following fields are returned via the book hash: | ||||||||
| 59 | |||||||||
| 60 | isbn (now returns isbn13) | ||||||||
| 61 | isbn10 | ||||||||
| 62 | isbn13 | ||||||||
| 63 | ean13 (industry name) | ||||||||
| 64 | author | ||||||||
| 65 | title | ||||||||
| 66 | book_link | ||||||||
| 67 | image_link | ||||||||
| 68 | description | ||||||||
| 69 | pubdate | ||||||||
| 70 | publisher | ||||||||
| 71 | binding (if known) | ||||||||
| 72 | pages (if known) | ||||||||
| 73 | weight (if known) (in grammes) | ||||||||
| 74 | width (if known) (in millimetres) | ||||||||
| 75 | height (if known) (in millimetres) | ||||||||
| 76 | |||||||||
| 77 | The book_link and image_link refer back to the Wheelers website. | ||||||||
| 78 | |||||||||
| 79 | =back | ||||||||
| 80 | |||||||||
| 81 | =cut | ||||||||
| 82 | |||||||||
| 83 | sub search { | ||||||||
| 84 | 3 | 3 | 1 | 27919 | my $self = shift; | ||||
| 85 | 3 | 10 | my $isbn = shift; | ||||||
| 86 | 3 | 28 | $self->found(0); | ||||||
| 87 | 3 | 57 | $self->book(undef); | ||||||
| 88 | |||||||||
| 89 | 3 | 48 | my $mech = WWW::Mechanize->new(); | ||||||
| 90 | 3 | 19989 | $mech->agent_alias( 'Linux Mozilla' ); | ||||||
| 91 | |||||||||
| 92 | 3 | 249 | eval { $mech->get( SEARCH . $isbn ) }; | ||||||
| 3 | 18 | ||||||||
| 93 | 3 | 50 | 33 | 9034652 | return $self->handler("Wheelers website appears to be unavailable.") | ||||
| 33 | |||||||||
| 94 | if($@ || !$mech->success() || !$mech->content()); | ||||||||
| 95 | |||||||||
| 96 | # The Book page | ||||||||
| 97 | 3 | 218 | my $html = $mech->content(); | ||||||
| 98 | |||||||||
| 99 | 3 | 50 | 11167 | return $self->handler("Failed to find that book on Wheelers website.") | |||||
| 100 | if($html =~ m!Your search returned 0 results!si); | ||||||||
| 101 | |||||||||
| 102 | #print STDERR "\n# html=[\n$html\n]\n"; | ||||||||
| 103 | |||||||||
| 104 | 3 | 12 | my $data; | ||||||
| 105 | 3 | 23684 | ($data->{image}) = $html =~ m!href="(.*?/large/\d+/\d+.jpg)!i; | ||||||
| 106 | 3 | 6914 | ($data->{thumb}) = $html =~ m!src="(.*?/small/\d+/\d+.jpg)!i; | ||||||
| 107 | 3 | 104 | ($data->{author})                   = $html =~ m! | ||||||
| 108 | 3 | 5228 | ($data->{title})                    = $html =~ m!\s* ([^,<]+)!i; | ||||||
| 109 | 3 | 14460 | ($data->{publisher}) = $html =~ m! | Publisher\s* | ]+>([^<]+)!i; | ||||
| 110 | 3 | 7571 | ($data->{pubdate}) = $html =~ m! | International Publication Date\s* | ([^<]+)!i; | ||||
| 111 | 3 | 6772 | ($data->{isbn13}) = $html =~ m! | ISBN-13\s* | (\d+)!i; | ||||
| 112 | 3 | 7707 | ($data->{isbn10}) = $html =~ m! | ISBN-10\s* | (\d+)!i; | ||||
| 113 | 3 | 112 | ($data->{binding}) = $html =~ m! | Format\s* | ([^<]+) | ||||
| 114 | 3 | 235 | ($data->{pages}) = $html =~ m! | Number of Pages\s* | ([^<]+)!s; | ||||
| 115 | 3 | 772 | ($data->{width},$data->{height}) = $html =~ m! | ||||||
| Dimensions\s* | Width:\s*([\d.]+)mm \s*Height:\s*([\d.]+)mm (?:Spine:\s*([\d.]+)mm)? | ||||||||
| 116 | 3 | 572 | ($data->{weight}) = $html =~ m! | ||||||
| Weight\s* | (\d+)g\s* | ||||||||
| 117 | 3 | 7640 | ($data->{description})              = $html =~ m! Description of this book\s*([^<]+)!i; | ||||||
| 118 | |||||||||
| 119 | 3 | 24 | $data->{binding} =~ s/,.*//; | ||||||
| 120 | 3 | 22 | for(qw(image thumb)) { | ||||||
| 121 | 6 | 50 | 33 | next unless(defined $data->{$_}); | |||||
| 122 | 6 | 50 | 28 | next if($data->{$_} =~ m!^http://!); | |||||
| 123 | 6 | 48 | $data->{$_} =~ s!^//!http://!; | ||||||
| 124 | } | ||||||||
| 125 | |||||||||
| 126 | 3 | 38 | my $base = $mech->uri(); | ||||||
| 127 | 3 | 342 | $base =~ s!(http://[^/]+).*!$1!; | ||||||
| 128 | |||||||||
| 129 | #use Data::Dumper; | ||||||||
| 130 | #print STDERR "\n# " . Dumper($data); | ||||||||
| 131 | |||||||||
| 132 | 3 | 50 | 74 | return $self->handler("Could not extract data from Wheelers result page.") | |||||
| 133 | unless(defined $data); | ||||||||
| 134 | |||||||||
| 135 | # trim top and tail | ||||||||
| 136 | 3 | 50 | 24 | foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } | |||||
| 42 | 79 | ||||||||
| 42 | 78 | ||||||||
| 42 | 168 | ||||||||
| 137 | |||||||||
| 138 | 3 | 30 | my $bk = { | ||||||
| 139 | 'ean13' => $data->{isbn13}, | ||||||||
| 140 | 'isbn13' => $data->{isbn13}, | ||||||||
| 141 | 'isbn10' => $data->{isbn10}, | ||||||||
| 142 | 'isbn' => $data->{isbn13}, | ||||||||
| 143 | 'author' => $data->{author}, | ||||||||
| 144 | 'title' => $data->{title}, | ||||||||
| 145 | 'book_link' => $mech->uri(), | ||||||||
| 146 | 'image_link' => $data->{image}, | ||||||||
| 147 | 'thumb_link' => $data->{thumb}, | ||||||||
| 148 | 'description' => $data->{description}, | ||||||||
| 149 | 'pubdate' => $data->{pubdate}, | ||||||||
| 150 | 'publisher' => $data->{publisher}, | ||||||||
| 151 | 'binding' => $data->{binding}, | ||||||||
| 152 | 'pages' => $data->{pages}, | ||||||||
| 153 | 'weight' => $data->{weight}, | ||||||||
| 154 | 'width' => $data->{width}, | ||||||||
| 155 | 'height' => $data->{height}, | ||||||||
| 156 | 'html' => $html | ||||||||
| 157 | }; | ||||||||
| 158 | |||||||||
| 159 | #use Data::Dumper; | ||||||||
| 160 | #print STDERR "\n# book=".Dumper($bk); | ||||||||
| 161 | |||||||||
| 162 | 3 | 139 | $self->book($bk); | ||||||
| 163 | 3 | 147 | $self->found(1); | ||||||
| 164 | 3 | 32 | return $self->book; | ||||||
| 165 | } | ||||||||
| 166 | |||||||||
| 167 | 1; | ||||||||
| 168 | __END__ |