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

filter: Statementstmt 25.3% 21 / 83 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 18.3% 28 / 153 CC 26 SCAR 63.5
CC
26
Cov
15%
search
702.0
BEGIN
1.0
BEGIN
1.0
CRAP 570.6
↑ Summary
1 package WWW::Scraper::ISBN::Booktopia_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  
6 5 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 $VERSION = '0.23';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::Booktopia_Driver - Search driver for Booktopia online book catalog.
14  
15 =head1 SYNOPSIS
16  
17 See parent class documentation (L<WWW::Scraper::ISBN::Driver>)
18  
19 =head1 DESCRIPTION
20  
21 Searches for book information from Booktopia online book catalog
22  
23 =cut
24  
25 #--------------------------------------------------------------------------
26  
27 ###########################################################################
28 # Inheritence
29  
30 5 ▶use base qw(WWW::Scraper::ISBN::Driver);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 ###########################################################################
33 # Modules
34  
35 5 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 ###########################################################################
38 # Constants
39  
40 5 ▶use constant SEARCH => 'http://www.booktopia.com.au/search.ep?cID=&submit.x=44&submit.y=7&submit=search&keywords=';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 my ($BAU_URL1,$BAU_URL2,$BAU_URL3) = ('http://www.booktopia.com.au','/[^/]+/prod','.html');
42  
43 #--------------------------------------------------------------------------
44  
45 ###########################################################################
46 # Public Interface
47  
48 =head1 METHODS
49  
50 =over 4
51  
52 =item C<search()>
53  
54 Creates a query string, then passes the appropriate form fields to the
55 Booktopia server.
56  
57 The returned page should be the correct catalog page for that ISBN. If not the
58 function returns zero and allows the next driver in the chain to have a go. If
59 a valid page is returned, the following fields are returned via the book hash:
60  
61 isbn (now returns isbn13)
62 isbn10
63 isbn13
64 ean13 (industry name)
65 author
66 title
67 book_link
68 image_link
69 description
70 pubdate
71 publisher
72 binding (if known)
73 pages (if known)
74 weight (if known) (in grammes)
75 width (if known) (in millimetres)
76 height (if known) (in millimetres)
77 depth (if known) (in millimetres)
78  
79 The book_link and image_link refer back to the Booktopia website.
80  
81 =back
82  
83 =cut
84  
85 0 ▶sub search {
Subroutine search: not called CC=26 SCAR=65.5
Pod documented
86 0 my $self = shift;
87 0 my $isbn = shift;
88 0 $self->found(0);
89 0 $self->book(undef);
90  
91 # validate and convert into EAN13 format
92 0 my $ean = $self->convert_to_ean13($isbn);
93 0 ▶ return $self->handler("Invalid ISBN specified [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if not $ean or length $isbn == 13 and $isbn ne $ean or length $isbn == 10 and $isbn ne $self->convert_to_isbn10($ean) 0 0 0
Condition0%0/12
not $ean or length $isbn == 13 and $isbn ne $ean or length $isbn == 10 and $isbn ne $self->convert_to_isbn10($ean)
l!l&&r!l&&!r
000
not $ean or length $isbn == 13 and $isbn ne $ean
l!l&&r!l&&!r
000
length $isbn == 13 and $isbn ne $ean
!ll&&!rl&&r
000
length $isbn == 10 and $isbn ne $self->convert_to_isbn10($ean)
!ll&&!rl&&r
000
MC/DC0%0/5
not $ean or length $isbn == 13 and $isbn ne $ean or length $isbn == 10 and $isbn ne $self->convert_to_isbn10($ean)
not $ean length $isbn == 13 $isbn ne $ean length $isbn == 10 $isbn ne $self->convert_to_isbn10($ean)
Truth table0%0/8
not(A) or B and C or D and E
A= not $eanB= length $isbn == 13C= $isbn ne $eanD= length $isbn == 10E= $isbn ne $self->convert_to_isbn10($ean)
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
94 if(!$ean || (length $isbn == 13 && $isbn ne $ean)
95 || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
96  
97 #print STDERR "\n# isbn=[$isbn] => ean=[$ean]\n";
98 0 $isbn = $ean;
99  
100 0 my $mech = WWW::Mechanize->new();
101 0 $mech->agent_alias( 'Linux Mozilla' );
102  
103 #print STDERR "\n# url=[".(SEARCH . $isbn)."]\n";
104  
105 0 eval { $mech->get( SEARCH . $isbn ) };
106 0 ▶ return $self->handler("Booktopia 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
107 if($@ || !$mech->success() || !$mech->content());
108  
109 0 my $pattern = $isbn;
110 0 ▶ if(length $isbn == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $isbn == 10) 0 0 0
111 0 $pattern = '978' . $isbn;
112 0 $pattern =~ s/.$/./;
113 }
114  
115 # The Book page
116 0 my $html = $mech->content();
117  
118 0 ▶ return $self->handler("Failed to find that book on Booktopia website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry, we couldn't find any matches for/is 0 0 0
119 if($html =~ m!Sorry, we couldn't find any matches for!si);
120
121 #print STDERR "\n# html=[\n$html\n]\n";
122  
123 0 my $data;
124 0 ($data->{publisher}) = $html =~ m!<b>Publisher:</b>([^<]+)<br>!si;
125 0 ($data->{pubdate}) = $html =~ m!<span class="label">\s*Published:\s*</span>\s*([^<]+)!si;
126  
127 0 ▶ $data->{publisher} =~ s!<[^>]+>!!g if($data->{publisher});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"publisher"} 0 0 0
128 0 ▶ $data->{pubdate} =~ s!\s+! !g if($data->{pubdate});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"pubdate"} 0 0 0
129  
130 0 ($data->{image}) = $html =~ m!(https://www.booktopia.com.au/http_coversbooktopiacomau/big/\d+/[-\w.]+\.jpg)"!si;
131 0 ($data->{thumb}) = $html =~ m!(https://www.booktopia.com.au/http_coversbooktopiacomau/\d+/\d+/[-\w.]+\.jpg)"!si;
132 0 ($data->{isbn13}) = $html =~ m!<b>\s*ISBN:\s*</b>\s*(\d+)!si;
133 0 ($data->{isbn10}) = $html =~ m!<b>\s*ISBN-10:\s*</b>\s*(\d+)!si;
134 0 ($data->{author}) = $html =~ m!<div id="contributors">\s*(?:By|Author):\s*(.*?)</div>!si;
135 0 ($data->{title}) = $html =~ m!<meta property="og:title" content="([^"]+)"!si;
136 0 ▶ ($data->{title}) = $html =~ m!<a href="[^"]+" class="largeLink">([^<]+)</a><br/><br/>!si unless($data->{title});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"title"} 0 0 0
137 0 ($data->{description}) = $html =~ m!<p id="google-preview-information" style="display:none;">\s*<b>[^<]+</b>\s*</p>\s*<p>(.*?)</p>!si;
138 0 ($data->{binding}) = $html =~ m!<span class="label">\s*Format:\s*</span>\s*([^<]+)!si;
139 0 ($data->{pages}) = $html =~ m!<b>\s*Number Of Pages:\s*</b>\s*([\d.]+)!si;
140 0 ($data->{weight}) = $html =~ m!<span class="label">\s*Weight \(kg\):\s*</span>\s*([\d.]+)!si;
141 ($data->{height},$data->{width},$data->{depth})
142 0 = $html =~ m!<span class="label">\s*Dimensions \(cm\):\s*</span>([\d.]+)\s*&nbsp;x&nbsp;\s*([\d.]+)\s*&nbsp;x&nbsp;\s*([\d.]+)!si;
143  
144 # despite it saying Kg (kilogrammes) the weight seems to vary between widely!
145 0 ▶ if($data->{weight}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"weight"}) 0 0 0
146 0 ▶ if( $data->{weight} < 1) {$data->{weight} = int($data->{weight} * 1000)}
Branch: 3 branches
BranchTrueFalseTotal
if ($data->{'weight'} < 1) { } 0 0 0
elsif ($data->{'weight'} < 100) { } 0 0 0
elsif ($data->{'weight'} < 1000) { } 0 0 0
147 0 elsif($data->{weight} < 100) {$data->{weight} = int($data->{weight} * 10)}
148 0 elsif($data->{weight} < 1000) {$data->{weight} = int($data->{weight})}
149 0 else {$data->{weight} = int($data->{weight})}
150 }
151
152 0 ▶ $data->{height} = int($data->{height} * 10) if($data->{height});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"height"} 0 0 0
153 0 ▶ $data->{width} = int($data->{width} * 10) if($data->{width});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"width"} 0 0 0
154 0 ▶ $data->{depth} = int($data->{depth} * 10) if($data->{depth});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"depth"} 0 0 0
155  
156 0 ▶ if($data->{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"author"}) 0 0 0
157 0 $data->{author} =~ s!<br\s*/>!,!g;
158 0 $data->{author} =~ s!<[^>]+>!!g;
159 0 $data->{author} =~ s!\s*,\s*!, !g;
160 0 $data->{author} =~ s!\s*,\s*$!!g;
161 }
162  
163 0 ▶ if($data->{description}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"description"}) 0 0 0
164 0 $data->{description} =~ s!Click on the Google Preview[^<]+!!s;
165 0 $data->{description} =~ s!<br\s*/?>!\n!gi;
166 0 $data->{description} =~ s!<[^>]+>!!g;
167 }
168  
169 #use Data::Dumper;
170 #print STDERR "\n# " . Dumper($data);
171  
172 0 ▶ return $self->handler("Could not extract data from Booktopia result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
173 unless(defined $data);
174  
175 # trim top and tail
176 0 ▶ foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
177  
178 my $bk = {
179 'ean13' => $data->{isbn13},
180 'isbn13' => $data->{isbn13},
181 'isbn10' => $data->{isbn10},
182 'isbn' => $data->{isbn13},
183 'author' => $data->{author},
184 'title' => $data->{title},
185 'book_link' => $mech->uri(),
186 'image_link' => $data->{image},
187 'thumb_link' => $data->{thumb},
188 'description' => $data->{description},
189 'pubdate' => $data->{pubdate},
190 'publisher' => $data->{publisher},
191 'binding' => $data->{binding},
192 'pages' => $data->{pages},
193 'weight' => $data->{weight},
194 'width' => $data->{width},
195 'height' => $data->{height},
196 'depth' => $data->{depth},
197 # 'html' => $html
198 0 };
199  
200 #use Data::Dumper;
201 #print STDERR "\n# book=".Dumper($bk);
202  
203 0 $self->book($bk);
204 0 $self->found(1);
205 0 return $self->book;
206 }
207  
208 5 1;
209  
210 __END__
↑ Summary