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