-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve_monograph_manifest
74 lines (62 loc) · 2.11 KB
/
retrieve_monograph_manifest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
# frozen_string_literal: true
# This script retrieves one or more monograph manifest files from Fulcrum.
#
# Invoke this script without any parameters to display the usage.
require 'optparse'
require 'ostruct'
require 'csv'
# Process the script parameters.
options = OpenStruct.new
options.directory_path = nil
options.fulcrum_host = nil
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [-f production|preview|staging] [--output_dir <output_dir>] <monograph_id> [<monograph_id>..]"
opts.on('-f', '--fulcrum_host [production|preview|staging]', 'Fulcrum environment') do |fulcrum_host|
options.fulcrum_host = fulcrum_host
end
opts.on('-o', '--output_dir [DIRECTORY]', 'Path of output directory') do |dir_path|
options.directory_path = dir_path
end
opts.on_tail('-h', '--help', 'Print this help message') do
puts opts
exit 0
end
end
option_parser.parse!(ARGV)
if ARGV.count < 1
puts option_parser.help
return
end
# Determine the root directory of the code base.
script_dir = File.expand_path(File.dirname(__FILE__))
root_dir = File.dirname(script_dir)
require_relative File.join(root_dir, 'lib', 'services')
if options.directory_path.nil?
options.directory_path = Dir.pwd
else
require 'fileutils'
FileUtils.mkpath options.directory_path
end
# Rest of the parameters is a list of monograph noids.
monograph_id_list = ARGV
# Create the serivce for retrieving the NOID.
service = UMPTG::Services::Heliotrope.new(
:fulcrum_host => options.fulcrum_host
)
monograph_id_list.each do |monograph_id|
# Attempt to download the monograph manifest.
service_response = service.monograph_export(noid: monograph_id)
unless service_response.empty?
# Not empty, save the manifest using its id as the file name.
output_path = File.join(options.directory_path, File.basename(monograph_id) + ".csv")
puts "Writing CSV file #{output_path}"
File.open(output_path, "w") do |f|
service_response.each do |id,manifest_list|
manifest_list.each do |m|
f.write(m)
end
end
end
end
end