| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Scraper::ISBN::ORA_Driver; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 6 |  |  | 6 |  | 173344 | use strict; | 
|  | 6 |  |  |  |  | 18 |  | 
|  | 6 |  |  |  |  | 390 |  | 
| 4 | 6 |  |  | 6 |  | 40 | use warnings; | 
|  | 6 |  |  |  |  | 16 |  | 
|  | 6 |  |  |  |  | 253 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 6 |  |  | 6 |  | 33 | use vars qw($VERSION @ISA); | 
|  | 6 |  |  |  |  | 110 |  | 
|  | 6 |  |  |  |  | 538 |  | 
| 7 |  |  |  |  |  |  | $VERSION = '0.21'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | WWW::Scraper::ISBN::ORA_Driver - Search driver for O'Reilly Media's online catalog. | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | See parent class documentation (L) | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | Searches for book information from the O'Reilly Media's online catalog. | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =cut | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | ########################################################################### | 
| 28 |  |  |  |  |  |  | # Inheritence | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 6 |  |  | 6 |  | 31 | use base qw(WWW::Scraper::ISBN::Driver); | 
|  | 6 |  |  |  |  | 14 |  | 
|  | 6 |  |  |  |  | 12325 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | ########################################################################### | 
| 33 |  |  |  |  |  |  | # Modules | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 6 |  |  | 6 |  | 19613 | use WWW::Mechanize; | 
|  | 6 |  |  |  |  | 1137573 |  | 
|  | 6 |  |  |  |  | 331 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | ########################################################################### | 
| 38 |  |  |  |  |  |  | # Constants | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 6 |  |  | 6 |  | 69 | use constant	ORA		=> 'http://www.oreilly.com'; | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 484 |  | 
| 41 | 6 |  |  | 6 |  | 30 | use constant	SEARCH	=> 'http://search.oreilly.com'; | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 231 |  | 
| 42 | 6 |  |  | 6 |  | 56 | use constant	QUERY	=> '?submit.x=17&submit.y=8&q=%s'; | 
|  | 6 |  |  |  |  | 15 |  | 
|  | 6 |  |  |  |  | 4668 |  | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | ########################################################################### | 
| 47 |  |  |  |  |  |  | # Public Interface | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =head1 METHODS | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =over 4 | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =item C | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | Creates a query string, then passes the appropriate form fields to the | 
| 56 |  |  |  |  |  |  | O'Reilly Media server. | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | The returned page should be the correct catalog page for that ISBN. If not the | 
| 59 |  |  |  |  |  |  | function returns zero and allows the next driver in the chain to have a go. If | 
| 60 |  |  |  |  |  |  | a valid page is returned, the following fields are returned via the book hash: | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | isbn          (now returns isbn13) | 
| 63 |  |  |  |  |  |  | isbn10 | 
| 64 |  |  |  |  |  |  | isbn13 | 
| 65 |  |  |  |  |  |  | ean13         (industry name) | 
| 66 |  |  |  |  |  |  | author | 
| 67 |  |  |  |  |  |  | title | 
| 68 |  |  |  |  |  |  | book_link | 
| 69 |  |  |  |  |  |  | image_link | 
| 70 |  |  |  |  |  |  | thumb_link | 
| 71 |  |  |  |  |  |  | description | 
| 72 |  |  |  |  |  |  | pubdate | 
| 73 |  |  |  |  |  |  | publisher | 
| 74 |  |  |  |  |  |  | binding       (if known) | 
| 75 |  |  |  |  |  |  | pages         (if known) | 
| 76 |  |  |  |  |  |  | weight        (if known) (in grammes) | 
| 77 |  |  |  |  |  |  | width         (if known) (in millimetres) | 
| 78 |  |  |  |  |  |  | height        (if known) (in millimetres) | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | The book_link and image_link refer back to the O'Reilly US website. | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =back | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =cut | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub search { | 
| 87 | 1 |  |  | 1 | 1 | 252 | my $self = shift; | 
| 88 | 1 |  |  |  |  | 2 | my $isbn = shift; | 
| 89 | 1 |  |  |  |  | 8 | $self->found(0); | 
| 90 | 1 |  |  |  |  | 30 | $self->book(undef); | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 1 |  |  |  |  | 17 | my $mech = WWW::Mechanize->new(); | 
| 93 | 1 |  |  |  |  | 15484 | $mech->agent_alias( 'Linux Mozilla' ); | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 1 |  |  |  |  | 73 | my $url = SEARCH . sprintf(QUERY,$isbn); | 
| 96 | 1 |  |  |  |  | 2 | eval { $mech->get( $url ) }; | 
|  | 1 |  |  |  |  | 6 |  | 
| 97 | 1 | 50 | 33 |  |  | 1646741 | return $self->handler("O'Reilly Media website appears to be unavailable.") | 
|  |  |  | 33 |  |  |  |  | 
| 98 |  |  |  |  |  |  | if($@ || !$mech->success() || !$mech->content()); | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | # The Search Results page | 
| 101 | 1 |  |  |  |  | 80 | my $content = $mech->content(); | 
| 102 | 1 |  |  |  |  | 58 | my ($book) = $content =~ m! | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 1 | 50 |  |  |  | 6 | unless(defined $book) { | 
| 105 |  |  |  |  |  |  | #print STDERR "\n#url=$url\n"; | 
| 106 |  |  |  |  |  |  | #print STDERR "\n#content=".$mech->content(); | 
| 107 | 0 |  |  |  |  | 0 | return $self->handler("Could not extract data from the O'Reilly Media search page [".($mech->uri())."]."); | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 1 |  |  |  |  | 2 | eval { $mech->get( $book ) }; | 
|  | 1 |  |  |  |  | 6 |  | 
| 111 | 1 | 50 | 33 |  |  | 1165172 | return $self->handler("O'Reilly Media website appears to be unavailable.") | 
|  |  |  | 33 |  |  |  |  | 
| 112 |  |  |  |  |  |  | if($@ || !$mech->success() || !$mech->content()); | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 1 |  |  |  |  | 71 | my $html = $mech->content(); | 
| 115 | 1 |  |  |  |  | 23 | my $data = {}; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 1 |  |  |  |  | 4 | for my $name ('book.isbn','ean','target','reference','isbn','graphic','graphic_medium','graphic_large','book_title','author','keywords','description','date') { | 
| 118 | 13 | 100 |  |  |  | 18534 | next    unless($html =~ m!!i); | 
| 119 | 11 |  |  |  |  | 59 | $data->{$name} = $1; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 1 |  |  |  |  | 14 | my (@isbns) = split(',',$data->{target}); | 
| 123 | 1 |  |  |  |  | 5 | for my $isbn (@isbns) { | 
| 124 | 5 |  |  |  |  | 44 | $isbn =~ s/\D+//g; | 
| 125 | 5 | 100 |  |  |  | 15 | next unless($isbn); | 
| 126 | 4 | 100 |  |  |  | 21 | $data->{isbn10} = $isbn if(length($isbn) == 10); | 
| 127 | 4 | 100 |  |  |  | 15 | $data->{isbn13} = $isbn if(length($isbn) == 13); | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | #($data->{isbn13},$data->{isbn10}) = $html =~ m!(?:Print )?ISBN:]+>([\d-]+)\s* \| ISBN 10: ([\d-]+)!; | 
| 131 | 1 |  |  |  |  | 42 | ($data->{pages}) = $html =~ m! Pages: (\d+)!; | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 1 |  | 33 |  |  | 12 | $data->{graphic} ||= $data->{$_}    for('graphic_medium','graphic_large');  # alternative graphic fields | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 1 | 50 |  |  |  | 5 | unless(defined $data) { | 
| 136 |  |  |  |  |  |  | #print STDERR "\n#url=$book\n"; | 
| 137 |  |  |  |  |  |  | #print STDERR "\n#content=".$mech->content(); | 
| 138 | 0 |  |  |  |  | 0 | return $self->handler("Could not extract data from the O'Reilly Media result page [".($mech->uri())."]."); | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | # trim top and tail | 
| 142 | 1 | 50 |  |  |  | 8 | foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } | 
|  | 14 |  |  |  |  | 33 |  | 
|  | 14 |  |  |  |  | 33 |  | 
|  | 14 |  |  |  |  | 81 |  | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 1 | 50 |  |  |  | 14 | my $bk = { | 
|  |  | 50 |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | 'ean13'		    => $data->{ean}, | 
| 146 |  |  |  |  |  |  | 'isbn13'		=> $data->{ean}, | 
| 147 |  |  |  |  |  |  | 'isbn10'		=> $data->{isbn10}, | 
| 148 |  |  |  |  |  |  | 'isbn'			=> $data->{ean}, | 
| 149 |  |  |  |  |  |  | 'author'		=> $data->{author}, | 
| 150 |  |  |  |  |  |  | 'title'			=> $data->{book_title}, | 
| 151 |  |  |  |  |  |  | 'book_link'		=> $mech->uri(), | 
| 152 |  |  |  |  |  |  | 'image_link'	=> ($data->{graphic} !~ /^http/ ? ORA : '') . $data->{graphic}, | 
| 153 |  |  |  |  |  |  | 'thumb_link'	=> ($data->{graphic} !~ /^http/ ? ORA : '') . $data->{graphic}, | 
| 154 |  |  |  |  |  |  | 'description'	=> $data->{description}, | 
| 155 |  |  |  |  |  |  | 'pubdate'		=> $data->{date}, | 
| 156 |  |  |  |  |  |  | 'publisher'		=> q!O'Reilly Media!, | 
| 157 |  |  |  |  |  |  | 'binding'	    => $data->{binding}, | 
| 158 |  |  |  |  |  |  | 'pages'		    => $data->{pages}, | 
| 159 |  |  |  |  |  |  | 'weight'		=> $data->{weight}, | 
| 160 |  |  |  |  |  |  | 'width'		    => $data->{width}, | 
| 161 |  |  |  |  |  |  | 'height'		=> $data->{height} | 
| 162 |  |  |  |  |  |  | }; | 
| 163 | 1 |  |  |  |  | 83 | $self->book($bk); | 
| 164 | 1 |  |  |  |  | 16 | $self->found(1); | 
| 165 | 1 |  |  |  |  | 12 | return $self->book; | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | 1; | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | __END__ |