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

filter: Statementstmt 31.3% 26 / 83 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 23.6% 35 / 148 SCAR 55.0
CC
21
Cov
20%
search
462.0
BEGIN
1.0
BEGIN
1.0
CRAP 244.2
↑ Summary
1 package WWW::Scraper::ISBN::WHSmith_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.08';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::WHSmith_Driver - Search driver for the WHSmith 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 the WHSmith 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 REFERER => 'http://www.whsmith.co.uk';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use constant SEARCH => 'http://www.whsmith.co.uk/pws/ProductDetails.ice?ProductID=%s&keywords=%s&redirect=true';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use constant PRODUCT => '/products/[^/]+/product/';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 #--------------------------------------------------------------------------
45  
46 ###########################################################################
47 # Public Interface
48  
49 =head1 METHODS
50  
51 =over 4
52  
53 =item C<search()>
54  
55 Creates a query string, then passes the appropriate form fields to the
56 WHSmith server.
57  
58 The returned page should be the correct catalog page for that ISBN. If not the
59 function returns zero and allows the next driver in the chain to have a go. If
60 a valid page is returned, the following fields are returned via the book hash:
61  
62 isbn (now returns isbn13)
63 isbn10
64 isbn13
65 ean13 (industry name)
66 author
67 title
68 book_link
69 image_link
70 description
71 pubdate
72 publisher
73 binding (if known)
74 pages (if known)
75 weight (if known) (in grammes)
76 width (if known) (in millimetres)
77 height (if known) (in millimetres)
78  
79 The book_link and image_link refer back to the WHSmith website.
80  
81 =back
82  
83 =cut
84  
85 sub search {
86 0 ▶ my $self = shift;
Subroutine search: not called CC=21 SCAR=61.4
Pod documented
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 0 $isbn = $ean;
98 #print STDERR "\n# isbn=[\n$isbn\n]\n";
99  
100 0 my $mech = WWW::Mechanize->new();
101 0 $mech->agent_alias( 'Linux Mozilla' );
102 0 $mech->add_header( 'Accept-Encoding' => undef );
103 0 $mech->add_header( 'Referer' => REFERER );
104  
105 0 my $search = sprintf SEARCH, $isbn, $isbn;
106 #print STDERR "\n# search=[$search]\n";
107 0 eval { $mech->get( $search ) };
108 0 ▶ return $self->handler("the WHSmith 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
109 if($@ || !$mech->success() || !$mech->content());
110  
111 # The Book page
112 0 my $html = $mech->content();
113 0 ▶ return $self->handler("Failed to find that book on the WHSmith website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry, no products were found/is or not $html =~ m[</html>]is 0 0 0
Condition0%0/3
$html =~ /Sorry, no products were found/is or not $html =~ m[</html>]is
l!l&&r!l&&!r
000
MC/DC0%0/2
$html =~ /Sorry, no products were found/is or not $html =~ m[</html>]is
$html =~ /Sorry, no products were found/is not $html =~ m[</html>]is
Truth table0%0/3
A or not(B)
A= $html =~ /Sorry, no products were found/isB= not $html =~ m[</html>]is
ABresult
1X1
011
000
114 if($html =~ m!Sorry, no products were found!si ||
115 $html !~ m!</html>!si); # sometimes the WHSmith site only sends back a small portion of the page :(
116  
117 0 my $url = $mech->uri();
118 0 ▶ return $self->handler("Failed to find that book on the WHSmith website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /Error.aspx/is 0 0 0
119 if($url =~ m!Error.aspx!si);
120  
121 0 $html =~ s/&amp;/&/g;
122 0 $html =~ s/&#0?39;/'/g;
123 0 $html =~ s/&nbsp;/ /g;
124  
125 #print STDERR "\n# html=[\n$html\n]\n";
126  
127 0 my $data;
128 0 ($data->{isbn13}) = $html =~ m!<li itemprop="ISBN13">\s*<strong>ISBN13:</strong>\s*(.*?)</li>!si;
129 0 ($data->{isbn10}) = $html =~ m!<li itemprop="ISBN10">\s*<strong>ISBN10:</strong>\s*(.*?)</li>!si;
130 0 ($data->{publisher}) = $html =~ m!<span class="bold ">Publisher:\s*</span><span><a href="[^"]+" style="text-decoration:underline;">([^<]+)</a></span>!si;
131 0 ($data->{pubdate}) = $html =~ m!<li itemprop="publication date">\s*<strong>publication date:</strong>\s*(.*?)</li>!si;
132 0 ($data->{title}) = $html =~ m!<h1 itemprop="name" id="product_title">([^<]*)</h1>!si;
133 0 ($data->{binding}) = $html =~ m!<div id="product_title_container">.*?<em class="secondary">([^<]+)</em></div>!si;
134 0 ▶ ($data->{binding}) = $html =~ m!<li itemprop="Format">\s*<strong>Format:</strong>\s*(.*?)</li>!si unless($data->{binding});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"binding"} 0 0 0
135 0 ($data->{pages}) = $html =~ m!<li itemprop="Number Of Pages">\s*<strong>Number Of Pages:</strong>\s*(.*?)</li>!si;
136 0 ($data->{author}) = $html =~ m!<span class="secondary"><strong>By:</strong>(.*?)</span>!si;
137 0 ($data->{image}) = $html =~ m!<meta itemprop="image" content="([^"]*)">!si;
138 0 ($data->{description}) = $html =~ m!<meta name="description" content="([^"]*)" />!si;
139  
140 0 ▶ if($data->{image}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"image"}) 0 0 0
141 0 $data->{thumb} = $data->{image};
142 0 $data->{thumb} =~ s!/x?large/!/small/!;
143 }
144  
145 # currently not provided
146 0 ($data->{width}) = $html =~ m!<span class="bold ">Width:\s*</span><span>([^<]+)</span>!si;
147 0 ($data->{height}) = $html =~ m!<span class="bold ">Height:\s*</span><span>([^<]+)</span>!si;
148 0 ($data->{weight}) = $html =~ m!<li itemprop="weight">\s*<strong>weight:</strong>\s*(.*?)</li>!s;
149  
150 0 ▶ $data->{width} = int($data->{width}) if($data->{width});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"width"} 0 0 0
151 0 ▶ $data->{height} = int($data->{height}) if($data->{height});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"height"} 0 0 0
152 0 ▶ $data->{weight} = int($data->{weight}) if($data->{weight});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"weight"} 0 0 0
153  
154 0 ▶ if($data->{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"author"}) 0 0 0
155 0 $data->{author} =~ s/<[^>]*>//g;
156 0 $data->{author} =~ s/\(author\)/ /g;
157 0 $data->{author} =~ s/\s+/ /g;
158 0 $data->{author} =~ s/\s+,\s+/, /g;
159 }
160  
161 #use Data::Dumper;
162 #print STDERR "\n# data=" . Dumper($data);
163  
164 0 ▶ return $self->handler("Could not extract data from The WHSmith result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
165 unless(defined $data);
166  
167 # trim top and tail
168 0 foreach (keys %$data) {
169 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
170 0 $data->{$_} =~ s/^\s+//;
171 0 $data->{$_} =~ s/\s+$//;
172 }
173  
174 my $bk = {
175 'ean13' => $data->{isbn13},
176 'isbn13' => $data->{isbn13},
177 'isbn10' => $data->{isbn10},
178 'isbn' => $data->{isbn13},
179 'author' => $data->{author},
180 'title' => $data->{title},
181 'book_link' => $url,
182 'image_link' => $data->{image},
183 'thumb_link' => $data->{thumb},
184 'description' => $data->{description},
185 'pubdate' => $data->{pubdate},
186 'publisher' => $data->{publisher},
187 'binding' => $data->{binding},
188 'pages' => $data->{pages},
189 'weight' => $data->{weight},
190 'width' => $data->{width},
191 'height' => $data->{height},
192 0 'html' => $html
193 };
194  
195 #use Data::Dumper;
196 #print STDERR "\n# book=".Dumper($bk);
197  
198 0 $self->book($bk);
199 0 $self->found(1);
200 0 return $self->book;
201 }
202  
203 5 1;
204  
205 __END__
↑ Summary