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

filter: Statementstmt 26.5% 21 / 79 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 19.1% 28 / 146 SCAR 58.0
CC
23
Cov
17%
search
552.0
BEGIN
1.0
BEGIN
1.0
CRAP 330.6
↑ Summary
1 package WWW::Scraper::ISBN::WordPower_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::WordPower_Driver - Search driver for Word Power 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 Word Power 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 SEARCH => 'http://www.word-power.co.uk/searchBook.php?options=isbn&imageField.x=19&imageField.y=9&keywords=';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 my ($URL1,$URL2,$URL3) = ('http://www.word-power.co.uk','/books/[^>]+-I','/');
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 Word
55 Power 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 Word Power website.
79  
80 =back
81  
82 =cut
83  
84 sub search {
85 0 ▶ my $self = shift;
Subroutine search: not called CC=23 SCAR=63.1
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 [$isbn]")
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  
100 0 eval { $mech->get( SEARCH . $isbn ) };
101 0 ▶ return $self->handler("WordPower 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
102 if($@ || !$mech->success() || !$mech->content());
103  
104 0 my $content = $mech->content;
105 0 my ($link) = $content =~ m!($URL2$ean$URL3)!si;
106 #print STDERR "\n# link1=[$URL2$ean$URL3]\n";
107 #print STDERR "\n# link2=[$URL1$link]\n";
108 #print STDERR "\n# content1=[\n$content\n]\n";
109 #print STDERR "\n# is_html=".$mech->is_html().", content type=".$mech->content_type()."\n";
110 #print STDERR "\n# dump headers=".$mech->dump_headers."\n";
111  
112 0 ▶ return $self->handler("Failed to find that book on WordPower website.")
Branch: 1 branch
BranchTrueFalseTotal
unless $link 0 0 0
113 unless($link);
114  
115 0 eval { $mech->get( $URL1 . $link ) };
116 0 ▶ return $self->handler("WordPower 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
117 if($@ || !$mech->success() || !$mech->content());
118  
119 # The Book page
120 0 my $html = $mech->content();
121  
122 0 ▶ return $self->handler("Failed to find that book on WordPower website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry, we couldn't find any matches for/is 0 0 0
123 if($html =~ m!Sorry, we couldn't find any matches for!si);
124
125 #print STDERR "\n# content2=[\n$html\n]\n";
126  
127 0 my $data;
128 0 ($data->{isbn10}) = $self->convert_to_isbn10($ean);
129 0 ($data->{publisher}) = $html =~ m!<td[^>]+>Publisher</td>\s*<td[^>]+><a[^>]+>([^<]+)</a></td>!i;
130 0 ($data->{pubdate}) = $html =~ m!<td[^>]+>Publication date</td>\s*<td[^>]+>([^<]+)</td>!i;
131  
132 0 ▶ $data->{publisher} =~ s!<[^>]+>!!g if($data->{publisher});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"publisher"} 0 0 0
133 0 ▶ $data->{pubdate} =~ s!\s+! !g if($data->{pubdate});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"pubdate"} 0 0 0
134  
135 0 ($data->{isbn13}) = $html =~ m!<td[^>]+>ISBN13</td>\s*<td[^>]+>([^<]+)</td>!i;
136 0 ($data->{isbn10}) = $html =~ m!<td[^>]+>ISBN</td>\s*<td[^>]+>([^<]+)</td>!i;
137 0 ($data->{image}) = $html =~ m!"(http://.*?/product_images/$data->{isbn13}.jpg)"!i;
138 0 ($data->{thumb}) = $html =~ m!"(http://.*?/product_images/$data->{isbn13}.jpg)"!i;
139 0 ($data->{author}) = $html =~ m!by\s*<a href="/author/[^/]+/">([^<]+)</a>!i;
140 0 ($data->{title}) = $html =~ m!<p class="p_bookTitle"><b>([^<]+)</b>!i;
141 0 ($data->{description}) = $html =~ m!<div class="TabbedPanelsContentGroup">\s*<div class="TabbedPanelsContent">([^~]+)</div>!si;
142 0 ($data->{binding}) = $html =~ m!<td[^>]+>Format</td>\s*<td[^>]+>([^<]+)</td>!s;
143 0 ($data->{pages}) = $html =~ m!<tr valign="top">\s*<td valign="middle">Pages</td>\s*<td valign="middle">([\d.]+)</td>\s*</tr>!s;
144 0 ($data->{weight}) = $html =~ m!<tr valign="top">\s*<td valign="middle">Weight .grammes.</td>\s*<td valign="middle">([\d.]+)</td>\s*</tr>!s;
145 0 ($data->{width}) = $html =~ m!<td[^>]+>Width \(mm\)</td>\s*<td[^>]+>([^<]+)</td>!s;
146 0 ($data->{height}) = $html =~ m!<td[^>]+>Height \(mm\)</td>\s*<td[^>]+>([^<]+)</td>!s;
147  
148 0 for my $key (qw(weight width height)) {
149 0 ▶ next unless($data->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$key} 0 0 0
150 0 $data->{$key} =~ s/\.0+$//;
151 }
152  
153 0 ▶ $data->{author} =~ s!<[^>]+>!!g if($data->{author});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"author"} 0 0 0
154 0 ▶ if($data->{description}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"description"}) 0 0 0
155 0 $data->{description} =~ s!<script.*!!si;
156 0 $data->{description} =~ s!<(p|br\s*/)>!\n!g;
157 0 $data->{description} =~ s!<[^>]+>!!gs;
158 0 $data->{description} =~ s! +$!!gm;
159 0 $data->{description} =~ s!\n\n!\n!gs;
160 }
161  
162 #use Data::Dumper;
163 #print STDERR "\n# " . Dumper($data);
164  
165 0 ▶ return $self->handler("Could not extract data from WordPower result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
166 unless(defined $data);
167  
168 # trim top and tail
169 0 foreach (keys %$data) {
170 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
171 0 $data->{$_} =~ s!&nbsp;! !g;
172 0 $data->{$_} =~ s/^\s+//;
173 0 $data->{$_} =~ s/\s+$//;
174 }
175  
176 my $bk = {
177 'ean13' => $data->{isbn13},
178 'isbn13' => $data->{isbn13},
179 'isbn10' => $data->{isbn10},
180 'isbn' => $data->{isbn13},
181 'author' => $data->{author},
182 'title' => $data->{title},
183 'book_link' => $mech->uri(),
184 'image_link' => $data->{image},
185 'thumb_link' => $data->{thumb},
186 'description' => $data->{description},
187 'pubdate' => $data->{pubdate},
188 'publisher' => $data->{publisher},
189 'binding' => $data->{binding},
190 'pages' => $data->{pages},
191 'weight' => $data->{weight},
192 'width' => $data->{width},
193 'height' => $data->{height},
194 0 'html' => $html
195 };
196  
197 #use Data::Dumper;
198 #print STDERR "\n# book=".Dumper($bk);
199  
200 0 $self->book($bk);
201 0 $self->found(1);
202 0 return $self->book;
203 }
204  
205 5 1;
206  
207 __END__
↑ Summary