| 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 | 101996 | use strict; | |||||
| 6 | 15 | ||||||||
| 6 | 237 | ||||||||
| 4 | 6 | 6 | 28 | use warnings; | |||||
| 6 | 10 | ||||||||
| 6 | 197 | ||||||||
| 5 | |||||||||
| 6 | 6 | 6 | 23 | use vars qw($VERSION @ISA); | |||||
| 6 | 9 | ||||||||
| 6 | 484 | ||||||||
| 7 | $VERSION = '0.14'; | ||||||||
| 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 | 32 | use base qw(WWW::Scraper::ISBN::Driver); | |||||
| 6 | 9 | ||||||||
| 6 | 3057 | ||||||||
| 31 | |||||||||
| 32 | ########################################################################### | ||||||||
| 33 | # Modules | ||||||||
| 34 | |||||||||
| 35 | 6 | 6 | 8580 | use WWW::Mechanize; | |||||
| 6 | 844825 | ||||||||
| 6 | 312 | ||||||||
| 36 | |||||||||
| 37 | ########################################################################### | ||||||||
| 38 | # Constants | ||||||||
| 39 | |||||||||
| 40 | 6 | 6 | 64 | use constant SEARCH => 'http://www.wheelers.co.nz/search/results/?query='; | |||||
| 6 | 9 | ||||||||
| 6 | 5301 | ||||||||
| 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 | 22044 | my $self = shift; | ||||
| 85 | 3 | 6 | my $isbn = shift; | ||||||
| 86 | 3 | 15 | $self->found(0); | ||||||
| 87 | 3 | 46 | $self->book(undef); | ||||||
| 88 | |||||||||
| 89 | 3 | 36 | my $mech = WWW::Mechanize->new(); | ||||||
| 90 | 3 | 29542 | $mech->agent_alias( 'Linux Mozilla' ); | ||||||
| 91 | |||||||||
| 92 | 3 | 270 | eval { $mech->get( SEARCH . $isbn ) }; | ||||||
| 3 | 16 | ||||||||
| 93 | 3 | 50 | 33 | 8630102 | return $self->handler("Wheelers website appears to be unavailable.") | ||||
| 33 | |||||||||
| 94 | if($@ || !$mech->success() || !$mech->content()); | ||||||||
| 95 | |||||||||
| 96 | # The Book page | ||||||||
| 97 | 3 | 221 | my $html = $mech->content(); | ||||||
| 98 | |||||||||
| 99 | 3 | 50 | 11105 | 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 | 14 | my $data; | ||||||
| 105 | 3 | 23391 | ($data->{image}) = $html =~ m!href="(.*?/large/\d+/\d+.jpg)!i; | ||||||
| 106 | 3 | 8263 | ($data->{thumb}) = $html =~ m!src="(.*?/small/\d+/\d+.jpg)!i; | ||||||
| 107 | 3 | 56 | ($data->{author}) = $html =~ m! | ||||||
| 108 | 3 | 6972 | ($data->{title}) = $html =~ m!\s*([^,<]+)!i; |
||||||
| 109 | 3 | 13180 | ($data->{publisher}) = $html =~ m! | Publisher | \s*]+>([^<]+) | !i;||||
| 110 | 3 | 6681 | ($data->{pubdate}) = $html =~ m! | International Publication Date | \s*([^<]+) | !i;||||
| 111 | 3 | 6762 | ($data->{isbn13}) = $html =~ m! | ISBN-13 | \s*(\d+) | !i;||||
| 112 | 3 | 6437 | ($data->{isbn10}) = $html =~ m! | ISBN-10 | \s*(\d+) | !i;||||
| 113 | 3 | 120 | ($data->{binding}) = $html =~ m! | Format | \s*([^<]+) | ||||
| 114 | 3 | 97 | ($data->{pages}) = $html =~ m! | Number of Pages | \s*([^<]+) | !s;||||
| 115 | 3 | 343 | ($data->{width},$data->{height}) = $html =~ m! | ||||||
| Dimensions | \s*Width:\s*([\d.]+)mm Height:\s*([\d.]+)mm (?:Spine:\s*([\d.]+)mm)? | \s*||||||||
| 116 | 3 | 308 | ($data->{weight}) = $html =~ m! | ||||||
| Weight | \s*(\d+)g | \s*||||||||
| 117 | 3 | 5952 | ($data->{description}) = $html =~ m!Description of this book\s*([^<]+) !i; |
||||||
| 118 | |||||||||
| 119 | 3 | 27 | $data->{binding} =~ s/,.*//; | ||||||
| 120 | 3 | 17 | for(qw(image thumb)) { | ||||||
| 121 | 6 | 50 | 28 | next unless(defined $data->{$_}); | |||||
| 122 | 6 | 50 | 25 | next if($data->{$_} =~ m!^http://!); | |||||
| 123 | 6 | 43 | $data->{$_} =~ s!^//!http://!; | ||||||
| 124 | } | ||||||||
| 125 | |||||||||
| 126 | 3 | 32 | my $base = $mech->uri(); | ||||||
| 127 | 3 | 136 | $base =~ s!(http://[^/]+).*!$1!; | ||||||
| 128 | |||||||||
| 129 | #use Data::Dumper; | ||||||||
| 130 | #print STDERR "\n# " . Dumper($data); | ||||||||
| 131 | |||||||||
| 132 | 3 | 50 | 59 | return $self->handler("Could not extract data from Wheelers result page.") | |||||
| 133 | unless(defined $data); | ||||||||
| 134 | |||||||||
| 135 | # trim top and tail | ||||||||
| 136 | 3 | 50 | 20 | foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } | |||||
| 42 | 69 | ||||||||
| 42 | 71 | ||||||||
| 42 | 155 | ||||||||
| 137 | |||||||||
| 138 | 3 | 18 | 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 | 101 | $self->book($bk); | ||||||
| 163 | 3 | 65 | $self->found(1); | ||||||
| 164 | 3 | 30 | return $self->book; | ||||||
| 165 | } | ||||||||
| 166 | |||||||||
| 167 | 1; | ||||||||
| 168 | __END__ |