blib/lib/WWW/Scraper/ISBN/Pearson_Driver.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 21 | 56 | 37.5 |
branch | 0 | 12 | 0.0 |
condition | 0 | 12 | 0.0 |
subroutine | 7 | 8 | 87.5 |
pod | 1 | 1 | 100.0 |
total | 29 | 89 | 32.5 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package WWW::Scraper::ISBN::Pearson_Driver; | ||||||
2 | |||||||
3 | 5 | 5 | 287119 | use strict; | |||
5 | 41 | ||||||
5 | 149 | ||||||
4 | 5 | 5 | 27 | use warnings; | |||
5 | 11 | ||||||
5 | 182 | ||||||
5 | |||||||
6 | 5 | 5 | 35 | use vars qw($VERSION @ISA); | |||
5 | 10 | ||||||
5 | 510 | ||||||
7 | $VERSION = '0.24'; | ||||||
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 | 5 | 5 | 38 | use base qw(WWW::Scraper::ISBN::Driver); | |||
5 | 10 | ||||||
5 | 2632 | ||||||
31 | |||||||
32 | ########################################################################### | ||||||
33 | # Modules | ||||||
34 | |||||||
35 | 5 | 5 | 9152 | use WWW::Mechanize; | |||
5 | 765235 | ||||||
5 | 265 | ||||||
36 | |||||||
37 | ########################################################################### | ||||||
38 | # Constants | ||||||
39 | |||||||
40 | 5 | 5 | 48 | use constant SEARCH => 'http://www.pearsoned.co.uk/Bookshop/'; | |||
5 | 13 | ||||||
5 | 337 | ||||||
41 | 5 | 5 | 32 | use constant DETAIL => 'http://www.pearsoned.co.uk/Bookshop/detail.asp?item='; | |||
5 | 13 | ||||||
5 | 3288 | ||||||
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 | 0 | 0 | 1 | my $self = shift; | |||
86 | 0 | my $isbn = shift; | |||||
87 | 0 | $self->found(0); | |||||
88 | 0 | $self->book(undef); | |||||
89 | |||||||
90 | 0 | my $mech = WWW::Mechanize->new(); | |||||
91 | 0 | $mech->agent_alias( 'Linux Mozilla' ); | |||||
92 | |||||||
93 | 0 | eval { $mech->get( SEARCH ) }; | |||||
0 | |||||||
94 | 0 | 0 | 0 | return $self->handler("Pearson Education website appears to be unavailable.") | |||
0 | |||||||
95 | if($@ || !$mech->success() || !$mech->content()); | ||||||
96 | |||||||
97 | #print STDERR "\n# content=[\n".$mech->content()."\n]\n"; | ||||||
98 | |||||||
99 | 0 | $mech->form_id('hipGlobalSearchForm'); | |||||
100 | 0 | $mech->set_fields( 'txtSearch' => $isbn ); | |||||
101 | |||||||
102 | 0 | eval { $mech->submit() }; | |||||
0 | |||||||
103 | 0 | 0 | 0 | return $self->handler("Failed to find that book on Pearson Education website.") | |||
0 | |||||||
104 | if($@ || !$mech->success() || !$mech->content()); | ||||||
105 | |||||||
106 | # The Book page | ||||||
107 | 0 | my $html = $mech->content(); | |||||
108 | #print STDERR "\n# html=[\n$html\n]\n"; | ||||||
109 | |||||||
110 | 0 | 0 | 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 | 0 | my $data; | |||||
114 | 0 | ($data->{image}) = $html =~ m!"(http://images.pearsoned-ema.com/jpeg/large/\d+\.jpg)"!i; | |||||
115 | 0 | ($data->{thumb}) = $html =~ m!"(http://images.pearsoned-ema.com/jpeg/small/\d+\.jpg)"!i; | |||||
116 | ($data->{title}, | ||||||
117 | $data->{author}, | ||||||
118 | $data->{pubdate}, | ||||||
119 | $data->{binding}, | ||||||
120 | $data->{pages}, | ||||||
121 | 0 | $data->{isbn13}) = $html =~ m! \s* |
|||||
122 | (.*?)\s*(?:.*?\s*)? |
||||||
123 | ]+>(.*?)\s*\s* |
||||||
124 | ([^,]+),\s*([^,]+)(?:,\s*(\d+)\s+pages)?(?:)? \s* |
||||||
125 | ISBN(?:13)?:\s*(\d+)\s* !ix; |
||||||
126 | 0 | ($data->{description}) = $html =~ m! ([^<]+)!is; |
|||||
127 | 0 | ($data->{bookid}) = $html =~ m!recommend.asp\?item=(\d+)!i; | |||||
128 | |||||||
129 | #use Data::Dumper; | ||||||
130 | #print STDERR "\n# " . Dumper($data); | ||||||
131 | |||||||
132 | 0 | 0 | return $self->handler("Could not extract data from Pearson Education result page.") | ||||
133 | unless(defined $data); | ||||||
134 | |||||||
135 | # remove HTML tags | ||||||
136 | 0 | for(qw(author binding)) { | |||||
137 | 0 | 0 | next unless(defined $data->{$_}); | ||||
138 | 0 | $data->{$_} =~ s!<[^>]+>!!g; | |||||
139 | } | ||||||
140 | |||||||
141 | # trim top and tail | ||||||
142 | 0 | for(keys %$data) { | |||||
143 | 0 | 0 | next unless(defined $data->{$_}); | ||||
144 | 0 | $data->{$_} =~ s/^\s+//; | |||||
145 | 0 | $data->{$_} =~ s/\s+$//; | |||||
146 | } | ||||||
147 | |||||||
148 | 0 | my $uri = $mech->uri(); #DETAIL . $data->{bookid}; | |||||
149 | |||||||
150 | my $bk = { | ||||||
151 | 'ean13' => $data->{isbn13}, | ||||||
152 | 'isbn13' => $data->{isbn13}, | ||||||
153 | 'isbn10' => $self->convert_to_isbn10( $data->{isbn13} ), | ||||||
154 | 'isbn' => $data->{isbn13}, | ||||||
155 | 'author' => $data->{author}, | ||||||
156 | 'title' => $data->{title}, | ||||||
157 | 'book_link' => "$uri", | ||||||
158 | 'image_link' => $data->{image}, | ||||||
159 | 'thumb_link' => $data->{thumb}, | ||||||
160 | 'description' => $data->{description}, | ||||||
161 | 'pubdate' => $data->{pubdate}, | ||||||
162 | 'publisher' => q!Pearson Education!, | ||||||
163 | 'binding' => $data->{binding}, | ||||||
164 | 'pages' => $data->{pages}, | ||||||
165 | 'weight' => $data->{weight}, | ||||||
166 | 'width' => $data->{width}, | ||||||
167 | 'height' => $data->{height}, | ||||||
168 | 0 | 'html' => $html | |||||
169 | }; | ||||||
170 | |||||||
171 | #use Data::Dumper; | ||||||
172 | #print STDERR "\n# book=".Dumper($bk); | ||||||
173 | |||||||
174 | 0 | $self->book($bk); | |||||
175 | 0 | $self->found(1); | |||||
176 | 0 | return $self->book; | |||||
177 | } | ||||||
178 | |||||||
179 | 1; | ||||||
180 | __END__ |