2018-10-09 22:21:41 +08:00
|
|
|
require 'rubygems/package'
|
|
|
|
require 'zlib'
|
|
|
|
|
|
|
|
desc "downloads MaxMind's GeoLite2-City database"
|
2018-10-10 08:16:28 +08:00
|
|
|
task "maxminddb:get" do
|
2018-10-09 22:21:41 +08:00
|
|
|
|
2018-10-31 06:08:57 +08:00
|
|
|
def download_mmdb(name)
|
|
|
|
puts "Downloading MaxMindDb #{name}"
|
|
|
|
uri = URI("http://geolite.maxmind.com/download/geoip/database/#{name}.tar.gz")
|
|
|
|
tar_gz_archive = Net::HTTP.get(uri)
|
2018-10-09 22:21:41 +08:00
|
|
|
|
2018-10-31 06:08:57 +08:00
|
|
|
extractor = Gem::Package::TarReader.new(Zlib::GzipReader.new(StringIO.new(tar_gz_archive)))
|
|
|
|
extractor.rewind
|
2018-10-09 22:21:41 +08:00
|
|
|
|
2018-10-31 06:08:57 +08:00
|
|
|
extractor.each do |entry|
|
|
|
|
next unless entry.full_name.ends_with?(".mmdb")
|
|
|
|
|
|
|
|
filename = File.join(Rails.root, 'vendor', 'data', "#{name}.mmdb")
|
|
|
|
puts "Writing #{filename}..."
|
|
|
|
File.open(filename, "wb") do |f|
|
|
|
|
f.write(entry.read)
|
|
|
|
end
|
2018-10-09 22:21:41 +08:00
|
|
|
end
|
2018-10-31 06:08:57 +08:00
|
|
|
|
|
|
|
extractor.close
|
2018-10-09 22:21:41 +08:00
|
|
|
end
|
|
|
|
|
2018-10-31 06:08:57 +08:00
|
|
|
download_mmdb('GeoLite2-City')
|
|
|
|
download_mmdb('GeoLite2-ASN')
|
2018-10-09 22:21:41 +08:00
|
|
|
end
|