"""MONETA Money Bank: 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 # Prices are already per 1 unit even when multiplyFactor says 100, for example JPY is # 0.13245, so dividing by the factor would make the rate a hundred times too small. PRICE_AMOUNT = 1 MILLISECONDS_PER_SECOND = 1000 def parse(document: str) -> ParsedRates: items = json.loads(document)["responseObject"]["exchangeRates"] if not items: raise ValueError("Moneta rate list is empty") # A discontinued currency stays in the list with its last date, for example BGN. latest = max(item["effectiveDate"] for item in items) rates = [ # rateAccounting is an accounting rate, not a middle of these prices, so it is not used. make_rate( currency=item["symbol"], amount=PRICE_AMOUNT, buy=item["rateCashlessBuy"], sell=item["rateCashlessSell"], ) for item in items if item["effectiveDate"] == latest and item["rateCashlessBuy"] and item["rateCashlessSell"] ] valid_from = datetime.fromtimestamp(latest / MILLISECONDS_PER_SECOND, PRAGUE_ZONE) return ParsedRates(valid_date=valid_from.date(), rates=rates)