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

filter: Statementstmt 32.1% 27 / 84 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 21.1% 36 / 170 CC 28 SCAR 64.5
CC
28
Cov
19%
search
812.0
BEGIN
1.0
BEGIN
1.0
CRAP 631.8
↑ 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.09';
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 5 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 ###########################################################################
39 # Constants
40  
41 5 ▶use constant REFERER => 'https://www.waterstones.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use constant SEARCH => 'https://www.waterstones.com/index/search?term=';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 my ($URL1,$URL2) = ('http://www.waterstones.com/book/','/[^?]+\?b=\-3\&amp;t=\-26\#Bibliographicdata\-26');
44  
45 #--------------------------------------------------------------------------
46  
47 ###########################################################################
48 # Public Interface
49  
50 =head1 METHODS
51  
52 =over 4
53  
54 =item C<search()>
55  
56 Creates a query string, then passes the appropriate form fields to the
57 Book Depository server.
58  
59 The returned page should be the correct catalog page for that ISBN. If not the
60 function returns zero and allows the next driver in the chain to have a go. If
61 a valid page is returned, the following fields are returned via the book hash:
62  
63 isbn (now returns isbn13)
64 isbn10
65 isbn13
66 ean13 (industry name)
67 author
68 title
69 book_link
70 image_link
71 thumb_link
72 description
73 pubdate
74 publisher
75 binding (if known)
76 pages (if known)
77  
78 The book_link, image_link and thumb_link all refer back to the Waterstones
79 website.
80  
81 =back
82  
83 =cut
84  
85 0 ▶sub search {
Subroutine search: not called CC=28 SCAR=67.0
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")
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 my $mech = WWW::Mechanize->new();
98 0 $mech->agent_alias( 'Linux Mozilla' );
99 0 $mech->add_header( 'Accept-Encoding' => undef );
100 0 $mech->add_header( 'Referer' => REFERER );
101  
102 0 eval { $mech->get( SEARCH . $ean ) };
103 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
104 if($@ || !$mech->success() || !$mech->content());
105  
106 #print STDERR "\n# search=[".SEARCH."$ean]\n";
107 #print STDERR "\n# is_html=".$mech->is_html().", content type=".$mech->content_type()."\n";
108 #print STDERR "\n# dump headers=".$mech->dump_headers."\n";
109  
110 # we get back a redirect
111 0 my $response = $mech->response();
112 0 my $url = $response->header( 'X-Meta-Og-Url' );
113 #print STDERR "\n# url=[$url]\n";
114  
115 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
116 if($url eq REFERER || $url eq REFERER . "/books/search/term/$ean");
117  
118 0 eval { $mech->get( $url ) };
119 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
120 if($@ || !$mech->success() || !$mech->content());
121  
122 # The Book page
123 0 my $html = $mech->content();
124  
125 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
126 if($html =~ m|<strong>Sorry!</strong> We did not find any results for|si);
127  
128 0 ▶ return $self->handler("Waterstones website has crashed. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Exception was UseCaseError: \d+/is 0 0 0
129 if($html =~ m|Exception was UseCaseError: \d+|si);
130  
131 0 $html =~ s/&amp;/&/g;
132 #print STDERR "\n# content2=[\n$html\n]\n";
133  
134 0 my $data;
135 ($data->{title},$data->{author})
136 0 = $html =~ m!<title>(.*?)\s*by\s*(.*?) \| Waterstones</title>!si;
137 0 ($data->{binding}) = $html =~ m!<span class="book-title" itemprop="name" id="scope_book_title">.*? \((.*?)\)</span>!si;
138 0 ($data->{description}) = $html =~ m!<div itemprop="description" id="scope_book_description">(.*?)</div>!si;
139 0 ($data->{publisher}) = $html =~ m!<span itemprop="publisher">([^<]+)</span>!si;
140 0 ($data->{pubdate}) = $html =~ m!<meta itemprop="datePublished" content="[^"]+" />([\d\/]+)\s*</span>!si;
141 0 ($data->{isbn13}) = $html =~ m!<span itemprop="isbn">([^<]+)</span>!si;
142 0 ($data->{image}) = $html =~ m!<img itemprop="image" id="scope_book_image" src="([^"]+$ean.jpg)"!si;
143 0 my ($json) = $html =~ m!<script>\s*ws_dl = \[(.*?)\]\s*</script>!si;
144 #print STDERR "\n# json=[\n$json\n]\n";
145  
146 0 ▶ if($json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json) 0 0 0
147 0 $data->{json} = decode_json( $json );
148 0 for(qw(author title imprint publication_date format)) {
149 0 $data->{$_} = $data->{json}{'gtm-books'}[0]{$_};
150 }
151  
152 0 ▶ $data->{binding} ||= $data->{format};
Condition0%0/3
$data->{"binding"} ||= $data->{"format"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"binding"} ||= $data->{"format"}
$data->{"binding"} $data->{"format"}
Truth table0%0/3
A ||= B
A= $data->{"binding"}B= $data->{"format"}
ABresult
1X1
011
000
153 0 ▶ $data->{pubdate} ||= $data->{publication_date};
Condition0%0/3
$data->{"pubdate"} ||= $data->{"publication_date"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"pubdate"} ||= $data->{"publication_date"}
$data->{"pubdate"} $data->{"publication_date"}
Truth table0%0/3
A ||= B
A= $data->{"pubdate"}B= $data->{"publication_date"}
ABresult
1X1
011
000
154 }
155  
156 #use Data::Dumper;
157 #print STDERR "\n# data=" . Dumper($data);
158  
159 0 ▶ return $self->handler("Could not extract data from the Waterstones result page. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
160 unless(defined $data);
161  
162 0 for(qw(author publisher description title)) {
163 0 ▶ $data->{$_} =~ s/&#0?39;/'/g if($data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$_} 0 0 0
164 }
165  
166 0 $data->{isbn10} = $self->convert_to_isbn10($ean);
167 0 ▶ $data->{title} =~ s!\s*\($data->{binding}\)\s*!! if($data->{title});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"title"} 0 0 0
168 0 ▶ $data->{description} =~ s!<[^>]+>!! if($data->{description});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"description"} 0 0 0
169  
170 0 ▶ if($data->{image}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"image"}) 0 0 0
171 0 $data->{thumb} = $data->{image};
172 0 $data->{thumb} =~ s!/images/nbd/[lms]/!/images/nbd/s/!;
173 0 $data->{image} =~ s!/images/nbd/[lms]/!/images/nbd/l/!;
174 }
175  
176 #use Data::Dumper;
177 #print STDERR "\n# data=" . Dumper($data);
178  
179 # trim top and tail
180 0 foreach (keys %$data) {
181 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
182 0 $data->{$_} =~ s!&nbsp;! !g;
183 0 $data->{$_} =~ s/^\s+//;
184 0 $data->{$_} =~ s/\s+$//;
185 }
186  
187 # my $url = $mech->uri();
188 # $url =~ s/\?.*//;
189  
190 my $bk = {
191 'ean13' => $data->{isbn13},
192 'isbn13' => $data->{isbn13},
193 'isbn10' => $data->{isbn10},
194 'isbn' => $data->{isbn13},
195 'author' => $data->{author},
196 'title' => $data->{title},
197 'book_link' => "$url",
198 'image_link' => $data->{image},
199 'thumb_link' => $data->{thumb},
200 'description' => $data->{description},
201 'pubdate' => $data->{pubdate},
202 'publisher' => $data->{publisher},
203 'binding' => $data->{binding},
204 'pages' => $data->{pages},
205 'json' => $data->{json},
206 0 'html' => $html
207 };
208  
209 #use Data::Dumper;
210 #print STDERR "\n# book=".Dumper($bk);
211  
212 0 $self->book($bk);
213 0 $self->found(1);
214 0 return $self->book;
215 }
216  
217 5 1;
218  
219 __END__
↑ Summary