"""Fio banka: HTML table of foreign exchange conversion rates, which are non-cash.""" import re from datetime import date from app.models import ParsedRates from app.sources.base import make_rate, parse_comma_decimal VALIDITY_PATTERN = re.compile(r'name="keDni_den"[^>]*value="(\d{2})\.(\d{2})\.(\d{4})"') # The table also has a "Kurz ČNB" column. It is not a Fio price, so the middle is computed. ROW_PATTERN = re.compile( r'class="col1">(?P[A-Z]{3})\s*' r'[^<]*\s*' # country r'(?P\d+)\s*' r'(?P[\d,]+)\s*' r'(?P[\d,]+)' ) def parse(document: str) -> ParsedRates: validity = VALIDITY_PATTERN.search(document) if validity is None: raise ValueError("Fio validity date not found") day, month, year = validity.groups() rates = [ make_rate( currency=row["currency"], amount=int(row["amount"]), buy=parse_comma_decimal(row["buy"]), sell=parse_comma_decimal(row["sell"]), ) for row in ROW_PATTERN.finditer(document) ] return ParsedRates(valid_date=date(int(year), int(month), int(day)), rates=rates)