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

filter: Statementstmt 32.4% 24 / 74 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 21.7% 32 / 147 CC 24 SCAR 61.4
CC
24
Cov
19%
search
600.0
BEGIN
1.0
BEGIN
1.0
CRAP 464.7
↑ Summary
1 package WWW::Scraper::ISBN::Waterstones_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::Waterstones_Driver - Search driver for the Waterstones 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 Waterstones 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 => 'https://www.waterstones.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use constant SEARCH => 'https://www.waterstones.com/index/search?term=';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 my ($URL1,$URL2) = ('http://www.waterstones.com/book/','/[^?]+\?b=\-3\&amp;t=\-26\#Bibliographicdata\-26');
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 Book Depository 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 thumb_link
71 description
72 pubdate
73 publisher
74 binding (if known)
75 pages (if known)
76  
77 The book_link, image_link and thumb_link all refer back to the Waterstones
78 website.
79  
80 =back
81  
82 =cut
83  
84 sub search {
85 0 ▶ my $self = shift;
Subroutine search: not called CC=24 SCAR=64.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  
96 0 my $mech = WWW::Mechanize->new();
97 0 $mech->agent_alias( 'Linux Mozilla' );
98 0 $mech->add_header( 'Accept-Encoding' => undef );
99 0 $mech->add_header( 'Referer' => REFERER );
100  
101 0 eval { $mech->get( SEARCH . $ean ) };
102 0 ▶ return $self->handler("The Waterstones 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
103 if($@ || !$mech->success() || !$mech->content());
104  
105 #print STDERR "\n# search=[".SEARCH."$ean]\n";
106 #print STDERR "\n# is_html=".$mech->is_html().", content type=".$mech->content_type()."\n";
107 #print STDERR "\n# dump headers=".$mech->dump_headers."\n";
108  
109 # we get back a redirect
110 0 my $response = $mech->response();
111 0 my $url = $response->header( 'X-Meta-Og-Url' );
112 #print STDERR "\n# url=[$url]\n";
113  
114 0 ▶ return $self->handler("Failed to find that book on the Waterstones website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $url eq "https://www.waterstones.com" or $url eq 'https://www.waterstones.com' . "/books/search/term/$ean" 0 0 0
Condition0%0/3
$url eq "https://www.waterstones.com" or $url eq 'https://www.waterstones.com' . "/books/search/term/$ean"
l!l&&r!l&&!r
000
MC/DC0%0/2
$url eq "https://www.waterstones.com" or $url eq 'https://www.waterstones.com' . "/books/search/term/$ean"
$url eq "https://www.waterstones.com" $url eq 'https://www.waterstones.com' . "/books/search/term/$ean"
Truth table0%0/3
A or B
A= $url eq "https://www.waterstones.com"B= $url eq 'https://www.waterstones.com' . "/books/search/term/$ean"
ABresult
1X1
011
000
115 if($url eq REFERER || $url eq REFERER . "/books/search/term/$ean");
116  
117 0 eval { $mech->get( $url ) };
118 0 ▶ return $self->handler("Failed to find that book on the Waterstones website. [$isbn]")
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
119 if($@ || !$mech->success() || !$mech->content());
120  
121 # The Book page
122 0 my $html = $mech->content();
123  
124 0 ▶ return $self->handler("Failed to find that book on the Waterstones website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ m[<strong>Sorry!</strong> We did not find any results for]is 0 0 0
125 if($html =~ m|<strong>Sorry!</strong> We did not find any results for|si);
126  
127 0 ▶ return $self->handler("Waterstones website has crashed. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Exception was UseCaseError: \d+/is 0 0 0
128 if($html =~ m|Exception was UseCaseError: \d+|si);
129  
130 0 $html =~ s/&amp;/&/g;
131 #print STDERR "\n# content2=[\n$html\n]\n";
132  
133 0 my $data;
134 ($data->{title},$data->{author})
135 0 = $html =~ m!<title>(.*?)\s*by\s*(.*?) \| Waterstones.com</title>!si;
136 0 ($data->{binding}) = $html =~ m!<span class="book-title" itemprop="name" id="scope_book_title">.*? \((.*?)\)</span>!si;
137 0 ($data->{description}) = $html =~ m!<div itemprop="description" id="scope_book_description">(.*?)</div>!si;
138 0 ($data->{publisher}) = $html =~ m!<span itemprop="publisher">([^<]+)</span>!si;
139 0 ($data->{pubdate}) = $html =~ m!<meta itemprop="datePublished" content="[^"]+" />([\d\/]+)\s*</span>!si;
140 0 ($data->{isbn13}) = $html =~ m!<span itemprop="isbn">([^<]+)</span>!si;
141 0 ($data->{image}) = $html =~ m!<img itemprop="image" id="scope_book_image" src="([^"]+$ean.jpg)"!si;
142  
143 #use Data::Dumper;
144 #print STDERR "\n# data=" . Dumper($data);
145  
146 0 ▶ return $self->handler("Could not extract data from the Waterstones result page. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
147 unless(defined $data);
148  
149 0 for(qw(author publisher description title)) {
150 0 ▶ $data->{$_} =~ s/&#0?39;/'/g if($data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$_} 0 0 0
151 }
152  
153 0 $data->{isbn10} = $self->convert_to_isbn10($ean);
154 0 ▶ $data->{title} =~ s!\s*\($data->{binding}\)\s*!! if($data->{title});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"title"} 0 0 0
155 0 ▶ $data->{description} =~ s!<[^>]+>!! if($data->{description});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"description"} 0 0 0
156  
157 0 ▶ if($data->{image}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"image"}) 0 0 0
158 0 $data->{thumb} = $data->{image};
159 0 $data->{thumb} =~ s!/images/nbd/[lms]/!/images/nbd/s/!;
160 0 $data->{image} =~ s!/images/nbd/[lms]/!/images/nbd/l/!;
161 }
162  
163 #use Data::Dumper;
164 #print STDERR "\n# data=" . Dumper($data);
165  
166 # trim top and tail
167 0 foreach (keys %$data) {
168 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
169 0 $data->{$_} =~ s!&nbsp;! !g;
170 0 $data->{$_} =~ s/^\s+//;
171 0 $data->{$_} =~ s/\s+$//;
172 }
173  
174 # my $url = $mech->uri();
175 # $url =~ s/\?.*//;
176  
177 my $bk = {
178 'ean13' => $data->{isbn13},
179 'isbn13' => $data->{isbn13},
180 'isbn10' => $data->{isbn10},
181 'isbn' => $data->{isbn13},
182 'author' => $data->{author},
183 'title' => $data->{title},
184 'book_link' => "$url",
185 'image_link' => $data->{image},
186 'thumb_link' => $data->{thumb},
187 'description' => $data->{description},
188 'pubdate' => $data->{pubdate},
189 'publisher' => $data->{publisher},
190 'binding' => $data->{binding},
191 'pages' => $data->{pages},
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