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

filter: Statementstmt 33.3% 23 / 69 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 27.1% 31 / 114 CC 13 SCAR 47.8
CC
13
Cov
24%
search
182.0
BEGIN
1.0
BEGIN
1.0
CRAP 118.7
↑ Summary
1 package WWW::Scraper::ISBN::Blackwell_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.10';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::Blackwell_Driver - Search driver for Blackwell's 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 Blackwell's 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://bookshop.blackwell.co.uk';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use constant SEARCH => 'http://bookshop.blackwell.co.uk/jsp/search_results.jsp?wcp=1&quicksearch=1&cntType=&searchType=keywords&searchData=%s&x=10&y=10';
Subroutine BEGIN: called CC=1 SCAR=0.0
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 Blackwell 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 thumb_link
70 description
71 pubdate
72 publisher
73 binding (if known)
74 weight (if known) (in grammes)
75 width (if known) (in millimetres)
76 height (if known) (in millimetres)
77  
78 The book_link, image_link and thumb_link all refer back to the Blackwell website.
79  
80 =back
81  
82 =cut
83  
84 sub search {
85 0 ▶ my $self = shift;
Subroutine search: not called CC=13 SCAR=52.0
Pod documented
86 0 my $isbn = shift;
87 0 $self->found(0);
88 0 $self->book(undef);
89  
90 # validate and convert into EAN13 format
91 0 my $ean = $self->convert_to_ean13($isbn);
92 0 ▶ return $self->handler("Invalid ISBN specified")
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
93 if(!$ean || (length $isbn == 13 && $isbn ne $ean)
94 || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
95 0 my $isbn10 = $self->convert_to_isbn10($ean);
96  
97 0 my $mech = WWW::Mechanize->new();
98 0 $mech->agent_alias( 'Windows IE 6' );
99 0 $mech->add_header( 'Accept-Encoding' => undef );
100  
101 0 my $search = sprintf SEARCH , $ean;
102 #print STDERR "\n# search=[$search]\n";
103  
104 0 eval { $mech->get( $search ) };
105 0 ▶ return $self->handler("Blackwell's 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
106 if($@ || !$mech->success() || !$mech->content());
107  
108 # The Book page
109 0 my $html = $mech->content();
110  
111 0 ▶ return $self->handler("Failed to find that book on the Blackwell website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry, there are no results for/is 0 0 0
112 if($html =~ m!Sorry, there are no results for!si);
113
114 0 $html =~ s/&amp;/&/g;
115 0 $html =~ s/&nbsp;/ /g;
116 #print STDERR "\n# html=[\n$html\n]\n";
117  
118 0 my $data;
119 0 ($data->{isbn13}) = $html =~ m!<meta property="book:isbn"\s+content="(\d+)"\s+/>!si;
120 0 ($data->{title}) = $html =~ m!<meta property="og:title"\s+content="([^"]+)"\s+/>!si;
121 0 ($data->{author}) = $html =~ m!<p class="product__author">\s*<a[^>]+>([^<]+)</a>!si;
122 0 ($data->{binding}) = $html =~ m!<p class="product__format">\s*<span itemprop="bookFormat" >([^<\(]+)!si;
123 0 ($data->{publisher}) = $html =~ m!<td>Publisher:</td>\s*<td itemprop="publisher">\s*<a[^>]+>([^<]+)</a>!si;
124 0 ($data->{pubdate}) = $html =~ m!<td>Pub date:</td>\s*<td itemprop="datePublished" content="([^"]+)">!si;
125 0 ($data->{description}) = $html =~ m!<meta property="og:description"\s+content="([^"]+)"\s+/>!si;
126 0 ($data->{pages}) = $html =~ m!<td>Number of pages:</td>\s*<td itemprop="numberOfPages">(\d+)</td>!si;
127 0 ($data->{weight}) = $html =~ m!<td>Weight:</td>\s*<td itemprop="weight">(\d+)g</td>!si;
128 0 ($data->{height}) = $html =~ m!<td>Height:</td>\s*<td>(\d+)mm</td>!si;
129 0 ($data->{width}) = $html =~ m!<td>Width:</td>\s*<td>(\d+)mm</td>!si;
130 0 ($data->{image}) = $html =~ m!<meta property="og:image"\s+content="([^"]+)"\s+/>!si;
131  
132 0 $data->{image} =~ s!https:///!https://bookshop.blackwell.co.uk/!;
133 0 $data->{thumb} = $data->{image};
134 0 $data->{isbn10} = $isbn10;
135 0 $data->{publisher} =~ s/&#0?39;/'/g;
136  
137 #use Data::Dumper;
138 #print STDERR "\n# " . Dumper($data);
139  
140 0 ▶ return $self->handler("Could not extract data from the Blackwell result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
141 unless(defined $data);
142  
143 # trim top and tail
144 0 foreach (keys %$data) {
145 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
146 0 $data->{$_} =~ s!&nbsp;! !g;
147 0 $data->{$_} =~ s/^\s+//;
148 0 $data->{$_} =~ s/\s+$//;
149 }
150  
151 0 my $url = $mech->uri();
152  
153 my $bk = {
154 'ean13' => $data->{isbn13},
155 'isbn13' => $data->{isbn13},
156 'isbn10' => $data->{isbn10},
157 'isbn' => $data->{isbn13},
158 'author' => $data->{author},
159 'title' => $data->{title},
160 'book_link' => $url,
161 'image_link' => $data->{image},
162 'thumb_link' => $data->{thumb},
163 'description' => $data->{description},
164 'pubdate' => $data->{pubdate},
165 'publisher' => $data->{publisher},
166 'binding' => $data->{binding},
167 'pages' => $data->{pages},
168 'weight' => $data->{weight},
169 'height' => $data->{height},
170 'width' => $data->{width},
171 0 };
172  
173 #use Data::Dumper;
174 #print STDERR "\n# book=".Dumper($bk);
175  
176 0 $self->book($bk);
177 0 $self->found(1);
178 0 return $self->book;
179 }
180  
181 5 1;
182  
183 __END__
↑ Summary