blib/lib/WWW/Scraper/ISBN/ISBNnu_Driver.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 12 | 61 | 19.6 |
branch | 0 | 12 | 0.0 |
condition | 0 | 12 | 0.0 |
subroutine | 4 | 6 | 66.6 |
pod | 2 | 2 | 100.0 |
total | 18 | 93 | 19.3 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package WWW::Scraper::ISBN::ISBNnu_Driver; | ||||||
2 | |||||||
3 | 5 | 5 | 296255 | use strict; | |||
5 | 37 | ||||||
5 | 150 | ||||||
4 | 5 | 5 | 38 | use warnings; | |||
5 | 11 | ||||||
5 | 306 | ||||||
5 | |||||||
6 | our $VERSION = '0.25'; | ||||||
7 | |||||||
8 | #-------------------------------------------------------------------------- | ||||||
9 | |||||||
10 | ########################################################################### | ||||||
11 | # Inheritence | ||||||
12 | |||||||
13 | 5 | 5 | 31 | use base qw(WWW::Scraper::ISBN::Driver); | |||
5 | 12 | ||||||
5 | 2691 | ||||||
14 | |||||||
15 | ########################################################################### | ||||||
16 | # Modules | ||||||
17 | |||||||
18 | 5 | 5 | 9077 | use WWW::Mechanize; | |||
5 | 775323 | ||||||
5 | 4377 | ||||||
19 | |||||||
20 | ########################################################################### | ||||||
21 | # Variables | ||||||
22 | |||||||
23 | my $IN2MM = 0.0393700787; # number of inches in a millimetre (mm) | ||||||
24 | my $LB2G = 0.00220462; # number of pounds (lbs) in a gram | ||||||
25 | my $OZ2G = 0.035274; # number of ounces (oz) in a gram | ||||||
26 | |||||||
27 | #-------------------------------------------------------------------------- | ||||||
28 | |||||||
29 | ########################################################################### | ||||||
30 | # Public Interface | ||||||
31 | |||||||
32 | sub trim { | ||||||
33 | 0 | 0 | 1 | my ($self,$value) = @_; | |||
34 | |||||||
35 | 0 | 0 | return '' unless(defined $value); | ||||
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 | 0 | 1 | my ($self,$isbn) = @_; | |||
48 | 0 | my %data; | |||||
49 | |||||||
50 | 0 | $self->found(0); | |||||
51 | 0 | $self->book(undef); | |||||
52 | |||||||
53 | 0 | my $post_url = "https://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 ) }; | |||||
0 | |||||||
59 | 0 | 0 | 0 | return $self->handler("isbn.nu website appears to be unavailable.") | |||
0 | |||||||
60 | if($@ || !$mech->success() || !$mech->content()); | ||||||
61 | |||||||
62 | 0 | my $html = $mech->content(); | |||||
63 | 0 | my ($title) = $html =~ / |
|||||
64 | 0 | $data{title} = $self->trim($title); | |||||
65 | |||||||
66 | return $self->handler("Failed to find that book on the isbn.nu website.") | ||||||
67 | 0 | 0 | 0 | if (!$data{title} || $data{title} eq "No Title Found"); | |||
68 | |||||||
69 | 0 | ($data{publisher}) = $html =~ m!Publisher\s*([^<]+)\s*!si; | |||||
70 | 0 | ($data{pubdate}) = $html =~ m!Publication date\s*([^<]+)\s*!si; | |||||
71 | 0 | ($data{pages}) = $html =~ m!Pages\s*([0-9]+)\s*!si; | |||||
72 | 0 | ($data{edition}) = $html =~ m!Edition\s*([^<]+)\s*!si; | |||||
73 | 0 | ($data{volume}) = $html =~ m!Volume\s*([^<]+)\s*!si; | |||||
74 | 0 | ($data{binding}) = $html =~ m!Binding\s*([^<]+)\s*!si; | |||||
75 | 0 | ($data{isbn13}) = $html =~ m!ISBN-13\s*([0-9]+)\s*!si; | |||||
76 | 0 | ($data{isbn10}) = $html =~ m!ISBN-10\s*([0-9X]+)\s*!si; | |||||
77 | 0 | ($data{weight}) = $html =~ m!Weight\s*([0-9\.]+) lbs.\s*!si; | |||||
78 | 0 | ($data{author}) = $html =~ m! By\s*(.*?)\s* !; |
|||||
79 | 0 | ($data{description})= $html =~ m! Summary ([^<]+)
| |||||
80 | 0 | 0 | ($data{description})= $html =~ m! | ||||
81 | |||||||
82 | 0 | $data{$_} = $self->trim($data{$_}) for(qw(publisher pubdate binding author description)); | |||||
83 | |||||||
84 | 0 | 0 | if($data{weight}) { | ||||
85 | 0 | $data{weight} = int($data{weight} / $LB2G); | |||||
86 | } | ||||||
87 | |||||||
88 | 0 | my @size = $html =~ m!Dimensions\s*([0-9\.]+) by ([0-9\.]+) by ([0-9\.]+) in.\s*!; | |||||
89 | 0 | 0 | if(@size) { | ||||
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 | 0 | $data{isbn} = $data{isbn13} || $isbn; | ||||
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 | 1; | ||||||
109 | |||||||
110 | __END__ |