| 1 | package WWW::Scraper::ISBN::ORA_Driver; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | $VERSION = '0.22'; | |||||||||||||||||||||||||||||||||||||||||
| 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<WWW::Scraper::ISBN::Driver>) | ||||||||||||||||||||||||||||||||||||||||||
| 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 | 5 | ▶ | use base qw(WWW::Scraper::ISBN::Driver); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # Modules | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 5 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # Constants | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | ▶ | use constant ORA => 'http://www.oreilly.com'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | use constant SEARCH => 'http://search.oreilly.com'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | use constant QUERY => '?submit.x=17&submit.y=8&q=%s'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # Public Interface | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =item C<search()> | ||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $isbn = shift; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $self->found(0); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $self->book(undef); | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my $mech = WWW::Mechanize->new(); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $mech->agent_alias( 'Linux Mozilla' ); | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $url = SEARCH . sprintf(QUERY,$isbn); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | eval { $mech->get( $url ) }; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return $self->handler("O'Reilly Media website appears to be unavailable.") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$@ or not $mech->success or not $mech->content
$@ or not $mech->success
MC/DC0%0/3
$@ or not $mech->success or not $mech->content
$@
not $mech->success
not $mech->content
Truth table0%0/4
A or not(B) or not(C)
A= $@B= not $mech->successC= not $mech->content
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | if($@ || !$mech->success() || !$mech->content()); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # The Search Results page | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $content = $mech->content(); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my ($book) = $content =~ m!<div class="book_text">\s*<p class="title">\s*<a href="([^"]+)"!; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | unless(defined $book) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | #print STDERR "\n#url=$url\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | #print STDERR "\n#content=".$mech->content(); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | return $self->handler("Could not extract data from the O'Reilly Media search page [".($mech->uri())."]."); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | eval { $mech->get( $book ) }; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | return $self->handler("O'Reilly Media website appears to be unavailable.") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$@ or not $mech->success or not $mech->content
$@ or not $mech->success
MC/DC0%0/3
$@ or not $mech->success or not $mech->content
$@
not $mech->success
not $mech->content
Truth table0%0/4
A or not(B) or not(C)
A= $@B= not $mech->successC= not $mech->content
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | if($@ || !$mech->success() || !$mech->content()); | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $html = $mech->content(); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $data = {}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | for my $name ('book.isbn','ean','target','reference','isbn','graphic','graphic_medium','graphic_large','book_title','author','keywords','description','date') { | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | next unless($html =~ m!<meta name="$name" content="([^"]+)"\s*/>!i); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $data->{$name} = $1; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my (@isbns) = split(',',$data->{target}); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | for my $isbn (@isbns) { | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $isbn =~ s/\D+//g; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | next unless($isbn); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $data->{isbn10} = $isbn if(length($isbn) == 10); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | $data->{isbn13} = $isbn if(length($isbn) == 13); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | #($data->{isbn13},$data->{isbn10}) = $html =~ m!<dt>(?:Print )?ISBN:</dt><dd[^>]+>([\d-]+)</dd>\s*<dt class="isbn-10"> \| ISBN 10:</dt> <dd>([\d-]+)</dd>!; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ($data->{pages}) = $html =~ m!<div class="default">Pages: (\d+)</div>!; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $data->{graphic} ||= $data->{$_} for('graphic_medium','graphic_large'); # alternative graphic fields | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$data->{"graphic"} ||= $data->{$_}
MC/DC0%0/2
$data->{"graphic"} ||= $data->{$_}
$data->{"graphic"}
$data->{$_}
Truth table0%0/3
A ||= B
A= $data->{"graphic"}B= $data->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | unless(defined $data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | #print STDERR "\n#url=$book\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | #print STDERR "\n#content=".$mech->content(); | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 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 | 0 | ▶ | foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | my $bk = { | ||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | $self->book($bk); | |||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | $self->found(1); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | return $self->book; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||||||||||||||||