-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapprivoiser.rb
63 lines (60 loc) · 1.51 KB
/
apprivoiser.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
require 'tempfile'
require 'open-uri'
require 'zip'
require 'find'
INPUT_PATH = 'input.csv'
PATTERN_ZIP = '*_bldg_*.gml'
PATTERN_7Z = 'bldg'
N_ATTEMPTS = 10
def apprivoiser(url, fn)
$stderr.print "apprivoiser #{url}\n"
dst_path = "#{fn}.tar.gz"
return if File.exist?(dst_path)
Dir.mktmpdir {|tmpdir|
if /zip$/.match(url)
cmd = <<-EOS
curl -C - -o #{tmpdir}/#{fn}.zip #{url}
EOS
cmd = cmd * N_ATTEMPTS
cmd += <<-EOS
unzip -q -d #{tmpdir}/#{fn} -j #{tmpdir}/#{fn}.zip '#{PATTERN_ZIP}'
EOS
print cmd
system cmd
elsif /7z$/.match(url)
cmd = <<-EOS
curl -C - -o #{tmpdir}/#{fn}.7z #{url}
EOS
cmd = cmd * N_ATTEMPTS
cmd += <<-EOS
7z x -o#{tmpdir}/7zx #{tmpdir}/#{fn}.7z -ir!"#{PATTERN_7Z}"
mkdir #{tmpdir}/#{fn}
EOS
print cmd
system cmd
Find.find("#{tmpdir}/7zx") {|path|
next unless /#{PATTERN_7Z.gsub('*', '.*')}/.match path
system <<-EOS
mv #{path} #{tmpdir}/#{fn}/#{File.basename(path)}
EOS
}
else
raise "Could not handle #{url}."
end
system <<-EOS
tar czf #{dst_path} -C #{tmpdir} #{fn}
EOS
(cid, filename) = `ipfs add #{fn}.tar.gz`.split[1..2]
$stderr.print <<-EOS
- [#{filename}](https://smb.optgeo.org/ipfs/#{cid}?filename=#{filename})
EOS
}
end
File.foreach(INPUT_PATH) {|l|
url = l.strip
next if /^#/.match(url)
fn = url.split('/')[-1].split('_')[0..2].join('_')
# next unless fn[0..1].to_i == 22
# next unless /^22220/.match fn
apprivoiser(url, fn)
}