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

filter: Statementstmt 32.7% 20 / 61 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 30.0% 27 / 90 SCAR 41.2
CC
11
Cov
25%
search
132.0
BEGIN
1.0
BEGIN
1.0
CRAP 61.4
↑ Summary
1 package WWW::Scraper::ISBN::Wheelers_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.14';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::Wheelers_Driver - Search drivers for the Wheelers' 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 Wheelers' 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.wheelers.co.nz/search/results/?query=';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 #--------------------------------------------------------------------------
43  
44 ###########################################################################
45 # Public Interface
46  
47 =head1 METHODS
48  
49 =over 4
50  
51 =item C<search()>
52  
53 Creates a query string, then passes the appropriate form fields to the
54 Wheelers server.
55  
56 The returned page should be the correct catalog page for that ISBN. If not the
57 function returns zero and allows the next driver in the chain to have a go. If
58 a valid page is returned, the following fields are returned via the book hash:
59  
60 isbn (now returns isbn13)
61 isbn10
62 isbn13
63 ean13 (industry name)
64 author
65 title
66 book_link
67 image_link
68 description
69 pubdate
70 publisher
71 binding (if known)
72 pages (if known)
73 weight (if known) (in grammes)
74 width (if known) (in millimetres)
75 height (if known) (in millimetres)
76  
77 The book_link and image_link refer back to the Wheelers website.
78  
79 =back
80  
81 =cut
82  
83 sub search {
84 0 ▶ my $self = shift;
Subroutine search: not called CC=11 SCAR=48.8
Pod documented
85 0 my $isbn = shift;
86 0 $self->found(0);
87 0 $self->book(undef);
88  
89 0 my $mech = WWW::Mechanize->new();
90 0 $mech->agent_alias( 'Linux Mozilla' );
91  
92 0 eval { $mech->get( SEARCH . $isbn ) };
93 0 ▶ return $self->handler("Wheelers 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
94 if($@ || !$mech->success() || !$mech->content());
95  
96 # The Book page
97 0 my $html = $mech->content();
98  
99 0 ▶ return $self->handler("Failed to find that book on Wheelers website.")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ m[Your search returned <b>0 results</b>]is 0 0 0
100 if($html =~ m!Your search returned <b>0 results</b>!si);
101  
102 #print STDERR "\n# html=[\n$html\n]\n";
103  
104 0 my $data;
105 0 ($data->{image}) = $html =~ m!href="(.*?/large/\d+/\d+.jpg)!i;
106 0 ($data->{thumb}) = $html =~ m!src="(.*?/small/\d+/\d+.jpg)!i;
107 0 ($data->{author}) = $html =~ m!<title>\s*.*?by ([^-]+)- ISBN:[^<]+</title><!i;
108 0 ($data->{title}) = $html =~ m!<a id="title" name="title"></a>\s*<h1>([^,<]+)</h1>!i;
109 0 ($data->{publisher}) = $html =~ m!<th>Publisher</th>\s*<td><a[^>]+>([^<]+)</a></td>!i;
110 0 ($data->{pubdate}) = $html =~ m!<th>International Publication Date</th>\s*<td>([^<]+)</td>!i;
111 0 ($data->{isbn13}) = $html =~ m!<th>ISBN-13</th>\s*<td>(\d+)</td>!i;
112 0 ($data->{isbn10}) = $html =~ m!<th>ISBN-10</th>\s*<td>(\d+)</td>!i;
113 0 ($data->{binding}) = $html =~ m!<th>Format</th>\s*<td>([^<]+)<!s;
114 0 ($data->{pages}) = $html =~ m!<th>Number of Pages</th>\s*<td>([^<]+)</td>!s;
115 0 ($data->{width},$data->{height}) = $html =~ m!<tr>\s*<th>Dimensions</th>\s*<td>Width:\s*([\d.]+)mm<br />Height:\s*([\d.]+)mm<br />(?:Spine:\s*([\d.]+)mm)?</td>\s*</tr>!s;
116 0 ($data->{weight}) = $html =~ m!<tr>\s*<th>Weight</th>\s*<td>(\d+)g</td>\s*</tr>!s;
117 0 ($data->{description}) = $html =~ m!<h2>Description of this book</h2>\s*<p>([^<]+)</p>!i;
118  
119 0 $data->{binding} =~ s/,.*//;
120 0 for(qw(image thumb)) {
121 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
122 0 ▶ next if($data->{$_} =~ m!^http://!);
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$_} =~ m[^http://] 0 0 0
123 0 $data->{$_} =~ s!^//!http://!;
124 }
125  
126 0 my $base = $mech->uri();
127 0 $base =~ s!(http://[^/]+).*!$1!;
128  
129 #use Data::Dumper;
130 #print STDERR "\n# " . Dumper($data);
131  
132 0 ▶ return $self->handler("Could not extract data from Wheelers result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
133 unless(defined $data);
134  
135 # trim top and tail
136 0 ▶ foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
137  
138 my $bk = {
139 'ean13' => $data->{isbn13},
140 'isbn13' => $data->{isbn13},
141 'isbn10' => $data->{isbn10},
142 'isbn' => $data->{isbn13},
143 'author' => $data->{author},
144 'title' => $data->{title},
145 'book_link' => $mech->uri(),
146 'image_link' => $data->{image},
147 'thumb_link' => $data->{thumb},
148 'description' => $data->{description},
149 'pubdate' => $data->{pubdate},
150 'publisher' => $data->{publisher},
151 'binding' => $data->{binding},
152 'pages' => $data->{pages},
153 'weight' => $data->{weight},
154 'width' => $data->{width},
155 'height' => $data->{height},
156 0 'html' => $html
157 };
158  
159 #use Data::Dumper;
160 #print STDERR "\n# book=".Dumper($bk);
161  
162 0 $self->book($bk);
163 0 $self->found(1);
164 0 return $self->book;
165 }
166  
167 5 1;
168 __END__
↑ Summary