AmazonUS_Driver.pm

filter: Statementstmt 80.3% 82 / 102 Branchbran 65.9% 29 / 44 Conditioncond 28.5% 12 / 42 MC/DCmcdc 21.7% 5 / 23 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 61.8% 136 / 220 CC 43 SCAR 42.2
CC
43
Cov
65%
search
182.0
_parse
32.9
BEGIN
1.0
CRAP 67.8
« AmazonUK_Driver.pm ↑ Summary Amazon_Driver.pm »
1 package WWW::Scraper::ISBN::AmazonUS_Driver;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 $VERSION = '0.41';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::AmazonUS_Driver - Search driver for Amazon.com
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 (US) Amazon online catalog.
22  
23 =cut
24  
25 #--------------------------------------------------------------------------
26  
27 ###########################################################################
28 # Inheritence
29  
30 6 ▶use base qw(WWW::Scraper::ISBN::Driver);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 ###########################################################################
33 # Modules
34  
35 6 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 6 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 ###########################################################################
39 # Variables
40  
41 6 my $AMA_SEARCH = 'http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias=us-stripbooks-tree&field-keywords=%s';
42 6 my $AMA_URL = 'http://www.amazon.com/[^/]+/dp/[\dX]+/ref=sr_1_1.*?sr=1-1';
43 6 my $IN2MM = 0.0393700787; # number of inches in a millimetre (mm)
44 6 my $LB2G = 0.00220462; # number of pounds (lbs) in a gram
45 6 my $OZ2G = 0.035274; # number of ounces (oz) in a gram
46  
47 #--------------------------------------------------------------------------
48  
49 ###########################################################################
50 # Public Interface
51  
52 =head1 METHODS
53  
54 =over 4
55  
56 =item C<search()>
57  
58 Creates a query string, then passes the appropriate form fields to the
59 Amazon (US) server.
60  
61 The returned page should be the correct catalog page for that ISBN. If not the
62 function returns zero and allows the next driver in the chain to have a go. If
63 a valid page is returned, the following fields are returned via the book hash:
64  
65 isbn (now returns isbn13)
66 isbn10
67 isbn13
68 ean13 (industry name)
69 author
70 title
71 book_link
72 thumb_link
73 image_link
74 pubdate
75 publisher
76 binding (if known)
77 pages (if known)
78 weight (if known) (in grams)
79 width (if known) (in millimetres)
80 height (if known) (in millimetres)
81 depth (if known) (in millimetres)
82  
83 The book_link, thumb_link and image_link refer back to the Amazon (US) website.
84  
85 =back
86  
87 =cut
88  
89 sub search {
90 0 ▶ my $self = shift;
Subroutine search: not called CC=13 SCAR=52.0
Pod documented
91 0 my $isbn = shift;
92 0 $self->found(0);
93 0 $self->book(undef);
94  
95 # validate and convert into EAN13 format
96 0 my $ean = $self->convert_to_ean13($isbn);
97 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
98 if(!$ean || (length $isbn == 13 && $isbn ne $ean)
99 || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
100  
101 0 my $mech = WWW::Mechanize->new();
102 0 $mech->agent_alias( 'Linux Mozilla' );
103  
104 0 my $search = sprintf $AMA_SEARCH, $ean;
105  
106 0 eval { $mech->get( $search ) };
107 0 ▶ return $self->handler("Amazon US 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
108 if($@ || !$mech->success() || !$mech->content());
109  
110 0 my $content = $mech->content();
111 0 my ($link) = $content =~ m!($AMA_URL)!s;
112 0 ▶ return $self->handler("Failed to find that book on Amazon US website.")
Branch: 1 branch
BranchTrueFalseTotal
unless $link 0 0 0
113 unless($link);
114  
115 0 eval { $mech->get( $link ) };
116 0 ▶ return $self->handler("Amazon US 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 0 return $self->_parse($mech);
120 }
121  
122 sub _parse {
123 2 ▶ my $self = shift;
Subroutine _parse: called CC=31 SCAR=34.9
124 2 my $mech = shift;
125  
126 # The Book page
127 2 my $html = $mech->content;
128 2 my $data = {};
129  
130 #print STDERR "\n# html=[$html]\n";
131  
132 2 my @patterns = (
133 qr/<meta name="description" content="(.*?) \[(.*?)\] on Amazon.com/si,
134 qr/<meta name="description" content="(.*?): (.*?): (\d+): Amazon.com: Books/si,
135 qr/<meta name="description" content="(?:Amazon.com:)?\s*(.*?).\d+.:\s+([^:]+): Books/si,
136 qr/<meta name="description" content="(?:Amazon.com: Books: )?\s*(.*?)(?:\s+by|,)\s+(.*)/si,
137 qr/<meta name="description" content="(?:Amazon.com:)?\s*(.*?)(?:\s+by|,|:)\s+([^:]+): Books/si
138 );
139  
140 2 for my $pattern (@patterns) {
141 6 my ($title,$author) = $html =~ $pattern;
142 6 ▶ $data->{title} ||= $title;
Condition66%2/3
$data->{"title"} ||= $title
l!l&&r!l&&!r
015
MC/DC50%1/2
$data->{"title"} ||= $title
$data->{"title"} $title
Truth table66%2/3
A ||= B
A= $data->{"title"}B= $title
ABresult
1X1
011
000
143 6 ▶ $data->{author} ||= $author;
Condition66%2/3
$data->{"author"} ||= $author
l!l&&r!l&&!r
015
MC/DC50%1/2
$data->{"author"} ||= $author
$data->{"author"} $author
Truth table66%2/3
A ||= B
A= $data->{"author"}B= $author
ABresult
1X1
011
000
144 #print STDERR "\n# title=[$data->{title}], author=[$data->{author}] pattern=[$pattern]\n";
145  
146 6 ▶ last if($data->{title} && $data->{author});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"title"} and $data->{"author"} 1 5 6
Condition66%2/3
$data->{"title"} and $data->{"author"}
!ll&&!rl&&r
501
MC/DC50%1/2
$data->{"title"} and $data->{"author"}
$data->{"title"} $data->{"author"}
Truth table66%2/3
A and B
A= $data->{"title"}B= $data->{"author"}
ABresult
0X0
100
111
147 }
148  
149 2 ($data->{binding},$data->{pages}) = $html =~ m!<li><b>(Paperback|Hardcover):</b>\s*([\d.]+)\s*pages</li>!si;
150 2 ($data->{published}) = $html =~ m!<li><b>Publisher:</b>\s*(.*?)</li>!si;
151 2 ($data->{isbn10}) = $html =~ m!<li><b>ISBN-10:</b>\s*(.*?)</li>!si;
152 2 ($data->{isbn13}) = $html =~ m!<li><b>ISBN-13:</b>\s*(.*?)</li>!si;
153 2 ($data->{content}) = $html =~ m!<meta name="description" content="([^"]+)"!si;
154  
155 2 @patterns = (
156 qr!<h2>Book Description</h2>.*?<div id="postBodyPS"[^>]+>\s*<div[^>]*>\s*<p>\s*(.*?)\s*</p>\s*</div!si,
157 qr!<h2>Book Description</h2>.*?<div id="postBodyPS"[^>]+>\s*<div[^>]*>\s*(.*?)\s*</div!si,
158 qr!<h3 class="productDescriptionSource">(?:Product Description|From the Back Cover)</h3>\s*<div class="productDescriptionWrapper">\s*<p>([^<]+)!si,
159 qr!<h3 class="productDescriptionSource">(?:Product Description|From the Back Cover)</h3>\s*<div class="productDescriptionWrapper">\s*(.*?)<div!si,
160 qr!<div id="bookDescription_feature_div"[^>]+>\s*<script[^>]*>.*?</script>\s*<noscript>(.*?)</noscript>!si
161 );
162  
163 2 for my $pattern (@patterns) {
164 6 my ($desc) = $html =~ $pattern;
165 6 ▶ $desc =~ s/\s+$//s if($desc);
Branch: 1 branch
BranchTrueFalseTotal
if $desc 1 5 6
166 6 ▶ $data->{description} ||= $desc;
Condition66%2/3
$data->{"description"} ||= $desc
l!l&&r!l&&!r
015
MC/DC50%1/2
$data->{"description"} ||= $desc
$data->{"description"} $desc
Truth table66%2/3
A ||= B
A= $data->{"description"}B= $desc
ABresult
1X1
011
000
167  
168 6 ▶ last if($data->{description});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"description"} 1 5 6
169 }
170  
171 2 for my $key (qw(description)) {
172 2 ▶ next unless($data->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$key} 1 1 2
173 1 $data->{$key} =~ s/<[^>]*>//gs;
174 }
175  
176 # amazon use both ounces and pounds
177 2 my $weight;
178 2 ($data->{weight},$weight) = $html =~ m!<li><b>Shipping Weight:</b>\s*([\d.]+)\s*(ounces|pounds)!si;
179 2 ▶ $data->{weight} = int($data->{weight} / $OZ2G) if($data->{weight} && $weight eq 'ounces');
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"weight"} and $weight eq "ounces" 1 1 2
Condition66%2/3
$data->{"weight"} and $weight eq "ounces"
!ll&&!rl&&r
101
MC/DC50%1/2
$data->{"weight"} and $weight eq "ounces"
$data->{"weight"} $weight eq "ounces"
Truth table66%2/3
A and B
A= $data->{"weight"}B= $weight eq "ounces"
ABresult
0X0
100
111
180 2 ▶ $data->{weight} = int($data->{weight} / $LB2G) if($data->{weight} && $weight eq 'pounds');
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"weight"} and $weight eq "pounds" 0 2 2
Condition66%2/3
$data->{"weight"} and $weight eq "pounds"
!ll&&!rl&&r
110
MC/DC0%0/2
$data->{"weight"} and $weight eq "pounds"
$data->{"weight"} $weight eq "pounds"
Truth table66%2/3
A and B
A= $data->{"weight"}B= $weight eq "pounds"
ABresult
0X0
100
111
181  
182 # amazon change this regularly
183 2 my @size = $html =~ m!<li><b>\s*Product Dimensions:\s*</b>\s*([\d.]+) x ([\d.]+) x ([\d.]+) (cm)\s*</li>!si;
184 2 ▶ @size = $html =~ m!<li><b>\s*Product Dimensions:\s*</b>\s*([\d.]+) x ([\d.]+) x ([\d.]+) (inches)\s*</li>!si unless(@size);
Branch: 1 branch
BranchTrueFalseTotal
unless @size 2 0 2
185 2 ▶ if(@size) {
Branch: 1 branch
BranchTrueFalseTotal
if (@size) 1 1 2
186 1 my $type = pop @size;
187 1 ($data->{depth},$data->{width},$data->{height}) = sort @size;
188 1 ▶ if($type eq 'cm') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'cm') { } 0 1 1
elsif ($type eq 'inches') { } 1 0 1
189 0 $data->{$_} = int($data->{$_} * 10) for(qw( height width depth ));
190 } elsif($type eq 'inches') {
191 1 $data->{$_} = int($data->{$_} / $IN2MM) for(qw( height width depth ));
192 }
193 }
194  
195 # The images
196 2 my ($json) = $html =~ /var colorImages = ([^;]+);/si;
197 2 ▶ if($json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json) { } 1 1 2
198 1 my $code = decode_json($json);
199 1 my @order = grep {$_} $code->{initial}[0]{thumb}, $code->{initial}[0]{landing}, @{$code->{initial}[0]{main}}, $code->{initial}[0]{large};
200 1 ▶ $data->{thumb_link} = $order[0] if(@order);
Branch: 1 branch
BranchTrueFalseTotal
if @order 1 0 1
201 1 ▶ $data->{image_link} = $order[-1] if(@order);
Branch: 1 branch
BranchTrueFalseTotal
if @order 1 0 1
202 } else {
203 1 ($data->{thumb_link}) = $html =~ m!imageGalleryData.*?thumbUrl":"([^"]+)"!;
204 1 ($data->{image_link}) = $html =~ m!imageGalleryData.*?mainUrl":"([^"]+)"!;
205 }
206  
207 2 ▶ ($data->{publisher},$data->{pubdate}) = ($data->{published} =~ /\s*(.*?)(?:;.*?)?\s+\((.*?)\)/) if($data->{published});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"published"} 1 1 2
208 2 ▶ $data->{isbn10} =~ s/[^\dX]+//g if($data->{isbn10});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"isbn10"} 1 1 2
209 2 ▶ $data->{isbn13} =~ s/\D+//g if($data->{isbn13});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"isbn13"} 1 1 2
210  
211  
212 return $self->handler("Could not extract data from Amazon US result page.")
213 2 ▶ unless(defined $data->{isbn13});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"isbn13"} 1 1 2
214  
215 # trim top and tail
216 1 ▶ foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 17 17
217  
218 my $bk = {
219 'ean13' => $data->{isbn13},
220 'isbn13' => $data->{isbn13},
221 'isbn10' => $data->{isbn10},
222 'isbn' => $data->{isbn13},
223 'author' => $data->{author},
224 'title' => $data->{title},
225 'image_link' => $data->{image_link},
226 'thumb_link' => $data->{thumb_link},
227 'publisher' => $data->{publisher},
228 'pubdate' => $data->{pubdate},
229 'book_link' => $mech->uri(),
230 'content' => $data->{content},
231 'binding' => $data->{binding},
232 'pages' => $data->{pages},
233 'weight' => $data->{weight},
234 'width' => $data->{width},
235 'height' => $data->{height},
236 'depth' => $data->{depth},
237 'description' => $data->{description},
238 1 'html' => $html
239 };
240 1 $self->book($bk);
241 1 $self->found(1);
242 1 return $self->book;
243 }
244  
245 6 q{currently reading: 'Soul Music' by Terry Pratchett};
246  
247 __END__
« AmazonUK_Driver.pm ↑ Summary Amazon_Driver.pm »