"""Raiffeisenbank: the JSON endpoint used by the bank's own rate list page.""" import json from datetime import datetime from app.models import ParsedRates from app.sources.base import PRAGUE_ZONE, make_rate def parse(document: str) -> ParsedRates: # The bank publishes several lists a day, only the last one is currently valid. rate_lists = json.loads(document)["exchangeRateLists"] current = next((item for item in rate_lists if item["lastRates"]), None) if current is None: raise ValueError("Raiffeisenbank current rate list not found") # exchangeRateBuy and exchangeRateSell are non-cash, the cash rates have a Cash suffix. rates = [ make_rate( currency=item["currencyFrom"], amount=item["unitsFrom"], buy=item["exchangeRateBuy"], sell=item["exchangeRateSell"], middle=item["exchangeRateCenter"], ) for item in current["exchangeRates"] if item["exchangeRateBuy"] and item["exchangeRateSell"] ] valid_from = datetime.fromisoformat(current["effectiveDateFrom"]) return ParsedRates(valid_date=valid_from.astimezone(PRAGUE_ZONE).date(), rates=rates)