Posted by Anurag in
utilities
Monday, January 5. 2009
I keep looking at stuff and try guessing its price in INR. Look around for calculator, check USD exchange rate and punch it in.
#!/usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'
FPAGE = 'http://finance.yahoo.com/q?s=USDINR=X'
XPATH = '/html/body/div/div[2]/div[4]/div[2]/div/div[2]/table/tr/td/big/b/span/'
amount = (ARGV.length == 1) ? ARGV[0].to_f : 1
amount = 1 if amount == 0
doc = Hpricot(URI.parse( FPAGE ).read)
element = doc.search( XPATH )
exchange_rate = element[0].to_s.to_f
puts "#{amount} USD = #{amount * exchange_rate} INR"
Now i no longer have to. Just running $ usdinr 197.21 gives the amount in INR. Save this file in /usr/bin/usdinr and give execute permissions.
[Update 2009-02-19: Updated the script with new XPath]
[Update 2010-12-08: Updated the script with new Yahoo finance page and XPath]