Skip to content

Instantly share code, notes, and snippets.

@iterion
Created September 1, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iterion/0fdecf6ec9cf722e2fd6b313c4b8c834 to your computer and use it in GitHub Desktop.
Save iterion/0fdecf6ec9cf722e2fd6b313c4b8c834 to your computer and use it in GitHub Desktop.
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