-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_component_csv
136 lines (119 loc) · 4.07 KB
/
create_component_csv
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'optparse'
require 'ostruct'
# Process the script parameters.
options = OpenStruct.new
options.output_dir = Dir.pwd
options.press = "ebc"
options.fulcrum_host = nil
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [-d output_dir] [-f production|preview|staging] -p [bar|ebc|heb] monograph_id [monograph_id...]"
opts.on('-d', '--output_dir [DIRECTORY]', 'Output directory') do |output_dir|
options.output_dir = output_dir
end
opts.on('-f', '--fulcrum_host [production|preview|staging]', 'Fulcrum environment') do |fulcrum_host|
options.fulcrum_host = fulcrum_host
end
opts.on('-p', '--press [bar|ebc]', 'Press') do |press|
options.press = press.downcase
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
exit 0
end
# Rest of the parameters is a list of monograph noids.
monograph_id_list = ARGV
fail "Error: invalid press #{options.press}" unless ["bar", "ebc", "heb"].include?(options.press)
output_dir = File.expand_path(options.output_dir)
fail "Error: output directory #{output_dir} is not valid." unless File.directory?(output_dir)
# 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")
require_relative File.join(root_dir, "lib", "fulcrum", "manifest")
# Create the serivce for retrieving the NOID.
service = UMPTG::Services::Heliotrope.new(
:fulcrum_host => options.fulcrum_host
)
puts "Using press #{options.press}"
components_list = []
monograph_id_list.each do |monograph_id|
puts "*" * 10 + " #{monograph_id} " + "*" * 10
# Retrieve the monograph manifest.
csv_body = service.monograph_export(identifier: monograph_id)
if csv_body.nil? or csv_body.empty?
puts "Error: no manifest found for noid #{monograph_id}"
next
end
manifest = UMPTG::Fulcrum::Manifest::Document.new(
:csv_body => csv_body
)
unless manifest.monograph_row.nil?
noid = manifest.monograph_row['noid']
title = manifest.monograph_row['title']
case options.press
when 'ebc'
doi = manifest.monograph_row['doi']
if doi.nil? or doi.empty?
puts "Warning: no DOI for ID #{monograph_id}"
else
sales_id = doi
end
else
# BAR and HEB presses
identifier = manifest.monograph_row['identifier(s)']
if identifier.nil? or identifier.empty?
puts "Warning: no identifier value for ID #{monograph_id}"
else
identifier_list = identifier.split(';')
identifier_list.each do |id|
id.strip.downcase!
case options.press
when 'bar'
if id.start_with?('bar_number:')
sales_id = id.split(':')[1].strip
break
end
when 'heb'
if id.match?(/^heb[0-9]{5}\.[0-9]{4}\.[0-9]{3}/)
sales_id = id
break
end
end
end
puts "Warning: no sales id found for ID #{monograph_id}" if sales_id.nil?
end
end
sales_id = "**NOT FOUND**" if sales_id.nil?
components_list << {
monograph_id: monograph_id,
noid: noid,
title: title,
sales_id: sales_id
}
end
end
# Write the file with the full values.
id_file = File.join(output_dir, "id_noid_salesid.csv")
puts "Writing CSV #{File.basename(id_file)}"
File.open(id_file, "w") do |fp|
components_list.each do |item|
fp.puts "#{item[:monograph_id]},#{item[:noid]},#{item[:sales_id]}"
#fp.puts "[#{item[:title]}|#{item[:sales_id]}]"
end
end
# Write the file for creating the components.
components_file = File.join(output_dir, "noid_salesid.csv")
puts "Writing CSV #{File.basename(components_file)}"
File.open(components_file, "w") do |fp|
components_list.each do |item|
fp.puts "#{item[:noid]},#{item[:sales_id]}"
end
end