diff options
Diffstat (limited to 'currencies.py')
| -rw-r--r-- | currencies.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/currencies.py b/currencies.py new file mode 100644 index 0000000..d95df95 --- /dev/null +++ b/currencies.py @@ -0,0 +1,26 @@ +import requests +import json + +class CurrencyConv: + def __init__(self, currency_json_url: str="https://open.er-api.com/v6/latest/USD"): + try: + self._currency_data = json.loads(requests.get(currency_json_url).text) + except: + raise Exception("Unable to load currency data from " + currency_json_url) + self._currency_rates = self._currency_data["rates"] + + def convert(self, amount: float, from_currency: str, to_currency: str) -> float: + # handle some conversion errors + from_currency = from_currency.replace("\xa0", "").strip().upper() + from_currency= from_currency.replace("₱", "PHP") + from_currency = from_currency.replace("¥", "JPY") + try: + if from_currency == to_currency: + return amount + return amount * self._currency_rates[to_currency] / self._currency_rates[from_currency] + except: + raise Exception("Unable to convert from " + from_currency + " to " + to_currency) + +if __name__ == "__main__": + converter = CurrencyConv() + print(converter.convert(100.0, "SGD", "USD")) |
