AmazonUK_Driver.pm

filter: Statementstmt 73.7% 73 / 99 Branchbran 61.3% 27 / 44 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 57.7% 108 / 187 SCAR 46.9
CC
34
Cov
60%
search
182.0
_parse
24.6
BEGIN
1.0
CRAP 108.7
« AmazonUS_Driver.pm ↑ Summary Amazon_Driver.pm »
1 package WWW::Scraper::ISBN::AmazonUK_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);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 $VERSION = '0.40';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::AmazonUK_Driver - Search driver for Amazon.co.uk
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 (UK) Amazon 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 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 ###########################################################################
39 # Variables
40  
41 5 my $AMA_SEARCH = 'http://www.amazon.co.uk/s/ref=nb_sb_noss?url=search-alias%3Daps&x=18&y=16&field-keywords=';
42 5 my $AMA_URL = 'http://www.amazon.co.uk/[^/]+/dp/[\dX]+/ref=sr_1_1/';
43 5 my $IN2MM = 0.0393700787; # number of inches in a millimetre (mm)
44 5 my $LB2G = 0.00220462; # number of pounds (lbs) in a gram
45 5 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 (UK) 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 (UK) 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 = $AMA_SEARCH . $ean;
105  
106 0 eval { $mech->get( $search ) };
107 0 ▶ return $self->handler("Amazon UK 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 #print STDERR "\n# content=[$content]\n";
112 0 my ($link) = $content =~ m!($AMA_URL)!s;
113 0 ▶ return $self->handler("Failed to find that book on Amazon UK website.")
Branch: 1 branch
BranchTrueFalseTotal
unless $link 0 0 0
114 unless($link);
115  
116 0 eval { $mech->get( $link ) };
117 0 ▶ return $self->handler("Amazon UK 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
118 if($@ || !$mech->success() || !$mech->content());
119  
120 0 return $self->_parse($mech);
121 }
122  
123 sub _parse {
124 2 ▶ my $self = shift;
Subroutine _parse: called CC=22 SCAR=32.0
125 2 my $mech = shift;
126  
127 # The Book page
128 2 my $html = $mech->content;
129 2 my $data = {};
130  
131 #print STDERR "\n# html=[$html]\n";
132  
133 2 my @size = $html =~ m!<li><b>\s*Product Dimensions:\s*</b>\s*([\d.]+) x ([\d.]+) x ([\d.]+) (cm)\s*</li>!si;
134 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 1 1 2
135 2 ▶ if(@size) {
Branch: 1 branch
BranchTrueFalseTotal
if (@size) 1 1 2
136 1 my $type = pop @size;
137 1 ($data->{depth},$data->{width},$data->{height}) = sort @size;
138 1 ▶ if($type eq 'cm') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'cm') { } 1 0 1
elsif ($type eq 'inches') { } 0 0 0
139 1 $data->{$_} = int($data->{$_} * 10) for(qw( height width depth ));
140 } elsif($type eq 'inches') {
141 0 $data->{$_} = int($data->{$_} / $IN2MM) for(qw( height width depth ));
142 }
143 }
144  
145 2 ($data->{binding},$data->{pages}) = $html =~ m!<li><b>(Paperback|Hardcover):</b>\s*([\d.]+)\s*pages</li>!si;
146 2 ($data->{weight}) = $html =~ m!<li><b>Shipping Weight:</b>\s*([\d.]+)\s*ounces</li>!si;
147 2 ($data->{published}) = $html =~ m!<li><b>Publisher:</b>\s*(.*?)</li>!si;
148 2 ($data->{isbn10}) = $html =~ m!<li><b>ISBN-10:</b>\s*(.*?)</li>!si;
149 2 ($data->{isbn13}) = $html =~ m!<li><b>ISBN-13:</b>\s*(.*?)</li>!si;
150 2 ($data->{content}) = $html =~ m!<meta name="description" content="([^"]+)"!si;
151 2 ($data->{description}) = $html =~ m!From the Back Cover</h3>\s*<div class="productDescriptionWrapper"\s*>\s*<P>(.*?)<div!si;
152 2 ▶ ($data->{description}) = $html =~ m!<div id="bookDescription_feature_div"[^>]*>.*?<noscript>(.*?)</noscript!si unless($data->{description});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"description"} 1 1 2
153  
154 2 ▶ $data->{weight} = int($data->{weight} / $OZ2G) if($data->{weight});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"weight"} 1 1 2
155  
156 2 ▶ if($data->{description}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"description"}) 1 1 2
157 1 $data->{description} =~ s!<[^>]+>!!g;
158 1 $data->{description} =~ s! +! !g;
159 }
160  
161 # The images
162 2 my ($json) = $html =~ /var colorImages = ([^;]+);/si;
163 2 ▶ if($json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json) { } 0 2 2
164 0 my $code = decode_json($json);
165 0 my @order = grep {$_} $code->{initial}[0]{thumb}, $code->{initial}[0]{landing}, @{$code->{initial}[0]{main}}, $code->{initial}[0]{large};
166 0 ▶ $data->{thumb_link} = $order[0] if(@order);
Branch: 1 branch
BranchTrueFalseTotal
if @order 0 0 0
167 0 ▶ $data->{image_link} = $order[-1] if(@order);
Branch: 1 branch
BranchTrueFalseTotal
if @order 0 0 0
168  
169 #use Data::Dumper;
170 #print STDERR "\n# code=[".Dumper($code)."]\n";
171 } else {
172 2 my ($code) = $html =~ /'imageGalleryData'\s*:\s*([^;]+);/si;
173 2 ▶ if($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 1 1 2
174 1 ($data->{thumb_link}) = $code =~ /"thumbUrl":\s*"([^+]+)"/;
175 1 ($data->{image_link}) = $code =~ /"mainUrl":\s*"([^+]+)"/;
176 }
177 #use Data::Dumper;
178 #print STDERR "\n# code=[".Dumper($code)."]\n";
179 }
180  
181  
182 # {\"initial\":[{\"large\":\"http://ecx.images-amazon.com/images/I/31cLTIXHKgL.jpg\",\"landing\":[\"http://ecx.images-amazon.com/images/I/31cLTIXHKgL._SY300_.jpg\"],\"thumb\":\"http://ecx.images-amazon.com/images/I/31cLTIXHKgL._SS40_.jpg\",\"main\":[\"http://ecx.images-amazon.com/images/I/31cLTIXHKgL._SX342_.jpg\",\"http://ecx.images-amazon.com/images/I/31cLTIXHKgL._SX385_.jpg\"]}]};
183  
184 2 ▶ if($data->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"content"}) 1 1 2
185 1 $data->{content} =~ s/Amazon\.co\.uk.*?://i;
186 1 $data->{content} =~ s/: Books.*//i;
187 1 ($data->{title},$data->{author}) = split(/\s+by\s+/,$data->{content});
188 1 $data->{title} =~ s/^Buy\s+//;
189 1 $data->{author} =~ s/\s*\(.*//;
190 }
191  
192 2 ▶ ($data->{publisher},$data->{pubdate}) = ($data->{published} =~ /\s*(.*?)(?:;.*?)?\s+\((.*?)\)/) if($data->{published});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"published"} 1 1 2
193 2 ▶ $data->{isbn10} =~ s/[^\dX]+//g if($data->{isbn10});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"isbn10"} 1 1 2
194 2 ▶ $data->{isbn13} =~ s/\D+//g if($data->{isbn13});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"isbn13"} 1 1 2
195 2 ▶ $data->{pubdate} =~ s/^.*?\(// if($data->{pubdate});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"pubdate"} 1 1 2
196  
197 return $self->handler("Could not extract data from Amazon UK result page.")
198 2 ▶ unless(defined $data->{isbn13});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"isbn13"} 1 1 2
199  
200 # trim top and tail
201 1 ▶ foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 17 17
202  
203 #use Data::Dumper;
204 #print STDERR "\n# data=[".Dumper($data)."]\n";
205  
206 my $bk = {
207 'ean13' => $data->{isbn13},
208 'isbn13' => $data->{isbn13},
209 'isbn10' => $data->{isbn10},
210 'isbn' => $data->{isbn13},
211 'author' => $data->{author},
212 'title' => $data->{title},
213 'image_link' => $data->{image_link},
214 'thumb_link' => $data->{thumb_link},
215 'publisher' => $data->{publisher},
216 'pubdate' => $data->{pubdate},
217 'book_link' => $mech->uri(),
218 'content' => $data->{content},
219 'binding' => $data->{binding},
220 'pages' => $data->{pages},
221 'weight' => $data->{weight},
222 'width' => $data->{width},
223 'height' => $data->{height},
224 'depth' => $data->{depth},
225 'description' => $data->{description},
226 1 'html' => $html
227 };
228 1 $self->book($bk);
229 1 $self->found(1);
230 1 return $self->book;
231 }
232  
233 5 q{currently reading: 'Torn Apart: The Life of Ian Curtis' by Mick Middles and Lindsay Reade};
234  
235 __END__
« AmazonUS_Driver.pm ↑ Summary Amazon_Driver.pm »