Skip to content

Instantly share code, notes, and snippets.

@dionysio
Created January 6, 2016 00:46
Show Gist options
  • Save dionysio/752437f537349f6aec8a to your computer and use it in GitHub Desktop.
Save dionysio/752437f537349f6aec8a to your computer and use it in GitHub Desktop.
Python master task
# -*- coding: utf-8 -*-
__author__ = 'diony_000'
#original list of currencies is from https://code.google.com/p/dspl/source/browse/datasets/google/canonical/currencies.csv
symbol_to_currency = {
'$':['USD','AUD','ARS','BBD','BMD','BND','BSD','CAD','CLP','COP','FJD','GYD','HKD','KYD','LRD','MXN','NZD','SBD','SGD','SRD','SVC','XCD'],
'$b':['BOB'],
'$U':['UYU'],
'£':['GBP','EGP','FKP','GIP','LBP','SHP','SYP'],
'¥':['CNY','JPY'],
'؋':['AFN'],
'฿':['THB'],
'៛':['KHR'],
'₡':['CRC'],
'₦':['NGN'],
'₩':['KPW','KRW'],
'₪':['ILS'],
'₫':['VND'],
'€':['EUR'],
'₭':['LAK'],
'₮':['MNT'],
'₱':['CUP'],
'₴':['UAH'],
'₨':['LKR','MUR','NPR','PKR','SCR'],
'﷼':['IRR','OMR','QAR','SAR','YER'],
'B/.':['PAB'],
'Bs':['VEF'],
'BZ$':['BZD'],
'C$':['NIO'],
'Ft':['HUF'],
'ƒ':['ANG','AWG'],
'Gs':['PYG'],
'CHF':['CHF'],
'J$':['JMD'],
'Kč':['CZK'],
'KM':['BAM'],
'kn':['HRK'],
'kr':['DKK','ISK','NOK','SEK'],
'L':['HNL'],
'lei':['RON'],
'Lek':['ALL'],
'Ls':['LVL'],
'Lt':['LTL'],
'MT':['MZN'],
'NT$':['TWD'],
'P':['BWP'],
'p.':['BYR'],
'Php':['PHP'],
'Q':['GTQ'],
'R':['ZAR'],
'R$':['BRL'],
'RD$':['DOP'],
'RM':['MYR'],
'Rp':['IDR'],
'S':['SOS'],
'S/.':['PEN'],
'TL':['TRY'],
'TT$':['TTD'],
'zł':['PLN'],
'ден':['MKD'],
'Дин.':['RSD'],
'лв':['BGN','KGS','KZT','UZS'],
'ман':['AZN'],
'руб':['RUB']
}
# -*- coding: utf-8 -*-
__author__ = '[email protected]'
import argparse
from currencies import symbol_to_currency
from json import dumps
from lxml import etree
yahoo_api_url = 'http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("%s")&env=store://datatables.org/alltableswithkeys'
exchange_rate_type = 'Ask' #or Bid
all_currencies = [code for codes in symbol_to_currency.values() for code in codes]
def parse_currency(currency):
result = currency
if result not in all_currencies:
try:
result = symbol_to_currency[currency]
if len(result) > 1:
while True:
print('There are multiple (%d) currencies with this symbol "%s". Please choose the correct currency code from this list:' % (len(result), currency))
for currency_option in result:
print('%s' % (currency_option, ))
currency_option = input('Enter your currency code:')
if currency_option in result:
result = currency_option
break
else:
print('It seems you entered incorrect currency, lets try again!')
except KeyError:
print('Currency "%s" does not exist.' % currency)
raise
return result
def convert_amount(amount, input_currency, output_currency=None):
try:
amount = float(amount)
except ValueError:
print('Amount has to be a float number.')
raise
input_currency = parse_currency(input_currency)
if output_currency:
output_currency = [parse_currency(output_currency)]
else:
output_currency = all_currencies
currency_pairs = [input_currency+currency for currency in output_currency]
root = etree.parse(yahoo_api_url % '", "'.join(currency_pairs)).getroot()
output = {'output':{}}
for currency_pair in currency_pairs:
exchange_rate = float(root.find('results/rate[@id="%s"]/%s' % (currency_pair, exchange_rate_type)).text)
output['output'][currency_pair[3:]] = round(exchange_rate * amount, 2)
output['input'] = {'amount': amount, 'currency': input_currency}
return dumps(output, sort_keys=True, indent=4, separators=(',', ': '))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Converts currency with use of public Yahoo Finance API.')
parser.add_argument('--amount', required=True, type=float, help='Amount to be converted.')
parser.add_argument('--input_currency', required=True, help='Currency of amount.')
parser.add_argument('--output_currency', help='Desired currency to convert to. If omitted, converts to all currencies.')
args = parser.parse_args()
print(convert_amount(amount=args.amount, input_currency=args.input_currency, output_currency=args.output_currency))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment