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
|
# File 'lib/unicoder/downloader.rb', line 7
def self.fetch(identifier,
unicode_version: CURRENT_UNICODE_VERSION,
emoji_version: CURRENT_EMOJI_VERSION,
destination_directory: LOCAL_DATA_DIRECTORY,
destination: nil,
filename: nil
)
filename = UNICODE_FILES[identifier.to_sym] || filename
raise ArgumentError, "No valid file identifier or filename given" if !filename
filename = filename.dup
filename.sub! 'UNICODE_VERSION', unicode_version
filename.sub! 'EMOJI_VERSION', emoji_version
filename.sub! 'EMOJI_RELATED_VERSION', EMOJI_RELATED_UNICODE_VERSIONS[emoji_version]
if filename =~ /\A(https?|ftp):\/\//
source = filename
destination ||= destination_directory + filename.sub(/\A(https?|ftp):\//, "")
else
source = UNICODE_DATA_ENDPOINT + filename
destination ||= destination_directory + filename
end
puts "GET #{source} => #{destination}"
if source =~ %r[^(?<outer_path>.*).zip/(?<inner_path>.*)$]
zip = true
source = $~[:outer_path] + ".zip"
inner_zip_filename = $~[:inner_path]
if destination =~ %r[^(?<outer_path>.*).zip/(?<inner_path>.*)$]
destination = $~[:outer_path] + ".zip"
destination_files = $~[:outer_path]
else
raise "uncoder bug"
end
else
zip = false
end
if File.exist?(destination)
puts "Skipping download of #{source} (already exists)"
else
URI.open(source){ |f|
FileUtils.mkdir_p(File.dirname(destination))
File.write(destination, f.read)
}
end
if zip
unzip(destination, [inner_zip_filename], destination_files)
end
rescue => e
$stderr.puts "#{e.class}: #{e.message}"
end
|