Skip to content

Instantly share code, notes, and snippets.

@Nephos
Created April 27, 2018 11:49
Show Gist options
  • Save Nephos/18daa275a8270618e8c5bef591c4cc10 to your computer and use it in GitHub Desktop.
Save Nephos/18daa275a8270618e8c5bef591c4cc10 to your computer and use it in GitHub Desktop.
require "json"
require "uri"
require "csv"
SLEEP = 3
errors = []
output_io = ARGV[1] ? File.open(ARGV[1], "w") : STDOUT
errors_io = ARGV[2] ? File.open(ARGV[2], "w") : STDERR
lines = CSV.read(ARGV[0], headers: true, col_sep: ',')
average = 0
output = lines.map.with_index do |row, idx|
next if row["type"] != "sncf"
time_start = Time.now
city = { "coord" => row["coord"], "name" => row["city"] }
res = nil
begin
coord = city["coord"].split(",").map(&:strip)
search = city["name"]
res = `curl "https://nominatim.openstreetmap.org/reverse?format=json&lat=#{coord[0]}&lon=#{coord[1]}" 2> /dev/null`
json = JSON.parse res
agglomeration = json["address"]["city"] || json["address"]["town"] || json["address"]["village"] || json["address"]["county"] || json["address"]["state"]
row["city"] = agglomeration
output_io.puts(row.to_h.to_json)
output_io.flush
sleep SLEEP
time_end = Time.now
duration = (time_end - time_start)
average = (average * (idx + 1) + duration) / (idx + 2)
remaining = average * (lines.size - idx - 1).round
puts "Last request: #{duration.round(2)} seconds (+ #{SLEEP})."
puts "Average time: #{average.round(2)} seconds (+ #{SLEEP})."
puts "Remaining time: #{(remaining / 60 / 60).round} hours, #{(remaining / 60 % 60).round}, minutes #{(remaining % 60).round} seconds."
rescue
errors_io.puts({ "error" => { "city" => city, "res" => res } }.to_json)
errors_io.flush
nil
end
end.compact
output_io.close
errors_io.close
if ARGV[1]
data = File.read(ARGV[1]).split("\n").join(",\n")
File.write(ARGV[1], "[" + data + "]")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment