Skip to content

Commit 4bd123d

Browse files
committed
Add alma-oclc-lookup script
1 parent 389a3ae commit 4bd123d

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# (next)
2+
3+
- Add `alma-oclc-lookup` script.
4+
15
# 0.0.7.1 (24 May 2022)
26

37
- Set minimum Nokogiri version to address

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,26 @@ b1234
263263
9912348245906531 9912348245906531
264264
b110824349 b110824349 991038544199706532
265265
```
266+
267+
268+
### `alma-oclc-lookup`: 035$a OCLC number lookup
269+
270+
The `alma-oclc-lookup` script takes one ore more record IDs (either Millennium bib
271+
numbers or Alma MMS IDs) and attempts to read the corresponding MARC records via
272+
[SRU](https://developers.exlibrisgroup.com/alma/integrations/sru/) and extract
273+
the OCLC number(s) from the 035$a field.
274+
275+
#### Output format
276+
277+
The output is tab-separated, in the form
278+
279+
```none
280+
<record ID> <OCLC number(s)…>
281+
```
282+
283+
E.g.
284+
285+
```none
286+
$ echo '991051353589706532' | bin/alma-oclc-lookup ✹ ✚main ‹ruby-2.7.5›
287+
991051353589706532 1097551039
288+
```

bin/alma-oclc-lookup

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)