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

filter: Statementstmt 26.3% 19 / 72 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 18.9% 25 / 132 CC 20 SCAR 58.2
CC
20
Cov
17%
search
420.0
BEGIN
1.0
BEGIN
1.0
CRAP 336.2
↑ Summary
1 package WWW::Scraper::ISBN::Foyles_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.18';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::Foyles_Driver - Search driver for the Foyles 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 Foyles 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 my $REFERER = 'http://www.foyles.co.uk';
41 5 my $FORMNAME = 'aspnetForm';
42  
43 #--------------------------------------------------------------------------
44  
45 ###########################################################################
46 # Public Interface
47  
48 =head1 METHODS
49  
50 =over 4
51  
52 =item C<search()>
53  
54 Creates a query string, then passes the appropriate form fields to the
55 Foyles server.
56  
57 The returned page should be the correct catalog page for that ISBN. If not the
58 function returns zero and allows the next driver in the chain to have a go. If
59 a valid page is returned, the following fields are returned via the book hash:
60  
61 isbn (now returns isbn13)
62 isbn10
63 isbn13
64 ean13 (industry name)
65 author
66 title
67 book_link
68 image_link
69 thumb_link
70 description
71 pubdate
72 publisher
73 binding (if known)
74  
75 The book_link, image_link and thumb_link all refer back to the Foyles website.
76  
77 =back
78  
79 =cut
80  
81 0 ▶sub search {
Subroutine search: not called CC=20 SCAR=60.4
Pod documented
82 0 my $self = shift;
83 0 my $isbn = shift;
84 0 $self->found(0);
85 0 $self->book(undef);
86  
87 # validate and convert into EAN13 format
88 0 my $ean = $self->convert_to_ean13($isbn);
89 0 ▶ return $self->handler("Invalid ISBN specified")
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
90 if(!$ean || (length $isbn == 13 && $isbn ne $ean)
91 || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
92  
93 0 my $mech = WWW::Mechanize->new();
94 0 $mech->agent_alias( 'Windows IE 6' );
95 0 $mech->add_header( 'Accept-Encoding' => undef );
96  
97 0 eval { $mech->get( $REFERER ) };
98 0 ▶ return $self->handler("The Foyles 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
99 if($@ || !$mech->success() || !$mech->content());
100  
101 0 my @forms = $mech->forms;
102 0 my %forms = map {$_->attr('name') => 1} @forms;
103  
104 return $self->handler("The Foyles website appears to be broken [".join(',',keys %forms)."].")
105 0 ▶ unless($forms{$FORMNAME});
Branch: 1 branch
BranchTrueFalseTotal
unless $forms{$FORMNAME} 0 0 0
106  
107 0 $mech->form_name( $FORMNAME );
108 0 $mech->field( 'ctl00$txtTerm', $ean );
109 0 $mech->field( '__EVENTTARGET', 'ctl00$LinkBtnQuickSearchBy' );
110 0 $mech->field( '__EVENTARGUMENT', '' );
111  
112 0 eval { $mech->submit(); };
113 0 ▶ return $self->handler("The Foyles 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
114 if($@ || !$mech->success() || !$mech->content());
115  
116 # The Book page
117 0 my $html = $mech->content();
118  
119 0 ▶ return $self->handler("The Foyles website appears to be unavailable.")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /I'm sorry we have encountered a problem with this page/is 0 0 0
120 if($html =~ m!I'm sorry we have encountered a problem with this page!si);
121  
122 0 ▶ return $self->handler("Failed to find that book on the Foyles website. [$isbn]")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry, there are no results for|This item is not currently listed on the Foyles Website/is 0 0 0
123 if($html =~ m!Sorry, there are no results for|This item is not currently listed on the Foyles Website!si);
124
125 0 $html =~ s/&amp;/&/g;
126 0 $html =~ s/&nbsp;/ /g;
127 #print STDERR "\n# content2=[\n$html\n]\n";
128  
129 0 my $data;
130 0 ($data->{title}) = $html =~ m!<div class="BookTitle">\s*<span itemprop="name">([^<]+)</span>!si;
131 0 ($data->{author}) = $html =~ m!<div class="Author">\s*<a class="Author" href="[^"]+">([^<]+)</a>!si;
132 0 ($data->{binding}) = $html =~ m!<span class="ListItem">Type: <strong>([^<]+)</strong></span>!si;
133 0 ($data->{publisher}) = $html =~ m!<span class="ListItem">Publisher: <strong><span itemprop="publisher">([^<]+)</span></strong></span>!si;
134 0 ($data->{pubdate}) = $html =~ m!<span class="ListItem">Publication Date: <strong><meta itemprop="datePublished" content="[^"]+">([^<]+)</strong></span>!si;
135 0 ($data->{isbn13}) = $html =~ m!<span class="ListItem">ISBN-13: <strong><span itemprop="identifier" content="isbn:([^"]+)">!si;
136 0 ($data->{description}) = $html =~ m!<span itemprop="description">([^<]+)!si;
137 0 ($data->{image}) = $html =~ m!<div class="BookCover">\s*<img.*?src="([^"]+)"[^>]+>!;
138  
139 0 $data->{thumb} = $data->{image};
140 0 $data->{isbn10} = $self->convert_to_isbn10($ean);
141 0 $data->{isbn13} = $ean;
142  
143 0 for(qw(publisher)) {
144 0 ▶ next unless($data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$_} 0 0 0
145 0 $data->{$_} =~ s/&#0?39;/'/g;
146 }
147  
148 #use Data::Dumper;
149 #print STDERR "\n# " . Dumper($data);
150  
151 0 ▶ return $self->handler("Could not extract data from the Foyles result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
152 unless(defined $data);
153  
154 # trim top and tail
155 0 foreach (keys %$data) {
156 0 ▶ next unless(defined $data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
157 0 $data->{$_} =~ s!&nbsp;! !g;
158 0 $data->{$_} =~ s/^\s+//;
159 0 $data->{$_} =~ s/\s+$//;
160 }
161  
162 my $bk = {
163 'ean13' => $data->{isbn13},
164 'isbn13' => $data->{isbn13},
165 'isbn10' => $data->{isbn10},
166 'isbn' => $data->{isbn13},
167 'author' => $data->{author},
168 'title' => $data->{title},
169 'book_link' => $mech->uri(),
170 'image_link' => $data->{image},
171 'thumb_link' => $data->{thumb},
172 'description' => $data->{description},
173 'pubdate' => $data->{pubdate},
174 'publisher' => $data->{publisher},
175 'binding' => $data->{binding},
176 'pages' => $data->{pages},
177 0 'html' => $html
178 };
179  
180 #use Data::Dumper;
181 #print STDERR "\n# book=".Dumper($bk);
182  
183 0 $self->book($bk);
184 0 $self->found(1);
185 0 return $self->book;
186 }
187  
188 5 1;
189  
190 __END__
↑ Summary