line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Scraper::ISBN::ORA_Driver; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
93431
|
use strict; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
239
|
|
4
|
6
|
|
|
6
|
|
25
|
use warnings; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
193
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
25
|
use vars qw($VERSION @ISA); |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
430
|
|
7
|
|
|
|
|
|
|
$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) |
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
|
|
27
|
use base qw(WWW::Scraper::ISBN::Driver); |
|
6
|
|
|
|
|
5
|
|
|
6
|
|
|
|
|
2895
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
########################################################################### |
33
|
|
|
|
|
|
|
# Modules |
34
|
|
|
|
|
|
|
|
35
|
6
|
|
|
6
|
|
8659
|
use WWW::Mechanize; |
|
6
|
|
|
|
|
728661
|
|
|
6
|
|
|
|
|
284
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
########################################################################### |
38
|
|
|
|
|
|
|
# Constants |
39
|
|
|
|
|
|
|
|
40
|
6
|
|
|
6
|
|
50
|
use constant ORA => 'http://www.oreilly.com'; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
362
|
|
41
|
6
|
|
|
6
|
|
26
|
use constant SEARCH => 'http://search.oreilly.com'; |
|
6
|
|
|
|
|
6
|
|
|
6
|
|
|
|
|
207
|
|
42
|
6
|
|
|
6
|
|
22
|
use constant QUERY => '?submit.x=17&submit.y=8&q=%s'; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
3372
|
|
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
|
182
|
my $self = shift; |
88
|
1
|
|
|
|
|
1
|
my $isbn = shift; |
89
|
1
|
|
|
|
|
6
|
$self->found(0); |
90
|
1
|
|
|
|
|
19
|
$self->book(undef); |
91
|
|
|
|
|
|
|
|
92
|
1
|
|
|
|
|
10
|
my $mech = WWW::Mechanize->new(); |
93
|
1
|
|
|
|
|
11962
|
$mech->agent_alias( 'Linux Mozilla' ); |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
73
|
my $url = SEARCH . sprintf(QUERY,$isbn); |
96
|
1
|
|
|
|
|
2
|
eval { $mech->get( $url ) }; |
|
1
|
|
|
|
|
4
|
|
97
|
1
|
50
|
33
|
|
|
991289
|
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
|
|
|
|
|
60
|
my $content = $mech->content(); |
102
|
1
|
|
|
|
|
42
|
my ($book) = $content =~ m! |
103
|
|
|
|
|
|
|
|
104
|
1
|
50
|
|
|
|
4
|
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
|
|
|
|
|
4
|
|
111
|
1
|
50
|
33
|
|
|
1185358
|
return $self->handler("O'Reilly Media website appears to be unavailable.") |
|
|
|
33
|
|
|
|
|
112
|
|
|
|
|
|
|
if($@ || !$mech->success() || !$mech->content()); |
113
|
|
|
|
|
|
|
|
114
|
1
|
|
|
|
|
60
|
my $html = $mech->content(); |
115
|
1
|
|
|
|
|
16
|
my $data = {}; |
116
|
|
|
|
|
|
|
|
117
|
1
|
|
|
|
|
3
|
for my $name ('book.isbn','ean','target','reference','isbn','graphic','graphic_medium','graphic_large','book_title','author','keywords','description','date') { |
118
|
13
|
100
|
|
|
|
14760
|
next unless($html =~ m!!i); |
119
|
11
|
|
|
|
|
54
|
$data->{$name} = $1; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
1
|
|
|
|
|
13
|
my (@isbns) = split(',',$data->{target}); |
123
|
1
|
|
|
|
|
5
|
for my $isbn (@isbns) { |
124
|
5
|
|
|
|
|
18
|
$isbn =~ s/\D+//g; |
125
|
5
|
100
|
|
|
|
11
|
next unless($isbn); |
126
|
4
|
100
|
|
|
|
13
|
$data->{isbn10} = $isbn if(length($isbn) == 10); |
127
|
4
|
100
|
|
|
|
9
|
$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
|
|
|
|
|
38
|
($data->{pages}) = $html =~ m! Pages: (\d+) !; |
132
|
|
|
|
|
|
|
|
133
|
1
|
|
33
|
|
|
8
|
$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
|
|
|
|
7
|
foreach (keys %$data) { next unless(defined $data->{$_});$data->{$_} =~ s/^\s+//;$data->{$_} =~ s/\s+$//; } |
|
14
|
|
|
|
|
19
|
|
|
14
|
|
|
|
|
19
|
|
|
14
|
|
|
|
|
52
|
|
143
|
|
|
|
|
|
|
|
144
|
1
|
50
|
|
|
|
13
|
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
|
|
|
|
|
60
|
$self->book($bk); |
164
|
1
|
|
|
|
|
14
|
$self->found(1); |
165
|
1
|
|
|
|
|
8
|
return $self->book; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
1; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__END__ |