blib/lib/WWW/Amazon/Wishlist.pm

filter: Statementstmt 41.1% 104 / 253 Branchbran 14.4% 11 / 76 Conditioncond 13.7% 7 / 51 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 75.0% 15 / 20 Podpod 0.0% 0 / 1 totaltotal 31.9% 137 / 429 CC 65 SCAR 60.8
CC
65
Cov
32%
_extract
941.0
get_list
87.1
_match_priority
12.0
CRAP 435.0
↑ Summary
1
2 package WWW::Amazon::Wishlist;
3
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 our
8 1 $VERSION = 2.019;
9
10 1 ▶use vars qw( @ISA @EXPORT @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 1 ▶use constant COM => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant UK => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
19
20 1 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant DEBUG_HTML => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant DEBUG_NEXT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
23
24 1 require Exporter;
25
26 1 @ISA = qw(Exporter);
27 1 @EXPORT = qw(
28 );
29 1 @EXPORT_OK = qw(
30 get_list
31 UK
32 COM
33 );
34
35 =pod
36
37 =head1 NAME
38
39 WWW::Amazon::Wishlist - grab all the details from your Amazon wishlist
40
41 =head1 SYNOPSIS
42
43 use WWW::Amazon::Wishlist qw(get_list COM UK);
44
45 my @wishlist;
46
47 @wishlist = get_list($my_amazon_com_id); # gets it from amazon.com
48 @wishlist = get_list($my_amazon_com_id, COM); # same, explicitly
49 @wishlist = get_list($my_amazon_couk_id, UK); # gets it from amazon.co.uk
50
51 # Or, if you didn't import the COM and UK constants:
52 @wishlist = get_list ($my_amazon_couk_id, WWW::Amazon::Wishlist::UK);
53
54 # The elements of @wishlist are hashrefs that contain the following elements:
55 foreach my $book (@wishlist)
56 {
57 print $book->{title}, # the, err, title
58 $book->{author}, # and the author(s)
59 $book->{asin}, # the asin number, its unique id on Amazon
60 $book->{price}, # how much it will set you back
61 $book->{quantity}, # how many you said you want
62 $book->{priority}, # how urgently you said you want it (1-5)
63 $book->{type}; # Hardcover/Paperback/CD/DVD etc (not available in the US)
64 } # foreach
65
66 =head1 DESCRIPTION
67
68 Goes to amazon.(com|co.uk), scrapes your wishlist, and returns it
69 in a array of hashrefs so that you can fiddle with it to your heart's
70 content.
71
72 =head1 GETTING YOUR AMAZON ID
73
74 The best way to do this is to search for your own wishlist in the search
75 tools.
76
77 Searching for mine (simon@twoshortplanks.com) on amazon.com takes me to
78 the URL something like
79
80 http://www.amazon.com/exec/obidos/wishlist/2EAJG83WS7YZM/...
81
82 there's some more cruft after that last string of numbers and letters
83 but it's the
84
85 2EAJG83WS7YZM
86
87 bit that's important.
88
89 Doing the same for amazon.co.uk is just as easy.
90
91 Apparently, some people have had problems getting to their wishlist right
92 after it gets set up. You may have to wait a while for it to become
93 browseable.
94
95 =head1 SHOWING YOUR APPRECIATION
96
97 There was a thread on london.pm mailing list about working in a vacuum -
98 that it was a bit depressing to keep writing modules but never get any
99 feedback. So, if you use and like this module then please send me an
100 email and make my day.
101
102 All it takes is a few little bytes.
103
104
105 =head1 BUGS
106
107 B<IMPORTANT>
108
109 C<WWW::Amazon::Wishlist> is a screen scraper and is there for
110 is vulnerable to any changes that Amazon make to their HTML.
111
112 If it starts returning no items then this is very likely the reason
113 and I will get around to fixing it as soon as possible.
114
115 You might want to look at the C<Net::Amazon> module instead.
116
117 It doesn't cope with anything apart from the UK and USA versions of Amazon.
118
119 I don't think it likes unavailable items - trying to work around this
120 breaks UK compatability.
121
122 The code has accumulated lots of cruft.
123
124 Lack of testing. It works for the pages I've tried it for but that's
125 no guarantee.
126
127 =head1 LICENSE
128
129 Copyright (c) 2003 Simon Wistow
130
131 Distributed under the same terms as Perl itself.
132
133 This software is under no warranty and will probably destroy your wish
134 list, kill your friends, burn your house and bring about the apocalypse
135
136 =head1 AUTHOR
137
138 Simon Wistow <simon@thegestalt.org>
139 Currently maintained by Martin Thurn <mthurn@cpan.org>
140
141 =head1 SEE ALSO
142
143 L<perl>, L<LWP::UserAgent>, L<amazonwish>
144
145 =cut
146
147 1 my $USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
148
149 2 ▶sub get_list {
Subroutine get_list: called CC=18 SCAR=44.7
Pod undocumented
150 # Required arg = wishlist ID:
151 2 ▶ my $id = shift || croak "No ID given to get_list() function\n";
Condition50%1/2
shift() || croak("No ID given to get_list() function\n")
l!l
20
MC/DC0%0/1
shift() || croak("No ID given to get_list() function\n")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
152 # Optional arg = whether we're accessing the UK site. Default is "no":
153 2 ▶ my $uk = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
154 # Optional arg = turn on debugging:
155 2 ▶ my $test = shift || DEBUG;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
156 # Note to self ... should we UC the id? Nahhhh. Not yet.
157 # fairly self explanatory
158 2 ▶ my $domain = ($uk) ? "co.uk" : "com";
Branch: 1 branch
BranchTrueFalseTotal
$uk ? : 0 2 2
159 2 my $sBase = qq'http://www.amazon.$domain';
160 # set up some variables
161 2 my $iPage = 1;
162 2 my @items;
163 my $url;
164 # and awaaaaaaaaaaaaay we go ....
165 INFINITE:
166 2 while (1)
167 {
168 2 ▶ $url ||= $uk ? "$sBase/gp/registry/wishlist/ref=cm_wl_search_1?page=$iPage&cid=$id" :
Branch: 1 branch
BranchTrueFalseTotal
$uk ? : 0 2 2
Condition33%1/3
$url ||= $uk ? "$sBase/gp/registry/wishlist/ref=cm_wl_search_1?page=$iPage&cid=$id" : "$sBase/gp/registry/wishlist/$id"
l!l&&r!l&&!r
020
MC/DC0%0/2
$url ||= $uk ? "$sBase/gp/registry/wishlist/ref=cm_wl_search_1?page=$iPage&cid=$id" : "$sBase/gp/registry/wishlist/$id"
$url $uk ? "$sBase/gp/registry/wishlist/ref=cm_wl_search_1?page=$iPage&cid=$id" : "$sBase/gp/registry/wishlist/$id"
Truth table33%1/3
A ||= B
A= $urlB= $uk ? "$sBase/gp/registry/wishlist/ref=cm_wl_search_1?page=$iPage&cid=$id" : "$sBase/gp/registry/wishlist/$id"
ABresult
1X1
011
000
169 "$sBase/gp/registry/wishlist/$id";
170 # This is a typical complete .com URL as of 2008-12:
171 # http://www.amazon.com/gp/registry/wishlist/2O4B95NPM1W3L
172 2 DEBUG_HTML && warn " DDD fetching wishlist for $id, page $iPage...\n";
173 # Don't overwhelm the server:
174 2 ▶ sleep(3) if (1 < $iPage);
Branch: 1 branch
BranchTrueFalseTotal
if 1 < $iPage 0 2 2
175 2 my $content = _fetch_page($url, $domain);
176 2 if (DEBUG_HTML == 88)
177 {
178 warn $content;
179 exit 88;
180 } # if
181 # As of 2009-08, Amazon returns HTML with MISSING BRACKETS:
182 2 $content =~ s/(<tbody\s[^>\r\n]+)(\s+<)/$1>\n$2/g;
183 # There seems to be a bug in HTML::TreeBuilder that causes
184 # abutting tags to be skpped!?!
185 2 $content =~ s!><!> <!g;
186 2 ▶ if (9 < $test)
Branch: 1 branch
BranchTrueFalseTotal
if (9 < $test) 0 2 2
187 {
188 0 eval "use File::Slurp";
189 0 my $sFname = qq'Pages/fetched-$domain.html';
190 0 write_file($sFname, $content);
191 0 warn " DDD wrote HTML to $sFname\n";
192 0 exit 88;
193 } # if
194 2 my $iLen = length($content);
195 # warn " DDD fetched $iLen bytes.\n";
196
197 2 my $result = _extract($uk, $content, $test);
198 # print Dumper($result);
199 # exit 88;
200 2 ▶ if (! defined $result)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 2 2
201 {
202 0 DEBUG && warn " WWW _extract() returned nothing\n";
203 0 last INFINITE;
204 } # if
205 2 ▶ if (! ref $result->{items})
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $result->{"items"}) 2 0 2
206 {
207 # Probably an empty wish list
208 2 DEBUG && warn " WWW _extract() returned no items\n";
209 2 last INFINITE;
210 } # if
211 # Clean up the parsed items and add them to our local @items
212 # array:
213 ITEM:
214 0 foreach my $item (@{$result->{items}})
215 {
216 0 $item->{'author'} =~ s!\n!!g;
217 0 $item->{'author'} =~ s!^\s*by\s+!!g;
218 0 $item->{'author'} =~ s!</span></b><br />\n*!!s;
219 0 ▶ $item->{'quantity'} = $1 if ($item->{'priority'} =~ m!Desired:\s*</b>\s*(\d+)!i);
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"priority"} =~ m[Desired:\s*</b>\s*(\d+)]i 0 0 0
220 0 ▶ $item->{'priority'} = $1 if ($item->{'priority'} =~ m!Priority:\s*</b>\s*(\d)!i);
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"priority"} =~ m[Priority:\s*</b>\s*(\d)]i 0 0 0
221 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($uk and $item->{"image"} and not $item->{"image"} =~ /^http:/) 0 0 0
Condition0%0/6
$uk and $item->{"image"} and not $item->{"image"} =~ /^http:/
!ll&&!rl&&r
000
$uk and $item->{"image"}
!ll&&!rl&&r
000
MC/DC0%0/3
$uk and $item->{"image"} and not $item->{"image"} =~ /^http:/
$uk $item->{"image"} not $item->{"image"} =~ /^http:/
Truth table0%0/4
A and B and not(C)
A= $ukB= $item->{"image"}C= not $item->{"image"} =~ /^http:/
ABCresult
0XX0
10X0
1100
1111
222 $uk
223 &&
224 $item->{image}
225 &&
226 ($item->{image} !~ m!^http:!)
227 )
228 {
229 0 $item->{image} = q"http://images-eu.amazon.com/images/P/". $item->{image};
230 } # if
231 0 push @items, $item;
232 0 DEBUG_HTML && warn " DDD added one item to \@items\n";
233 } # foreach ITEM
234 # Assumes an absolute path without hostname:
235 0 ▶ if ( ! defined $result->{next})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result->{"next"}) 0 0 0
236 {
237 0 DEBUG_NEXT && warn " WWW did not find next url\n";
238 0 DEBUG_NEXT && write_file(qq'Pages/no-next.html', $content);
239 0 last INFINITE;
240 } # if
241 0 $url = $sBase . $result->{next};
242 0 $iPage++;
243 } # while INFINITE
244 2 return @items;
245 } # get_list
246
247 2 ▶sub _fetch_page {
Subroutine _fetch_page: called CC=5 SCAR=18.3
248 2 my ($url, $domain) = @_;
249 2 if (0)
250 {
251 eval "use File::Slurp";
252 # For debugging UK site:
253 return read_file('Pages/uk-2008-12-page1.html');
254 # For debugging USA site:
255 return read_file('Pages/2008-12.html');
256 } # if 0
257 # Set up the UA:
258 2 my $ua = new LWP::UserAgent(
259 keep_alive => 1,
260 timeout => 30,
261 agent => $USER_AGENT,
262 );
263 # Setting it in the 'new' seems not to work sometimes
264 2 $ua->agent($USER_AGENT);
265 # For some reason, this makes stuff work:
266 # $ua->max_redirect( 0 );
267 # Make a full set of headers:
268 2 my $h = new HTTP::Headers(
269 'Host' => "www.amazon.$domain",
270 'Referer' => $url,
271 'User-Agent' => $USER_AGENT,
272 'Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1',
273 'Accept-Language' => 'en-us,en;q=0.5',
274 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
275 #'Accept-Encoding' => 'gzip,deflate',
276 'Keep-Alive' => '300',
277 'Connection' => 'keep-alive',
278 );
279 2 $h->referer("$url");
280 2 my $request = HTTP::Request->new ( 'GET', $url, $h );
281 2 my $response;
282 2 my $times = 0;
283 # LWP should be able to do this but seemingly fails sometimes
284 2 ▶ while ($times++<3)
Branch: 1 branch
BranchTrueFalseTotal
if ($times++ < 3) 2 0 2
285 {
286 2 $response = $ua->request($request);
287 2 ▶ last if $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_success 2 0 2
288 0 ▶ if ($response->is_redirect)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_redirect) 0 0 0
289 {
290 0 $url = $response->header("Location");
291 #$h->header("Referer", $url);
292 0 $h->referer("$url");
293 0 $request = HTTP::Request->new ( 'GET', $url, $h );
294 } # if
295 } # while
296 2 ▶ if (!$response->is_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 2 2
297 {
298 0 croak "Failed to retrieve $url";
299 0 return undef;
300 } # if
301 2 my $s = $response->content;
302 # Clean the CRAP off the page:
303 2 $s =~ s!<script>.+?</script>!!gs;
304 2 return $s;
305 } # _fetch_page
306
307 # This is the HTML parsing version written by Martin Thurn:
308
309 2 ▶sub _extract {
Subroutine _extract: called CC=37 SCAR=68.5
310 # Required arg1 = whether we are parsing the UK site or not (Boolean):
311 2 ▶ my $iUK = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
312 # Required arg2 = the HTML contents of the webpage:
313 2 ▶ my $s = shift || '';
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
314 # Optional arg = debugging level:
315 2 ▶ my $iDebug = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
316 2 DEBUG_HTML && warn " DDD start _extract()\n";
317 2 my $rh = {};
318 2 my $oTree = new HTML::TreeBuilder;
319 2 $oTree->parse($s);
320 2 $oTree->eof;
321 2 my $sTag = q/div/;
322 2 my $sClass = q/a-fixed-left-grid a-spacing-none/;
323 2 ▶ $sClass = q/a-text-left a-fixed-left-grid-col a-col-right/ if $iUK;
Branch: 1 branch
BranchTrueFalseTotal
if $iUK 0 2 2
324 # $sClass = q/a-fixed-left-grid a-spacing-large/ if $iUK;
325
326 2 my @aoSPAN = $oTree->look_down(_tag => $sTag,
327 class => $sClass,
328 );
329 2 my $iCountSPAN = scalar(@aoSPAN);
330 2 DEBUG_HTML && warn " DDD _extract() found $iCountSPAN $sTag tags of class '$sClass'\n";
331 SPAN_TAG:
332 2 foreach my $oSPAN (@aoSPAN)
333 {
334 0 ▶ next SPAN_TAG unless ref $oSPAN;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $oSPAN 0 0 0
335 0 DEBUG_HTML && warn " DDD _extract() found toplevel item tagset\n";
336 0 if (9 < DEBUG_HTML)
337 {
338 my $s = $oSPAN->as_HTML;
339 warn " DDD ==$s==\n";
340 } # if
341 0 my $sASIN = q{};
342 0 my $sName = q{};
343 0 my $sTitle = q{};
344 0 my @aoA = $oSPAN->look_down(_tag => 'a');
345 0 DEBUG_HTML && warn sprintf(" DDD _extract(): oSPAN contains %d <A> tags\n", scalar(@aoA));
346 A_TAG:
347 0 foreach my $oA (@aoA)
348 {
349 0 ▶ next A_TAG if ! ref $oA;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $oA 0 0 0
350 0 my $sA = $oA->as_HTML;
351 0 DEBUG_HTML && warn " DDD _extract(): try A\n";
352 0 if (9 < DEBUG_HTML)
353 {
354 warn " DDD ==$sA==\n";
355 } # if
356 0 ▶ $sTitle = $oA->attr('title') || $oA->as_text;
Condition0%0/3
$oA->attr('title') || $oA->as_text
l!l&&r!l&&!r
000
MC/DC0%0/2
$oA->attr('title') || $oA->as_text
$oA->attr('title') $oA->as_text
Truth table0%0/3
A || B
A= $oA->attr('title')B= $oA->as_text
ABresult
1X1
011
000
357 # Strip leading whitespace:
358 0 $sTitle =~ s!\A\s+!!;
359 # Strip trailing whitespace:
360 0 $sTitle =~ s!\s+\Z!!;
361 # Ignore empty (image-only) tags:
362 0 ▶ next A_TAG if ($sTitle !~ m/\S/);
Branch: 1 branch
BranchTrueFalseTotal
unless $sTitle =~ /\S/ 0 0 0
363 # Strip out zero-width spaces scattered about randomly in item titles
364 0 $sTitle =~ s/\x{200b}//g;
365 0 DEBUG_HTML && warn " DDD _extract(): found item named '$sTitle'\n";
366 0 ▶ next A_TAG if ($sTitle eq 'Universal Wish List Button');
Branch: 1 branch
BranchTrueFalseTotal
if $sTitle eq "Universal Wish List Button" 0 0 0
367 0 ▶ next A_TAG if ($sTitle eq 'Buying this gift elsewhere?');
Branch: 1 branch
BranchTrueFalseTotal
if $sTitle eq "Buying this gift elsewhere?" 0 0 0
368 0 my $sURL = $oA->attr('href');
369 0 DEBUG_HTML && warn " DDD _extract(): URL ==$sURL==\n";
370 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($sURL =~ m[/detail(?:/offer-listing)?/-/(.+?)/ref] or $sURL =~ m[/gp/product/(.+?)/ref] or $sURL =~ m[/dp/(.+?)/(_encoding|ref|\?col)]) { } 0 0 0
Condition0%0/6
$sURL =~ m[/detail(?:/offer-listing)?/-/(.+?)/ref] or $sURL =~ m[/gp/product/(.+?)/ref] or $sURL =~ m[/dp/(.+?)/(_encoding|ref|\?col)]
l!l&&r!l&&!r
000
$sURL =~ m[/detail(?:/offer-listing)?/-/(.+?)/ref] or $sURL =~ m[/gp/product/(.+?)/ref]
l!l&&r!l&&!r
000
MC/DC0%0/3
$sURL =~ m[/detail(?:/offer-listing)?/-/(.+?)/ref] or $sURL =~ m[/gp/product/(.+?)/ref] or $sURL =~ m[/dp/(.+?)/(_encoding|ref|\?col)]
$sURL =~ m[/detail(?:/offer-listing)?/-/(.+?)/ref] $sURL =~ m[/gp/product/(.+?)/ref] $sURL =~ m[/dp/(.+?)/(_encoding|ref|\?col)]
Truth table0%0/4
A or B or C
A= $sURL =~ m[/detail(?:/offer-listing)?/-/(.+?)/ref]B= $sURL =~ m[/gp/product/(.+?)/ref]C= $sURL =~ m[/dp/(.+?)/(_encoding|ref|\?col)]
ABCresult
1XX1
01X1
0011
0000
371 ($sURL =~ m!/detail(?:/offer-listing)?/-/(.+?)/ref!)
372 ||
373 ($sURL =~ m!/gp/product/(.+?)/ref!)
374 ||
375 ($sURL =~ m!/dp/(.+?)/(_encoding|ref|\?col)!)
376 )
377 {
378 # It's a match!
379 0 $sASIN = $1;
380 0 last A_TAG;
381 } # if
382 else
383 {
384 0 DEBUG_HTML && warn " EEE url does not contain asin\n";
385 }
386 } # foreach A_TAG
387 0 DEBUG_HTML && warn " DDD _extract(): ASIN ==$sASIN==\n";
388 0 ▶ if ($sASIN eq q{})
Branch: 1 branch
BranchTrueFalseTotal
if ($sASIN eq "") 0 0 0
389 {
390 0 next SPAN_TAG;
391 } # if
392 # Grab the smallest-containing ancestor of this item:
393 0 ▶ my $oParent = $iUK
Branch: 1 branch
BranchTrueFalseTotal
$iUK ? : 0 0 0
394 ? $oSPAN->look_up(_tag => 'tbody',
395 class => 'itemWrapper',
396 )
397 : $oSPAN;
398 0 $oParent = $oSPAN;
399 0 ▶ if (! ref $oParent)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $oParent) 0 0 0
400 {
401 0 DEBUG_HTML && warn " WWW did not find ancestor TBODY\n";
402 0 next SPAN_TAG;
403 } # if
404 0 my $sParentHTML = $oParent->as_HTML;
405 0 DEBUG_HTML && warn " DDD _extract(): parent HTML ==$sParentHTML==\n";
406 0 my $sParent = $oParent->as_text;
407 # Manual text clean-up:
408 0 $sParent =~ s/(DESIRED|RECEIVED|PRIORITY)/; $1: /g;
409 0 DEBUG_HTML && warn " DDD _extract(): parent text ==$sParent==\n";
410 0 my $iDesired = _match_desired($sParent);
411 0 DEBUG_HTML && warn " DDD _extract(): desired set to =$iDesired=\n";
412 0 my $sPriority = _match_priority($sParent);
413 0 DEBUG_HTML && warn " DDD _extract(): priority set to =$sPriority=\n";
414 0 my @aoTDtiny = $oParent->look_down(_tag => 'td',
415 class => 'tiny',
416 );
417 QUANT_TAG:
418 0 foreach my $oSPAN (@aoTDtiny)
419 {
420 0 ▶ next QUANT_TAG unless ref $oSPAN;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $oSPAN 0 0 0
421 0 my $sSpan = $oSPAN->as_text;
422 0 DEBUG_HTML && warn " DDD _extract(): TDtiny=$sSpan=\n";
423 0 ▶ $sPriority ||= _match_priority($sSpan);
Condition0%0/3
$sPriority ||= _match_priority($sSpan)
l!l&&r!l&&!r
000
MC/DC0%0/2
$sPriority ||= _match_priority($sSpan)
$sPriority _match_priority($sSpan)
Truth table0%0/3
A ||= B
A= $sPriorityB= _match_priority($sSpan)
ABresult
1X1
011
000
424 0 DEBUG_HTML && warn " DDD _extract(): priority set to =$sPriority=\n";
425 0 ▶ $iDesired ||= _match_desired($sSpan);
Condition0%0/3
$iDesired ||= _match_desired($sSpan)
l!l&&r!l&&!r
000
MC/DC0%0/2
$iDesired ||= _match_desired($sSpan)
$iDesired _match_desired($sSpan)
Truth table0%0/3
A ||= B
A= $iDesiredB= _match_desired($sSpan)
ABresult
1X1
011
000
426 0 DEBUG_HTML && warn " DDD _extract(): desired set to =$iDesired=\n";
427 } # foreach QUANT_TAG
428 0 ▶ if (! $iDesired || ! $sPriority)
Branch: 1 branch
BranchTrueFalseTotal
unless ($iDesired and $sPriority) 0 0 0
Condition0%0/3
$iDesired and $sPriority
!ll&&!rl&&r
000
MC/DC0%0/2
$iDesired and $sPriority
$iDesired $sPriority
Truth table0%0/3
A and B
A= $iDesiredB= $sPriority
ABresult
0X0
100
111
429 {
430 # See if they are encoded in a FORM:
431 # Find the priority:
432 0 ▶ if ($sParentHTML =~ m!<option selected="yes" value=([-0-9]+)>!)
Branch: 1 branch
BranchTrueFalseTotal
if ($sParentHTML =~ /<option selected="yes" value=([-0-9]+)>/) { } 0 0 0
433 {
434 0 $sPriority = $1;
435 0 DEBUG_HTML && warn " DDD _extract(): priority set to =$sPriority=\n";
436 } # if
437 else
438 {
439 0 DEBUG_HTML && warn " WWW did not find <option> for priority\n";
440 }
441 # Find the quantity desired:
442 0 ▶ if ($sParentHTML =~ m!<input class="tiny" name="requestedQty.+?" size=\d+ type="text" value=(\d+)>!)
Branch: 1 branch
BranchTrueFalseTotal
if ($sParentHTML =~ /<input class="tiny" name="requestedQty.+?" size=\d+ type="text" value=(\d+)>/) { } 0 0 0
443 {
444 0 $iDesired = $1;
445 0 DEBUG_HTML && warn " DDD _extract(): desired set to =$iDesired=\n";
446 } # if
447 else
448 {
449 0 DEBUG_HTML && warn " WWW did not find <input> for desired-quantity\n";
450 }
451 } # if
452 # Put in default values if we never found them:
453 0 ▶ $sPriority ||= 'medium';
Condition0%0/2
$sPriority ||= "medium"
l!l
00
MC/DC0%0/1
$sPriority ||= "medium"
$sPriority
Truth table0%0/2
A
A= $sPriority
Aresult
11
00
454 0 DEBUG_HTML && warn " DDD _extract(): priority set to =$sPriority=\n";
455 0 ▶ $iDesired ||= 1;
Condition0%0/2
$iDesired ||= 1
l!l
00
MC/DC0%0/1
$iDesired ||= 1
$iDesired
Truth table0%0/2
A
A= $iDesired
Aresult
11
00
456 # Find the date added:
457 0 my $sDate = '';
458 0 ▶ if ($sParentHTML =~ m!>added\s+(.+?)<!)
Branch: 1 branch
BranchTrueFalseTotal
if ($sParentHTML =~ />added\s+(.+?)</) { } 0 0 0
459 {
460 0 $sDate = $1;
461 0 DEBUG_HTML && warn " DDD _extract(): date=$sDate=\n";
462 } # if
463 else
464 {
465 0 DEBUG_HTML && warn " WWW did not find text for date-added\n";
466 }
467
468 # Find the "author" of this item:
469 0 my @aoTDauthor;
470 0 ▶ if ($iUK)
Branch: 1 branch
BranchTrueFalseTotal
if ($iUK) { } 0 0 0
471 {
472 0 @aoTDauthor = $oParent->look_down(_tag => 'td',
473 class => 'small',
474 );
475 }
476 else
477 {
478 @aoTDauthor = $oParent->look_down(_tag => 'span',
479 sub
480 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
481 0 my $sHtml = $_[0]->as_HTML;
482 # DEBUG_HTML && warn " DDD _extract(): try oTDauthor span==$sHtml==\n";
483 0 ▶ my $s = $_[0]->attr('class') || q{};
Condition0%0/2
$_[0]->attr('class') || ''
l!l
00
MC/DC0%0/1
$_[0]->attr('class') || ''
$_[0]->attr('class')
Truth table0%0/2
A
A= $_[0]->attr('class')
Aresult
11
00
484 0 $s =~ m'BYLINE'i;
485 },
486 0 );
487 } # else
488 0 my $sAuthor = '';
489 AUTHOR_TAG:
490 0 foreach my $oTD (@aoTDauthor)
491 {
492 0 ▶ next AUTHOR_TAG unless ref $oTD;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $oTD 0 0 0
493 0 my $s = $oTD->as_HTML;
494 0 DEBUG_HTML && warn " DDD _extract(): try oTDauthor==$s==\n";
495 0 $s = $oTD->as_text;
496 0 ▶ if ($s =~ s!\A\s*(by|~)\s+!!)
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/\A\s*(by|~)\s+//) 0 0 0
497 {
498 0 $sAuthor = $s;
499 0 last AUTHOR_TAG;
500 } # if
501 } # foreach AUTHOR_TAG
502 0 DEBUG_HTML && warn " DDD _extract(): author=$sAuthor=\n";
503 # Find the price of this item:
504 0 my $sPrice = '';
505 my $oTDprice = $oParent->look_down(_tag => 'span',
506 sub
507 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
508 0 ▶ my $s = $_[0]->attr('class') || q{};
Condition0%0/2
$_[0]->attr('class') || ''
l!l
00
MC/DC0%0/1
$_[0]->attr('class') || ''
$_[0]->attr('class')
Truth table0%0/2
A
A= $_[0]->attr('class')
Aresult
11
00
509 0 $s =~ m'PRICE'i;
510 },
511 0 );
512 0 ▶ if (! ref $oTDprice)
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $oTDprice) { } 0 0 0
513 {
514 0 DEBUG_HTML && warn " WWW did not find TD for price\n";
515 # warn $oParent->as_HTML;
516 # exit 88;
517 # next SPAN_TAG;
518 } # if
519 else
520 {
521 0 $sPrice = $oTDprice->as_text;
522 0 ▶ if ($sPrice =~ m!Price:\s+(.+)\Z!)
Branch: 1 branch
BranchTrueFalseTotal
if ($sPrice =~ /Price:\s+(.+)\Z/) 0 0 0
523 {
524 0 $sPrice = $1;
525 } # if
526 0 $sPrice =~ s!\A\s+!!;
527 0 $sPrice =~ s!\s+\Z!!;
528 0 DEBUG_HTML && warn " DDD _extract(): price=$sPrice=\n";
529 } # else
530 # Add this item to the result set:
531 0 my %hsItem = (
532 asin => $sASIN,
533 author => $sAuthor,
534 # image => $sImageURL,
535 price => $sPrice,
536 priority => $sPriority,
537 quantity => $iDesired,
538 title => $sTitle,
539 # type => $sType,
540 );
541 0 DEBUG_HTML && warn Dumper(\%hsItem);
542 # warn " DDD _extract() added one item to \$rh->{items}\n";
543 0 push @{$rh->{items}}, \%hsItem;
544 # All done with this item:
545 0 $oParent->detach;
546 0 $oParent->delete;
547 } # foreach SPAN_TAG
548 # Look for the next-page link:
549 my @aoA = $oTree->look_down(_tag => 'a',
550 role => 'link',
551 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
552 0 ▶ return 0 if (length($_[0]->attr('href')) < 55);
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0]->attr("href") < 55 0 0 0
553 # my $s = $_[0]->as_text || q{};
554 # DEBUG_NEXT && warn " DDD _extract(): try next <A> ==$s==\n";
555 # $s =~ m/\A\s*(NEXT|SEE\s+MORE)\s*\z/i;
556 0 my $s = $_[0]->attr('class');
557 0 DEBUG_NEXT && warn " DDD _extract(): try next <A> ==$s==\n";
558 0 $s =~ m/wl-see-more/
559 },
560 2 );
561 2 my $iCountA = scalar(@aoA);
562 2 DEBUG_NEXT && warn " DDD _extract(): found $iCountA <A> tags that match 'next'\n";
563 2 my $oA = shift @aoA;
564 2 ▶ if (ref $oA)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $oA) { } 0 2 2
565 {
566 0 $rh->{next} = $oA->attr('href');
567 0 DEBUG_NEXT && warn " DDD _extract(): raw next URL is ==$rh->{next}==\n";
568 } # if
569 else
570 {
571 2 DEBUG_NEXT && warn " DDD _extract(): did not find next URL\n";
572 }
573 2 return $rh;
574 } # _extract
575
576 0 ▶sub _match_priority {
Subroutine _match_priority: not called CC=3 SCAR=24.8
577 0 ▶ my $s = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
578 0 ▶ if ($s =~ m'PRIORITY:?\s*(\w+?)(\s|\z)'i)
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /PRIORITY:?\s*(\w+?)(\s|\z)/i) 0 0 0
579 {
580 0 return lc $1;
581 } # if
582 0 return;
583 } # _match_priority
584
585 0 ▶sub _match_desired {
Subroutine _match_desired: not called CC=3 SCAR=24.8
586 0 ▶ my $s = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
587 0 ▶ if ($s =~ m'(?:DESIRED|WANTS):?\s*(\d+)'i)
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /(?:DESIRED|WANTS):?\s*(\d+)/i) 0 0 0
588 {
589 0 return lc $1;
590 } # if
591 0 return;
592 } # _match_desired
593
594 1 1;
595
596 __END__
↑ Summary