File Coverage

blib/lib/WWW/Scraper/ISBN/BarnesNoble_Driver.pm
Criterion Covered Total %
statement 81 81 100.0
branch 12 22 54.5
condition 6 18 33.3
subroutine 10 10 100.0
pod 1 1 100.0
total 110 132 83.3


line stmt bran cond sub pod time code
1             package WWW::Scraper::ISBN::BarnesNoble_Driver;
2              
3 7     7   117490 use strict;
  7         18  
  7         281  
4 7     7   35 use warnings;
  7         14  
  7         241  
5              
6 7     7   27 use vars qw($VERSION @ISA);
  7         12  
  7         524  
7             $VERSION = '0.20';
8              
9             #--------------------------------------------------------------------------
10              
11             =head1 NAME
12              
13             WWW::Scraper::ISBN::BarnesNoble_Driver - Search driver for the Barnes and Noble online book catalog.
14              
15             =head1 SYNOPSIS
16              
17             See parent class documentation (L)
18              
19             =head1 DESCRIPTION
20              
21             Searches for book information from the Barnes and Noble online book catalog
22              
23             =cut
24              
25             #--------------------------------------------------------------------------
26              
27             ###########################################################################
28             # Inheritence
29              
30 7     7   30 use base qw(WWW::Scraper::ISBN::Driver);
  7         11  
  7         3405  
31              
32             ###########################################################################
33             # Modules
34              
35 7     7   10489 use WWW::Mechanize;
  7         928438  
  7         409  
36              
37             ###########################################################################
38             # Constants
39              
40 7     7   79 use constant REFERER => 'http://www.barnesandnoble.com/';
  7         9  
  7         509  
41 7     7   31 use constant SEARCH => 'http://www.barnesandnoble.com/include/quicksearch_newSearch.asp?page=index&prod=univ&choice=allproducts&query=%s&flag=False&ATL_lid=hu0MIVVMgy&ATL_userid=hu0MIVVMgy&ATL_sid=v24Vd2sM16&ATL_seqnum=5';
  7         10  
  7         274  
42 7     7   26 use constant IN2MM => 25.4; # number of inches in a millimetre (mm)
  7         14  
  7         6829  
43              
44             #--------------------------------------------------------------------------
45              
46             ###########################################################################
47             # Public Interface
48              
49             =head1 METHODS
50              
51             =over 4
52              
53             =item C
54              
55             Creates a query string, then passes the appropriate form fields to the
56             Barnes and Noble server.
57              
58             The returned page should be the correct catalog page for that ISBN. If not the
59             function returns zero and allows the next driver in the chain to have a go. If
60             a valid page is returned, the following fields are returned via the book hash:
61              
62             isbn (now returns isbn13)
63             isbn10
64             isbn13
65             ean13 (industry name)
66             author
67             title
68             book_link
69             image_link
70             description
71             pubdate
72             publisher
73             binding (if known)
74             pages (if known)
75             weight (if known) (in grammes)
76             width (if known) (in millimetres)
77             height (if known) (in millimetres)
78             depth (if known) (in millimetres)
79              
80             The book_link and image_link refer back to the Barnes and Noble website.
81              
82             =back
83              
84             =cut
85              
86             sub search {
87 1     1 1 284 my $self = shift;
88 1         2 my $isbn = shift;
89 1         8 $self->found(0);
90 1         25 $self->book(undef);
91              
92             # validate and convert into EAN13 format
93 1         17 my $ean = $self->convert_to_ean13($isbn);
94 1 50 33     72 return $self->handler("Invalid ISBN specified [$isbn]")
      33        
      33        
      33        
95             if(!$ean || (length $isbn == 13 && $isbn ne $ean)
96             || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
97              
98 1         8 my $mech = WWW::Mechanize->new();
99 1         19072 $mech->agent_alias( 'Linux Mozilla' );
100 1         101 $mech->add_header( 'Accept-Encoding' => undef );
101 1         15 $mech->add_header( 'Referer' => REFERER );
102              
103 1         14 my $url = sprintf SEARCH, $ean, $ean;
104             #print STDERR "\n# ean=$ean, link=[$url]\n";
105              
106 1         3 eval { $mech->get( $url ) };
  1         6  
107 1 50 33     1829373 return $self->handler("the Barnes and Noble website appears to be unavailable. [$@]")
      33        
108             if($@ || !$mech->success() || !$mech->content());
109              
110             # The Book page
111 1         58 my $html = $mech->content();
112              
113 1 50   1   1182 return $self->handler("Failed to find that book on the Barnes and Noble website. [$isbn]")
  1         11  
  1         2  
  1         15  
114             if($html =~ m!Sorry. We did not find any results|Sorry, we could not find what you were looking for!si);
115            
116 1         24451 $html =~ s/&/&/g;
117 1         742 $html =~ s/�?39;/'/g;
118 1         741 $html =~ s/ / /g;
119             #print STDERR "\n# html=[\n$html\n]\n";
120              
121 1         3 my $data;
122 1         19 ($data->{isbn10}) = $self->convert_to_isbn10($ean);
123 1         2499 ($data->{isbn13}) = $html =~ m!ISBN-13:\s*(\d+)!si;
124 1         2467 ($data->{publisher}) = $html =~ m!Publisher: \s*([^<]+)\s*!si;
125 1         2484 ($data->{pubdate}) = $html =~ m!Publication date: \s*([^<]+)\s*!si;
126 1         102 ($data->{title}) = $html =~ m!]*>!si;
127 1         757 ($data->{author}) = $html =~ m!
    (.*?)
!si;
128 1         2490 ($data->{pages}) = $html =~ m!Pages: \s*(.*?)\s*!si;
129 1         61 ($data->{binding}) = $html =~ m![^\|]+\|[^\|]+\|\s*(.*?)\s*\|[^<]+!si;
130 1         122 ($data->{image}) = $html =~ m!]*>!si;
131 1         122 ($data->{thumb}) = $html =~ m!]*>!si;
132 1         900 ($data->{description}) = $html =~ m!product-commentary-overview-1.*?

