This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreports.rb
executable file
·128 lines (109 loc) · 3.21 KB
/
reports.rb
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
#!/usr/bin/env ruby
require 'io/console'
require 'net/http'
require 'uri'
require 'json'
require_relative 'astools'
@repo = "/repositories/2"
@path = "/home/kevin"
data_model_opts = {
'1' => "resources",
'2' => "archival_objects",
'3' => "agents",
'4' => "subjects",
'5' => "digital_objects",
'6' => "accessions",
'7' => "top_containers",
'8' => "container_profiles",
'9' => "events"
}
agent_types = ["people", "corporate_entities", "families", "software"]
def selector(type, opts)
puts "Select a #{type}:"
opts.each do |k,v|
puts "* (#{k}) #{v}"
end
opt = gets.chomp
if opts.has_key?(opt)
return opts[opt]
else
puts "Invalid entry, try again."
selector(type, opts)
end
end
def get_report_type(model)
opts = case model
when "resources"
{ '1' => "json", '2' => "ead", '3' => "marc" }
when "agents"
{ '1' => "json", '2' => "eac" }
else
{ '1' => "json" }
end
selector("report type", opts)
end
def write_single_file(filename, data)
File.delete?(filename) if File.exist?(filename)
File.open(filename, 'w') {|f| f.write data}
end
def run_report(type, model)
request_url = case model
when /subjects/, /^agents/, /container_profiles/
"/#{model}"
else
"#{@repo}/#{model}"
end
ids = ASTools::HTTP.get_json(request_url, 'all_ids' => true)
model = model.gsub(/agents\//,'').gsub('software', 'softwares')
case type
# generic JSON output
when "json"
file_output = "#{@path}/#{model}_report.json"
File.delete(file_output) if File.exist?(file_output)
f = File.open(file_output, 'a')
f.write("{\"#{model}\":[")
ids.each_with_index do |id, i|
print "Writing #{model} #{type} record #{i+1} of #{ids.length}... \r"
json = ASTools::HTTP.get_json("#{request_url}/#{id}")
f.write(json.to_json)
f.write(",") unless i == ids.length - 1
end
f.write("]}")
f.close unless f.nil?
# Encoded Archival Context (EAC) output
when "eac"
url = "#{@repo}/archival_contexts/#{model}"
ids.each_with_index do |id, i|
print "Writing #{type} record #{i+1} of #{ids.length}... \r"
data = ASTools::HTTP.get_data("#{request_url}/#{id}.xml")
write_single_file("#{@path}/eac/#{model}_#{id}_eac.xml", data)
end
# MARCXML output
when "marc"
ids.each_with_index do |id, i|
print "Writing #{type} record #{i+1} of #{ids.length}... \r"
num = ASTools::HTTP.get_json("/#{@repo}/#{model}/#{id}")['id_0'].downcase
data = ASTools::HTTP.get_data("/#{@repo}/#{model}/marc21/#{id}.xml")
write_single_file("#{@path}/marc/#{num}_marc.xml", data)
end
# Encoded Archival Description output
when "ead"
ids.each_with_index do |id, i|
print "Writing #{type} record #{i+1} of #{ids.length}... \r"
num = ASTools::HTTP.get_json("#{@repo}/resources/#{id}")['id_0'].downcase
data = ASTools::HTTP.get_data("#{@repo}/resource_descriptions/#{id}.xml")
write_single_file("#{@path}/ead/#{num}_ead.xml", data)
end
end
puts "\nDone."
end
ASTools::User.get_session
model = selector("data model", data_model_opts)
type = get_report_type(model)
if model == "agents"
agent_types.each do |atype|
run_report(type, "#{model}/#{atype}")
end
else
run_report(type, model)
end