line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Google::ISBNNumbers; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1154
|
use HTTP::Tiny; |
|
1
|
|
|
|
|
40394
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
7
|
use Cpanel::JSON::XS; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
52
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
6
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
7
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
271
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = "1.00"; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
1
|
|
|
1
|
0
|
496
|
my ($class, $google_api_key) = @_; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# the API key is required |
15
|
1
|
50
|
|
|
|
5
|
croak "Google API Key is required to use Google::ISBNNumbers.\n". |
16
|
|
|
|
|
|
|
"Please set up a key at https://console.cloud.google.com/apis/credentials" unless $google_api_key; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# become self, with an HTTP::Tiny and Cpanel::JSON objects |
19
|
1
|
|
|
|
|
5
|
my $self = bless { |
20
|
|
|
|
|
|
|
'api_key' => $google_api_key, |
21
|
|
|
|
|
|
|
'http' => HTTP::Tiny->new, |
22
|
|
|
|
|
|
|
'json_coder' => Cpanel::JSON::XS->new->utf8->allow_nonref->allow_blessed, |
23
|
|
|
|
|
|
|
}, $class; |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
94
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# method to query the open library for an ISBN number |
29
|
|
|
|
|
|
|
sub lookup_isbn { |
30
|
1
|
|
|
1
|
0
|
314
|
my ($self, $isbn_number) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# can't do much with a valid number |
33
|
1
|
|
|
|
|
5
|
$isbn_number =~ s/\-|\s//g; |
34
|
1
|
50
|
33
|
|
|
13
|
croak "Valid ISBN number required for lookup_book()" unless $isbn_number =~ /^97/ && $isbn_number !~ /\D/; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# do the lookup! |
37
|
1
|
|
|
|
|
3
|
my $response = HTTP::Tiny->new->get( 'https://www.googleapis.com/books/v1/volumes?q=isbn:'.$isbn_number.'&key='.$self->{api_key} ); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# alert if failure |
40
|
1
|
50
|
|
|
|
392323
|
croak "Lookup failed: ".$response->{reason} unless $response->{success}; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# translate JSON to data struct |
43
|
1
|
|
|
|
|
530
|
my $results = $self->{json_coder}->decode( $response->{content} ); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# must be an array |
46
|
1
|
50
|
|
|
|
6
|
croak "Invalid results returned. Check your API key." unless ref($$results{items}) eq 'ARRAY'; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# we have a book: simplify and return |
49
|
1
|
|
|
|
|
3
|
my $book_info = $$results{items}[0]{volumeInfo}; |
50
|
|
|
|
|
|
|
return { |
51
|
|
|
|
|
|
|
'title' => $$book_info{title}, |
52
|
|
|
|
|
|
|
'author_name' => $$book_info{authors}[0], |
53
|
|
|
|
|
|
|
'publication_date' => $$book_info{publishedDate}, |
54
|
|
|
|
|
|
|
'description' => $$book_info{description}, |
55
|
|
|
|
|
|
|
'cover_link' => $$book_info{imageLinks}{smallThumbnail}, |
56
|
1
|
|
|
|
|
19
|
}; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
__END__ |