File Coverage

blib/lib/WWW/Scraper/ISBN/Pearson_Driver.pm
Criterion Covered Total %
statement 56 56 100.0
branch 7 12 58.3
condition 4 12 33.3
subroutine 8 8 100.0
pod 1 1 100.0
total 76 89 85.3


line stmt bran cond sub pod time code
1             package WWW::Scraper::ISBN::Pearson_Driver;
2              
3 6     6   96794 use strict;
  6         14  
  6         212  
4 6     6   19 use warnings;
  6         7  
  6         189  
5              
6 6     6   21 use vars qw($VERSION @ISA);
  6         12  
  6         504  
7             $VERSION = '0.22';
8              
9             #--------------------------------------------------------------------------
10              
11             =head1 NAME
12              
13             WWW::Scraper::ISBN::Pearson_Driver - Search driver for the Pearson Education 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 Pearson Education's online catalog.
22              
23             =cut
24              
25             #--------------------------------------------------------------------------
26              
27             ###########################################################################
28             # Inheritence
29              
30 6     6   26 use base qw(WWW::Scraper::ISBN::Driver);
  6         7  
  6         2784  
31              
32             ###########################################################################
33             # Modules
34              
35 6     6   8929 use WWW::Mechanize;
  6         772507  
  6         246  
36              
37             ###########################################################################
38             # Constants
39              
40 6     6   50 use constant SEARCH => 'http://www.pearsoned.co.uk/Bookshop/';
  6         8  
  6         402  
41 6     6   28 use constant DETAIL => 'http://www.pearsoned.co.uk/Bookshop/detail.asp?item=';
  6         7  
  6         3370  
42              
43             #--------------------------------------------------------------------------
44              
45             ###########################################################################
46             # Public Interface
47              
48             =head1 METHODS
49              
50             =over 4
51              
52             =item C
53              
54             Creates a query string, then passes the appropriate form fields to the Pearson
55             Education server.
56              
57             The returned page should be the correct catalog page for that ISBN. If not the
58             function returns zero and allows the next driver in the chain to have a go. If
59             a valid page is returned, the following fields are returned via the book hash:
60              
61             isbn (now returns isbn13)
62             isbn10
63             isbn13
64             ean13 (industry name)
65             author
66             title
67             book_link
68             image_link
69             description
70             pubdate
71             publisher
72             binding (if known)
73             pages (if known)
74             weight (if known) (in grammes)
75             width (if known) (in millimetres)
76             height (if known) (in millimetres)
77              
78             The book_link and image_link refer back to the Pearson Education UK website.
79              
80             =back
81              
82             =cut
83              
84             sub search {
85 2     2 1 7328 my $self = shift;
86 2         4 my $isbn = shift;
87 2         10 $self->found(0);
88 2         28 $self->book(undef);
89              
90 2         24 my $mech = WWW::Mechanize->new();
91 2         12724 $mech->agent_alias( 'Linux Mozilla' );
92              
93 2         121 eval { $mech->get( SEARCH ) };
  2         7  
94 2 50 33     428350 return $self->handler("Pearson Education website appears to be unavailable.")
      33        
95             if($@ || !$mech->success() || !$mech->content());
96              
97             #print STDERR "\n# content=[\n".$mech->content()."\n]\n";
98              
99 2         144 $mech->form_id('hipGlobalSearchForm');
100 2         17905 $mech->set_fields( 'txtSearch' => $isbn );
101            
102 2         166 eval { $mech->submit() };
  2         12  
103 2 50 33     1293616 return $self->handler("Failed to find that book on Pearson Education website.")
      33        
104             if($@ || !$mech->success() || !$mech->content());
105              
106             # The Book page
107 2         106 my $html = $mech->content();
108             #print STDERR "\n# html=[\n$html\n]\n";
109              
110 2 50       1583 return $self->handler("Failed to find that book on Pearson Education website.")
111             if($html =~ m!

Your search for \d+ returned 0 results. Please search again.

!si);
112              
113 2         4 my $data;
114 2         196 ($data->{image}) = $html =~ m!"(http://images.pearsoned-ema.com/jpeg/large/\d+\.jpg)"!i;
115 2         190 ($data->{thumb}) = $html =~ m!"(http://images.pearsoned-ema.com/jpeg/small/\d+\.jpg)"!i;
116 2         1384 ($data->{title},
117             $data->{author},
118             $data->{pubdate},
119             $data->{binding},
120             $data->{pages},
121             $data->{isbn13},
122             $data->{isbn10}) = $html =~ m! \s*
123             (.*?)\s*(?:.*?\s*)?
124             ]+>(.*?)\s*\s*
125             ([^,]+),\s*([^,]+)(?:,\s*(\d+)\s+pages)?\s*
126             ISBN13:\s*(\d+)\s*\s*
127             ISBN10:\s*([\dX]+)\s*!ix;
128 2         1245 ($data->{description}) = $html =~ m!

([^<]+)!is;

129 2         838 ($data->{bookid}) = $html =~ m!recommend.asp\?item=(\d+)!i;
130              
131             #use Data::Dumper;
132             #print STDERR "\n# " . Dumper($data);
133              
134 2 50       10 return $self->handler("Could not extract data from Pearson Education result page.")
135             unless(defined $data);
136              
137             # remove HTML tags
138 2         6 for(qw(author binding)) {
139 4 50       11 next unless(defined $data->{$_});
140 4         37 $data->{$_} =~ s!<[^>]+>!!g;
141             }
142              
143             # trim top and tail
144 2         12 for(keys %$data) {
145 22 100       36 next unless(defined $data->{$_});
146 21         36 $data->{$_} =~ s/^\s+//;
147 21         69 $data->{$_} =~ s/\s+$//;
148             }
149              
150 2         15 my $uri = $mech->uri(); #DETAIL . $data->{bookid};
151              
152 2         61 my $bk = {
153             'ean13' => $data->{isbn13},
154             'isbn13' => $data->{isbn13},
155             'isbn10' => $data->{isbn10},
156             'isbn' => $data->{isbn13},
157             'author' => $data->{author},
158             'title' => $data->{title},
159             'book_link' => "$uri",
160             'image_link' => $data->{image},
161             'thumb_link' => $data->{thumb},
162             'description' => $data->{description},
163             'pubdate' => $data->{pubdate},
164             'publisher' => q!Pearson Education!,
165             'binding' => $data->{binding},
166             'pages' => $data->{pages},
167             'weight' => $data->{weight},
168             'width' => $data->{width},
169             'height' => $data->{height},
170             'html' => $html
171             };
172              
173             #use Data::Dumper;
174             #print STDERR "\n# book=".Dumper($bk);
175              
176 2         47 $self->book($bk);
177 2         32 $self->found(1);
178 2         15 return $self->book;
179             }
180              
181             1;
182             __END__