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

filter: Statementstmt 30.3% 24 / 79 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 21.1% 32 / 151 SCAR 59.1
CC
25
Cov
18%
search
650.0
BEGIN
1.0
BEGIN
1.0
CRAP 367.3
↑ Summary
1 package WWW::Scraper::ISBN::BookDepository_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.12';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::BookDepository_Driver - Search driver for The Book Depository 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 Book Depository 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.bookdepository.co.uk/';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use constant SEARCH => 'http://www.bookdepository.co.uk/search?search=search&searchTerm=';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 my ($URL1,$URL2) = ('http://www.bookdepository.co.uk/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 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 The Book Depository website.
80  
81 =back
82  
83 =cut
84  
85 sub search {
86 0 ▶ my $self = shift;
Subroutine search: not called CC=25 SCAR=64.8
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")
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( 'Windows IE 6' );
99 0 $mech->add_header( 'Accept-Encoding' => undef );
100 0 $mech->add_header( 'Referer' => REFERER );
101  
102 #print STDERR "\n# search=[".SEARCH."$ean]\n";
103 0 eval { $mech->get( SEARCH . $ean ) };
104 0 ▶ return $self->handler("The Book Depository 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
105 if($@ || !$mech->success() || !$mech->content());
106  
107 # The Book page
108 0 my $html = $mech->content();
109  
110 0 ▶ return $self->handler("Failed to find that book on The Book Depository website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry, there are no results for/is 0 0 0
111 if($html =~ m!Sorry, there are no results for!si);
112
113 0 $html =~ s/&amp;/&/g;
114 #print STDERR "\n# content2=[\n$html\n]\n";
115  
116 0 my $data;
117  
118 # first pass wih metadata
119 0 ($data->{isbn13}) = $html =~ m!<meta itemprop="isbn" content="([^"]+)"!si;
120 0 ($data->{publisher}) = $html =~ m!<meta itemprop="publisher" content="([^"]+)"!si;
121 0 ($data->{pubdate}) = $html =~ m!<meta itemprop="datePublished" content="([^"]+)"!si;
122 0 ($data->{title}) = $html =~ m!<meta itemprop="name" content="([^"]+)"!si;
123 0 ($data->{author}) = $html =~ m!<meta itemprop="author" content="([^"]+)"!si;
124 0 ($data->{description}) = $html =~ m!<meta itemprop="description" content="([^"]+)"!si;
125 0 ($data->{image}) = $html =~ m!<meta itemprop="image" content="([^"]+)"!si;
126 0 ($data->{url}) = $html =~ m!<meta itemprop="url" content="([^"]+)"!si;
127 0 ($data->{pages}) = $html =~ m!<meta itemprop="numberOfPages" content="([^"]+)"!si;
128  
129 # second pass with page data
130 0 ▶ ($data->{isbn13}) = $html =~ m!<strong>ISBN 13:</strong><span property="dc:identifier">([^<]+)</span>!si unless($data->{isbn13});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"isbn13"} 0 0 0
131 0 ▶ ($data->{publisher}) = $html =~ m!<li><strong>Publisher:</strong> <a property=[^>]+>([^<]+)</a></li>!si unless($data->{publisher});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"publisher"} 0 0 0
132 0 ▶ ($data->{pubdate}) = $html =~ m!<strong>Publication date:</strong>\s*<span property='dc:available'>([^<]+)</span>!si unless($data->{pubdate});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"pubdate"} 0 0 0
133 0 ▶ ($data->{pages}) = $html =~ m!<span property='dc:SizeOrDuration'>\s*(\d+) pages</span>!si unless($data->{pages});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"pages"} 0 0 0
134 0 ▶ ($data->{image}) = $html =~ m!"(http://\w+.bdcdn.net/assets/images/book/large/\d+/\d+/\d+.jpg)"!si unless($data->{image});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"image"} 0 0 0
135 ($data->{title},$data->{author})
136 0 ▶ = $html =~ m!<title>(.*):\s+([^:]+)\s+:\s+\d+\s*</title>! unless($data->{title} && $data->{author});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"title"} and $data->{"author"} 0 0 0
Condition0%0/3
$data->{"title"} and $data->{"author"}
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"title"} and $data->{"author"}
$data->{"title"} $data->{"author"}
Truth table0%0/3
A and B
A= $data->{"title"}B= $data->{"author"}
ABresult
0X0
100
111
137  
138 # other page data
139 0 ($data->{isbn10}) = $html =~ m!<li><strong>ISBN 10:</strong>\s*([^<]+)</li>!si;
140 0 ($data->{binding}) = $html =~ m!<strong>Format:</strong>\s*<span property="dc:format">([^<]+)!si;
141 0 ($data->{thumb}) = $html =~ m!"(http://\w+.bdcdn.net/assets/images/book/medium/\d+/\d+/\d+.jpg)"!si;
142 ($data->{width},$data->{height},$data->{depth},$data->{weight})
143 0 = $html =~ m!<strong>Dimensions:</strong>\s*(\d+)mm\s*x\s*(\d+)mm\s*x\s*(\d+)mm\s*\|\s*(\d+)g!;
144  
145 # clean up
146 0 ▶ $data->{publisher} =~ s/&#0?39;/'/g if($data->{publisher});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"publisher"} 0 0 0
147 0 ▶ $data->{width} = int($data->{width}) if($data->{width});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"width"} 0 0 0
148 0 ▶ $data->{height} = int($data->{height}) if($data->{height});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"height"} 0 0 0
149 0 ▶ $data->{weight} = int($data->{weight}) if($data->{weight});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"weight"} 0 0 0
150 0 ▶ unless($data->{thumb}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"thumb"}) 0 0 0
151 0 $data->{thumb} = $data->{image};
152 0 $data->{thumb} =~ s!/large/!/medium/!;
153 }
154  
155 #use Data::Dumper;
156 #print STDERR "\n# " . Dumper($data);
157  
158 0 ▶ return $self->handler("Could not extract data from The Book Depository 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!&nbsp;! !g;
165 0 $data->{$_} =~ s/^\s+//;
166 0 $data->{$_} =~ s/\s+$//;
167 }
168  
169 0 my $url = $mech->uri();
170 0 $url =~ s/\?.*//;
171  
172 my $bk = {
173 'ean13' => $data->{isbn13},
174 'isbn13' => $data->{isbn13},
175 'isbn10' => $data->{isbn10},
176 'isbn' => $data->{isbn13},
177 'author' => $data->{author},
178 'title' => $data->{title},
179 'book_link' => "$url",
180 'image_link' => $data->{image},
181 'thumb_link' => $data->{thumb},
182 'description' => $data->{description},
183 'pubdate' => $data->{pubdate},
184 'publisher' => $data->{publisher},
185 'binding' => $data->{binding},
186 'pages' => $data->{pages},
187 'weight' => $data->{weight},
188 'width' => $data->{width},
189 'height' => $data->{height},
190 0 'html' => $html
191 };
192  
193 #use Data::Dumper;
194 #print STDERR "\n# book=".Dumper($bk);
195  
196 0 $self->book($bk);
197 0 $self->found(1);
198 0 return $self->book;
199 }
200  
201 5 1;
202  
203 __END__
↑ Summary