forked from kevintyll/ofac
/
ofac_sdn_loader.rb
323 lines (282 loc) · 11.2 KB
/
ofac_sdn_loader.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
require 'net/http'
require 'active_record'
require 'active_record/connection_adapters/mysql_adapter'
class OfacSdnLoader
#Loads the most recent file from http://www.treas.gov/offices/enforcement/ofac/sdn/delimit/index.shtml
def self.load_current_sdn_file
puts "Reloading OFAC sdn data"
puts "Downloading OFAC data from http://www.treas.gov/offices/enforcement/ofac/sdn"
yield "Downloading OFAC data from http://www.treas.gov/offices/enforcement/ofac/sdn" if block_given?
#get the 3 data files
sdn = Tempfile.new('sdn')
bytes = sdn.write(Net::HTTP.get(URI.parse('http://www.treasury.gov/ofac/downloads/sdn.pip')))
sdn.rewind
if bytes == 0 || convert_line_to_array(sdn.readline).size != 12
puts "Trouble downloading file. The url may have changed."
yield "Trouble downloading file. The url may have changed." if block_given?
return
else
sdn.rewind
end
address = Tempfile.new('sdn')
address.write(Net::HTTP.get(URI.parse('http://www.treasury.gov/ofac/downloads/add.pip')))
address.rewind
alt = Tempfile.new('sdn')
alt.write(Net::HTTP.get(URI.parse('http://www.treasury.gov/ofac/downloads/alt.pip')))
alt.rewind
if OfacSdn.connection.kind_of?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
puts "Converting file to csv format for Mysql import. This could take several minutes."
yield "Converting file to csv format for Mysql import. This could take several minutes." if block_given?
csv_file = convert_to_flattened_csv(sdn, address, alt)
bulk_mysql_update(csv_file)
else
active_record_file_load(sdn, address, alt)
end
sdn.close
@address.close
@alt.close
end
private
#convert the file's null value to an empty string
#and removes " chars.
def self.clean_file_string(line)
line.gsub!(/-0-(\s)?/, '')
line.gsub!(/\n/, '')
line.gsub(/\"/, '')
end
#split the line into an array
def self.convert_line_to_array(line)
clean_file_string(line).split('|') unless line.nil?
end
#return an 2 arrays of the records matching the sdn primary key
#1 array of address records and one array of alt records
def self.foreign_key_records(sdn_id)
address_records = []
alt_records = []
#the first element in each array is the primary and foreign keys
#we are denormalizing the data
if @current_address_hash && @current_address_hash[:id] == sdn_id
address_records << @current_address_hash
loop do
@current_address_hash = address_text_to_hash(@address.gets)
if @current_address_hash && @current_address_hash[:id] == sdn_id
address_records << @current_address_hash
else
break
end
end
end
if @current_alt_hash && @current_alt_hash[:id] == sdn_id
alt_records << @current_alt_hash
loop do
@current_alt_hash = alt_text_to_hash(@alt.gets)
if @current_alt_hash && @current_alt_hash[:id] == sdn_id
alt_records << @current_alt_hash
else
break
end
end
end
return address_records, alt_records
end
def self.sdn_text_to_hash(line)
unless line.nil?
value_array = convert_line_to_array(line)
{:id => value_array[0],
:name => value_array[1],
:sdn_type => value_array[2],
:program => value_array[3],
:title => value_array[4],
:vessel_call_sign => value_array[5],
:vessel_type => value_array[6],
:vessel_tonnage => value_array[7],
:gross_registered_tonnage => value_array[8],
:vessel_flag => value_array[9],
:vessel_owner => value_array[10],
:remarks => value_array[11]
}
end
end
def self.address_text_to_hash(line)
unless line.nil?
value_array = convert_line_to_array(line)
{:id => value_array[0],
:address => value_array[2],
:city => value_array[3],
:country => value_array[4],
:address_remarks => value_array[5]
}
end
end
def self.alt_text_to_hash(line)
unless line.nil?
value_array = convert_line_to_array(line)
{:id => value_array[0],
:alternate_identity_type => value_array[2],
:alternate_identity_name => value_array[3],
:alternate_identity_remarks => value_array[4]
}
end
end
def self.convert_hash_to_mysql_import_string(record_hash)
# empty field for id to be generated by mysql.
new_line = "``|" +
# :name
"`#{record_hash[:name]}`|" +
# :sdn_type
"`#{record_hash[:sdn_type]}`|" +
# :program
"`#{record_hash[:program]}`|" +
# :title
"`#{record_hash[:title]}`|" +
# :vessel_call_sign
"`#{record_hash[:vessel_call_sign]}`|" +
# :vessel_type
"`#{record_hash[:vessel_type]}`|" +
# :vessel_tonnage
"`#{record_hash[:vessel_tonnage]}`|" +
# :gross_registered_tonnage
"`#{record_hash[:gross_registered_tonnage]}`|" +
# :vessel_flag
"`#{record_hash[:vessel_flag]}`|" +
# :vessel_owner
"`#{record_hash[:vessel_owner]}`|" +
# :remarks
"`#{record_hash[:remarks]}`|" +
# :address
"`#{record_hash[:address]}`|" +
# :city
"`#{record_hash[:city]}`|" +
# :country
"`#{record_hash[:country]}`|" +
# :address_remarks
"`#{record_hash[:address_remarks]}`|" +
# :alternate_identity_type
"`#{record_hash[:alternate_identity_type]}`|" +
# :alternate_identity_name
"`#{record_hash[:alternate_identity_name]}`|" +
# :alternate_identity_remarks
"`#{record_hash[:alternate_identity_remarks]}`|" +
#:created_at
"`#{Time.now.to_s(:db)}`|" +
# updated_at
"`#{Time.now.to_s(:db)}`" + "\n"
new_line
end
def self.convert_to_flattened_csv(sdn_file, address_file, alt_file)
@address = address_file
@alt = alt_file
csv_file = Tempfile.new("ofac") # create temp file for converted csv format.
#get the first line from the address and alt files
@current_address_hash = address_text_to_hash(@address.gets)
@current_alt_hash = alt_text_to_hash(@alt.gets)
start = Time.now
sdn_file.each_with_index do |line, i|
#initialize the address and alt atributes to empty strings
address_attributes = address_text_to_hash("|||||")
alt_attributes = alt_text_to_hash("||||")
sdn_attributes = sdn_text_to_hash(line)
#get the foreign key records for this sdn
address_records, alt_records = foreign_key_records(sdn_attributes[:id])
if address_records.empty?
#no matching address records, so initialized blank values will be used.
if alt_records.empty?
#no matching address records, so initialized blank values will be used.
csv_file.syswrite(convert_hash_to_mysql_import_string(sdn_attributes.merge(address_attributes).merge(alt_attributes)))
else
alt_records.each do |alt|
csv_file.syswrite(convert_hash_to_mysql_import_string(sdn_attributes.merge(address_attributes).merge(alt)))
end
end
else
address_records.each do |address|
if alt_records.empty?
#no matching address records, so initialized blank values will be used.
csv_file.syswrite(convert_hash_to_mysql_import_string(sdn_attributes.merge(address).merge(alt_attributes)))
else
alt_records.each do |alt|
csv_file.syswrite(convert_hash_to_mysql_import_string(sdn_attributes.merge(address).merge(alt)))
end
end
end
end
if (i % 1000 == 0) && (i > 0)
puts "#{i} records processed."
yield "#{i} records processed." if block_given?
end
end
puts "File conversion ran for #{(Time.now - start) / 60} minutes."
yield "File conversion ran for #{(Time.now - start) / 60} minutes." if block_given?
return csv_file
end
def self.active_record_file_load(sdn_file, address_file, alt_file)
@address = address_file
@alt = alt_file
#OFAC data is a complete list, so we have to dump and load
OfacSdn.delete_all
#get the first line from the address and alt files
@current_address_hash = address_text_to_hash(@address.gets)
@current_alt_hash = alt_text_to_hash(@alt.gets)
attributes = {}
sdn_file.each_with_index do |line, i|
#initialize the address and alt atributes to empty strings
address_attributes = address_text_to_hash("|||||")
alt_attributes = alt_text_to_hash("||||")
sdn_attributes = sdn_text_to_hash(line)
#get the foreign key records for this sdn
address_records, alt_records = foreign_key_records(sdn_attributes[:id])
if address_records.empty?
#no matching address records, so initialized blank values will be used.
if alt_records.empty?
#no matching address records, so initialized blank values will be used.
attributes = sdn_attributes.merge(address_attributes).merge(alt_attributes)
attributes.delete(:id)
OfacSdn.create(attributes)
else
alt_records.each do |alt|
attributes = sdn_attributes.merge(address_attributes).merge(alt)
attributes.delete(:id)
OfacSdn.create(attributes)
end
end
else
address_records.each do |address|
if alt_records.empty?
#no matching address records, so initialized blank values will be used.
attributes = sdn_attributes.merge(address).merge(alt_attributes)
attributes.delete(:id)
OfacSdn.create(attributes)
else
alt_records.each do |alt|
attributes = sdn_attributes.merge(address).merge(alt)
attributes.delete(:id)
OfacSdn.create(attributes)
end
end
end
end
if (i % 5000 == 0) && (i > 0)
puts "#{i} records processed."
yield "#{i} records processed." if block_given?
end
end
end
# For mysql, use:
# LOAD DATA LOCAL INFILE 'ssdm1.csv' INTO TABLE death_master_files FIELDS TERMINATED BY '|' ENCLOSED BY "`" LINES TERMINATED BY '\n';
# This is a much faster way of loading large amounts of data into mysql. For information on the LOAD DATA command
# see http://dev.mysql.com/doc/refman/5.1/en/load-data.html
def self.bulk_mysql_update(csv_file)
puts "Deleting all records in ofac_sdn..."
yield "Deleting all records in ofac_sdn..." if block_given?
#OFAC data is a complete list, so we have to dump and load
OfacSdn.delete_all
puts "Importing into Mysql..."
yield "Importing into Mysql..." if block_given?
mysql_command = <<-TEXT
LOAD DATA LOCAL INFILE '#{csv_file.path}' REPLACE INTO TABLE ofac_sdns FIELDS TERMINATED BY '|' ENCLOSED BY "`" LINES TERMINATED BY '\n';
TEXT
OfacSdn.connection.execute(mysql_command)
puts "Mysql import complete."
yield "Mysql import complete." if block_given?
end
end