blib/lib/WWW/Scraper/ISBN/ISBNnu_Driver.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 10 | 12 | 83.3 |
branch | n/a | ||
condition | n/a | ||
subroutine | 4 | 4 | 100.0 |
pod | n/a | ||
total | 14 | 16 | 87.5 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package WWW::Scraper::ISBN::ISBNnu_Driver; | ||||||
2 | |||||||
3 | 6 | 6 | 99677 | use strict; | |||
6 | 15 | ||||||
6 | 235 | ||||||
4 | 6 | 6 | 26 | use warnings; | |||
6 | 7 | ||||||
6 | 303 | ||||||
5 | |||||||
6 | our $VERSION = '0.24'; | ||||||
7 | |||||||
8 | #-------------------------------------------------------------------------- | ||||||
9 | |||||||
10 | ########################################################################### | ||||||
11 | # Inheritence | ||||||
12 | |||||||
13 | 6 | 6 | 26 | use base qw(WWW::Scraper::ISBN::Driver); | |||
6 | 14 | ||||||
6 | 3068 | ||||||
14 | |||||||
15 | ########################################################################### | ||||||
16 | # Modules | ||||||
17 | |||||||
18 | 6 | 6 | 8656 | use WWW::Mechanize; | |||
0 | |||||||
0 | |||||||
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 | my ($self,$value) = @_; | ||||||
34 | |||||||
35 | return '' unless(defined $value); | ||||||
36 | |||||||
37 | $value =~ s/^\s+//; # trim leading whitespace | ||||||
38 | $value =~ s/\s+$//; # trim trailing whitespace | ||||||
39 | $value =~ s/\n//g; # trim newlines? | ||||||
40 | $value =~ s/ +/ /g; # trim extra middle space | ||||||
41 | $value =~ s/<[^>]+>//g; # remove tags | ||||||
42 | |||||||
43 | return $value; | ||||||
44 | } | ||||||
45 | |||||||
46 | sub search { | ||||||
47 | my ($self,$isbn) = @_; | ||||||
48 | my %data; | ||||||
49 | |||||||
50 | $self->found(0); | ||||||
51 | $self->book(undef); | ||||||
52 | |||||||
53 | my $post_url = "http://isbn.nu/".$isbn; | ||||||
54 | my $mech = WWW::Mechanize->new(); | ||||||
55 | $mech->agent_alias( 'Linux Mozilla' ); | ||||||
56 | $mech->add_header( 'Accept-Encoding' => undef ); | ||||||
57 | |||||||
58 | eval { $mech->get( $post_url ) }; | ||||||
59 | return $self->handler("isbn.nu website appears to be unavailable.") | ||||||
60 | if($@ || !$mech->success() || !$mech->content()); | ||||||
61 | |||||||
62 | my $html = $mech->content(); | ||||||
63 | my ($title) = $html =~ / |
||||||
64 | $data{title} = $self->trim($title); | ||||||
65 | |||||||
66 | return $self->handler("Failed to find that book on the isbn.nu website.") | ||||||
67 | if (!$data{title} || $data{title} eq "No Title Found"); | ||||||
68 | |||||||
69 | ($data{publisher}) = $html =~ m!Publisher\s*([^<]+)!si; | ||||||
70 | ($data{pubdate}) = $html =~ m!Publication date\s*([^<]+)!; | ||||||
71 | ($data{pages}) = $html =~ m!Pages\s*([0-9]+)!; | ||||||
72 | ($data{edition}) = $html =~ m!Edition\s*([^<]+)!; | ||||||
73 | ($data{volume}) = $html =~ m!Volume\s*([^<]+)!; | ||||||
74 | ($data{binding}) = $html =~ m!Binding\s*([^<]+)!; | ||||||
75 | ($data{isbn13}) = $html =~ m!ISBN-13\s*([0-9]+)!; | ||||||
76 | ($data{isbn10}) = $html =~ m!ISBN-10\s*([0-9X]+)!; | ||||||
77 | ($data{weight}) = $html =~ m!Weight\s*([0-9\.]+) lbs.!; | ||||||
78 | ($data{author}) = $html =~ m! By\s*(.*?)\s* !; |
||||||
79 | ($data{description})= $html =~ m! Summary ([^<]+)
| ||||||
80 | ($data{description})= $html =~ m! | ||||||
81 | |||||||
82 | $data{$_} = $self->trim($data{$_}) for(qw(publisher pubdate binding author description)); | ||||||
83 | |||||||
84 | if($data{weight}) { | ||||||
85 | $data{weight} = int($data{weight} / $LB2G); | ||||||
86 | } | ||||||
87 | |||||||
88 | my @size = $html =~ m!Dimensions\s*([0-9\.]+) by ([0-9\.]+) by ([0-9\.]+) in.!; | ||||||
89 | if(@size) { | ||||||
90 | ($data{depth},$data{width},$data{height}) = sort @size; | ||||||
91 | $data{$_} = int($data{$_} / $IN2MM) for(qw( height width depth )); | ||||||
92 | } | ||||||
93 | |||||||
94 | #print STDERR "#html=".Dumper(\%data)."\n"; | ||||||
95 | |||||||
96 | $data{book_link} = $mech->uri(); | ||||||
97 | |||||||
98 | $data{ean13} = $data{isbn13}; | ||||||
99 | $data{isbn} = $data{isbn13} || $isbn; | ||||||
100 | $data{html} = $html; | ||||||
101 | |||||||
102 | $self->book(\%data); | ||||||
103 | |||||||
104 | $self->found(1); | ||||||
105 | return $self->book; | ||||||
106 | } | ||||||
107 | |||||||
108 | 1; | ||||||
109 | |||||||
110 | __END__ |