blib/lib/WWW/Scraper/ISBN/Booktopia_Driver.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 18 | 80 | 22.5 |
branch | 0 | 36 | 0.0 |
condition | 0 | 18 | 0.0 |
subroutine | 6 | 7 | 85.7 |
pod | 1 | 1 | 100.0 |
total | 25 | 142 | 17.6 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package WWW::Scraper::ISBN::Booktopia_Driver; | ||||||
2 | |||||||
3 | 5 | 5 | 234384 | use strict; | |||
5 | 31 | ||||||
5 | 137 | ||||||
4 | 5 | 5 | 22 | use warnings; | |||
5 | 7 | ||||||
5 | 148 | ||||||
5 | |||||||
6 | 5 | 5 | 25 | use vars qw($VERSION @ISA); | |||
5 | 10 | ||||||
5 | 399 | ||||||
7 | $VERSION = '0.23'; | ||||||
8 | |||||||
9 | #-------------------------------------------------------------------------- | ||||||
10 | |||||||
11 | =head1 NAME | ||||||
12 | |||||||
13 | WWW::Scraper::ISBN::Booktopia_Driver - Search driver for Booktopia 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 Booktopia online book catalog | ||||||
22 | |||||||
23 | =cut | ||||||
24 | |||||||
25 | #-------------------------------------------------------------------------- | ||||||
26 | |||||||
27 | ########################################################################### | ||||||
28 | # Inheritence | ||||||
29 | |||||||
30 | 5 | 5 | 29 | use base qw(WWW::Scraper::ISBN::Driver); | |||
5 | 8 | ||||||
5 | 2209 | ||||||
31 | |||||||
32 | ########################################################################### | ||||||
33 | # Modules | ||||||
34 | |||||||
35 | 5 | 5 | 7494 | use WWW::Mechanize; | |||
5 | 621633 | ||||||
5 | 197 | ||||||
36 | |||||||
37 | ########################################################################### | ||||||
38 | # Constants | ||||||
39 | |||||||
40 | 5 | 5 | 39 | use constant SEARCH => 'http://www.booktopia.com.au/search.ep?cID=&submit.x=44&submit.y=7&submit=search&keywords='; | |||
5 | 18 | ||||||
5 | 5469 | ||||||
41 | my ($BAU_URL1,$BAU_URL2,$BAU_URL3) = ('http://www.booktopia.com.au','/[^/]+/prod','.html'); | ||||||
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 | ||||||
55 | Booktopia 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 | depth (if known) (in millimetres) | ||||||
78 | |||||||
79 | The book_link and image_link refer back to the Booktopia website. | ||||||
80 | |||||||
81 | =back | ||||||
82 | |||||||
83 | =cut | ||||||
84 | |||||||
85 | sub search { | ||||||
86 | 0 | 0 | 1 | my $self = shift; | |||
87 | 0 | my $isbn = shift; | |||||
88 | 0 | $self->found(0); | |||||
89 | 0 | $self->book(undef); | |||||
90 | |||||||
91 | # validate and convert into EAN13 format | ||||||
92 | 0 | my $ean = $self->convert_to_ean13($isbn); | |||||
93 | 0 | 0 | 0 | return $self->handler("Invalid ISBN specified [$isbn]") | |||
0 | |||||||
0 | |||||||
0 | |||||||
94 | if(!$ean || (length $isbn == 13 && $isbn ne $ean) | ||||||
95 | || (length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean))); | ||||||
96 | |||||||
97 | #print STDERR "\n# isbn=[$isbn] => ean=[$ean]\n"; | ||||||
98 | 0 | $isbn = $ean; | |||||
99 | |||||||
100 | 0 | my $mech = WWW::Mechanize->new(); | |||||
101 | 0 | $mech->agent_alias( 'Linux Mozilla' ); | |||||
102 | |||||||
103 | #print STDERR "\n# url=[".(SEARCH . $isbn)."]\n"; | ||||||
104 | |||||||
105 | 0 | eval { $mech->get( SEARCH . $isbn ) }; | |||||
0 | |||||||
106 | 0 | 0 | 0 | return $self->handler("Booktopia website appears to be unavailable.") | |||
0 | |||||||
107 | if($@ || !$mech->success() || !$mech->content()); | ||||||
108 | |||||||
109 | 0 | my $pattern = $isbn; | |||||
110 | 0 | 0 | if(length $isbn == 10) { | ||||
111 | 0 | $pattern = '978' . $isbn; | |||||
112 | 0 | $pattern =~ s/.$/./; | |||||
113 | } | ||||||
114 | |||||||
115 | # The Book page | ||||||
116 | 0 | my $html = $mech->content(); | |||||
117 | |||||||
118 | 0 | 0 | return $self->handler("Failed to find that book on Booktopia website. [$isbn]") | ||||
119 | if($html =~ m!Sorry, we couldn't find any matches for!si); | ||||||
120 | |||||||
121 | #print STDERR "\n# html=[\n$html\n]\n"; | ||||||
122 | |||||||
123 | 0 | my $data; | |||||
124 | 0 | ($data->{publisher}) = $html =~ m!Publisher:([^<]+) !si; |
|||||
125 | 0 | ($data->{pubdate}) = $html =~ m!\s*Published:\s*\s*([^<]+)!si; | |||||
126 | |||||||
127 | 0 | 0 | $data->{publisher} =~ s!<[^>]+>!!g if($data->{publisher}); | ||||
128 | 0 | 0 | $data->{pubdate} =~ s!\s+! !g if($data->{pubdate}); | ||||
129 | |||||||
130 | 0 | ($data->{image}) = $html =~ m!(https://www.booktopia.com.au/http_coversbooktopiacomau/big/\d+/[-\w.]+\.jpg)"!si; | |||||
131 | 0 | ($data->{thumb}) = $html =~ m!(https://www.booktopia.com.au/http_coversbooktopiacomau/\d+/\d+/[-\w.]+\.jpg)"!si; | |||||
132 | 0 | ($data->{isbn13}) = $html =~ m!\s*ISBN:\s*\s*(\d+)!si; | |||||
133 | 0 | ($data->{isbn10}) = $html =~ m!\s*ISBN-10:\s*\s*(\d+)!si; | |||||
134 | 0 | ($data->{author}) = $html =~ m! \s*(?:By|Author):\s*(.*?) !si; |
|||||
135 | 0 | ($data->{title}) = $html =~ m! | |||||
136 | 0 | 0 | ($data->{title}) = $html =~ m!([^<]+) !si unless($data->{title}); |
||||
137 | 0 | ($data->{description}) = $html =~ m! (.*?) !si; |
\s*|||||
138 | 0 | ($data->{binding}) = $html =~ m!\s*Format:\s*\s*([^<]+)!si; | |||||
139 | 0 | ($data->{pages}) = $html =~ m!\s*Number Of Pages:\s*\s*([\d.]+)!si; | |||||
140 | 0 | ($data->{weight}) = $html =~ m!\s*Weight \(kg\):\s*\s*([\d.]+)!si; | |||||
141 | ($data->{height},$data->{width},$data->{depth}) | ||||||
142 | 0 | = $html =~ m!\s*Dimensions \(cm\):\s*([\d.]+)\s* x \s*([\d.]+)\s* x \s*([\d.]+)!si; | |||||
143 | |||||||
144 | # despite it saying Kg (kilogrammes) the weight seems to vary between widely! | ||||||
145 | 0 | 0 | if($data->{weight}) { | ||||
146 | 0 | 0 | if( $data->{weight} < 1) {$data->{weight} = int($data->{weight} * 1000)} | ||||
0 | 0 | ||||||
0 | |||||||
147 | 0 | elsif($data->{weight} < 100) {$data->{weight} = int($data->{weight} * 10)} | |||||
148 | 0 | elsif($data->{weight} < 1000) {$data->{weight} = int($data->{weight})} | |||||
149 | 0 | else {$data->{weight} = int($data->{weight})} | |||||
150 | } | ||||||
151 | |||||||
152 | 0 | 0 | $data->{height} = int($data->{height} * 10) if($data->{height}); | ||||
153 | 0 | 0 | $data->{width} = int($data->{width} * 10) if($data->{width}); | ||||
154 | 0 | 0 | $data->{depth} = int($data->{depth} * 10) if($data->{depth}); | ||||
155 | |||||||
156 | 0 | 0 | if($data->{author}) { | ||||
157 | 0 | $data->{author} =~ s! !,!g; |
|||||
158 | 0 | $data->{author} =~ s!<[^>]+>!!g; | |||||
159 | 0 | $data->{author} =~ s!\s*,\s*!, !g; | |||||
160 | 0 | $data->{author} =~ s!\s*,\s*$!!g; | |||||
161 | } | ||||||
162 | |||||||
163 | 0 | 0 | if($data->{description}) { | ||||
164 | 0 | $data->{description} =~ s!Click on the Google Preview[^<]+!!s; | |||||
165 | 0 | $data->{description} =~ s! !\n!gi; |
|||||
166 | 0 | $data->{description} =~ s!<[^>]+>!!g; | |||||
167 | } | ||||||
168 | |||||||
169 | #use Data::Dumper; | ||||||
170 | #print STDERR "\n# " . Dumper($data); | ||||||
171 | |||||||
172 | 0 | 0 | return $self->handler("Could not extract data from Booktopia result page.") | ||||
173 | unless(defined $data); | ||||||
174 | |||||||
175 | # trim top and tail | ||||||
176 | 0 | 0 | foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } | ||||
0 | |||||||
0 | |||||||
0 | |||||||
177 | |||||||
178 | my $bk = { | ||||||
179 | 'ean13' => $data->{isbn13}, | ||||||
180 | 'isbn13' => $data->{isbn13}, | ||||||
181 | 'isbn10' => $data->{isbn10}, | ||||||
182 | 'isbn' => $data->{isbn13}, | ||||||
183 | 'author' => $data->{author}, | ||||||
184 | 'title' => $data->{title}, | ||||||
185 | 'book_link' => $mech->uri(), | ||||||
186 | 'image_link' => $data->{image}, | ||||||
187 | 'thumb_link' => $data->{thumb}, | ||||||
188 | 'description' => $data->{description}, | ||||||
189 | 'pubdate' => $data->{pubdate}, | ||||||
190 | 'publisher' => $data->{publisher}, | ||||||
191 | 'binding' => $data->{binding}, | ||||||
192 | 'pages' => $data->{pages}, | ||||||
193 | 'weight' => $data->{weight}, | ||||||
194 | 'width' => $data->{width}, | ||||||
195 | 'height' => $data->{height}, | ||||||
196 | 'depth' => $data->{depth}, | ||||||
197 | # 'html' => $html | ||||||
198 | 0 | }; | |||||
199 | |||||||
200 | #use Data::Dumper; | ||||||
201 | #print STDERR "\n# book=".Dumper($bk); | ||||||
202 | |||||||
203 | 0 | $self->book($bk); | |||||
204 | 0 | $self->found(1); | |||||
205 | 0 | return $self->book; | |||||
206 | } | ||||||
207 | |||||||
208 | 1; | ||||||
209 | |||||||
210 | __END__ |