require 'httparty' | |
require 'nokogiri' | |
require 'pry' | |
curr = {} | |
stats = nil | |
begin | |
res = HTTParty.get("http://www.xe.com/iso4217.php") | |
html = Nokogiri::HTML.parse(res) | |
links = html.css("#currencyTable td a").map { |a| a["href"] }.reject { |link| link[0] == "#" } | |
currencies = [] | |
links.each do |l| | |
curr = {} | |
uri = URI.extract(URI.encode("http://www.xe.com#{l}")).first | |
curr_description = HTTParty.get(uri) | |
desc_html = Nokogiri::HTML.parse(curr_description) | |
short_name = desc_html.css(".currencystats .factsTitle").first.text | |
curr[:short_name] = short_name[0,3] | |
stats = desc_html.css(".currencystats p").map { |p| p.text } | |
matches = stats[0].match(/Name: (.*)$/) | |
curr[:name] = matches[1] | |
matches = stats[1].match(/\s*Symbol: (.*)\s(.*):? ?(.*)/) | |
if matches.nil? | |
curr[:bad_symbol] = stats[1] | |
else | |
curr[:symbol] = matches[1] | |
curr[:minor_name] = matches[2] | |
curr[:minor_symbol] = matches[3] | |
end | |
currencies << curr | |
puts curr | |
end | |
CSV.open("data.csv", "wb") do |csv| | |
csv << ["short_name", "name", "symbol", "minor_name", "minor_symbol", "bad_symbol"] | |
currencies.each do |hash| | |
csv << hash.values_at(:short_name, :name, :symbol, :minor_name, :minor_symbol, :bad_symbol) | |
end | |
end | |
rescue StandardError => e | |
binding.pry | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment