blob: d95df950305e3d1f0dfd6a33d0ebf31b1460a885 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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"))
|