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

filter: Statementstmt 25.7% 17 / 66 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 21.9% 23 / 105 SCAR 46.4
CC
13
Cov
19%
search
156.0
trim
6.0
BEGIN
1.0
CRAP 103.2
↑ Summary
1 package WWW::Scraper::ISBN::ISBNnu_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 our $VERSION = '0.24';
7  
8 #--------------------------------------------------------------------------
9  
10 ###########################################################################
11 # Inheritence
12  
13 5 ▶use base qw(WWW::Scraper::ISBN::Driver);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 ###########################################################################
16 # Modules
17  
18 5 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ###########################################################################
21 # Variables
22  
23 5 my $IN2MM = 0.0393700787; # number of inches in a millimetre (mm)
24 5 my $LB2G = 0.00220462; # number of pounds (lbs) in a gram
25 5 my $OZ2G = 0.035274; # number of ounces (oz) in a gram
26  
27 #--------------------------------------------------------------------------
28  
29 ###########################################################################
30 # Public Interface
31  
32 sub trim {
33 0 ▶ my ($self,$value) = @_;
Subroutine trim: not called CC=2 SCAR=17.9
Pod documented
34  
35 0 ▶ return '' unless(defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
36  
37 0 $value =~ s/^\s+//; # trim leading whitespace
38 0 $value =~ s/\s+$//; # trim trailing whitespace
39 0 $value =~ s/\n//g; # trim newlines?
40 0 $value =~ s/ +/ /g; # trim extra middle space
41 0 $value =~ s/<[^>]+>//g; # remove tags
42  
43 0 return $value;
44 }
45
46 sub search {
47 0 ▶ my ($self,$isbn) = @_;
Subroutine search: not called CC=12 SCAR=50.5
Pod documented
48 0 my %data;
49
50 0 $self->found(0);
51 0 $self->book(undef);
52  
53 0 my $post_url = "http://isbn.nu/".$isbn;
54 0 my $mech = WWW::Mechanize->new();
55 0 $mech->agent_alias( 'Linux Mozilla' );
56 0 $mech->add_header( 'Accept-Encoding' => undef );
57  
58 0 eval { $mech->get( $post_url ) };
59 0 ▶ return $self->handler("isbn.nu 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
60 if($@ || !$mech->success() || !$mech->content());
61  
62 0 my $html = $mech->content();
63 0 my ($title) = $html =~ /<title>([^<]+)<\/title>/;
64 0 $data{title} = $self->trim($title);
65  
66 return $self->handler("Failed to find that book on the isbn.nu website.")
67 0 ▶ if (!$data{title} || $data{title} eq "No Title Found");
Branch: 1 branch
BranchTrueFalseTotal
if not $data{"title"} or $data{"title"} eq "No Title Found" 0 0 0
Condition0%0/3
not $data{"title"} or $data{"title"} eq "No Title Found"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $data{"title"} or $data{"title"} eq "No Title Found"
not $data{"title"} $data{"title"} eq "No Title Found"
Truth table0%0/3
not(A) or B
A= not $data{"title"}B= $data{"title"} eq "No Title Found"
ABresult
1X1
011
000
68  
69 0 ($data{publisher}) = $html =~ m!<span class="bi_col_title">Publisher</span>\s*<span class="bi_col_value">([^<]+)</span></div>!si;
70 0 ($data{pubdate}) = $html =~ m!<span class="bi_col_title">Publication date</span>\s*<span class="bi_col_value">([^<]+)</span></div>!;
71 0 ($data{pages}) = $html =~ m!<span class="bi_col_title">Pages</span>\s*<span class="bi_col_value">([0-9]+)</span></div>!;
72 0 ($data{edition}) = $html =~ m!<span class="bi_col_title">Edition</span>\s*<span class="bi_col_value">([^<]+)</span></div>!;
73 0 ($data{volume}) = $html =~ m!<span class="bi_col_title">Volume</span>\s*<span class="bi_col_value">([^<]+)</span></div>!;
74 0 ($data{binding}) = $html =~ m!<span class="bi_col_title">Binding</span>\s*<span class="bi_col_value">([^<]+)</span></div>!;
75 0 ($data{isbn13}) = $html =~ m!<span class="bi_col_title">ISBN-13</span>\s*<span class="bi_col_value">([0-9]+)</span></div>!;
76 0 ($data{isbn10}) = $html =~ m!<span class="bi_col_title">ISBN-10</span>\s*<span class="bi_col_value">([0-9X]+)</span></div>!;
77 0 ($data{weight}) = $html =~ m!<span class="bi_col_title">Weight</span>\s*<span class="bi_col_value">([0-9\.]+) lbs.</span></div>!;
78 0 ($data{author}) = $html =~ m!<div class="d_descriptive">By\s*(.*?)\s*</div>!;
79 0 ($data{description})= $html =~ m!<div class="bi_annotation_text"><div class="bi_anno_text_head">Summary</div>([^<]+)<!;
80 0 ▶ ($data{description})= $html =~ m!<div class="bi_wide bi_annotation_text"><a name="amazondesc"></a><b>Amazon.com description:</b> <b>Product Description</b>:([^<]+)<! unless($data{description});
Branch: 1 branch
BranchTrueFalseTotal
unless $data{"description"} 0 0 0
81  
82 0 $data{$_} = $self->trim($data{$_}) for(qw(publisher pubdate binding author description));
83  
84 0 ▶ if($data{weight}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"weight"}) 0 0 0
85 0 $data{weight} = int($data{weight} / $LB2G);
86 }
87  
88 0 my @size = $html =~ m!<span class="bi_col_title">Dimensions</span>\s*<span class="bi_col_value">([0-9\.]+) by ([0-9\.]+) by ([0-9\.]+) in.</span></div>!;
89 0 ▶ if(@size) {
Branch: 1 branch
BranchTrueFalseTotal
if (@size) 0 0 0
90 0 ($data{depth},$data{width},$data{height}) = sort @size;
91 0 $data{$_} = int($data{$_} / $IN2MM) for(qw( height width depth ));
92 }
93  
94 #print STDERR "#html=".Dumper(\%data)."\n";
95  
96 0 $data{book_link} = $mech->uri();
97  
98 0 $data{ean13} = $data{isbn13};
99 0 ▶ $data{isbn} = $data{isbn13} || $isbn;
Condition0%0/3
$data{'isbn13'} || $isbn
l!l&&r!l&&!r
000
MC/DC0%0/2
$data{'isbn13'} || $isbn
$data{'isbn13'} $isbn
Truth table0%0/3
A || B
A= $data{'isbn13'}B= $isbn
ABresult
1X1
011
000
100 0 $data{html} = $html;
101  
102 0 $self->book(\%data);
103  
104 0 $self->found(1);
105 0 return $self->book;
106 }
107  
108 5 1;
109  
110 __END__
↑ Summary