Use token-based access to xID to churn through records
authorDan Scott <dscott@laurentian.ca>
Wed, 21 Jan 2015 17:40:49 +0000 (12:40 -0500)
committerDan Scott <dscott@laurentian.ca>
Wed, 21 Jan 2015 17:40:49 +0000 (12:40 -0500)
Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/src/sql/Pg/update_marc_records_in_database.sql

index 214ca27..f7f8cab 100644 (file)
@@ -1,3 +1,20 @@
+CREATE OR REPLACE FUNCTION conifer.external_ip() RETURNS text AS $func$
+use strict;
+use warnings;
+use LWP::UserAgent;
+
+my $ua = LWP::UserAgent->new;
+$ua->timeout(20);
+my $ip = $ua->get('http://ipinfo.io/ip')->decoded_content;
+chomp($ip);
+return $ip;
+$func$ LANGUAGE PLPERLU STABLE;
+
+CREATE TABLE conifer.settings (setting TEXT, value TEXT);
+INSERT INTO conifer.settings (setting, value) SELECT 'ipaddress', conifer.external_ip();
+INSERT INTO conifer.settings (setting, value) VALUES ('xidtoken', '');
+INSERT INTO conifer.settings (setting, value) VALUES ('xidsecret', '');
+
 CREATE OR REPLACE FUNCTION conifer.enrich_uris(record BIGINT) RETURNS TEXT AS $func$
 use strict;
 use MARC::Record;
@@ -7,15 +24,23 @@ use Encode;
 use Unicode::Normalize;
 use LWP::UserAgent;
 use JSON::XS;
+use Digest::MD5;
 
 MARC::Charset->assume_unicode(1);
 
+my $md5 = Digest::MD5->new;
 my $json = new JSON::XS;
 $json->latin1(1);
 
 my $q = spi_prepare('SELECT marc FROM biblio.record_entry WHERE id = $1', 'BIGINT');
 my $marc = spi_exec_prepared($q, $_[0])->{rows}->[0]->{marc};
 
+# Move these into org_unit settings eventually
+$q = spi_prepare('SELECT value FROM conifer.settings WHERE setting = $1', 'TEXT');
+my $ip_address = spi_exec_prepared($q, 'ipaddress')->{rows}->[0]->{value};
+my $xid_token = spi_exec_prepared($q, 'xidtoken')->{rows}->[0]->{value};
+my $xid_secret = spi_exec_prepared($q, 'xidsecret')->{rows}->[0]->{value};
+
 my $record = MARC::Record->new_from_xml($marc);
 
 my @eights = $record->field('856');
@@ -107,7 +132,10 @@ if ($lccn_number) {
     # trim the whitespace
     $lccn_number =~ s{^\s*(\S+)\s*$}{$1};
     $lccn->update('a', $lccn_number);
-    my $response = $ua->get("http://xisbn.worldcat.org/webservices/xid/lccn/$lccn_number?method=getMetadata&format=json&fl=*");
+    my $base_req= "http://xisbn.worldcat.org/webservices/xid/lccn/$lccn_number";
+    $md5->add("$base_req|$ip_address|$xid_secret");
+    my $hash = $md5->hexdigest;
+    my $response = $ua->get("$base_req?method=getMetadata&format=json&fl=*&token=$xid_token&hash=$hash");
     my $metadata = $json->decode($response->decoded_content);
     if ($response->is_success) {
         $oclc_number = $metadata->{'list'}->[0]->{'oclcnum'}->[0];
@@ -129,14 +157,20 @@ if ($lccn_number) {
         # 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=*");
+        my $base_req= "http://xisbn.worldcat.org/webservices/xid/isbn/$isbn";
+        $md5->add("$base_req|$ip_address|$xid_secret");
+        my $hash = $md5->hexdigest;
+        my $response = $ua->get("$base_req?method=getMetadata&format=json&fl=*&token=$xid_token&hash=$hash");
         next unless $response->is_success;
         my $metadata = $json->decode($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_fields_ordered($nf);
-            my $response = $ua->get("http://xisbn.worldcat.org/webservices/xid/lccn/$lccn_number?method=getMetadata&format=json&fl=*");
+            my $base_req= "http://xisbn.worldcat.org/webservices/xid/lccn/$lccn_number";
+            $md5->add("$base_req|$ip_address|$xid_secret");
+            my $hash = $md5->hexdigest;
+            my $response = $ua->get("$base_req?method=getMetadata&format=json&fl=*&token=$xid_token&hash=$hash");
             if ($response->is_success) {
                 my $lccn_metadata = $json->decode($response->decoded_content);
                 $owi_number = $lccn_metadata->{'list'}->[0]->{'owi'}->[0];
@@ -144,7 +178,10 @@ if ($lccn_number) {
         }
         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=*");
+            my $base_req= "http://xisbn.worldcat.org/webservices/xid/oclcnum/$oclc_number";
+            $md5->add("$base_req|$ip_address|$xid_secret");
+            my $hash = $md5->hexdigest;
+            my $response = $ua->get("$base_req?method=getMetadata&format=json&fl=*&token=$xid_token&hash=$hash");
             if ($response->is_success) {
                 my $isbn_metadata = $json->decode($response->decoded_content);
                 $owi_number = $isbn_metadata->{'list'}->[0]->{'owi'}->[0];