Skip to content

Instantly share code, notes, and snippets.

@FBosler
Created December 4, 2021 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FBosler/9f9da74f2531070f5a0092d19fd9adc6 to your computer and use it in GitHub Desktop.
Save FBosler/9f9da74f2531070f5a0092d19fd9adc6 to your computer and use it in GitHub Desktop.
minimal working example to pull exchange rates from free (ECB) endpoint
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