- GET /{source_id} pro cnb, kb, csob, moneta, jt, mbank, ppf, trinity, fio, creditas
- jednotny tvar odpovedi s bezhotovostnimi kurzy za 1 jednotku meny
- parametr ?base pro ceny v jine mene nez CZK
- pole buy_sell_origin a middle_origin rikaji, co je stazene a co dopocitane
- pravidelne stahovani na pozadi, ukladani do JSON, priznak stale
- testy a dokumentace ve slozce documentation
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""Czech National Bank: official text file with one reference rate per currency."""
|
|
|
|
import re
|
|
from datetime import date
|
|
|
|
from app.models import ParsedRates
|
|
from app.sources.base import make_rate, parse_comma_decimal
|
|
|
|
HEADER_PATTERN = re.compile(r"^(\d{2})\.(\d{2})\.(\d{4}) #\d+$")
|
|
COLUMN_COUNT = 5
|
|
|
|
|
|
def parse(document: str) -> ParsedRates:
|
|
lines = [line.strip() for line in document.splitlines() if line.strip()]
|
|
if len(lines) < 3:
|
|
raise ValueError("CNB document is too short")
|
|
|
|
header = HEADER_PATTERN.match(lines[0])
|
|
if header is None:
|
|
raise ValueError(f"Unexpected CNB header: {lines[0]!r}")
|
|
day, month, year = header.groups()
|
|
|
|
rates = []
|
|
# lines[1] holds the column names
|
|
for line in lines[2:]:
|
|
columns = line.split("|")
|
|
if len(columns) != COLUMN_COUNT:
|
|
raise ValueError(f"Unexpected CNB row: {line!r}")
|
|
_, _, amount, currency, raw_rate = columns
|
|
rate = parse_comma_decimal(raw_rate)
|
|
# CNB has no spread, so the reference rate fills all prices of the universal contract.
|
|
rates.append(
|
|
make_rate(
|
|
currency,
|
|
int(amount),
|
|
buy=rate,
|
|
sell=rate,
|
|
middle=rate,
|
|
buy_sell_origin="computed",
|
|
)
|
|
)
|
|
|
|
return ParsedRates(valid_date=date(int(year), int(month), int(day)), rates=rates)
|