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

filter: Statementstmt 32.2% 20 / 62 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 23.6% 27 / 114 CC 18 SCAR 55.5
CC
18
Cov
20%
search
342.0
BEGIN
1.0
BEGIN
1.0
CRAP 256.8
↑ Summary
1 package WWW::Scraper::ISBN::TheNile_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.14';
8  
9 #--------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::Scraper::ISBN::TheNile_Driver - Search driver for TheNile 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 TheNile 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 ▶use constant SEARCH => 'http://www.thenile.com.au/search.php?s=';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 #--------------------------------------------------------------------------
43  
44 ###########################################################################
45 # Public Interface
46  
47 =head1 METHODS
48  
49 =over 4
50  
51 =item C<search()>
52  
53 Creates a query string, then passes the appropriate form fields to the
54 TheNile server.
55  
56 The returned page should be the correct catalog page for that ISBN. If not the
57 function returns zero and allows the next driver in the chain to have a go. If
58 a valid page is returned, the following fields are returned via the book hash:
59  
60 isbn (now returns isbn13)
61 isbn10
62 isbn13
63 ean13 (industry name)
64 author
65 title
66 book_link
67 image_link
68 description
69 pubdate
70 publisher
71 binding (if known)
72 pages (if known)
73 weight (if known) (in grammes)
74 width (if known) (in millimetres)
75 height (if known) (in millimetres)
76  
77 The book_link and image_link refer back to the TheNile website.
78  
79 =back
80  
81 =cut
82  
83 0 ▶sub search {
Subroutine search: not called CC=18 SCAR=58.3
Pod documented
84 0 my $self = shift;
85 0 my $isbn = shift;
86 0 $self->found(0);
87 0 $self->book(undef);
88  
89 # validate and convert into EAN13 format
90 0 my $ean = $self->convert_to_ean13($isbn);
91 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
92 if(!$ean || (length $isbn == 13 && $isbn ne $ean)
93 || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
94 0 $isbn = $ean;
95  
96 0 my $mech = WWW::Mechanize->new();
97 0 $mech->agent_alias( 'Linux Mozilla' );
98  
99 0 eval { $mech->get( SEARCH . $isbn ) };
100 0 ▶ return $self->handler("TheNile 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
101 if($@ || !$mech->success() || !$mech->content());
102  
103 # The Book page
104 0 my $html = $mech->content();
105  
106 0 ▶ return $self->handler("Failed to find that book on TheNile website.")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /Sorry your search.*?did not return any results|This book is currently unavailable/is 0 0 0
107 if($html =~ m!Sorry your search.*?did not return any results|This book is currently unavailable!si);
108
109 #print STDERR "\n# html=[\n$html\n]\n";
110  
111 0 my $data;
112 0 ($data->{image}) = $html =~ m!(http://c\d+\.mrcdn\.net/[\w\.\/]+\.jpg)!si;
113 0 ($data->{thumb}) = $html =~ m!(http://c\d+\.mrcdn\.net/[\w\.\/]+\.jpg)!si;
114 0 ($data->{isbn13},$data->{isbn10}) = $html =~ m!<li><span>ISBN</span>\s*<div[^>]+>\s*(\d+)\s*/\s*(\d+)\s*</div></li>!si;
115 0 ($data->{author}) = $html =~ m!<li><span>Authors?</span>\s*((?:<a[^>]+>[^<]+</a>(?:\s*(?:and|,)\s*)?)+)</li>!si;
116 0 ▶ ($data->{author}) = $html =~ m!<li><span>Authors?</span>\s*([^<]+)</li>!si unless($data->{author});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"author"} 0 0 0
117 0 ($data->{title}) = $html =~ m!<li><span>Title</span>\s*([^,<]+)</li>!si;
118 0 ($data->{publisher}) = $html =~ m!<li><span>Publisher</span>\s*<div[^>]+>\s*<div[^>]+>\s*([^<]+)</div></div></li>!si;
119 0 ▶ ($data->{publisher}) = $html =~ m!<li><span>Publisher</span>\s*([^<]+)</li>!si unless($data->{publisher});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"publisher"} 0 0 0
120 0 ($data->{pubdate}) = $html =~ m!<li><span>Year</span>\s*([^<]+)</li>!si;
121 0 ($data->{binding}) = $html =~ m!<li><span>Format</span>\s*<div[^>]+>\s*([^<]+)</div></li>!si;
122 0 ($data->{pages}) = $html =~ m!<li><span>Pages</span>\s*([\d.]+)</li>!si;
123 0 ($data->{weight}) = $html =~ m!<li><span>Weight</span>\s*(\d+)g</li>!si;
124 0 ($data->{width},$data->{height}) = $html =~ m!<li><span>Dimensions</span>\s*([\d.]+)mm x ([\d.]+)mm(?: x [\d.]+mm)?</li>!si;
125 0 ($data->{description}) = $html =~ m!<h4>Annotation</h4>\s*</div>\s*<div class="content readable">(.*?)</div>!si;
126 0 ▶ ($data->{description}) = $html =~ m!<h4>Publisher Description</h4>\s*</div>\s*<div class="content readable">(.*?)</div>!si unless($data->{description});
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"description"} 0 0 0
127
128 0 for(qw(author publisher description)) {
129 0 ▶ $data->{$_} =~ s!<[^>]+>!!g if($data->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$_} 0 0 0
130 }
131  
132 #use Data::Dumper;
133 #print STDERR "\n# " . Dumper($data);
134  
135 0 ▶ return $self->handler("Could not extract data from TheNile result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
136 unless(defined $data);
137  
138 # trim top and tail
139 0 ▶ foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 0 0 0
140  
141 0 my $url = $mech->uri();
142  
143 my $bk = {
144 'ean13' => $data->{isbn13},
145 'isbn13' => $data->{isbn13},
146 'isbn10' => $data->{isbn10},
147 'isbn' => $data->{isbn13},
148 'author' => $data->{author},
149 'title' => $data->{title},
150 'book_link' => "$url",
151 'image_link' => $data->{image},
152 'thumb_link' => $data->{thumb},
153 'description' => $data->{description},
154 'pubdate' => $data->{pubdate},
155 'publisher' => $data->{publisher},
156 'binding' => $data->{binding},
157 'pages' => $data->{pages},
158 'weight' => $data->{weight},
159 'width' => $data->{width},
160 'height' => $data->{height},
161 'depth' => $data->{depth},
162 0 'html' => $html
163 };
164  
165 #use Data::Dumper;
166 #print STDERR "\n# book=".Dumper($bk);
167  
168 0 $self->book($bk);
169 0 $self->found(1);
170 0 return $self->book;
171 }
172  
173 5 1;
174  
175 __END__
↑ Summary