MCPcopy
hub / github.com/exchangeratesapi/exchangeratesapi / update_rates

Function update_rates

exchangerates/app.py:48–67  ·  view source on GitHub ↗
(historic=False)

Source from the content-addressed store, hash-verified

46
47
48async def update_rates(historic=False):
49 r = requests.get(HISTORIC_RATES_URL if historic else LAST_90_DAYS_RATES_URL)
50 envelope = ElementTree.fromstring(r.content)
51
52 namespaces = {
53 "gesmes": "http://www.gesmes.org/xml/2002-08-01",
54 "eurofxref": "http://www.ecb.int/vocabulary/2002-08-01/eurofxref",
55 }
56
57 data = envelope.findall("./eurofxref:Cube/eurofxref:Cube[@time]", namespaces)
58 for d in data:
59 time = datetime.strptime(d.attrib["time"], "%Y-%m-%d").date()
60 rates = await ExchangeRates.get(time)
61 if not rates:
62 await ExchangeRates.create(
63 date=time,
64 rates={
65 c.attrib["currency"]: Decimal(c.attrib["rate"]) for c in list(d)
66 },
67 )
68
69
70@app.listener("before_server_start")

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected