| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Scraper::ISBN::Bookstore_Driver; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 6 |  |  | 6 |  | 1220267 | use strict; | 
|  | 6 |  |  |  |  | 14 |  | 
|  | 6 |  |  |  |  | 303 |  | 
| 4 | 6 |  |  | 6 |  | 33 | use warnings; | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 269 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 6 |  |  | 6 |  | 35 | use vars qw($VERSION @ISA); | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 545 |  | 
| 7 |  |  |  |  |  |  | $VERSION = '0.02'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | WWW::Scraper::ISBN::Bookstore_Driver - Search driver for The Bookstore 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 Bookstore online book catalog | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =cut | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | ########################################################################### | 
| 28 |  |  |  |  |  |  | # Inheritence | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 6 |  |  | 6 |  | 35 | use base qw(WWW::Scraper::ISBN::Driver); | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 3867 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | ########################################################################### | 
| 33 |  |  |  |  |  |  | # Modules | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 6 |  |  | 6 |  | 11057 | use WWW::Mechanize; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | ########################################################################### | 
| 38 |  |  |  |  |  |  | # Constants | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | use constant    SEARCH  => 'http://www.bookstore.co.uk/TBP.Direct/PurchaseProduct/OrderProduct/CustomerSelectProduct/SearchProducts.aspx?d=bookstore&s=C&r=10000046&ui=0&bc=0&productGroupId=&keywordSearch='; | 
| 41 |  |  |  |  |  |  | use constant    PRODUCT => 'http://www.bookstore.co.uk'; | 
| 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 |  |  |  |  |  |  | Bookstore 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 |  |  |  |  |  |  | thumb_link | 
| 70 |  |  |  |  |  |  | description | 
| 71 |  |  |  |  |  |  | pubdate | 
| 72 |  |  |  |  |  |  | publisher | 
| 73 |  |  |  |  |  |  | binding       (if known) | 
| 74 |  |  |  |  |  |  | pages         (if known) | 
| 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 Bookstore website. | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =back | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =cut | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub search { | 
| 86 |  |  |  |  |  |  | my $self = shift; | 
| 87 |  |  |  |  |  |  | my $isbn = shift; | 
| 88 |  |  |  |  |  |  | $self->found(0); | 
| 89 |  |  |  |  |  |  | $self->book(undef); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | # validate and convert into EAN13 format | 
| 92 |  |  |  |  |  |  | my $ean = $self->convert_to_ean13($isbn); | 
| 93 |  |  |  |  |  |  | return $self->handler("Invalid ISBN specified [$isbn]") | 
| 94 |  |  |  |  |  |  | unless($ean); | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | #print STDERR "\n# isbn=[$isbn] => ean=[$ean]\n"; | 
| 97 |  |  |  |  |  |  | $isbn = $ean; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | my $mech = WWW::Mechanize->new(); | 
| 100 |  |  |  |  |  |  | $mech->agent_alias( 'Linux Mozilla' ); | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | #print STDERR "\n# url=[".(SEARCH . $isbn)."]\n"; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | eval { $mech->get( SEARCH . $isbn ) }; | 
| 105 |  |  |  |  |  |  | return $self->handler("The Bookstore website appears to be unavailable.") | 
| 106 |  |  |  |  |  |  | if($@ || !$mech->success() || !$mech->content()); | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | # The Results page | 
| 109 |  |  |  |  |  |  | my $html = $mech->content(); | 
| 110 |  |  |  |  |  |  | #print STDERR "\n# html=[\n$html\n]\n"; | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | my ($link) = $html =~ m! | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | #print STDERR "\n# link=[".(PRODUCT . $link)."]\n"; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | return $self->handler("Failed to find that book on The Bookstore website. [$isbn]") | 
| 117 |  |  |  |  |  |  | unless($link); | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | eval { $mech->get( PRODUCT . $link ) }; | 
| 120 |  |  |  |  |  |  | return $self->handler("The Bookstore website appears to be unavailable.") | 
| 121 |  |  |  |  |  |  | if($@ || !$mech->success() || !$mech->content()); | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | # The Book page | 
| 124 |  |  |  |  |  |  | $html = $mech->content(); | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | return $self->handler("Failed to find that book on The Bookstore website. [$isbn]") | 
| 127 |  |  |  |  |  |  | if($html =~ m!Sorry, we couldn't find any matches for!si); | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | #print STDERR "\n# html=[\n$html\n]\n"; | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | my $data; | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | ($data->{isbn13})       = $html =~ m! | ISBN/Cat.No\s* | \s*(\d+)[^<]*!si; | 
| 134 |  |  |  |  |  |  | ($data->{isbn10})       = $html =~ m! | ISBN-10\s* | \s*(\d+)[^<]*!si; | 
| 135 |  |  |  |  |  |  | ($data->{title})        = $html =~ m! | Title\s* | ([^<]+)!si; | 
| 136 |  |  |  |  |  |  | ($data->{author})       = $html =~ m! | Author/Artist\s* | ([^<]+)!si; | 
| 137 |  |  |  |  |  |  | ($data->{publisher})    = $html =~ m! | Publisher\s* | ([^<]+)!si; | 
| 138 |  |  |  |  |  |  | ($data->{pubdate})      = $html =~ m! | Publication Date\s* | ([^<]+)!si; | 
| 139 |  |  |  |  |  |  | ($data->{format})       = $html =~ m! | Format\s* | ([^<]+)!si; | 
| 140 |  |  |  |  |  |  | ($data->{description})  = $html =~ m! | ]*>\s*Summary\s* | 
.*?| ]*>([^<]+)!si; | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | ($data->{image})        = $html =~ m!   | 
| 143 |  |  |  |  |  |  | ($data->{thumb})        = $data->{image}; | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | ($data->{binding},$data->{height},$data->{width},$data->{depth},$data->{pages}) | 
| 146 |  |  |  |  |  |  | = $data->{format} =~ m!([^;]+);\s*H:(\d+);\s*W:(\d+);\s*D:(\d+);\s*(\d+)p\.!si; | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | #use Data::Dumper; | 
| 149 |  |  |  |  |  |  | #print STDERR "\n# " . Dumper($data); | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | return $self->handler("Could not extract data from Bookstore result page.") | 
| 152 |  |  |  |  |  |  | unless(defined $data); | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | # trim top and tail | 
| 155 |  |  |  |  |  |  | foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | my $bk = { | 
| 158 |  |  |  |  |  |  | 'ean13'         => $data->{isbn13}, | 
| 159 |  |  |  |  |  |  | 'isbn13'        => $data->{isbn13}, | 
| 160 |  |  |  |  |  |  | 'isbn10'        => $data->{isbn10}, | 
| 161 |  |  |  |  |  |  | 'isbn'          => $data->{isbn13}, | 
| 162 |  |  |  |  |  |  | 'author'        => $data->{author}, | 
| 163 |  |  |  |  |  |  | 'title'         => $data->{title}, | 
| 164 |  |  |  |  |  |  | 'book_link'     => $mech->uri(), | 
| 165 |  |  |  |  |  |  | 'image_link'    => $data->{image}, | 
| 166 |  |  |  |  |  |  | 'thumb_link'    => $data->{thumb}, | 
| 167 |  |  |  |  |  |  | 'description'   => $data->{description}, | 
| 168 |  |  |  |  |  |  | 'pubdate'       => $data->{pubdate}, | 
| 169 |  |  |  |  |  |  | 'publisher'     => $data->{publisher}, | 
| 170 |  |  |  |  |  |  | 'binding'       => $data->{binding}, | 
| 171 |  |  |  |  |  |  | 'pages'         => $data->{pages}, | 
| 172 |  |  |  |  |  |  | 'width'         => $data->{width}, | 
| 173 |  |  |  |  |  |  | 'height'        => $data->{height}, | 
| 174 |  |  |  |  |  |  | 'depth'         => $data->{depth}, | 
| 175 |  |  |  |  |  |  | 'html'          => $html | 
| 176 |  |  |  |  |  |  | }; | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | #use Data::Dumper; | 
| 179 |  |  |  |  |  |  | #print STDERR "\n# book=".Dumper($bk); | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | $self->book($bk); | 
| 182 |  |  |  |  |  |  | $self->found(1); | 
| 183 |  |  |  |  |  |  | return $self->book; | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | 1; | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | __END__ |