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

filter: Statementstmt 33.8% 24 / 71 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 24.8% 32 / 129 CC 19 SCAR 56.3
CC
19
Cov
22%
search
380.0
BEGIN
1.0
BEGIN
1.0
CRAP 278.0
↑ Summary
1 package WWW::Scraper::ISBN::PickABook_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::PickABook_Driver - Search driver for the Pick-A-Book 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 Pick-A-Book 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.pickabook.co.uk/';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use constant SEARCH => 'http://www.pickabook.co.uk/%s.aspx?ToSearch=TRUE';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 my ($URL1,$URL2) = ('http://www.PickABook.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 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 Pick-A-Book
78 website.
79  
80 =back
81  
82 =cut
83  
84 0 ▶sub search {
Subroutine search: not called CC=19 SCAR=59.4
Pod documented
85 0 my $self = shift;
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( 'Windows IE 6' );
98 0 $mech->add_header( 'Accept-Encoding' => undef );
99 0 $mech->add_header( 'Referer' => REFERER );
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("The Pick-A-Book 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 Pick-A-Book website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if not $html or $html =~ m[Your search for "<span id="lblresultline"><b>ISBN = \d+</b></span>"\s+has produced 0 results.]is 0 0 0
Condition0%0/3
not $html or $html =~ m[Your search for "<span id="lblresultline"><b>ISBN = \d+</b></span>"\s+has produced 0 results.]is
l!l&&r!l&&!r
000
MC/DC0%0/2
not $html or $html =~ m[Your search for "<span id="lblresultline"><b>ISBN = \d+</b></span>"\s+has produced 0 results.]is
not $html $html =~ m[Your search for "<span id="lblresultline"><b>ISBN = \d+</b></span>"\s+has produced 0 results.]is
Truth table0%0/3
not(A) or B
A= not $htmlB= $html =~ m[Your search for "<span id="lblresultline"><b>ISBN = \d+</b></span>"\s+has produced 0 results.]is
ABresult
1X1
011
000
112 if(!$html || $html =~ m!Your search for "<span id="lblresultline"><b>ISBN = \d+</b></span>"\s+has produced 0 results.!si);
113  
114 0 $html =~ s/&amp;/&/g;
115 #print STDERR "\n# content2=[\n$html\n]\n";
116  
117 0 my $data;
118 ($data->{author},$data->{title})
119 0 = $html =~ m!<span id="lblbooktitlevalue" title="([^"]+)"[^>]*>([^<]+)</span>!si;
120 0 ($data->{binding}) = $html =~ m!<span id="lblbindingvalue"[^>]*>([^<]+)</span>!si;
121 0 ($data->{isbn13}) = $html =~ m!<span id="lblisbn13"[^>]*>([^<]+)</span>!si;
122 0 ($data->{isbn10}) = $html =~ m!<span id="lblISBNvalue"[^>]*>([^<]+)</span>!si;
123 0 ($data->{publisher}) = $html =~ m!<span id="lblpublishervalue"[^>]*>([^<]+)</span>!si;
124 0 ($data->{pubdate}) = $html =~ m!<span id="lblpublishdatevalue"[^>]*>([^<]+)</span>!si;
125 0 ($data->{pages}) = $html =~ m!<span id="lblpagesvalue"[^>]*>([^<]+)</span>!si;
126 0 ($data->{description}) = $html =~ m!<span id="lbldescriptionvalueL?"[^>]*>([^<]+)</span>!si;
127 0 ($data->{isbn10}) = $html =~ m!<span id="lblISBNvalue"[^>]*>([^<]+)</span>!si;
128 0 ($data->{image}) = $html =~ m!<input type="image" name="imgbook" id="imgbook" title="[^"]+" src="([^"]+)"[^>]*>!si;
129  
130 0 ▶ if($data->{image}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"image"}) 0 0 0
131 0 $data->{image} = REFERER . $data->{image};
132 0 $data->{thumb} = $data->{image};
133 }
134 0 ▶ $data->{author} =~ s/.*?\s+by\s+// if($data->{author});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"author"} 0 0 0
135 0 ▶ $data->{publisher} =~ s/&#0?39;/'/g if($data->{publisher});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"publisher"} 0 0 0
136 0 for(qw(title author publisher)) {
137 0 ▶ next unless($data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$_} 0 0 0
138 0 $data->{$_} =~ s/([\w']+)/\u\L$1/g;
139 }
140  
141 #use Data::Dumper;
142 #print STDERR "\n# " . Dumper($data);
143  
144 0 ▶ return $self->handler("Could not extract data from the Pick-A-Book result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
145 unless(defined $data);
146  
147 # trim top and tail
148 0 foreach (keys %$data) {
149 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
150 0 $data->{$_} =~ s!&nbsp;! !g;
151 0 $data->{$_} =~ s/^\s+//;
152 0 $data->{$_} =~ s/\s+$//;
153 }
154  
155 0 my $url = $mech->uri();
156  
157 my $bk = {
158 'ean13' => $data->{isbn13},
159 'isbn13' => $data->{isbn13},
160 'isbn10' => $data->{isbn10},
161 'isbn' => $data->{isbn13},
162 'author' => $data->{author},
163 'title' => $data->{title},
164 'book_link' => "$url",
165 'image_link' => $data->{image},
166 'thumb_link' => $data->{thumb},
167 'description' => $data->{description},
168 'pubdate' => $data->{pubdate},
169 'publisher' => $data->{publisher},
170 'binding' => $data->{binding},
171 'pages' => $data->{pages},
172 0 'html' => $html
173 };
174  
175 #use Data::Dumper;
176 #print STDERR "\n# book=".Dumper($bk);
177  
178 0 $self->book($bk);
179 0 $self->found(1);
180 0 return $self->book;
181 }
182  
183 5 1;
184  
185 __END__
↑ Summary