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

filter: Statementstmt 39.6% 23 / 58 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 31.9% 31 / 97 SCAR 43.3
CC
13
Cov
28%
search
182.0
BEGIN
1.0
BEGIN
1.0
CRAP 76.0
↑ Summary
1 package WWW::Scraper::ISBN::Pearson_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.23';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::Pearson_Driver - Search driver for the Pearson Education 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 Pearson Education's online 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 SEARCH => 'http://www.pearsoned.co.uk/Bookshop/';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use constant DETAIL => 'http://www.pearsoned.co.uk/Bookshop/detail.asp?item=';
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 #--------------------------------------------------------------------------
44  
45 ###########################################################################
46 # Public Interface
47  
48 =head1 METHODS
49  
50 =over 4
51  
52 =item C<search()>
53  
54 Creates a query string, then passes the appropriate form fields to the Pearson
55 Education server.
56  
57 The returned page should be the correct catalog page for that ISBN. If not the
58 function returns zero and allows the next driver in the chain to have a go. If
59 a valid page is returned, the following fields are returned via the book hash:
60  
61 isbn (now returns isbn13)
62 isbn10
63 isbn13
64 ean13 (industry name)
65 author
66 title
67 book_link
68 image_link
69 description
70 pubdate
71 publisher
72 binding (if known)
73 pages (if known)
74 weight (if known) (in grammes)
75 width (if known) (in millimetres)
76 height (if known) (in millimetres)
77  
78 The book_link and image_link refer back to the Pearson Education UK website.
79  
80 =back
81  
82 =cut
83  
84 sub search {
85 0 ▶ my $self = shift;
Subroutine search: not called CC=13 SCAR=52.0
Pod documented
86 0 my $isbn = shift;
87 0 $self->found(0);
88 0 $self->book(undef);
89  
90 0 my $mech = WWW::Mechanize->new();
91 0 $mech->agent_alias( 'Linux Mozilla' );
92  
93 0 eval { $mech->get( SEARCH ) };
94 0 ▶ return $self->handler("Pearson Education 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
95 if($@ || !$mech->success() || !$mech->content());
96  
97 #print STDERR "\n# content=[\n".$mech->content()."\n]\n";
98  
99 0 $mech->form_id('hipGlobalSearchForm');
100 0 $mech->set_fields( 'txtSearch' => $isbn );
101
102 0 eval { $mech->submit() };
103 0 ▶ return $self->handler("Failed to find that book on Pearson Education website.")
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 # The Book page
107 0 my $html = $mech->content();
108 #print STDERR "\n# html=[\n$html\n]\n";
109  
110 0 ▶ return $self->handler("Failed to find that book on Pearson Education website.")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ m[<p>Your search for <b>\d+</b> returned 0 results. Please search again.</p>]is 0 0 0
111 if($html =~ m!<p>Your search for <b>\d+</b> returned 0 results. Please search again.</p>!si);
112  
113 0 my $data;
114 0 ($data->{image}) = $html =~ m!"(http://images.pearsoned-ema.com/jpeg/large/\d+\.jpg)"!i;
115 0 ($data->{thumb}) = $html =~ m!"(http://images.pearsoned-ema.com/jpeg/small/\d+\.jpg)"!i;
116 ($data->{title},
117 $data->{author},
118 $data->{pubdate},
119 $data->{binding},
120 $data->{pages},
121 0 $data->{isbn13}) = $html =~ m! <div\s*class="biblio">\s*
122 <h1\s*class="larger\s*bold">(.*?)</h1>\s*(?:.*?<br\s*/>\s*)?
123 <h2\s*class="body"><a\s*title[^>]+>(.*?)</a>\s*</h2>\s*
124 ([^,]+),\s*([^,]+)(?:,\s*(\d+)\s+pages)?(?:</a>)?<br\s*/>\s*
125 ISBN(?:13)?:\s*(\d+)\s*<br\s*/>!ix;
126 0 ($data->{description}) = $html =~ m!<div class="desc-text"><p><p>([^<]+)!is;
127 0 ($data->{bookid}) = $html =~ m!recommend.asp\?item=(\d+)!i;
128  
129 #use Data::Dumper;
130 #print STDERR "\n# " . Dumper($data);
131  
132 0 ▶ return $self->handler("Could not extract data from Pearson Education result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
133 unless(defined $data);
134  
135 # remove HTML tags
136 0 for(qw(author binding)) {
137 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
138 0 $data->{$_} =~ s!<[^>]+>!!g;
139 }
140  
141 # trim top and tail
142 0 for(keys %$data) {
143 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
144 0 $data->{$_} =~ s/^\s+//;
145 0 $data->{$_} =~ s/\s+$//;
146 }
147  
148 0 my $uri = $mech->uri(); #DETAIL . $data->{bookid};
149  
150 my $bk = {
151 'ean13' => $data->{isbn13},
152 'isbn13' => $data->{isbn13},
153 'isbn10' => $self->convert_to_isbn10( $data->{isbn13} ),
154 'isbn' => $data->{isbn13},
155 'author' => $data->{author},
156 'title' => $data->{title},
157 'book_link' => "$uri",
158 'image_link' => $data->{image},
159 'thumb_link' => $data->{thumb},
160 'description' => $data->{description},
161 'pubdate' => $data->{pubdate},
162 'publisher' => q!Pearson Education!,
163 'binding' => $data->{binding},
164 'pages' => $data->{pages},
165 'weight' => $data->{weight},
166 'width' => $data->{width},
167 'height' => $data->{height},
168 0 'html' => $html
169 };
170  
171 #use Data::Dumper;
172 #print STDERR "\n# book=".Dumper($bk);
173  
174 0 $self->book($bk);
175 0 $self->found(1);
176 0 return $self->book;
177 }
178  
179 5 1;
180 __END__
↑ Summary