blib/lib/WWW/Scraper/ISBN/GoogleBooks_Driver.pm

filter: Statementstmt 30.5% 40 / 131 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 85.7% 12 / 14 Podpod 100% 1 / 1 totaltotal 22.2% 53 / 238 CC 42 SCAR 60.1
CC
42
Cov
19%
search
650.0
_match
342.0
BEGIN
1.0
CRAP 407.6
↑ Summary
1 package WWW::Scraper::ISBN::GoogleBooks_Driver;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 $VERSION = '0.31';
9  
10 #--------------------------------------------------------------------------
11  
12 =head1 NAME
13  
14 WWW::Scraper::ISBN::GoogleBooks_Driver - Search driver for Google Books online book catalog.
15  
16 =head1 SYNOPSIS
17  
18 See parent class documentation (L<WWW::Scraper::ISBN::Driver>)
19  
20 =head1 DESCRIPTION
21  
22 Searches for book information from Google Books online book catalog
23  
24 =cut
25  
26 #--------------------------------------------------------------------------
27  
28 ###########################################################################
29 # Inheritence
30  
31 5 ▶use base qw(WWW::Scraper::ISBN::Driver);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 ###########################################################################
34 # Modules
35  
36 5 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 ###########################################################################
41 # Constants & Variables
42  
43 5 my $DOMAIN = 'http://books.google.com';
44  
45 5 ▶use constant SEARCH => '/books?jscmd=viewapi&callback=bookdata&bibkeys=ISBN:';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 5 ▶use constant LB2G => 453.59237; # number of grams in a pound (lb)
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use constant OZ2G => 28.3495231; # number of grams in an ounce (oz)
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use constant IN2MM => 25.4; # number of inches in a millimetre (mm)
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 5 my %LANG = (
51 'cz' => { Publisher => 'Vydavatel', Author => 'Autor', Title => 'Titul', Length => [ 'Délka', qr/\QD\x{e9}lka\E/, 'D&eacute;lka' ],
52 Pages => [ 'Počet stran:', qr/\QPo\x{10d}et stran:\E/, 'Po&#x10D;et stran:' ] },
53 'de' => { Publisher => 'Verlag', Author => 'Autor', Title => 'Titel', Length => qr{L.+nge}, Pages => 'Seiten' },
54 'en' => { Publisher => 'Publisher', Author => 'Author', Title => 'Title', Length => 'Length', Pages => 'pages' },
55 'es' => { Publisher => 'Editor', Author => 'Autor', Title => 'Título', Length => [ 'N.º de páginas', 'N.&ordm; de p&aacute;ginas' ],
56 Pages => [ 'páginas', 'p&aacute;ginas' ] },
57 'fr' => { Publisher => '.+diteur', Author => 'Auteur', Title => 'Titre', Length => 'Longueur', Pages => 'pages' },
58 'fi' => { Publisher => 'Kustantaja', Author => 'Kirjoittaja', Title => 'Otsikko', Length => 'Pituus', Pages => 'sivua' },
59 'nl' => { Publisher => 'Uitgever', Author => 'Auteur', Title => 'Titel', Length => 'Lengte', Pages => [ q{pagina's}, 'pagina&#39;s' ] },
60 'md' => { Publisher => 'Editor', Author => 'Autor', Title => 'Titlu', Length => 'Lungime', Pages => 'pagini' },
61 'ru' => { Publisher => ['Издатель', qr/\Q\x{418}\x{437}\x{434}\x{430}\x{442}\x{435}\x{43b}\x{44c}\E/, '&#x418;&#x437;&#x434;&#x430;&#x442;&#x435;&#x43B;&#x44C;', '&ETH;&#152;&ETH;&middot;&ETH;&acute;&ETH;&deg;&Ntilde;&#130;&ETH;&micro;&ETH;&raquo;&Ntilde;&#140;' ],
62 Author => 'Автор', Title => 'Название',
63 Length => [ 'Количество страниц', qr/\Q\x{41a}\x{43e}\x{43b}\x{438}\x{447}\x{435}\x{441}\x{442}\x{432}\x{43e} \x{441}\x{442}\x{440}\x{430}\x{43d}\x{438}\x{446}/, '&#x41A;&#x43E;&#x43B;&#x438;&#x447;&#x435;&#x441;&#x442;&#x432;&#x43E; &#x441;&#x442;&#x440;&#x430;&#x43D;&#x438;&#x446;', '&ETH;&#154;&ETH;&frac34;&ETH;&raquo;&ETH;&cedil;&Ntilde;&#135;&ETH;&micro;&Ntilde;&#129;&Ntilde;&#130;&ETH;&sup2;&ETH;&frac34; &Ntilde;&#129;&Ntilde;&#130;&Ntilde;&#128;&ETH;&deg;&ETH;&frac12;&ETH;&cedil;&Ntilde;&#134;' ],
64 Pages => [ 'Всего страниц:', qr/\Q\x{412}\x{441}\x{435}\x{433}\x{43e} \x{441}\x{442}\x{440}\x{430}\x{43d}\x{438}\x{446}:/, '&#x412;&#x441;&#x435;&#x433;&#x43E; &#x441;&#x442;&#x440;&#x430;&#x43D;&#x438;&#x446;:', '&ETH;&#146;&Ntilde;&#129;&ETH;&micro;&ETH;&sup3;&ETH;&frac34; &Ntilde;&#129;&Ntilde;&#130;&Ntilde;&#128;&ETH;&deg;&ETH;&frac12;&ETH;&cedil;&Ntilde;&#134;', '&ETH;&#146;&Ntilde;&#129;&ETH;&micro;&ETH;&sup3;&ETH;&frac34; &Ntilde;&#129;&Ntilde;&#130;&Ntilde;&#128;&ETH;&deg;&ETH;&frac12;&ETH;&cedil;&Ntilde;&#134;:' ] },
65 'iw' => { Publisher => [ '\x{5d4}\x{5d5}\x{5e6}\x{5d0}\x{5d4}', '&#x5D4;&#x5D5;&#x5E6;&#x5D0;&#x5D4;' ],
66 Author => 'Author', Title => 'Title', Length => [ qr/\Q\x{5d0}\x{5d5}\x{5e8}\x{5da}\E/, 'אורך', '\x{5d0}\x{5d5}\x{5e8}\x{5da}', '&#x5D0;&#x5D5;&#x5E8;&#x5DA;' ],
67 Pages => [ qr/\Q\x{5e2}\x{5de}\x{5d5}\x{5d3}\x{5d9}\x{5dd}\E/, 'עמודים', '\x{5e2}\x{5de}\x{5d5}\x{5d3}\x{5d9}\x{5dd}', '&#x5E2;&#x5DE;&#x5D5;&#x5D3;&#x5D9;&#x5DD;' ] }
68 );
69  
70 #--------------------------------------------------------------------------
71  
72 ###########################################################################
73 # Public Interface
74  
75 =head1 METHODS
76  
77 =over 4
78  
79 =item C<search>
80  
81 Creates a query string, then passes the appropriate form fields to the
82 GoogleBooks server.
83  
84 The returned page should be the correct catalog page for that ISBN. If not the
85 function returns zero and allows the next driver in the chain to have a go. If
86 a valid page is returned, the following fields are returned via the book hash:
87  
88 isbn (now returns isbn13)
89 isbn10
90 isbn13
91 ean13 (industry name)
92 author
93 title
94 book_link
95 image_link
96 pubdate
97 publisher
98 description (if available)
99 pages (if known)
100  
101 The book_link and image_link refer back to the GoogleBooks website.
102  
103 =back
104  
105 =cut
106  
107 0 ▶sub search {
Subroutine search: not called CC=25 SCAR=64.8
Pod documented
108 0 my $self = shift;
109 0 my $isbn = shift;
110 0 my $data;
111 0 $self->found(0);
112 0 $self->book(undef);
113  
114 # validate and convert into EAN13 format
115 0 my $ean = $self->convert_to_ean13($isbn);
116 0 ▶ return $self->handler("Invalid ISBN specified")
Branch: 1 branch
BranchTrueFalseTotal
unless $ean 0 0 0
117 unless($ean);
118  
119 0 my $mech = WWW::Mechanize->new();
120 0 $mech->agent_alias( 'Linux Mozilla' );
121  
122 0 ▶ my $search = ($ENV{GOOGLE_DOMAIN} || $DOMAIN) . SEARCH . $ean;
Condition0%0/3
$ENV{'GOOGLE_DOMAIN'} || $DOMAIN
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'GOOGLE_DOMAIN'} || $DOMAIN
$ENV{'GOOGLE_DOMAIN'} $DOMAIN
Truth table0%0/3
A || B
A= $ENV{'GOOGLE_DOMAIN'}B= $DOMAIN
ABresult
1X1
011
000
123 0 eval { $mech->get( $search ) };
124 0 ▶ return $self->handler("GoogleBooks website appears to be unavailable.")
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $mech->success or not $mech->content 0 0 0
Condition0%0/6
$@ or not $mech->success or not $mech->content
l!l&&r!l&&!r
000
$@ or not $mech->success
l!l&&r!l&&!r
000
MC/DC0%0/3
$@ or not $mech->success or not $mech->content
$@ not $mech->success not $mech->content
Truth table0%0/4
A or not(B) or not(C)
A= $@B= not $mech->successC= not $mech->content
ABCresult
1XX1
01X1
0011
0000
125 if($@ || !$mech->success() || !$mech->content());
126  
127 0 my $json = $mech->content();
128  
129 0 ▶ return $self->handler("Failed to find that book on GoogleBooks website.")
Branch: 1 branch
BranchTrueFalseTotal
if $json eq "bookdata({});" 0 0 0
130 if($json eq 'bookdata({});');
131  
132 0 $json =~ s/^bookdata\(//;
133 0 $json =~ s/\);$//;
134  
135 0 my $code = decode_json($json);
136 #use Data::Dumper;
137 #print STDERR "\n# code=".Dumper($code);
138  
139 return $self->handler("Failed to find that book on GoogleBooks website.")
140 0 ▶ unless($code->{'ISBN:'.$ean} || $code->{'ISBN:'.$isbn});
Branch: 1 branch
BranchTrueFalseTotal
unless $$code{'ISBN:' . $ean} or $$code{'ISBN:' . $isbn} 0 0 0
Condition0%0/3
$$code{'ISBN:' . $ean} or $$code{'ISBN:' . $isbn}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$code{'ISBN:' . $ean} or $$code{'ISBN:' . $isbn}
$$code{'ISBN:' . $ean} $$code{'ISBN:' . $isbn}
Truth table0%0/3
A or B
A= $$code{'ISBN:' . $ean}B= $$code{'ISBN:' . $isbn}
ABresult
1X1
011
000
141  
142 0 $data->{url} = $code->{'ISBN:'.$ean }{info_url};
143 0 ▶ $data->{url} ||= $code->{'ISBN:'.$isbn}{info_url};
Condition0%0/3
$data->{"url"} ||= $$code{'ISBN:' . $isbn}{"info_url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"url"} ||= $$code{'ISBN:' . $isbn}{"info_url"}
$data->{"url"} $$code{'ISBN:' . $isbn}{"info_url"}
Truth table0%0/3
A ||= B
A= $data->{"url"}B= $$code{'ISBN:' . $isbn}{"info_url"}
ABresult
1X1
011
000
144  
145 return $self->handler("Failed to find that book on GoogleBooks website.")
146 0 ▶ unless($data->{url});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"url"} 0 0 0
147  
148 0 eval { $mech->get( $data->{url} ) };
149 0 ▶ return $self->handler("GoogleBooks website appears to be unavailable.")
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $mech->success or not $mech->content 0 0 0
Condition0%0/6
$@ or not $mech->success or not $mech->content
l!l&&r!l&&!r
000
$@ or not $mech->success
l!l&&r!l&&!r
000
MC/DC0%0/3
$@ or not $mech->success or not $mech->content
$@ not $mech->success not $mech->content
Truth table0%0/4
A or not(B) or not(C)
A= $@B= not $mech->successC= not $mech->content
ABCresult
1XX1
01X1
0011
0000
150 if($@ || !$mech->success() || !$mech->content());
151  
152 # The Book page
153 #my $html = $mech->content();
154 0 my $html = encode_entities($mech->content(),'^\n\x20-\x25\x27-\x7e');
155 0 $html =~ s/\&amp;#39;/&#39;/sig;
156 0 $html =~ s/\\x\(([a-z\d]+)\)/\&#$1;/sig;
157 0 $html =~ s/&#55;/7/sig;
158  
159 0 ▶ return $self->handler("Failed to find that book on GoogleBooks website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry, we couldn't find any matches for/is 0 0 0
160 if($html =~ m!Sorry, we couldn't find any matches for!si);
161  
162 #use Data::Dumper;
163 #print STDERR "\n# " . Dumper($data);
164 #print STDERR "\n# html=[$html]\n";
165  
166 0 $data->{url} = $mech->uri();
167 0 my ($ccTLD) = $data->{url} =~ m{^http://[.\w]+\.google\.(\w\w)\b};
168  
169 0 my $lang = 'en'; # English (default)
170 0 ▶ $lang = 'de' if($data->{url} =~ m{^http://[.\w]+\.google\.(de|ch|at)\b}); # German
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"url"} =~ m[^http://[.\w]+\.google\.(de|ch|at)\b] 0 0 0
171 0 ▶ $lang = 'iw' if($data->{url} =~ m{^http://[.\w]+\.google\.co\.il\b}); # Hebrew
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"url"} =~ m[^http://[.\w]+\.google\.co\.il\b] 0 0 0
172 0 ▶ $lang = $ccTLD if($ccTLD && $LANG{$ccTLD}); # we have a ccTLD translation
Branch: 1 branch
BranchTrueFalseTotal
if $ccTLD and $LANG{$ccTLD} 0 0 0
Condition0%0/3
$ccTLD and $LANG{$ccTLD}
!ll&&!rl&&r
000
MC/DC0%0/2
$ccTLD and $LANG{$ccTLD}
$ccTLD $LANG{$ccTLD}
Truth table0%0/3
A and B
A= $ccTLDB= $LANG{$ccTLD}
ABresult
0X0
100
111
173  
174 0 ▶ return $self->handler("Language '".uc $lang."'not currently supported, patches welcome.")
Branch: 1 branch
BranchTrueFalseTotal
if $lang =~ /xx/ 0 0 0
175 if($lang =~ m!xx!);
176  
177 0 _match( $html, $data, $lang );
178  
179 # remove HTML tags
180 0 for(qw(author)) {
181 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
182 0 $data->{$_} =~ s!<[^>]+>!!g;
183 }
184  
185 # trim top and tail
186 0 ▶ for(keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
187  
188 # .com (and possibly others) don't always use Google's own CDN
189 0 ▶ if($data->{image} =~ m!^/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"image"} =~ m[^/]) 0 0 0
190 0 my $domain = $mech->uri();
191 0 $domain = s!^(http://[^/]+).*$!$1!;
192 0 $data->{image} = $domain . $data->{image};
193 0 $data->{thumb} = $data->{image};
194 }
195  
196 0 my $url = $mech->uri();
197  
198 my $bk = {
199 'ean13' => $data->{isbn13},
200 'isbn13' => $data->{isbn13},
201 'isbn10' => $data->{isbn10},
202 'isbn' => $data->{isbn13},
203 'author' => $data->{author},
204 'title' => $data->{title},
205 'book_link' => "$url",
206 'image_link' => $data->{image},
207 'thumb_link' => $data->{thumb},
208 'pubdate' => $data->{pubdate},
209 'publisher' => $data->{publisher},
210 'description' => $data->{description},
211 'pages' => $data->{pages},
212 0 'html' => $html
213 };
214  
215 #use Data::Dumper;
216 #print STDERR "\n# book=".Dumper($bk);
217  
218 0 $self->book($bk);
219 0 $self->found(1);
220 0 return $self->book;
221 }
222  
223 =head2 Private Methods
224  
225 =over 4
226  
227 =item C<_match>
228  
229 Pattern matches for book page.
230  
231 =back
232  
233 =cut
234  
235 0 ▶sub _match {
Subroutine _match: not called CC=18 SCAR=58.3
236 0 my ($html, $data, $lang) = @_;
237 0 my ($publisher);
238  
239 #print "\n# lang=$lang\n";
240  
241 # Some pages can present publisher text in multiple styles
242 0 ▶ my @pubs = ref($LANG{$lang}->{Publisher}) eq 'ARRAY' ? @{$LANG{$lang}->{Publisher}} : ($LANG{$lang}->{Publisher});
Branch: 1 branch
BranchTrueFalseTotal
ref $LANG{$lang}{'Publisher'} eq 'ARRAY' ? : 0 0 0
243 0 for my $pub (@pubs) {
244 0 ($publisher) = $html =~ m!<td class="metadata_label">(?:<span[^>]*>)?$pub(?:</span>)?</td><td class="metadata_value">(?:<span[^>]*>)?([^<]+)(?:</span>)?</td>!si;
245 0 ▶ last if($publisher);
Branch: 1 branch
BranchTrueFalseTotal
if $publisher 0 0 0
246 }
247 0 ▶ if($publisher) {
Branch: 1 branch
BranchTrueFalseTotal
if ($publisher) 0 0 0
248 0 my @publist = split(qr/\s*,\s*/,$publisher);
249 0 $data->{publisher} = $publist[0];
250 0 $data->{pubdate} = $publist[-1];
251 }
252  
253 # Some pages can present length/pages text in multiple styles
254 0 ▶ my @lengths = ref($LANG{$lang}->{Length}) eq 'ARRAY' ? @{$LANG{$lang}->{Length}} : ($LANG{$lang}->{Length});
Branch: 1 branch
BranchTrueFalseTotal
ref $LANG{$lang}{'Length'} eq 'ARRAY' ? : 0 0 0
255 0 ▶ my @pages = ref($LANG{$lang}->{Pages}) eq 'ARRAY' ? @{$LANG{$lang}->{Pages}} : ($LANG{$lang}->{Pages});
Branch: 1 branch
BranchTrueFalseTotal
ref $LANG{$lang}{'Pages'} eq 'ARRAY' ? : 0 0 0
256 0 for my $length (@lengths) {
257 0 for my $page (@pages) {
258 0 ($data->{pages}) = $html =~ m!<td class="metadata_label">(?:<span[^>]*>)?$length(?:</span>)?</td><td class="metadata_value">(?:<span[^>]*>)?(\d+)\s*$page(?:</span>)?</td>!si;
259 0 ▶ last if($data->{pages});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"pages"} 0 0 0
260 0 ($data->{pages}) = $html =~ m!<td class="metadata_label">(?:<span[^>]*>)?$length(?:</span>)?</td><td class="metadata_value">(?:<span[^>]*>)?\s*$page\s+(\d+)(?:</span>)?</td>!si;
261 0 ▶ last if($data->{pages});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"pages"} 0 0 0
262 }
263 0 ▶ last if($data->{pages});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"pages"} 0 0 0
264 }
265  
266 # get ISBN styles
267 0 my ($isbns) = $html =~ m!<td class="metadata_label">(?:<span[^>]*>)?ISBN(?:</span>)?</td><td class="metadata_value">(?:<span[^>]*>)?([^<]+)(?:</span>)?</td>!i;
268 0 my (@isbns) = split(qr/\s*,\s*/,$isbns);
269 0 for my $value (@isbns) {
270 0 ▶ $data->{isbn13} = $value if(length $value == 13);
Branch: 1 branch
BranchTrueFalseTotal
if length $value == 13 0 0 0
271 0 ▶ $data->{isbn10} = $value if(length $value == 10);
Branch: 1 branch
BranchTrueFalseTotal
if length $value == 10 0 0 0
272 }
273  
274 #use Data::Dumper;
275 #print STDERR "\n# isbns=[$isbns]";
276 #print STDERR "\n# " . Dumper($data);
277  
278 # get other fields
279 0 ($data->{image}) = $html =~ m!<div class="bookcover"><img src="([^"]+)"[^>]+id=summary-frontcover[^>]*></div>!i;
280 0 ▶ ($data->{image}) = $html =~ m!<div class="bookcover"><a[^>]+><img src="([^"]+)"[^>]+id=summary-frontcover[^>]*></a></div>!i unless($data->{image});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"image"} 0 0 0
281 0 ($data->{author}) = $html =~ m!<td class="metadata_label">(?:<span[^>]*>)?$LANG{$lang}->{Author}(?:</span>)?</td><td class="metadata_value">(.*?)</td>!i;
282 0 ▶ ($data->{author}) = $html =~ m!<td class="metadata_value"><a class="primary" href=".*?"><span dir=ltr>([^<]+)</span></a></td>!si unless($data->{author});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"author"} 0 0 0
283 0 ($data->{title}) = $html =~ m!<td class="metadata_label">(?:<span[^>]*>)?$LANG{$lang}->{Title}(?:</span>)?</td><td class="metadata_value">(?:<span[^>]*>)?([^<]+)(?:</span>)?!i;
284 0 ▶ ($data->{title}) = $html =~ m!<meta name="title" content="([^>]+)"\s*/>! unless($data->{title});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"title"} 0 0 0
285 0 ($data->{description}) = $html =~ m!<meta name="description" content="([^>]+)"\s*/>!si;
286  
287 0 $data->{author} =~ s/"//g;
288 0 $data->{thumb} = $data->{image};
289 }
290  
291 5 1;
292  
293 __END__
↑ Summary