Print

Print


Terry, wonderful and thank you. Given an OCLC number, I am using the following Perl code to extract call numbers from the WorldCat Classify service. The result is tab-delimited output of OCLC numbers and call numbers and/or error messages. --ELM


#!/usr/bin/perl

# call_numbers.pl - given OCLC numbers, extract call numbers from OCLC classify service

# Eric Lease Morgan <[log in to unmask]>
# July 17, 2012 - first cut; "Thanks to Terry Reese for suggesting classify!"


# configure
use constant QUERY => 'http://classify.oclc.org/classify2/Classify?oclc=##OCLC##&summary=true';

# require
use LWP;
use strict;
use XML::XPath;

# initialize
my $count = 0;
my $ua    = LWP::UserAgent->new;
$|        = 1;

# process each OCLC number
while ( <DATA> ) {

  # build the query and get it
  chop;
  my $oclc     =  $_;
  my $url      =  QUERY;
  $url         =~ s/##OCLC##/$oclc/e;
  my $response =  $ua->request( HTTP::Request->new( GET => $url ) );
  
  # check for successful HTTP request
  if ( $response->is_success ) {
  
    # (re-)initialize
    my $xpath = XML::XPath->new( xml => $response->content );
    
    # check for successful query
    if ( $xpath->getNodeText( 'classify/response/@code' ) eq 0 ) {
    
      # extract and output
      my $call_number = $xpath->getNodeText( '/classify/recommendations/lcc/mostPopular/@nsfa' );
      print "$oclc\t$call_number\n";
    
    }
    
    # error
    else { print "CLASSIFY FAILURE\n$url\n" }
  
  }
  
  # error
  else { print "HTTP FAILURE\t$url\n" }
  
  # increment and exit
  #$count++;
  #last if ( $count == 100 );

}


__DATA__
2239
2239
5478
10536
10799
10889
11887
16142
16198
17411
21885
22047
24095
28315
28351