Overview

\s*(.*?)\s*\s*!si;
133 1         2626 ($data->{width},$data->{height},$data->{depth})
134             = $html =~ m!Product dimensions:\s*([\d.]+)\s*\(w\)\s*x\s*([\d.]+)\s*\(h\)\s*x\s*([\d.]+)\s*\(d\)!si;
135              
136             # currently not provided
137 1         53 ($data->{weight}) = $html =~ m!Weight:\s*([^<]+)!s;
138              
139 1 50       11 $data->{depth} = int($data->{depth} * IN2MM) if($data->{depth});
140 1 50       4 $data->{width} = int($data->{width} * IN2MM) if($data->{width});
141 1 50       4 $data->{height} = int($data->{height} * IN2MM) if($data->{height});
142 1 50       4 $data->{weight} = int($data->{weight}) if($data->{weight});
143              
144 1         4 for(qw(author publisher description)) {
145 3 50       7 next unless($data->{$_});
146 3         63 $data->{$_} =~ s![ \t\n\r]+! !g;
147 3         34 $data->{$_} =~ s!<[^>]+>!!g;
148             }
149              
150 1 50       3 if($data->{author}) {
151 1         5 $data->{author} =~ s!^\s*by\s*!!;
152 1         5 $data->{author} =~ s!,\s*!, !g;
153             }
154              
155             #use Data::Dumper;
156             #print STDERR "\n# data=" . Dumper($data);
157              
158 1 50       4 return $self->handler("Could not extract data from the Barnes and Noble result page.")
159             unless(defined $data);
160              
161             # trim top and tail
162 1         6 foreach (keys %$data) {
163 15 100       20 next unless(defined $data->{$_});
164 14         21 $data->{$_} =~ s/^\s+//;
165 14         31 $data->{$_} =~ s/\s+$//;
166             }
167              
168 1         8 $url = $mech->uri();
169              
170 1         44 my $bk = {
171             'ean13' => $data->{isbn13},
172             'isbn13' => $data->{isbn13},
173             'isbn10' => $data->{isbn10},
174             'isbn' => $data->{isbn13},
175             'author' => $data->{author},
176             'title' => $data->{title},
177             'book_link' => $url,
178             'image_link' => $data->{image},
179             'thumb_link' => $data->{thumb},
180             'description' => $data->{description},
181             'pubdate' => $data->{pubdate},
182             'publisher' => $data->{publisher},
183             'binding' => $data->{binding},
184             'pages' => $data->{pages},
185             'weight' => $data->{weight},
186             'width' => $data->{width},
187             'height' => $data->{height},
188             'depth' => $data->{depth},
189             'html' => $html
190             };
191              
192             #use Data::Dumper;
193             #print STDERR "\n# book=".Dumper($bk);
194              
195 1         4 $self->book($bk);
196 1         22 $self->found(1);
197 1         8 return $self->book;
198             }
199              
200             1;
201              
202             __END__