|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# ------------------------------------------------------------ |
| 4 | +# Global setup |
| 5 | + |
| 6 | +# Don't buffer stdout or stderr |
| 7 | +$stdout.sync = true |
| 8 | +$stderr.sync = true |
| 9 | + |
| 10 | +# ------------------------------------------------------------ |
| 11 | +# Dependencies |
| 12 | + |
| 13 | +# Require gems |
| 14 | +require 'bundler/setup' |
| 15 | + |
| 16 | +# Require lib directory |
| 17 | +unless $LOAD_PATH.include?((lib_path = File.expand_path('../lib', __dir__))) |
| 18 | + puts "Adding #{lib_path} to $LOAD_PATH" |
| 19 | + $LOAD_PATH.unshift(lib_path) |
| 20 | +end |
| 21 | + |
| 22 | +require 'berkeley_library/alma' |
| 23 | +require 'marc/spec' |
| 24 | + |
| 25 | +# ------------------------------------------------------------ |
| 26 | +# Configuration |
| 27 | + |
| 28 | +# Configure Alma URLs etc. |
| 29 | +BerkeleyLibrary::Alma::Config.default! |
| 30 | + |
| 31 | +# Configure logging |
| 32 | +logger = BerkeleyLibrary::Logging::Loggers.new_readable_logger($stderr) |
| 33 | +logger.level = Logger::Severity::WARN |
| 34 | +BerkeleyLibrary::Logging.logger = logger |
| 35 | + |
| 36 | +# ------------------------------------------------------------ |
| 37 | +# Constants |
| 38 | + |
| 39 | +OCLC_NUM_RE = /(?<=\(OCoLC\))(?<num>\d+)/.freeze |
| 40 | + |
| 41 | +# ------------------------------------------------------------ |
| 42 | +# Utility methods |
| 43 | + |
| 44 | +# Read raw IDs from STDIN |
| 45 | +def raw_ids |
| 46 | + @raw_ids ||= Enumerator.new do |y| |
| 47 | + $stdin.each_line(chomp: true) do |ln| |
| 48 | + y << ln.strip |
| 49 | + end |
| 50 | + end |
| 51 | +end |
| 52 | + |
| 53 | +def ids_035a_for(record_id) |
| 54 | + return unless record_id |
| 55 | + return unless (marc_record = record_id.get_marc_record) |
| 56 | + |
| 57 | + MARC::Spec.find('035$a', marc_record) |
| 58 | +end |
| 59 | + |
| 60 | +def oclc_nums_for(record_id) |
| 61 | + return [] unless (ids_035a = ids_035a_for(record_id)) |
| 62 | + |
| 63 | + ids_035a.filter_map do |sf_035a| |
| 64 | + next unless (val = sf_035a.value) |
| 65 | + next unless (md = OCLC_NUM_RE.match(val)) |
| 66 | + |
| 67 | + md[:num] |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +# ------------------------------------------------------------ |
| 72 | +# Main program |
| 73 | + |
| 74 | +raw_ids.each do |raw_id| |
| 75 | + record_id = BerkeleyLibrary::Alma::RecordId.parse(raw_id) |
| 76 | + oclc_nums = oclc_nums_for(record_id) |
| 77 | + puts [raw_id, *oclc_nums].join("\t") |
| 78 | +end |
0 commit comments