blib/lib/WWW/Scraper/ISBN/WordPower_Driver.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 79 | 79 | 100.0 |
branch | 15 | 24 | 62.5 |
condition | 10 | 24 | 41.6 |
subroutine | 8 | 8 | 100.0 |
pod | 1 | 1 | 100.0 |
total | 113 | 136 | 83.0 |
line | stmt | bran | cond | sub | pod | time | code | ||
---|---|---|---|---|---|---|---|---|---|
1 | package WWW::Scraper::ISBN::WordPower_Driver; | ||||||||
2 | |||||||||
3 | 6 | 6 | 191078 | use strict; | |||||
6 | 19 | ||||||||
6 | 270 | ||||||||
4 | 6 | 6 | 36 | use warnings; | |||||
6 | 21 | ||||||||
6 | 260 | ||||||||
5 | |||||||||
6 | 6 | 6 | 34 | use vars qw($VERSION @ISA); | |||||
6 | 16 | ||||||||
6 | 671 | ||||||||
7 | $VERSION = '0.09'; | ||||||||
8 | |||||||||
9 | #-------------------------------------------------------------------------- | ||||||||
10 | |||||||||
11 | =head1 NAME | ||||||||
12 | |||||||||
13 | WWW::Scraper::ISBN::WordPower_Driver - Search driver for Word Power 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 Word Power online book catalog | ||||||||
22 | |||||||||
23 | =cut | ||||||||
24 | |||||||||
25 | #-------------------------------------------------------------------------- | ||||||||
26 | |||||||||
27 | ########################################################################### | ||||||||
28 | # Inheritence | ||||||||
29 | |||||||||
30 | 6 | 6 | 36 | use base qw(WWW::Scraper::ISBN::Driver); | |||||
6 | 17 | ||||||||
6 | 6481 | ||||||||
31 | |||||||||
32 | ########################################################################### | ||||||||
33 | # Modules | ||||||||
34 | |||||||||
35 | 6 | 6 | 20314 | use WWW::Mechanize; | |||||
6 | 1380278 | ||||||||
6 | 303 | ||||||||
36 | |||||||||
37 | ########################################################################### | ||||||||
38 | # Constants | ||||||||
39 | |||||||||
40 | 6 | 6 | 78 | use constant SEARCH => 'http://www.word-power.co.uk/searchBook.php?options=isbn&imageField.x=19&imageField.y=9&keywords='; | |||||
6 | 11 | ||||||||
6 | 9006 | ||||||||
41 | my ($URL1,$URL2,$URL3) = ('http://www.word-power.co.uk','/books/[^>]+-I','/'); | ||||||||
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 Word | ||||||||
55 | Power 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 Word Power website. | ||||||||
79 | |||||||||
80 | =back | ||||||||
81 | |||||||||
82 | =cut | ||||||||
83 | |||||||||
84 | sub search { | ||||||||
85 | 4 | 4 | 1 | 27214 | my $self = shift; | ||||
86 | 4 | 13 | my $isbn = shift; | ||||||
87 | 4 | 21 | $self->found(0); | ||||||
88 | 4 | 65 | $self->book(undef); | ||||||
89 | |||||||||
90 | # validate and convert into EAN13 format | ||||||||
91 | 4 | 46 | my $ean = $self->convert_to_ean13($isbn); | ||||||
92 | 4 | 50 | 66 | 208 | return $self->handler("Invalid ISBN specified [$isbn]") | ||||
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 | 74 | my $mech = WWW::Mechanize->new(); | ||||||
97 | 4 | 20767 | $mech->agent_alias( 'Linux Mozilla' ); | ||||||
98 | 4 | 279 | $mech->add_header('Accept-Encoding' => undef); | ||||||
99 | |||||||||
100 | 4 | 44 | eval { $mech->get( SEARCH . $isbn ) }; | ||||||
4 | 23 | ||||||||
101 | 4 | 50 | 33 | 280251 | return $self->handler("WordPower website appears to be unavailable.") | ||||
33 | |||||||||
102 | if($@ || !$mech->success() || !$mech->content()); | ||||||||
103 | |||||||||
104 | 4 | 256 | my $content = $mech->content; | ||||||
105 | 4 | 1196 | my ($link) = $content =~ m!($URL2$ean$URL3)!si; | ||||||
106 | #print STDERR "\n# link1=[$URL2$ean$URL3]\n"; | ||||||||
107 | #print STDERR "\n# link2=[$URL1$link]\n"; | ||||||||
108 | #print STDERR "\n# content1=[\n$content\n]\n"; | ||||||||
109 | #print STDERR "\n# is_html=".$mech->is_html().", content type=".$mech->content_type()."\n"; | ||||||||
110 | #print STDERR "\n# dump headers=".$mech->dump_headers."\n"; | ||||||||
111 | |||||||||
112 | 4 | 100 | 31 | return $self->handler("Failed to find that book on WordPower website.") | |||||
113 | unless($link); | ||||||||
114 | |||||||||
115 | 3 | 8 | eval { $mech->get( $URL1 . $link ) }; | ||||||
3 | 24 | ||||||||
116 | 3 | 50 | 33 | 151646 | return $self->handler("WordPower website appears to be unavailable.") | ||||
33 | |||||||||
117 | if($@ || !$mech->success() || !$mech->content()); | ||||||||
118 | |||||||||
119 | # The Book page | ||||||||
120 | 3 | 236 | my $html = $mech->content(); | ||||||
121 | |||||||||
122 | 3 | 50 | 1 | 4890 | return $self->handler("Failed to find that book on WordPower website. [$isbn]") | ||||
1 | 16 | ||||||||
1 | 2 | ||||||||
1 | 21 | ||||||||
123 | if($html =~ m!Sorry, we couldn't find any matches for!si); | ||||||||
124 | |||||||||
125 | #print STDERR "\n# content2=[\n$html\n]\n"; | ||||||||
126 | |||||||||
127 | 3 | 39028 | my $data; | ||||||
128 | 3 | 51 | ($data->{isbn10}) = $self->convert_to_isbn10($ean); | ||||||
129 | 3 | 4234 | ($data->{publisher}) = $html =~ m! | ]+>Publisher | \s*]+>]+>([^<]+) | !i;||||
130 | 3 | 4762 | ($data->{pubdate}) = $html =~ m! | ]+>Publication date | \s*]+>([^<]+) | !i;||||
131 | |||||||||
132 | 3 | 50 | 35 | $data->{publisher} =~ s!<[^>]+>!!g if($data->{publisher}); | |||||
133 | 3 | 50 | 60 | $data->{pubdate} =~ s!\s+! !g if($data->{pubdate}); | |||||
134 | |||||||||
135 | 3 | 4371 | ($data->{isbn13}) = $html =~ m! | ]+>ISBN13 | \s*]+>([^<]+) | !i;||||
136 | 3 | 4829 | ($data->{isbn10}) = $html =~ m! | ]+>ISBN | \s*]+>([^<]+) | !i;||||
137 | 3 | 1062 | ($data->{image}) = $html =~ m!"(http://.*?/product_images/$data->{isbn13}.jpg)"!i; | ||||||
138 | 3 | 1009 | ($data->{thumb}) = $html =~ m!"(http://.*?/product_images/$data->{isbn13}.jpg)"!i; | ||||||
139 | 3 | 3849 | ($data->{author}) = $html =~ m!by\s*([^<]+)!i; | ||||||
140 | 3 | 2615 | ($data->{title}) = $html =~ m! ([^<]+)!i; |
||||||
141 | 3 | 4277 | ($data->{description}) = $html =~ m! \s* ([^~]+) !si; |
||||||
142 | 3 | 368 | ($data->{binding}) = $html =~ m! | ]+>Format | \s*]+>([^<]+) | !s;||||
143 | 3 | 133 | ($data->{pages}) = $html =~ m! | ||||||
Pages | \s*([\d.]+) | \s*||||||||
144 | 3 | 170 | ($data->{weight}) = $html =~ m! | ||||||
Weight .grammes. | \s*([\d.]+) | \s*||||||||
145 | 3 | 381 | ($data->{width}) = $html =~ m! | ]+>Width \(mm\) | \s*]+>([^<]+) | !s;||||
146 | 3 | 383 | ($data->{height}) = $html =~ m! | ]+>Height \(mm\) | \s*]+>([^<]+) | !s;||||
147 | |||||||||
148 | 3 | 40 | for my $key (qw(weight width height)) { | ||||||
149 | 9 | 50 | 34 | next unless($data->{$key}); | |||||
150 | 9 | 42 | $data->{$key} =~ s/\.0+$//; | ||||||
151 | } | ||||||||
152 | |||||||||
153 | 3 | 100 | 20 | $data->{author} =~ s!<[^>]+>!!g if($data->{author}); | |||||
154 | 3 | 50 | 14 | if($data->{description}) { | |||||
155 | 3 | 439 | $data->{description} =~ s! | ||||||
156 | 3 | 77 | $data->{description} =~ s!<(p|br\s*/)>!\n!g; | ||||||
157 | 3 | 57 | $data->{description} =~ s!<[^>]+>!!gs; | ||||||
158 | 3 | 67 | $data->{description} =~ s! +$!!gm; | ||||||
159 | 3 | 31 | $data->{description} =~ s!\n\n!\n!gs; | ||||||
160 | } | ||||||||
161 | |||||||||
162 | #use Data::Dumper; | ||||||||
163 | #print STDERR "\n# " . Dumper($data); | ||||||||
164 | |||||||||
165 | 3 | 50 | 17 | return $self->handler("Could not extract data from WordPower result page.") | |||||
166 | unless(defined $data); | ||||||||
167 | |||||||||
168 | # trim top and tail | ||||||||
169 | 3 | 26 | foreach (keys %$data) { | ||||||
170 | 42 | 100 | 102 | next unless(defined $data->{$_}); | |||||
171 | 40 | 110 | $data->{$_} =~ s! ! !g; | ||||||
172 | 40 | 106 | $data->{$_} =~ s/^\s+//; | ||||||
173 | 40 | 186 | $data->{$_} =~ s/\s+$//; | ||||||
174 | } | ||||||||
175 | |||||||||
176 | 3 | 50 | my $bk = { | ||||||
177 | 'ean13' => $data->{isbn13}, | ||||||||
178 | 'isbn13' => $data->{isbn13}, | ||||||||
179 | 'isbn10' => $data->{isbn10}, | ||||||||
180 | 'isbn' => $data->{isbn13}, | ||||||||
181 | 'author' => $data->{author}, | ||||||||
182 | 'title' => $data->{title}, | ||||||||
183 | 'book_link' => $mech->uri(), | ||||||||
184 | 'image_link' => $data->{image}, | ||||||||
185 | 'thumb_link' => $data->{thumb}, | ||||||||
186 | 'description' => $data->{description}, | ||||||||
187 | 'pubdate' => $data->{pubdate}, | ||||||||
188 | 'publisher' => $data->{publisher}, | ||||||||
189 | 'binding' => $data->{binding}, | ||||||||
190 | 'pages' => $data->{pages}, | ||||||||
191 | 'weight' => $data->{weight}, | ||||||||
192 | 'width' => $data->{width}, | ||||||||
193 | 'height' => $data->{height}, | ||||||||
194 | 'html' => $html | ||||||||
195 | }; | ||||||||
196 | |||||||||
197 | #use Data::Dumper; | ||||||||
198 | #print STDERR "\n# book=".Dumper($bk); | ||||||||
199 | |||||||||
200 | 3 | 227 | $self->book($bk); | ||||||
201 | 3 | 68 | $self->found(1); | ||||||
202 | 3 | 36 | return $self->book; | ||||||
203 | } | ||||||||
204 | |||||||||
205 | 1; | ||||||||
206 | |||||||||
207 | __END__ |