Created
December 4, 2021 16:57
-
-
Save FBosler/9f9da74f2531070f5a0092d19fd9adc6 to your computer and use it in GitHub Desktop.
minimal working example to pull exchange rates from free (ECB) endpoint
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
from typing import Optional | |
from numbers import Number | |
from xml.dom.minidom import parseString | |
import requests | |
SETTINGS = { | |
"currency": "THB", | |
"threshold": 35, | |
"message": "The current exchange rate for EUR/{currency} is {value}", | |
} | |
URL = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml" | |
def check_exchange_rate(currency: str = None, threshold: Number = None) -> Optional[str]: | |
res = requests.get(URL) | |
# we have to parse XML (unfortunately I did not find a .json API) | |
parsed = parseString(str(res.content.decode("utf-8")).replace("\n", "").replace("\t", "")) | |
currency_rates = parsed.childNodes[0].childNodes[2].childNodes[0].childNodes | |
target_currency = currency or SETTINGS.get("currency") | |
target_threshold = threshold or SETTINGS.get("threshold") | |
for currency_rate in currency_rates: | |
currency = currency_rate.getAttribute("currency") | |
rate = float(currency_rate.getAttribute("rate")) | |
if currency == target_currency and rate > target_threshold: | |
msg = SETTINGS.get("message").format(currency=currency, value=rate) | |
print(msg) | |
return msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment