| 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 | 122991 | use strict; | |||
| 6 | 15 | ||||||
| 6 | 213 | ||||||
| 4 | 6 | 6 | 23 | use warnings; | |||
| 6 | 7 | ||||||
| 6 | 193 | ||||||
| 5 | |||||||
| 6 | 6 | 6 | 27 | use vars qw($VERSION @ISA); | |||
| 6 | 11 | ||||||
| 6 | 509 | ||||||
| 7 | $VERSION = '0.23'; | ||||||
| 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 | 27 | use base qw(WWW::Scraper::ISBN::Driver); | |||
| 6 | 6 | ||||||
| 6 | 2891 | ||||||
| 31 | |||||||
| 32 | ########################################################################### | ||||||
| 33 | # Modules | ||||||
| 34 | |||||||
| 35 | 6 | 6 | 8821 | use WWW::Mechanize; | |||
| 6 | 793337 | ||||||
| 6 | 259 | ||||||
| 36 | |||||||
| 37 | ########################################################################### | ||||||
| 38 | # Constants | ||||||
| 39 | |||||||
| 40 | 6 | 6 | 54 | use constant SEARCH => 'http://www.pearsoned.co.uk/Bookshop/'; | |||
| 6 | 7 | ||||||
| 6 | 431 | ||||||
| 41 | 6 | 6 | 26 | use constant DETAIL => 'http://www.pearsoned.co.uk/Bookshop/detail.asp?item='; | |||
| 6 | 12 | ||||||
| 6 | 3760 | ||||||
| 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 | 9731 | my $self = shift; | ||
| 86 | 2 | 5 | my $isbn = shift; | ||||
| 87 | 2 | 8 | $self->found(0); | ||||
| 88 | 2 | 30 | $self->book(undef); | ||||
| 89 | |||||||
| 90 | 2 | 64 | my $mech = WWW::Mechanize->new(); | ||||
| 91 | 2 | 13998 | $mech->agent_alias( 'Linux Mozilla' ); | ||||
| 92 | |||||||
| 93 | 2 | 153 | eval { $mech->get( SEARCH ) }; | ||||
| 2 | 10 | ||||||
| 94 | 2 | 50 | 33 | 352887 | 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 | 112 | $mech->form_id('hipGlobalSearchForm'); | ||||
| 100 | 2 | 13577 | $mech->set_fields( 'txtSearch' => $isbn ); | ||||
| 101 | |||||||
| 102 | 2 | 131 | eval { $mech->submit() }; | ||||
| 2 | 8 | ||||||
| 103 | 2 | 50 | 33 | 1358708 | 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 | 141 | my $html = $mech->content(); | ||||
| 108 | #print STDERR "\n# html=[\n$html\n]\n"; | ||||||
| 109 | |||||||
| 110 | 2 | 50 | 1627 | 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 | 5 | my $data; | ||||
| 114 | 2 | 256 | ($data->{image}) = $html =~ m!"(http://images.pearsoned-ema.com/jpeg/large/\d+\.jpg)"!i; | ||||
| 115 | 2 | 243 | ($data->{thumb}) = $html =~ m!"(http://images.pearsoned-ema.com/jpeg/small/\d+\.jpg)"!i; | ||||
| 116 | 2 | 1516 | ($data->{title}, | ||||
| 117 | $data->{author}, | ||||||
| 118 | $data->{pubdate}, | ||||||
| 119 | $data->{binding}, | ||||||
| 120 | $data->{pages}, | ||||||
| 121 | $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 | 2 | 1175 | ($data->{description}) = $html =~ m! ([^<]+)!is; |
||||
| 127 | 2 | 1244 | ($data->{bookid}) = $html =~ m!recommend.asp\?item=(\d+)!i; | ||||
| 128 | |||||||
| 129 | #use Data::Dumper; | ||||||
| 130 | #print STDERR "\n# " . Dumper($data); | ||||||
| 131 | |||||||
| 132 | 2 | 50 | 14 | return $self->handler("Could not extract data from Pearson Education result page.") | |||
| 133 | unless(defined $data); | ||||||
| 134 | |||||||
| 135 | # remove HTML tags | ||||||
| 136 | 2 | 7 | for(qw(author binding)) { | ||||
| 137 | 4 | 50 | 12 | next unless(defined $data->{$_}); | |||
| 138 | 4 | 32 | $data->{$_} =~ s!<[^>]+>!!g; | ||||
| 139 | } | ||||||
| 140 | |||||||
| 141 | # trim top and tail | ||||||
| 142 | 2 | 13 | for(keys %$data) { | ||||
| 143 | 20 | 100 | 37 | next unless(defined $data->{$_}); | |||
| 144 | 19 | 36 | $data->{$_} =~ s/^\s+//; | ||||
| 145 | 19 | 80 | $data->{$_} =~ s/\s+$//; | ||||
| 146 | } | ||||||
| 147 | |||||||
| 148 | 2 | 15 | my $uri = $mech->uri(); #DETAIL . $data->{bookid}; | ||||
| 149 | |||||||
| 150 | 2 | 80 | 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 | 'html' => $html | ||||||
| 169 | }; | ||||||
| 170 | |||||||
| 171 | #use Data::Dumper; | ||||||
| 172 | #print STDERR "\n# book=".Dumper($bk); | ||||||
| 173 | |||||||
| 174 | 2 | 172 | $self->book($bk); | ||||
| 175 | 2 | 29 | $self->found(1); | ||||
| 176 | 2 | 17 | return $self->book; | ||||
| 177 | } | ||||||
| 178 | |||||||
| 179 | 1; | ||||||
| 180 | __END__ |