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

filter: Statementstmt 35.1% 26 / 74 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 20.1% 35 / 174 CC 29 SCAR 65.3
CC
29
Cov
18%
search
870.0
BEGIN
1.0
BEGIN
1.0
CRAP 682.1
↑ Summary
1 package WWW::Scraper::ISBN::Yahoo_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.23';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::Yahoo_Driver - Search driver for Yahoo Books online 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 Yahoo Books 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::Scraper::ISBN::Driver;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 ###########################################################################
39 # Constants
40  
41 5 ▶use constant YAHOO => 'http://shopping.yahoo.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use constant SEARCH => 'https://shopping.yahoo.com/search?fr=yshoppingheader_test2&type=2button&did=0&p=';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 #--------------------------------------------------------------------------
45  
46 ###########################################################################
47 # Public Interface
48  
49 =head1 METHODS
50  
51 =over 4
52  
53 =item C<search()>
54  
55 Creates a query string, then passes the appropriate form fields to the
56 Yahoo Books server.
57  
58 The returned page should be the correct catalog page for that ISBN. If not the
59 function returns zero and allows the next driver in the chain to have a go. If
60 a valid page is returned, the following fields are returned via the book hash:
61  
62 isbn (now returns isbn13)
63 isbn10 (no longer provided by Yahoo on page)
64 isbn13
65 ean13 (industry name)
66 title
67 author
68 pubdate (no longer provided by Yahoo on page)
69 publisher
70 book_link
71 image_link
72 thumb_link (same as image_link)
73 description
74 binding (if known)
75 pages (no longer provided by Yahoo on page)
76 weight (no longer provided by Yahoo on page)
77 width (no longer provided by Yahoo on page)
78 height (no longer provided by Yahoo on page)
79  
80 The book_link and image_link refer back to the Yahoo Books website.
81  
82 =back
83  
84 =cut
85  
86 0 ▶sub search {
Subroutine search: not called CC=29 SCAR=67.7
Pod documented
87 0 my $self = shift;
88 0 my $isbn = shift;
89 0 $self->found(0);
90 0 $self->book(undef);
91 0 my $data = {};
92  
93 # validate and convert into EAN13 format
94 0 my $ean = $self->convert_to_ean13($isbn);
95 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
96 if(!$ean || (length $isbn == 13 && $isbn ne $ean)
97 || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
98 0 $isbn = $ean;
99  
100 0 my $mech = WWW::Mechanize->new;
101 0 $mech->agent_alias( 'Linux Mozilla' );
102  
103 0 eval { $mech->get( YAHOO . '/books' ) };
104 0 ▶ return $self->handler("Yahoo! book 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
105 if($@ || !$mech->success() || !$mech->content());
106  
107 0 eval { $mech->get( SEARCH . $isbn ) };
108 0 ▶ return $self->handler("Yahoo! book search 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
109 if($@ || !$mech->success() || !$mech->content());
110  
111  
112 # The Results page
113 0 my $content = $mech->content();
114 #print STDERR "\n# results=[\n$content\n]\n";
115  
116 0 my ($link,$thumb) = $content =~ m!<div class="img"><a href="([^"]+)"[^>]+><img src="([^"]+)"!is;
117 #print STDERR "\n# link=[$link]\n# thumb=[$thumb]\n";
118  
119 0 ▶ return $self->handler("Failed to find that book on Yahoo! book website.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $link 0 0 0
120 unless(defined $link);
121  
122 0 $data->{book_link} = $link;
123  
124 0 eval { $mech->get( $data->{book_link} ) };
125 0 ▶ return $self->handler("Yahoo! book search 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
126 if($@ || !$mech->success() || !$mech->content());
127  
128  
129 # The Book page
130 0 my $html = $mech->content();
131 #print STDERR "\n# page=[\n$html\n]\n";
132  
133 0 ▶ return $self->handler("Could not extract data from Yahoo! result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless $html =~ m[</body>\s*</html>] 0 0 0
134 unless($html =~ m!</body>\s*</html>!);
135  
136 0 ($data->{image_link}) = $html =~ m!<meta property="og:image" content="([^"]+)"/>!is;
137 0 ▶ $data->{image_link} ||= $thumb;
Condition0%0/3
$data->{"image_link"} ||= $thumb
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"image_link"} ||= $thumb
$data->{"image_link"} $thumb
Truth table0%0/3
A ||= B
A= $data->{"image_link"}B= $thumb
ABresult
1X1
011
000
138  
139 0 ($data->{title}) = $html =~ m!<span id="productTitle"[^>]*>([^<]*)</span>!is;
140 0 ▶ ($data->{title}) = $html =~ m!<meta property="og:title" content="([^"]+)"/>!is unless($data->{title});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"title"} 0 0 0
141  
142 0 ($data->{description}) = $html =~ m!<div class="exp-hide"><span itemprop="description">(.*?)</span></div>!is;
143 0 ▶ ($data->{description}) = $html =~ m!<meta property="og:description" content="([^"]+)"/>!is unless($data->{description});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"description"} 0 0 0
144  
145 0 ($data->{publisher}) = $html =~ m!<td class="label"><em>Publisher</em></td><td>([^<]+)</td>!is;
146 0 ($data->{binding}) = $html =~ m!<td class="label"><em>Book Format</em></td><td>([^<]+)</td>!is;
147 0 ($data->{author}) = $html =~ m!<td class="label"><em>Author</em></td><td>([^<]+)</td>!is;
148  
149 ($data->{publisher},$data->{pubdate})
150 0 ▶ = $data->{description} =~ m!This book is written by (?:[\s\w]+) Published by ([\s\w]+) In (\d+) and!is if(!$data->{publisher} && $data->{description});
Branch: 1 branch
BranchTrueFalseTotal
if not $data->{"publisher"} and $data->{"description"} 0 0 0
Condition0%0/3
not $data->{"publisher"} and $data->{"description"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $data->{"publisher"} and $data->{"description"}
not $data->{"publisher"} $data->{"description"}
Truth table0%0/3
not(A) and B
A= not $data->{"publisher"}B= $data->{"description"}
ABresult
0X0
100
111
151 ($data->{binding}) = $data->{description} =~ m!This book is written by (?:[\s\w]+) Published by (?:[\s\w]+) In (?:\d+) and is available in ([\w]+)!is
152 0 ▶ if(!$data->{binding} && $data->{description});
Branch: 1 branch
BranchTrueFalseTotal
if not $data->{"binding"} and $data->{"description"} 0 0 0
Condition0%0/3
not $data->{"binding"} and $data->{"description"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $data->{"binding"} and $data->{"description"}
not $data->{"binding"} $data->{"description"}
Truth table0%0/3
not(A) and B
A= not $data->{"binding"}B= $data->{"description"}
ABresult
0X0
100
111
153 0 ▶ ($data->{author}) = $data->{description} =~ m!This book is written by ([\s\w]+) Published by!is if(!$data->{author} && $data->{description});
Branch: 1 branch
BranchTrueFalseTotal
if not $data->{"author"} and $data->{"description"} 0 0 0
Condition0%0/3
not $data->{"author"} and $data->{"description"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $data->{"author"} and $data->{"description"}
not $data->{"author"} $data->{"description"}
Truth table0%0/3
not(A) and B
A= not $data->{"author"}B= $data->{"description"}
ABresult
0X0
100
111
154  
155 0 ($data->{isbn13}) = $ean;
156 0 ($data->{isbn10}) = $self->convert_to_isbn10($ean);
157  
158 0 ▶ return $self->handler("Could not extract data from Yahoo! result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
159 unless(defined $data);
160  
161 # trim top and tail
162 0 ▶ foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
163  
164 my $bk = {
165 'isbn13' => $data->{isbn13},
166 'isbn10' => $data->{isbn10},
167 'isbn' => $data->{isbn13},
168 'ean13' => $data->{isbn13},
169 'author' => $data->{author},
170 'title' => $data->{title},
171 'book_link' => $data->{book_link},
172 'image_link' => $thumb,
173 'thumb_link' => $thumb,
174 'description' => $data->{description},
175 'publisher' => $data->{publisher},
176 'pubdate' => $data->{pubdate},
177 'binding' => $data->{binding},
178 'pages' => $data->{pages},
179 'weight' => $data->{weight},
180 'width' => $data->{width},
181 'height' => $data->{height},
182 'depth' => $data->{depth},
183 0 'html' => $html
184 };
185 0 $self->book($bk);
186 0 $self->found(1);
187 0 return $self->book;
188 }
189  
190 5 q{currently listening to: 'Drunk In Public' by The Levellers};
191  
192 __END__
↑ Summary