Created
October 18, 2015 15:51
-
-
Save jamesevers/1bbdc0a106e46720f8e0 to your computer and use it in GitHub Desktop.
a currency converter that uses the yahoo finance API to find exchange rates
This file contains hidden or 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
import urllib2 | |
import json | |
def currencyConverter(from_value,target_value,currency_quantity): | |
yahoo_exchange_url = "https://query.yahooapis.com/v1/public/yql" | |
yahoo_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20("'+currency_from+currency_to+'")' | |
yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" | |
try: | |
yql_response = urllib2.urlopen(yql_query_url) | |
try: | |
yql_json = json.loads(yql_response.read()) | |
currency_output = currency_input * float(yql_json['query']['results']['rate']['Rate']) | |
return currency_output | |
except (ValueError, KeyError, TypeError): | |
return "JSON format error" | |
except IOError, e: | |
if hasattr(e, 'code'): | |
return e.code | |
elif hasattr(e, 'reason'): | |
return e.reason | |
currency_quantity = int(raw_input("How much currency should be converted?: ")) | |
from_value = str(raw_input("What is the currency that needs to be converted?: ")) | |
target_value = str(raw_input("Please enter currency to convert to: ")) | |
exchange_rate = currencyConverter(from_value,target_value,currency_quantity) | |
print exchange_rate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment