def exchange(amount, from_curr, to_curr, timeout=10):
"""Converts an amount of money from one currency to another
Args:
amount (float): The amount of money you want to convert
from_curr (str): The currency you want to convert from,
either country symbol (e.g USD) or currency smybol (e.g. £)
to_curr (str): The currency you want to convert to, same format as from_curr
timeout (int, optional): The time in seconds aiohttp will take to timeout the request
Returns:
float: the converted amount of money to 2 d.p., or the original amount of the conversion failed.
"""
try:
with aiohttp.ClientSession() as session:
with aiohttp.Timeout(timeout):
resp = yield from session.get("http://api.fixer.io/latest?symbols=" + from_curr + "," + to_curr)
data = yield from resp.json()
if "rates" in data:
return int((amount / data["rates"][from_curr]) * data["rates"][to_curr] * 100)/100
except:
return amount
评论列表
文章目录