Robustify the MARC record enrichment function
authorDan Scott <dscott@laurentian.ca>
Mon, 12 Jan 2015 19:41:22 +0000 (14:41 -0500)
committerDan Scott <dscott@laurentian.ca>
Mon, 12 Jan 2015 19:41:22 +0000 (14:41 -0500)
Short circuit if we have the numbers we want, so we don't
burn through our API limit overly quickly. Increase the
timeout period. And handle the chance that the response
won't be successful without simply erroring out.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/src/sql/Pg/update_marc_records_in_database.sql

index c838335..4d3395d 100644 (file)
@@ -1,4 +1,4 @@
-CREATE OR REPLACE FUNCTION conifer.fix_https_uris(record BIGINT) RETURNS TEXT AS $func$
+CREATE OR REPLACE FUNCTION conifer.enrich_uris(record BIGINT) RETURNS TEXT AS $func$
 use strict;
 use MARC::Record;
 use MARC::File::XML (BinaryEncoding => 'UTF-8');
@@ -73,7 +73,7 @@ foreach my $prov (@provs) {
 
 # Enrich the record
 my $ua = LWP::UserAgent->new;
-$ua->timeout(10);
+$ua->timeout(20);
 my $lccn_number;
 my $oclc_number;
 my $owi_number;
@@ -89,8 +89,10 @@ if ($lccn_number) {
     $lccn->update('a', $lccn_number);
     my $response = $ua->get("http://xisbn.worldcat.org/webservices/xid/lccn/$lccn_number?method=getMetadata&format=json&fl=*");
     my $metadata = decode_json($response->decoded_content);
-    $oclc_number = $metadata->{'list'}->[0]->{'oclcnum'}->[0];
-    $owi_number = $metadata->{'list'}->[0]->{'owi'}->[0];
+    if ($response->is_success) {
+        $oclc_number = $metadata->{'list'}->[0]->{'oclcnum'}->[0];
+        $owi_number = $metadata->{'list'}->[0]->{'owi'}->[0];
+    }
 } else {
     my @isbntags = $record->field('020');
     foreach my $isbntag (@isbntags) {
@@ -104,18 +106,29 @@ if ($lccn_number) {
         if ($rest and $isbntag->subfield('q') ne $rest) {
             $isbntag->update('q', $rest);
         }
+        # short-circuit to save API budget
+        next if $oclc_number and $owi_number and $lccn_number;
+
         my $response = $ua->get("http://xisbn.worldcat.org/webservices/xid/isbn/$isbn?method=getMetadata&format=json&fl=*");
+        next unless $response->is_success;
         my $metadata = decode_json($response->decoded_content);
         if (exists $metadata->{'list'}->[0]->{'lccn'} and !$lccn_number) {
             $lccn_number = $metadata->{'list'}->[0]->{'lccn'}->[0];
             my $nf = MARC::Field->new('010', '', '', 'a' => "$lccn_number");
             $record->insert_grouped_field($nf);
             my $response = $ua->get("http://xisbn.worldcat.org/webservices/xid/lccn/$lccn_number?method=getMetadata&format=json&fl=*");
-            my $metadata = decode_json($response->decoded_content);
-            $owi_number = $metadata->{'list'}->[0]->{'owi'}->[0];
+            if ($response->is_success) {
+                my $metadata = decode_json($response->decoded_content);
+                $owi_number = $metadata->{'list'}->[0]->{'owi'}->[0];
+            }
         }
         if (exists $metadata->{'list'}->[0]->{'oclcnum'} and !$oclc_number) {
             $oclc_number = $metadata->{'list'}->[0]->{'oclcnum'}->[0];
+            my $response = $ua->get("http://xisbn.worldcat.org/webservices/xid/oclcnum/$oclc_number?method=getMetadata&format=json&fl=*");
+            if ($response->is_success) {
+                my $metadata = decode_json($response->decoded_content);
+                $owi_number = $metadata->{'list'}->[0]->{'owi'}->[0];
+            }
         }
     }
 }
@@ -140,7 +153,7 @@ if ($owi_number) {
 if ($oclc_number) {
     my @idtags = $record->field('035');
     foreach my $idtag (@idtags) {
-        if ($idtag->subfield('a') =~ m#^(OCoLC)$oclc_number#) {
+        if ($idtag->subfield('a') =~ m#^\(OCoLC\)$oclc_number#) {
             $found_oclcnum = 1;
         }
     }
@@ -176,4 +189,3 @@ CREATE OR REPLACE FUNCTION conifer.marc_version() RETURNS TEXT AS $func$
 use MARC::Record;
 return $MARC::Record::VERSION;
 $func$ LANGUAGE PLPERLU;
-