]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/Amazon.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / AddedContent / Amazon.pm
1 package OpenILS::WWW::AddedContent::Amazon;
2 use strict; use warnings;
3 use OpenSRF::Utils::Logger qw/$logger/;
4 use OpenSRF::Utils::SettingsParser;
5 use OpenILS::WWW::AddedContent;
6 use OpenSRF::Utils::JSON;
7 use OpenSRF::EX qw/:try/;
8 use XML::LibXML;
9 use Business::ISBN;
10
11 my $AC = 'OpenILS::WWW::AddedContent';
12
13 sub new {
14     my( $class, $args ) = @_;
15     $class = ref $class || $class;
16     return bless($args, $class);
17 }
18
19 sub base_url {
20     my $self = shift;
21     return $self->{base_url};
22 }
23
24 sub userid {
25     my $self = shift;
26     return $self->{userid};
27 }
28
29
30 # --------------------------------------------------------------------------
31 sub jacket_small {
32     my( $self, $key ) = @_;
33     return $self->send_img(
34         $self->fetch_response('_SCMZZZZZZZ_.jpg', $key));
35 }
36
37 sub jacket_medium {
38     my( $self, $key ) = @_;
39     return $self->send_img(
40         $self->fetch_response('_SCMZZZZZZZ_.jpg', $key));
41
42 }
43 sub jacket_large {
44     my( $self, $key ) = @_;
45     return $self->send_img(
46         $self->fetch_response('_SCZZZZZZZ_.jpg', $key));
47 }
48
49 # --------------------------------------------------------------------------
50
51 sub send_img {
52     my($self, $response) = @_;
53     return { 
54         content_type => $response->header('Content-type'),
55         content => $response->content, 
56         binary => 1 
57     };
58 }
59
60 # returns the raw content returned from the URL fetch
61 sub fetch_content {
62     my( $self, $page, $key ) = @_;
63     return $self->fetch_response($page, $key)->content;
64 }
65
66 # returns the HTTP response object from the URL fetch
67 sub fetch_response {
68     my( $self, $page, $key ) = @_;
69     my $uname = $self->userid;
70
71     # Some sites have entered Amazon IDs in 020 $a, thus we cannot
72     # simply pass all incoming keys to Business::ISBN for normalization
73     if (length($key) > 10) {
74         # Use Business::ISBN to normalize the incoming ISBN
75         my $isbn = Business::ISBN->new( $key );
76         if (!defined $isbn) {
77             $logger->warning("'$key' is not a valid ISBN");
78             return 0;
79         }
80
81         # Amazon prefers ISBN10
82         $isbn = $isbn->as_isbn10 if $isbn->type eq 'ISBN13';
83         $key = $isbn->as_string([]);
84     }
85
86     my $url = $self->base_url . "$key.01.$page";
87     return $AC->get_url($url);
88 }
89
90
91
92 1;