File Coverage

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


\s*]+>]+>([^<]+)!i; \s*]+>([^<]+)!i; \s*]+>([^<]+)!i; \s*]+>([^<]+)!i; \s*]+>([^<]+)!s; \s*\s*\s*!s; \s*\s*\s*!s; \s*]+>([^<]+)!s; \s*]+>([^<]+)!s;
line stmt bran cond sub pod time code
1             package WWW::Scraper::ISBN::WordPower_Driver;
2              
3 6     6   173428 use strict;
  6         21  
  6         250  
4 6     6   35 use warnings;
  6         11  
  6         269  
5              
6 6     6   34 use vars qw($VERSION @ISA);
  6         12  
  6         568  
7             $VERSION = '0.08';
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   34 use base qw(WWW::Scraper::ISBN::Driver);
  6         10  
  6         5824  
31              
32             ###########################################################################
33             # Modules
34              
35 6     6   13645 use WWW::Mechanize;
  6         9014314  
  6         350  
36              
37             ###########################################################################
38             # Constants
39              
40 6     6   74 use constant SEARCH => 'http://www.word-power.co.uk/searchBook.php?options=isbn&imageField.x=19&imageField.y=9&keywords=';
  6         11  
  6         7399  
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 26999 my $self = shift;
86 4         8 my $isbn = shift;
87 4         20 $self->found(0);
88 4         65 $self->book(undef);
89              
90             # validate and convert into EAN13 format
91 4         43 my $ean = $self->convert_to_ean13($isbn);
92 4 50 66     188 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         69 my $mech = WWW::Mechanize->new();
97 4         36849 $mech->agent_alias( 'Linux Mozilla' );
98 4         255 $mech->add_header('Accept-Encoding' => undef);
99              
100 4         43 eval { $mech->get( SEARCH . $isbn ) };
  4         24  
101 4 50 33     410283 return $self->handler("WordPower website appears to be unavailable.")
      33        
102             if($@ || !$mech->success() || !$mech->content());
103              
104 4         463 my $content = $mech->content;
105 4         1424 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       35 return $self->handler("Failed to find that book on WordPower website.")
113             unless($link);
114              
115 3         10 eval { $mech->get( $URL1 . $link ) };
  3         32  
116 3 50 33     192564 return $self->handler("WordPower website appears to be unavailable.")
      33        
117             if($@ || !$mech->success() || !$mech->content());
118              
119             # The Book page
120 3         190 my $html = $mech->content();
121              
122 3 50   1   3037 return $self->handler("Failed to find that book on WordPower website. [$isbn]")
  1         8  
  1         4  
  1         15  
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         46019 my $data;
128 3         42 ($data->{isbn10}) = $self->convert_to_isbn10($ean);
129 3         3483 ($data->{publisher}) = $html =~ m!]+>Publisher
130 3         3593 ($data->{pubdate}) = $html =~ m!]+>Publication date
131              
132 3 50       31 $data->{publisher} =~ s!<[^>]+>!!g if($data->{publisher});
133 3 50       21 $data->{pubdate} =~ s!\s+! !g if($data->{pubdate});
134              
135 3         3677 ($data->{isbn13}) = $html =~ m!]+>ISBN13
136 3         3294 ($data->{isbn10}) = $html =~ m!]+>ISBN
137 3         1212 ($data->{image}) = $html =~ m!"(http://.*?/product_images/$data->{isbn13}.jpg)"!i;
138 3         1024 ($data->{thumb}) = $html =~ m!"(http://.*?/product_images/$data->{isbn13}.jpg)"!i;
139 3         3191 ($data->{author}) = $html =~ m!by\s*([^<]+)!i;
140 3         2555 ($data->{title}) = $html =~ m!

([^<]+)!i;

141 3         3341 ($data->{description}) = $html =~ m!
\s*
([^~]+)
!si;
142 3         358 ($data->{binding}) = $html =~ m!]+>Format
143 3         104 ($data->{pages}) = $html =~ m!
Pages([\d.]+)
144 3         124 ($data->{weight}) = $html =~ m!
Weight .grammes.([\d.]+)
145 3         338 ($data->{width}) = $html =~ m!]+>Width \(mm\)
146 3         334 ($data->{height}) = $html =~ m!]+>Height \(mm\)
147              
148 3         11 for my $key (qw(weight width height)) {
149 9 50       31 next unless($data->{$key});
150 9         37 $data->{$key} =~ s/\.0+$//;
151             }
152              
153 3 100       18 $data->{author} =~ s!<[^>]+>!!g if($data->{author});
154 3 50       8 if($data->{description}) {
155 3         403 $data->{description} =~ s!
156 3         101 $data->{description} =~ s!<(p|br\s*/)>!\n!g;
157 3         47 $data->{description} =~ s!<[^>]+>!!gs;
158 3         66 $data->{description} =~ s! +$!!gm;
159 3         29 $data->{description} =~ s!\n\n!\n!gs;
160             }
161              
162             #use Data::Dumper;
163             #print STDERR "\n# " . Dumper($data);
164              
165 3 50       41 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       97 next unless(defined $data->{$_});
171 40         71 $data->{$_} =~ s! ! !g;
172 40         99 $data->{$_} =~ s/^\s+//;
173 40         166 $data->{$_} =~ s/\s+$//;
174             }
175              
176 3         43 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         214 $self->book($bk);
201 3         67 $self->found(1);
202 3         32 return $self->book;
203             }
204              
205             1;
206              
207             __END__