Missile task for loading and unpacking

I would like to update the table of cities every week to reflect changes in cities around the world. I am creating a Rake task for this purpose. If possible, I would like to do this without adding another gem dependency .

The reserved file is a public zipped file at geonames.org/15000cities.zip .

My attempt:

require 'net/http'
require 'zip'

namespace :geocities do
  desc "Rake task to fetch Geocities city list every 3 days"
  task :fetch do

    uri = URI('http://download.geonames.org/export/dump/cities15000.zip')
    zipped_folder = Net::HTTP.get(uri) 

    Zip::File.open(zipped_folder) do |unzipped_folder| #erroring here
      unzipped_folder.each do |file|
        Rails.root.join("", "list_of_cities.txt").write(file)
      end
    end
  end
end

Return from rake geocities:fetch

rake aborted!
ArgumentError: string contains null byte

, list_of_cities.txt. , , , db . ( , db, , , .)

+4
2

zipped_folder , :

require 'net/http'                                                              
require 'zip'                                                                   

namespace :geocities do                                                         
  desc "Rake task to fetch Geocities city list every 3 days"                    
  task :fetch do                                                                

    uri = URI('http://download.geonames.org/export/dump/cities15000.zip')                          
    zipped_folder = Net::HTTP.get(uri)                                          

    File.open('cities.zip', 'wb') do |file|                                      
      file.write(zipped_folder)                                                 
    end                                                                         

    zip_file = Zip::File.open('cities.zip')                                     
    zip_file.each do |file|                                                     
      file.extract
    end                                                                         
  end                                                                           
end

zip , cities15000.txt.
cities15000.txt .

, file.extract :

zip_file.each do |file|                                                     
    file.extract('list_of_cities.txt')
end 
+3

, , wget unzip:

namespace :geocities do
  desc "Rake task to fetch Geocities city list every 3 days"
  task :fetch do
     `wget -c --tries=10 http://download.geonames.org/export/dump/cities15000.zip | unzip`
  end
end
0

Source: https://habr.com/ru/post/1608158/


All Articles