Created
April 28, 2012 16:33
-
-
Save zph/2520111 to your computer and use it in GitHub Desktop.
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 'spreadsheet' | |
require 'pry' | |
require 'open-uri' | |
require 'yaml' | |
# Download spreadsheet | |
website = "http://www.eia.gov/dnav/ng/hist/n9190us3m.htm" | |
web_base = "http://www.eia.gov/dnav/ng/hist_xls/" | |
file_name = open('http://www.eia.gov/dnav/ng/hist/n9190us3m.htm').read.scan(/([\w]+\.xls)/)[0][0] | |
if file_name | |
web_location = web_base + file_name | |
else | |
web_location = "http://www.eia.gov/dnav/ng/hist_xls/N9190US3a.xls" | |
end | |
xls = open(web_location).read | |
xls_location = "/tmp/#{file_name}#{$$}" | |
File.open(xls_location, 'w') { |f| f.write xls } | |
# save_location = '' | |
# Parse spreadsheet | |
Spreadsheet.client_encoding = 'UTF-8' | |
book = Spreadsheet.open(xls_location) | |
@sheet1 = book.worksheet 1 | |
@info = {} | |
def parse_excel | |
41.upto(1000).each do |i| # Jump ahead to useful part of excel worksheet | |
r = @sheet1.row(i) | |
next if r[0].nil? || r[1].nil? # Avoid nil errors | |
@info[r[0].to_date.strftime('%Y-%m-%d')] = sprintf "%.2f", r[1] # Sprintf for 2 decimals | |
end | |
@info | |
end | |
# Cleanup | |
File.unlink(xls_location) | |
# Run parse_excel function to do work | |
parse_excel | |
# Write to local location | |
# | |
# | |
# To pull most recent price | |
# @info.sort.last | |
output = '../logs/gas_price.yml' | |
File.unlink(output) if File.exists?(output) | |
File.open(output, 'w') { |f| f.write @info.sort.to_yaml } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment