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

filter: Statementstmt 32.5% 26 / 80 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 25.3% 35 / 138 CC 20 SCAR 57.0
CC
20
Cov
22%
search
420.0
BEGIN
1.0
BEGIN
1.0
CRAP 300.3
↑ Summary
1 package WWW::Scraper::ISBN::BarnesNoble_Driver;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 $VERSION = '0.20';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::BarnesNoble_Driver - Search driver for the Barnes and Noble 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 Barnes and Noble online book catalog
22  
23 =cut
24  
25 #--------------------------------------------------------------------------
26  
27 ###########################################################################
28 # Inheritence
29  
30 6 ▶use base qw(WWW::Scraper::ISBN::Driver);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 ###########################################################################
33 # Modules
34  
35 6 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 ###########################################################################
38 # Constants
39  
40 6 ▶use constant REFERER => 'http://www.barnesandnoble.com/';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use constant SEARCH => 'http://www.barnesandnoble.com/include/quicksearch_newSearch.asp?page=index&prod=univ&choice=allproducts&query=%s&flag=False&ATL_lid=hu0MIVVMgy&ATL_userid=hu0MIVVMgy&ATL_sid=v24Vd2sM16&ATL_seqnum=5';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use constant IN2MM => 25.4; # number of inches in a millimetre (mm)
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 Barnes and Noble 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 depth (if known) (in millimetres)
79  
80 The book_link and image_link refer back to the Barnes and Noble website.
81  
82 =back
83  
84 =cut
85  
86 sub search {
87 0 ▶ my $self = shift;
Subroutine search: not called CC=20 SCAR=60.4
Pod documented
88 0 my $isbn = shift;
89 0 $self->found(0);
90 0 $self->book(undef);
91  
92 # validate and convert into EAN13 format
93 0 my $ean = $self->convert_to_ean13($isbn);
94 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
95 if(!$ean || (length $isbn == 13 && $isbn ne $ean)
96 || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
97  
98 0 my $mech = WWW::Mechanize->new();
99 0 $mech->agent_alias( 'Linux Mozilla' );
100 0 $mech->add_header( 'Accept-Encoding' => undef );
101 0 $mech->add_header( 'Referer' => REFERER );
102  
103 0 my $url = sprintf SEARCH, $ean, $ean;
104 #print STDERR "\n# ean=$ean, link=[$url]\n";
105  
106 0 eval { $mech->get( $url ) };
107 0 ▶ return $self->handler("the Barnes and Noble 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
108 if($@ || !$mech->success() || !$mech->content());
109  
110 # The Book page
111 0 my $html = $mech->content();
112  
113 0 ▶ return $self->handler("Failed to find that book on the Barnes and Noble website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry. We did not find any results|Sorry, we could not find what you were looking for/is 0 0 0
114 if($html =~ m!Sorry. We did not find any results|Sorry, we could not find what you were looking for!si);
115
116 0 $html =~ s/&amp;/&/g;
117 0 $html =~ s/&#0?39;/'/g;
118 0 $html =~ s/&nbsp;/ /g;
119 #print STDERR "\n# html=[\n$html\n]\n";
120  
121 0 my $data;
122 0 ($data->{isbn10}) = $self->convert_to_isbn10($ean);
123 0 ($data->{isbn13}) = $html =~ m!<span>ISBN-13:</span>\s*(\d+)!si;
124 0 ($data->{publisher}) = $html =~ m!<span>Publisher: </span>\s*([^<]+)\s*</li>!si;
125 0 ($data->{pubdate}) = $html =~ m!<span>Publication date: </span>\s*([^<]+)\s*</li>!si;
126 0 ($data->{title}) = $html =~ m!<meta property="og:title" content="([^"]*)"[^>]*>!si;
127 0 ($data->{author}) = $html =~ m!<ul class="contributors\s*">(.*?)</ul>!si;
128 0 ($data->{pages}) = $html =~ m!<span>Pages: </span>\s*(.*?)\s*</li>!si;
129 0 ($data->{binding}) = $html =~ m!<title>[^\|]+\|[^\|]+\|\s*(.*?)\s*\|[^<]+</title>!si;
130 0 ($data->{image}) = $html =~ m!<meta property="og:image" content="([^"]+)"[^>]*>!si;
131 0 ($data->{thumb}) = $html =~ m!<meta property="og:image" content="([^"]+)"[^>]*>!si;
132 0 ($data->{description}) = $html =~ m!product-commentary-overview-1.*?<h3>Overview</h3>\s*(.*?)\s*</div>\s*</section>!si;
133 ($data->{width},$data->{height},$data->{depth})
134 0 = $html =~ m!<span>Product dimensions:\s*</span>([\d.]+)\s*\(w\)\s*x\s*([\d.]+)\s*\(h\)\s*x\s*([\d.]+)\s*\(d\)</li>!si;
135  
136 # currently not provided
137 0 ($data->{weight}) = $html =~ m!<span class="bold ">Weight:\s*</span><span>([^<]+)</span>!s;
138  
139 0 ▶ $data->{depth} = int($data->{depth} * IN2MM) if($data->{depth});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"depth"} 0 0 0
140 0 ▶ $data->{width} = int($data->{width} * IN2MM) if($data->{width});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"width"} 0 0 0
141 0 ▶ $data->{height} = int($data->{height} * IN2MM) if($data->{height});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"height"} 0 0 0
142 0 ▶ $data->{weight} = int($data->{weight}) if($data->{weight});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"weight"} 0 0 0
143  
144 0 for(qw(author publisher description)) {
145 0 ▶ next unless($data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$_} 0 0 0
146 0 $data->{$_} =~ s![ \t\n\r]+! !g;
147 0 $data->{$_} =~ s!<[^>]+>!!g;
148 }
149  
150 0 ▶ if($data->{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"author"}) 0 0 0
151 0 $data->{author} =~ s!^\s*by\s*!!;
152 0 $data->{author} =~ s!,\s*!, !g;
153 }
154  
155 #use Data::Dumper;
156 #print STDERR "\n# data=" . Dumper($data);
157  
158 0 ▶ return $self->handler("Could not extract data from the Barnes and Noble result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
159 unless(defined $data);
160  
161 # trim top and tail
162 0 foreach (keys %$data) {
163 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
164 0 $data->{$_} =~ s/^\s+//;
165 0 $data->{$_} =~ s/\s+$//;
166 }
167  
168 0 $url = $mech->uri();
169  
170 my $bk = {
171 'ean13' => $data->{isbn13},
172 'isbn13' => $data->{isbn13},
173 'isbn10' => $data->{isbn10},
174 'isbn' => $data->{isbn13},
175 'author' => $data->{author},
176 'title' => $data->{title},
177 'book_link' => $url,
178 'image_link' => $data->{image},
179 'thumb_link' => $data->{thumb},
180 'description' => $data->{description},
181 'pubdate' => $data->{pubdate},
182 'publisher' => $data->{publisher},
183 'binding' => $data->{binding},
184 'pages' => $data->{pages},
185 'weight' => $data->{weight},
186 'width' => $data->{width},
187 'height' => $data->{height},
188 'depth' => $data->{depth},
189 0 'html' => $html
190 };
191  
192 #use Data::Dumper;
193 #print STDERR "\n# book=".Dumper($bk);
194  
195 0 $self->book($bk);
196 0 $self->found(1);
197 0 return $self->book;
198 }
199  
200 6 1;
201  
202 __END__
↑ Summary