| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Scraper::ISBN::OpenLibrary_Driver; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 6 |  |  | 6 |  | 200897 | use strict; | 
|  | 6 |  |  |  |  | 19 |  | 
|  | 6 |  |  |  |  | 230 |  | 
| 4 | 6 |  |  | 6 |  | 30 | use warnings; | 
|  | 6 |  |  |  |  | 14 |  | 
|  | 6 |  |  |  |  | 300 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 6 |  |  | 6 |  | 35 | use vars qw($VERSION @ISA); | 
|  | 6 |  |  |  |  | 16 |  | 
|  | 6 |  |  |  |  | 1262 |  | 
| 7 |  |  |  |  |  |  | $VERSION = '0.08'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | WWW::Scraper::ISBN::OpenLibrary_Driver - Search driver for OpenLibrary 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 OpenLibrary online book catalog | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =cut | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | ########################################################################### | 
| 28 |  |  |  |  |  |  | # Inheritence | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 6 |  |  | 6 |  | 38 | use base qw(WWW::Scraper::ISBN::Driver); | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 5225 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | ########################################################################### | 
| 33 |  |  |  |  |  |  | # Modules | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 6 |  |  | 6 |  | 17103 | use WWW::Mechanize; | 
|  | 6 |  |  |  |  | 1326951 |  | 
|  | 6 |  |  |  |  | 259 |  | 
| 36 | 6 |  |  | 6 |  | 5049 | use JSON; | 
|  | 6 |  |  |  |  | 65153 |  | 
|  | 6 |  |  |  |  | 60 |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | ########################################################################### | 
| 39 |  |  |  |  |  |  | # Constants | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 6 |  |  | 6 |  | 1072 | use constant	SEARCH	=> 'http://openlibrary.org/api/books?jscmd=data&format=json&bibkeys=ISBN:'; | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 307 |  | 
| 42 | 6 |  |  | 6 |  | 27 | use constant	LB2G    => 453.59237;   # number of grams in a pound (lb) | 
|  | 6 |  |  |  |  | 11 |  | 
|  | 6 |  |  |  |  | 244 |  | 
| 43 | 6 |  |  | 6 |  | 27 | use constant	OZ2G    => 28.3495231;  # number of grams in an ounce (oz) | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 214 |  | 
| 44 | 6 |  |  | 6 |  | 54 | use constant	IN2MM   => 25.4;        # number of inches in a millimetre (mm) | 
|  | 6 |  |  |  |  | 9 |  | 
|  | 6 |  |  |  |  | 11722 |  | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | ########################################################################### | 
| 49 |  |  |  |  |  |  | # Public Interface | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =head1 METHODS | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =over 4 | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =item C | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | Creates a query string, then passes the appropriate form fields to the OpenLibrary | 
| 58 |  |  |  |  |  |  | server. | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | The returned page should be the correct catalog page for that ISBN. If not the | 
| 61 |  |  |  |  |  |  | function returns zero and allows the next driver in the chain to have a go. If | 
| 62 |  |  |  |  |  |  | a valid page is returned, the following fields are returned via the book hash: | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | isbn          (now returns isbn13) | 
| 65 |  |  |  |  |  |  | isbn10 | 
| 66 |  |  |  |  |  |  | isbn13 | 
| 67 |  |  |  |  |  |  | ean13         (industry name) | 
| 68 |  |  |  |  |  |  | author | 
| 69 |  |  |  |  |  |  | title | 
| 70 |  |  |  |  |  |  | book_link | 
| 71 |  |  |  |  |  |  | image_link | 
| 72 |  |  |  |  |  |  | pubdate | 
| 73 |  |  |  |  |  |  | publisher | 
| 74 |  |  |  |  |  |  | binding       (if known) | 
| 75 |  |  |  |  |  |  | pages         (if known) | 
| 76 |  |  |  |  |  |  | weight        (if known) (in grammes) | 
| 77 |  |  |  |  |  |  | width         (if known) (in millimetres) | 
| 78 |  |  |  |  |  |  | height        (if known) (in millimetres) | 
| 79 |  |  |  |  |  |  | depth         (if known) (in millimetres) | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | The book_link and image_link refer back to the OpenLibrary website. | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =back | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =cut | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub search { | 
| 88 | 4 |  |  | 4 | 1 | 27715 | my $self = shift; | 
| 89 | 4 |  |  |  |  | 11 | my $isbn = shift; | 
| 90 | 4 |  |  |  |  | 8 | my $data; | 
| 91 | 4 |  |  |  |  | 20 | $self->found(0); | 
| 92 | 4 |  |  |  |  | 67 | $self->book(undef); | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 4 |  |  |  |  | 62 | my $mech = WWW::Mechanize->new(); | 
| 95 | 4 |  |  |  |  | 26740 | $mech->agent_alias( 'Linux Mozilla' ); | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 4 |  |  |  |  | 242 | eval { $mech->get( SEARCH . $isbn ) }; | 
|  | 4 |  |  |  |  | 27 |  | 
| 98 | 4 | 50 | 33 |  |  | 2404004 | return $self->handler("OpenLibrary website appears to be unavailable.") | 
|  |  |  | 33 |  |  |  |  | 
| 99 |  |  |  |  |  |  | if($@ || !$mech->success() || !$mech->content()); | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 4 |  |  |  |  | 245 | my $code = decode_json($mech->content()); | 
| 102 |  |  |  |  |  |  | #use Data::Dumper; | 
| 103 |  |  |  |  |  |  | #print STDERR "\n# code=".Dumper($code); | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 4 | 100 |  |  |  | 298 | return $self->handler("Failed to find that book on OpenLibrary website.") | 
| 106 |  |  |  |  |  |  | unless($code->{'ISBN:'.$isbn}); | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 3 |  |  |  |  | 22 | $data->{isbn13}         = $code->{'ISBN:'.$isbn}{identifiers}{isbn_13}[0]; | 
| 109 | 3 |  |  |  |  | 16 | $data->{isbn10}         = $code->{'ISBN:'.$isbn}{identifiers}{isbn_10}[0]; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 3 | 50 | 66 |  |  | 24 | return $self->handler("Failed to find that book on OpenLibrary website.") | 
| 112 |  |  |  |  |  |  | unless($isbn eq $data->{isbn13} || $isbn eq $data->{isbn10}); | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | #use Data::Dumper; | 
| 115 |  |  |  |  |  |  | #print STDERR "\n# " . Dumper($data); | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 3 |  |  |  |  | 12 | $data->{isbn13}         = $code->{'ISBN:'.$isbn}{identifiers}{isbn_13}[0]; | 
| 118 | 3 |  |  |  |  | 14 | $data->{isbn10}         = $code->{'ISBN:'.$isbn}{identifiers}{isbn_10}[0]; | 
| 119 | 3 |  |  |  |  | 16 | $data->{image}          = $code->{'ISBN:'.$isbn}{cover}{large}; | 
| 120 | 3 |  |  |  |  | 12 | $data->{thumb}          = $code->{'ISBN:'.$isbn}{cover}{small}; | 
| 121 | 3 |  |  |  |  | 13 | $data->{author}         = $code->{'ISBN:'.$isbn}{authors}[0]{name}; | 
| 122 | 3 |  |  |  |  | 11 | $data->{title}          = $code->{'ISBN:'.$isbn}{title}; | 
| 123 | 3 |  |  |  |  | 14 | $data->{publisher}      = $code->{'ISBN:'.$isbn}{publishers}[0]{name}; | 
| 124 | 3 |  |  |  |  | 13 | $data->{pubdate}        = $code->{'ISBN:'.$isbn}{publish_date}; | 
| 125 | 3 |  |  |  |  | 13 | $data->{pages}          = $code->{'ISBN:'.$isbn}{number_of_pages}; | 
| 126 | 3 |  |  |  |  | 9 | $data->{url}            = $code->{'ISBN:'.$isbn}{url}; | 
| 127 | 3 |  |  |  |  | 11 | $data->{weight}         = $code->{'ISBN:'.$isbn}{weight}; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 3 | 100 | 100 |  |  | 62 | if($data->{weight}&& $data->{weight} =~ /([\d.]+)\s*(?:lbs|pounds)/) { | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  | 50 | 33 |  |  |  |  | 
| 130 | 1 |  |  |  |  | 10 | $data->{weight} = int($1 * LB2G); | 
| 131 |  |  |  |  |  |  | } elsif($data->{weight} && $data->{weight} =~ /([\d.]+)\s*(?:ozs|ounces)/) { | 
| 132 | 1 |  |  |  |  | 25 | $data->{weight} = int($1 * OZ2G); | 
| 133 |  |  |  |  |  |  | } elsif($data->{weight} && $data->{weight} =~ /([\d.]+)\s*(?:g|grams)/) { | 
| 134 | 0 |  |  |  |  | 0 | $data->{weight} = int($1); | 
| 135 |  |  |  |  |  |  | } else { | 
| 136 | 1 |  |  |  |  | 4 | $data->{weight} = undef; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 3 |  |  |  |  | 10 | eval { $mech->get( $data->{url} ) }; | 
|  | 3 |  |  |  |  | 20 |  | 
| 140 | 3 | 0 | 33 |  |  | 913844 | return $self->handler("OpenLibrary website appears to be unavailable.") | 
|  |  |  | 33 |  |  |  |  | 
| 141 |  |  |  |  |  |  | if($@ || !$mech->success() || !$mech->content()); | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | # The Book page | 
| 144 | 0 |  |  |  |  |  | my $html = $mech->content(); | 
| 145 |  |  |  |  |  |  | #print STDERR "\n# html=[\n$html\n]\n"; | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | # catch any data not in the JSON | 
| 148 | 0 |  |  |  |  |  | ($data->{height},$data->{width},$data->{depth}) | 
| 149 |  |  |  |  |  |  | = $html =~ m! | Dimensions\s* | ([\d.]+)\s+x\s+([\d.]+)\s+x\s+([\d.]+)\s+inches!i; | 
| 150 | 0 |  |  |  |  |  | ($data->{binding})                  = $html =~ m! | Format\s* | ([^<]+)!i; | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 0 | 0 |  |  |  |  | $data->{height} = int($data->{height} * IN2MM)  if($data->{height}); | 
| 153 | 0 | 0 |  |  |  |  | $data->{width}  = int($data->{width}  * IN2MM)  if($data->{width}); | 
| 154 | 0 | 0 |  |  |  |  | $data->{depth}  = int($data->{depth}  * IN2MM)  if($data->{depth}); | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | #use Data::Dumper; | 
| 157 |  |  |  |  |  |  | #print STDERR "\n# " . Dumper($data); | 
| 158 |  |  |  |  |  |  |  | 
| 159 | 0 | 0 |  |  |  |  | return $self->handler("Could not extract data from OpenLibrary result page.") | 
| 160 |  |  |  |  |  |  | unless(defined $data); | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | # trim top and tail | 
| 163 | 0 | 0 |  |  |  |  | foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 0 |  |  |  |  |  | my $bk = { | 
| 166 |  |  |  |  |  |  | 'ean13'		    => $data->{isbn13}, | 
| 167 |  |  |  |  |  |  | 'isbn13'		=> $data->{isbn13}, | 
| 168 |  |  |  |  |  |  | 'isbn10'		=> $data->{isbn10}, | 
| 169 |  |  |  |  |  |  | 'isbn'			=> $data->{isbn13}, | 
| 170 |  |  |  |  |  |  | 'author'		=> $data->{author}, | 
| 171 |  |  |  |  |  |  | 'title'			=> $data->{title}, | 
| 172 |  |  |  |  |  |  | 'book_link'		=> $mech->uri(), | 
| 173 |  |  |  |  |  |  | 'image_link'	=> $data->{image}, | 
| 174 |  |  |  |  |  |  | 'thumb_link'	=> $data->{thumb}, | 
| 175 |  |  |  |  |  |  | 'pubdate'		=> $data->{pubdate}, | 
| 176 |  |  |  |  |  |  | 'publisher'		=> $data->{publisher}, | 
| 177 |  |  |  |  |  |  | 'binding'	    => $data->{binding}, | 
| 178 |  |  |  |  |  |  | 'pages'		    => $data->{pages}, | 
| 179 |  |  |  |  |  |  | 'weight'		=> $data->{weight}, | 
| 180 |  |  |  |  |  |  | 'width'		    => $data->{width}, | 
| 181 |  |  |  |  |  |  | 'height'		=> $data->{height}, | 
| 182 |  |  |  |  |  |  | 'depth'		    => $data->{depth}, | 
| 183 |  |  |  |  |  |  | 'json'          => $code, | 
| 184 |  |  |  |  |  |  | 'html'          => $html | 
| 185 |  |  |  |  |  |  | }; | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | #use Data::Dumper; | 
| 188 |  |  |  |  |  |  | #print STDERR "\n# book=".Dumper($bk); | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 0 |  |  |  |  |  | $self->book($bk); | 
| 191 | 0 |  |  |  |  |  | $self->found(1); | 
| 192 | 0 |  |  |  |  |  | return $self->book; | 
| 193 |  |  |  |  |  |  | } | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | 1; | 
| 196 |  |  |  |  |  |  | __END__ |