Created
May 1, 2015 20:15
-
-
Save paulRbr/51aad9711edbc41fad73 to your computer and use it in GitHub Desktop.
Import BLS data into capitainetrain/stations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "csv" | |
gem "gares", "> 2.0.0.pre.pre" | |
require "gares" | |
parameters = { | |
:headers => true, | |
:col_sep => ';', | |
:encoding => 'UTF-8' | |
} | |
STATIONS = CSV.read("stations.csv", parameters) | |
STATIONS_BY_ID = STATIONS.inject({}) { |hash, station| hash[station["id"]] = station; hash } | |
parameters = { | |
:headers => true, | |
:col_sep => ',', | |
:encoding => 'UTF-8' | |
} | |
PRIMARY_KEY = "point de vente" | |
BLSS = CSV.read("bls.csv", parameters) | |
BLS_IN_STATION_BY_NAME = BLSS.inject({}) do |hash, bls| | |
hash[bls[PRIMARY_KEY]] = bls if bls["gare / boutique / bls"] == "gare" | |
hash | |
end | |
i = 0 | |
n = BLS_IN_STATION_BY_NAME.keys.size | |
reset = "\r\e[0K" | |
new_column = 'has_bls' | |
STATIONS.headers << new_column | |
BLS_IN_STATION_BY_NAME.map do |name, bls| | |
begin | |
gare = Gares::Station.search(name).first | |
has_bls = gare && bls['bls (borne libre service)'] == 'oui' ? 't' : 'f' | |
if gare && gare.id && STATIONS_BY_ID[gare.id.to_s] | |
STATIONS_BY_ID[gare.id.to_s][new_column] = has_bls | |
end | |
rescue => e | |
puts "#{reset}Error #{e} for id=#{gare.id}" | |
ensure | |
print "#{reset}[#{i += 1}/#{n} - #{(i.to_f / n * 100.0).round(1)}%]" | |
end | |
end | |
STATIONS_BY_ID.values.map do |station| | |
station[new_column] ||= nil | |
end | |
File.open('stations.csv', 'w') do |file| | |
file.write STATIONS.to_csv(:col_sep => ";") | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment