(historic=False)
| 46 | |
| 47 | |
| 48 | async 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") |
nothing calls this directly
no outgoing calls
no test coverage detected