Add a new Added Content handler: ImageURLinMARC user/paxed/ac-image-url-in-marc
authorPasi Kallinen <pasi.kallinen@pttk.fi>
Wed, 13 Nov 2013 17:52:10 +0000 (19:52 +0200)
committerPasi Kallinen <pasi.kallinen@pttk.fi>
Wed, 13 Nov 2013 21:34:41 +0000 (23:34 +0200)
Signed-off-by: Pasi Kallinen <pasi.kallinen@pttk.fi>
Open-ILS/examples/opensrf.xml.example
Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/ImageURLinMARC.pm [new file with mode: 0644]

index 658ced1..494760a 100644 (file)
@@ -263,6 +263,21 @@ vim:et:ts=4:sw=4:
                 <return_behavior_on_no_jacket_image>T</return_behavior_on_no_jacket_image>
             </ContentCafe>
 
+            <!-- Get cover image from the URL in the MARC data.
+                 url_field is the field containing the URL, if undefined, defaults to 856u.
+                 the cover URL is only used if the url_field content matches
+                 the url_regex. Leaving the regex undefined matches any value.
+                If defined, base_url is prefixed to the URL from MARC.
+            -->
+            <ImageURLinMARC>
+              <url_field>856u</url_field>
+              <url_regex>IMAGE</url_regex>
+             <!--
+             <base_url>foo</base_url>
+             -->
+           </ImageURLinMARC>
+
+
             <!--
  
             You can add free-form settings here and they will be accessible
index e1c0bf0..c550eb6 100644 (file)
@@ -137,6 +137,11 @@ sub handler {
         $keyhash = {};
         $keyhash->{"isbn"} = [$keyvalue];
     } else {
+
+        if ($handler->can('expects_record_id') && $handler->expects_record_id() eq 1) {
+            # Handler expects a record_id, short circuit
+        } else {
+
         my $key_data = get_rec_keys($keyvalue);
         my @isbns = grep {$_->{tag} eq '020'} @$key_data;
         my @issns = grep {$_->{tag} eq '022'} @$key_data;
@@ -163,14 +168,19 @@ sub handler {
             issn => [map {$_->{value}} @issns],
             upc  => [map {$_->{value}} @upcs]
         };
-    }
 
-    return Apache2::Const::NOT_FOUND unless @{$keyhash->{isbn}} || @{$keyhash->{issn}} || @{$keyhash->{upc}};
+       return Apache2::Const::NOT_FOUND unless @{$keyhash->{isbn}} || @{$keyhash->{issn}} || @{$keyhash->{upc}};
+
+       }
+    }
 
     try {
         if ($handler->can('expects_keyhash') && $handler->expects_keyhash() eq 1) {
             # Handler expects a keyhash
             $data = $handler->$method($keyhash);
+       } elsif ($handler->can('expects_record_id') && $handler->expects_record_id() eq 1) {
+            # Handler expects a record_id
+            $data = $handler->$method($keyvalue);
         } else {
             # Pass single ISBN as a scalar to the handler
             $data = $handler->$method($keyhash->{isbn}[0]);
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/ImageURLinMARC.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/ImageURLinMARC.pm
new file mode 100644 (file)
index 0000000..5abb591
--- /dev/null
@@ -0,0 +1,128 @@
+package OpenILS::WWW::AddedContent::ImageURLinMARC;
+use strict; use warnings;
+use OpenSRF::Utils::Logger qw/$logger/;
+use OpenSRF::Utils::SettingsParser;
+use OpenILS::WWW::AddedContent;
+use OpenSRF::Utils::JSON;
+use OpenSRF::EX qw/:try/;
+use XML::LibXML;
+
+my $AC = 'OpenILS::WWW::AddedContent';
+
+my $config_basename = 'ImageURLinMARC';
+
+sub new {
+    my( $class, $args ) = @_;
+    $class = ref $class || $class;
+    return bless($args, $class);
+}
+
+# base_url is prepended to the url
+sub base_url {
+    my $self = shift;
+    return $self->{$config_basename}->{base_url} || '';
+}
+
+# eg. '856u'
+sub url_field {
+    my $self = shift;
+    return $self->{$config_basename}->{url_field} || '856u';
+}
+# only if $url_field matches this regex
+sub url_regex {
+    my $self = shift;
+    return $self->{$config_basename}->{url_regex} || '.*';
+}
+
+sub expects_record_id {
+    # we expect a record_id as opposed to a simple scalar containing an ISBN
+    return 1;
+}
+
+
+
+
+# --------------------------------------------------------------------------
+sub jacket_small {
+    my( $self, $key ) = @_;
+    return $self->send_img(
+        $self->fetch_response('small', $key));
+}
+
+sub jacket_medium {
+    my( $self, $key ) = @_;
+    return $self->send_img(
+        $self->fetch_response('medium', $key));
+
+}
+sub jacket_large {
+    my( $self, $key ) = @_;
+    return $self->send_img(
+        $self->fetch_response('large', $key));
+}
+
+# --------------------------------------------------------------------------
+
+sub send_img {
+    my($self, $response) = @_;
+    return {
+        content_type => $response->header('Content-type'),
+        content => $response->content,
+        binary => 1
+    };
+}
+
+# returns the raw content returned from the URL fetch
+sub fetch_content {
+    my( $self, $page, $key ) = @_;
+    return $self->fetch_response($page, $key)->content;
+}
+
+# returns the HTTP response object from the URL fetch
+sub fetch_response {
+    my( $self, $page, $key ) = @_;
+
+    # TODO: we're ignoring $page, at least for now.
+
+    my $rec_dat = $self->get_record_marc($key);
+    return 0 if (!$rec_dat);
+
+    return 0 if (length($self->url_field()) != 4);
+
+    my $url_f = substr($self->url_field(), 0, 3);
+    my $url_sf = substr($self->url_field(), 3, 1);
+    my $url_re = $self->url_regex();
+
+    my $marc = $rec_dat->[0]->{marc};
+    my $doc = XML::LibXML->new->parse_string($marc);
+    $doc->documentElement->setNamespace( "http://www.loc.gov/MARC21/slim", "marc", 1 );
+
+    my $tagfind = "//marc:datafield[\@tag='" . $url_f . "']";
+
+    my @node = $doc->findnodes($tagfind);
+
+    my $img_url;
+    my $baseurl = $self->base_url();
+
+    foreach my $x (@node) {
+       $img_url = $x->findvalue("marc:subfield[\@code='" . $url_sf . "']");
+       return $AC->get_url($baseurl . $img_url) if ($img_url && ($img_url =~ /$url_re/));
+    }
+
+    return 0;
+}
+
+
+sub get_record_marc {
+    my ($self, $id) = @_;
+    return OpenILS::Utils::CStoreEditor->new->json_query({
+        select => {bre => ['marc']},
+        from => 'bre',
+        where => {
+            id => $id
+        }
+    });
+}
+
+
+1;