File Coverage

blib/lib/WWW/Scraper/ISBN/PickABook_Driver.pm
Criterion Covered Total %
statement 68 68 100.0
branch 10 18 55.5
condition 10 21 47.6
subroutine 8 8 100.0
pod 1 1 100.0
total 97 116 83.6


line stmt bran cond sub pod time code
1             package WWW::Scraper::ISBN::PickABook_Driver;
2              
3 6     6   209818 use strict;
  6         20  
  6         257  
4 6     6   54 use warnings;
  6         65  
  6         340  
5              
6 6     6   34 use vars qw($VERSION @ISA);
  6         19  
  6         589  
7             $VERSION = '0.06';
8              
9             #--------------------------------------------------------------------------
10              
11             =head1 NAME
12              
13             WWW::Scraper::ISBN::PickABook_Driver - Search driver for the Pick-A-Book 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 Pick-A-Book online book catalog.
22              
23             =cut
24              
25             #--------------------------------------------------------------------------
26              
27             ###########################################################################
28             # Inheritence
29              
30 6     6   34 use base qw(WWW::Scraper::ISBN::Driver);
  6         11  
  6         6140  
31              
32             ###########################################################################
33             # Modules
34              
35 6     6   15840 use WWW::Mechanize;
  6         1220139  
  6         298  
36              
37             ###########################################################################
38             # Constants
39              
40 6     6   72 use constant REFERER => 'http://www.pickabook.co.uk/';
  6         40  
  6         445  
41 6     6   31 use constant SEARCH => 'http://www.pickabook.co.uk/%s.aspx?ToSearch=TRUE';
  6         12  
  6         6921  
42             my ($URL1,$URL2) = ('http://www.PickABook.co.uk/book/','/[^?]+\?b=\-3\&t=\-26\#Bibliographicdata\-26');
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             Book Depository 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             thumb_link
71             description
72             pubdate
73             publisher
74             binding (if known)
75             pages (if known)
76              
77             The book_link, image_link and thumb_link all refer back to the Pick-A-Book
78             website.
79              
80             =back
81              
82             =cut
83              
84             sub search {
85 4     4 1 19408 my $self = shift;
86 4         8 my $isbn = shift;
87 4         16 $self->found(0);
88 4         57 $self->book(undef);
89              
90             # validate and convert into EAN13 format
91 4         38 my $ean = $self->convert_to_ean13($isbn);
92 4 50 66     182 return $self->handler("Invalid ISBN specified")
      33        
      66        
      33        
93             if(!$ean || (length $isbn == 13 && $isbn ne $ean)
94             || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)));
95              
96 4         112 my $mech = WWW::Mechanize->new();
97 4         17279 $mech->agent_alias( 'Windows IE 6' );
98 4         257 $mech->add_header( 'Accept-Encoding' => undef );
99 4         47 $mech->add_header( 'Referer' => REFERER );
100              
101 4         52 my $search = sprintf SEARCH , $ean;
102             #print STDERR "\n# search=[$search]\n";
103              
104 4         8 eval { $mech->get( $search ) };
  4         28  
105 4 50 33     729392 return $self->handler("The Pick-A-Book website appears to be unavailable.")
      33        
106             if($@ || !$mech->success() || !$mech->content());
107              
108             # The Book page
109 4         357 my $html = $mech->content();
110              
111 4 100 66     24214 return $self->handler("Failed to find that book on the Pick-A-Book website. [$isbn]")
112             if(!$html || $html =~ m!Your search for "ISBN = \d+"\s+has produced 0 results.!si);
113              
114 3         3927 $html =~ s/&/&/g;
115             #print STDERR "\n# content2=[\n$html\n]\n";
116              
117 3         14 my $data;
118 3         19002 ($data->{author},$data->{title})
119             = $html =~ m!]*>([^<]+)!si;
120 3         10968 ($data->{binding}) = $html =~ m!]*>([^<]+)!si;
121 3         16970 ($data->{isbn13}) = $html =~ m!]*>([^<]+)!si;
122 3         14320 ($data->{isbn10}) = $html =~ m!]*>([^<]+)!si;
123 3         13653 ($data->{publisher}) = $html =~ m!]*>([^<]+)!si;
124 3         13094 ($data->{pubdate}) = $html =~ m!]*>([^<]+)!si;
125 3         12188 ($data->{pages}) = $html =~ m!]*>([^<]+)!si;
126 3         14256 ($data->{description}) = $html =~ m!]*>([^<]+)!si;
127 3         11152 ($data->{isbn10}) = $html =~ m!]*>([^<]+)!si;
128 3         36432 ($data->{image}) = $html =~ m!]*>!si;
129              
130 3 50       32 if($data->{image}) {
131 3         21 $data->{image} = REFERER . $data->{image};
132 3         12 $data->{thumb} = $data->{image};
133             }
134 3 50       57 $data->{author} =~ s/.*?\s+by\s+// if($data->{author});
135 3 50       23 $data->{publisher} =~ s/�?39;/'/g if($data->{publisher});
136 3         11 for(qw(title author publisher)) {
137 9 50       27 next unless($data->{$_});
138 9         167 $data->{$_} =~ s/([\w']+)/\u\L$1/g;
139             }
140              
141             #use Data::Dumper;
142             #print STDERR "\n# " . Dumper($data);
143              
144 3 50       13 return $self->handler("Could not extract data from the Pick-A-Book result page.")
145             unless(defined $data);
146              
147             # trim top and tail
148 3         23 foreach (keys %$data) {
149 33 50       68 next unless(defined $data->{$_});
150 33         56 $data->{$_} =~ s! ! !g;
151 33         61 $data->{$_} =~ s/^\s+//;
152 33         118 $data->{$_} =~ s/\s+$//;
153             }
154              
155 3         37 my $url = $mech->uri();
156              
157 3         158 my $bk = {
158             'ean13' => $data->{isbn13},
159             'isbn13' => $data->{isbn13},
160             'isbn10' => $data->{isbn10},
161             'isbn' => $data->{isbn13},
162             'author' => $data->{author},
163             'title' => $data->{title},
164             'book_link' => "$url",
165             'image_link' => $data->{image},
166             'thumb_link' => $data->{thumb},
167             'description' => $data->{description},
168             'pubdate' => $data->{pubdate},
169             'publisher' => $data->{publisher},
170             'binding' => $data->{binding},
171             'pages' => $data->{pages},
172             'html' => $html
173             };
174              
175             #use Data::Dumper;
176             #print STDERR "\n# book=".Dumper($bk);
177              
178 3         84 $self->book($bk);
179 3         43 $self->found(1);
180 3         30 return $self->book;
181             }
182              
183             1;
184              
185             __END__