| blib/lib/WWW/Scraper/ISBN/TheNile_Driver.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % | 
| statement | 60 | 60 | 100.0 | 
| branch | 10 | 18 | 55.5 | 
| condition | 8 | 18 | 44.4 | 
| subroutine | 7 | 7 | 100.0 | 
| pod | 1 | 1 | 100.0 | 
| total | 86 | 104 | 82.6 | 
| line | stmt | bran | cond | sub | pod | time | code | 
|---|---|---|---|---|---|---|---|
| 1 | package WWW::Scraper::ISBN::TheNile_Driver; | ||||||
| 2 | |||||||
| 3 | 6 | 6 | 219242 | use strict; | |||
| 6 | 25 | ||||||
| 6 | 282 | ||||||
| 4 | 6 | 6 | 36 | use warnings; | |||
| 6 | 10 | ||||||
| 6 | 259 | ||||||
| 5 | |||||||
| 6 | 6 | 6 | 32 | use vars qw($VERSION @ISA); | |||
| 6 | 15 | ||||||
| 6 | 617 | ||||||
| 7 | $VERSION = '0.13'; | ||||||
| 8 | |||||||
| 9 | #-------------------------------------------------------------------------- | ||||||
| 10 | |||||||
| 11 | =head1 NAME | ||||||
| 12 | |||||||
| 13 | WWW::Scraper::ISBN::TheNile_Driver - Search driver for TheNile 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 TheNile 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 | 14 | ||||||
| 6 | 333146 | ||||||
| 31 | |||||||
| 32 | ########################################################################### | ||||||
| 33 | # Modules | ||||||
| 34 | |||||||
| 35 | 6 | 6 | 20867 | use WWW::Mechanize; | |||
| 6 | 1464460 | ||||||
| 6 | 330 | ||||||
| 36 | |||||||
| 37 | ########################################################################### | ||||||
| 38 | # Constants | ||||||
| 39 | |||||||
| 40 | 6 | 6 | 191 | use constant SEARCH => 'http://www.thenile.com.au/search.php?s='; | |||
| 6 | 14 | ||||||
| 6 | 8347 | ||||||
| 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 | TheNile 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 TheNile website. | ||||||
| 78 | |||||||
| 79 | =back | ||||||
| 80 | |||||||
| 81 | =cut | ||||||
| 82 | |||||||
| 83 | sub search { | ||||||
| 84 | 4 | 4 | 1 | 35146 | my $self = shift; | ||
| 85 | 4 | 384 | my $isbn = shift; | ||||
| 86 | 4 | 27 | $self->found(0); | ||||
| 87 | 4 | 70 | $self->book(undef); | ||||
| 88 | |||||||
| 89 | # validate and convert into EAN13 format | ||||||
| 90 | 4 | 54 | my $ean = $self->convert_to_ean13($isbn); | ||||
| 91 | 4 | 50 | 66 | 267 | return $self->handler("Invalid ISBN specified [$isbn]") | ||
| 33 | |||||||
| 66 | |||||||
| 33 | |||||||
| 92 | if(!$ean || (length $isbn == 13 && $isbn ne $ean) | ||||||
| 93 | || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean))); | ||||||
| 94 | 4 | 55 | $isbn = $ean; | ||||
| 95 | |||||||
| 96 | 4 | 37 | my $mech = WWW::Mechanize->new(); | ||||
| 97 | 4 | 18441 | $mech->agent_alias( 'Linux Mozilla' ); | ||||
| 98 | |||||||
| 99 | 4 | 253 | eval { $mech->get( SEARCH . $isbn ) }; | ||||
| 4 | 24 | ||||||
| 100 | 4 | 50 | 33 | 3565673 | return $self->handler("TheNile website appears to be unavailable.") | ||
| 33 | |||||||
| 101 | if($@ || !$mech->success() || !$mech->content()); | ||||||
| 102 | |||||||
| 103 | # The Book page | ||||||
| 104 | 4 | 282 | my $html = $mech->content(); | ||||
| 105 | |||||||
| 106 | 4 | 50 | 6326 | return $self->handler("Failed to find that book on TheNile website.") | |||
| 107 | if($html =~ m!Sorry your search.*?did not return any results|This book is currently unavailable!si); | ||||||
| 108 | |||||||
| 109 | #print STDERR "\n# html=[\n$html\n]\n"; | ||||||
| 110 | |||||||
| 111 | 4 | 101 | my $data; | ||||
| 112 | 4 | 227 | ($data->{image}) = $html =~ m!(http://c\d+\.mrcdn\.net/[\w\.\/]+\.jpg)!si; | ||||
| 113 | 4 | 183 | ($data->{thumb}) = $html =~ m!(http://c\d+\.mrcdn\.net/[\w\.\/]+\.jpg)!si; | ||||
| 114 | 4 | 3577 | ($data->{isbn13},$data->{isbn10})   = $html =~ m! ]+>\s*(\d+)\s*/\s*(\d+)\s* | ||||
| 115 | 4 | 3454 | ($data->{author})                   = $html =~ m! | ||||
| 116 | 4 | 50 | 23 | ($data->{author})                   = $html =~ m! | |||
| 117 | 4 | 1444 | ($data->{title})                    = $html =~ m! | ||||
| 118 | 4 | 4061 | ($data->{publisher})                = $html =~ m! ]+>\s* ]+>\s*([^<]+) | ||||
| 119 | 4 | 50 | 26 | ($data->{publisher})                = $html =~ m! | |||
| 120 | 4 | 1639 | ($data->{pubdate})                  = $html =~ m! | ||||
| 121 | 4 | 3636 | ($data->{binding})                  = $html =~ m! ]+>\s*([^<]+) | ||||
| 122 | 4 | 1661 | ($data->{pages})                    = $html =~ m! | ||||
| 123 | 4 | 4644 | ($data->{weight})                   = $html =~ m! | ||||
| 124 | 4 | 1652 | ($data->{width},$data->{height})    = $html =~ m! | ||||
| 125 | 4 | 1919 | ($data->{description})              = $html =~ m! Annotation\s*\s*(.*?)!si; | ||||
| 126 | 4 | 50 | 23 | ($data->{description})              = $html =~ m! Publisher Description\s*\s*(.*?)!si unless($data->{description}); | |||
| 127 | |||||||
| 128 | 4 | 18 | for(qw(author publisher description)) { | ||||
| 129 | 12 | 50 | 346 | $data->{$_} =~ s!<[^>]+>!!g if($data->{$_}); | |||
| 130 | } | ||||||
| 131 | |||||||
| 132 | #use Data::Dumper; | ||||||
| 133 | #print STDERR "\n# " . Dumper($data); | ||||||
| 134 | |||||||
| 135 | 4 | 50 | 17 | return $self->handler("Could not extract data from TheNile result page.") | |||
| 136 | unless(defined $data); | ||||||
| 137 | |||||||
| 138 | # trim top and tail | ||||||
| 139 | 4 | 100 | 39 | foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } | |||
| 56 | 124 | ||||||
| 52 | 118 | ||||||
| 52 | 164 | ||||||
| 140 | |||||||
| 141 | 4 | 54 | my $url = $mech->uri(); | ||||
| 142 | |||||||
| 143 | 4 | 212 | my $bk = { | ||||
| 144 | 'ean13' => $data->{isbn13}, | ||||||
| 145 | 'isbn13' => $data->{isbn13}, | ||||||
| 146 | 'isbn10' => $data->{isbn10}, | ||||||
| 147 | 'isbn' => $data->{isbn13}, | ||||||
| 148 | 'author' => $data->{author}, | ||||||
| 149 | 'title' => $data->{title}, | ||||||
| 150 | 'book_link' => "$url", | ||||||
| 151 | 'image_link' => $data->{image}, | ||||||
| 152 | 'thumb_link' => $data->{thumb}, | ||||||
| 153 | 'description' => $data->{description}, | ||||||
| 154 | 'pubdate' => $data->{pubdate}, | ||||||
| 155 | 'publisher' => $data->{publisher}, | ||||||
| 156 | 'binding' => $data->{binding}, | ||||||
| 157 | 'pages' => $data->{pages}, | ||||||
| 158 | 'weight' => $data->{weight}, | ||||||
| 159 | 'width' => $data->{width}, | ||||||
| 160 | 'height' => $data->{height}, | ||||||
| 161 | 'depth' => $data->{depth}, | ||||||
| 162 | 'html' => $html | ||||||
| 163 | }; | ||||||
| 164 | |||||||
| 165 | #use Data::Dumper; | ||||||
| 166 | #print STDERR "\n# book=".Dumper($bk); | ||||||
| 167 | |||||||
| 168 | 4 | 278 | $self->book($bk); | ||||
| 169 | 4 | 66 | $self->found(1); | ||||
| 170 | 4 | 45 | return $self->book; | ||||
| 171 | } | ||||||
| 172 | |||||||
| 173 | 1; | ||||||
| 174 | |||||||
| 175 | __END__ |