Pridani stahovani kurzu a jednotneho API
- 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
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
"""J&T Banka: the page embeds the rates as escaped JSON objects in its page data."""
|
||||
|
||||
import json
|
||||
import re
|
||||
from datetime import date
|
||||
|
||||
from app.models import ParsedRates
|
||||
from app.sources.base import make_rate
|
||||
|
||||
ESCAPED_RATE_PATTERN = re.compile(r'\{\\"currency\\":\\"[A-Z]{3}\\".*?\}')
|
||||
ISO_DATE_LENGTH = 10
|
||||
|
||||
|
||||
def parse(document: str) -> ParsedRates:
|
||||
# The same list can be embedded repeatedly, the first occurrence of a currency wins.
|
||||
items: dict[str, dict] = {}
|
||||
for match in ESCAPED_RATE_PATTERN.finditer(document):
|
||||
item = json.loads(match.group().replace('\\"', '"'))
|
||||
items.setdefault(item["currency"], item)
|
||||
if not items:
|
||||
raise ValueError("J&T rate list not found")
|
||||
|
||||
# The universal contract carries non-cash rates. A currency without them is not offered.
|
||||
rates = [
|
||||
make_rate(
|
||||
currency=currency,
|
||||
amount=item["unitAmount"],
|
||||
buy=item["nonCashBuy"],
|
||||
sell=item["nonCashSell"],
|
||||
)
|
||||
for currency, item in items.items()
|
||||
if item["nonCashBuy"] and item["nonCashSell"]
|
||||
]
|
||||
|
||||
first_item = next(iter(items.values()))
|
||||
valid_date = date.fromisoformat(first_item["date"][:ISO_DATE_LENGTH])
|
||||
return ParsedRates(valid_date=valid_date, rates=rates)
|
||||
Reference in New Issue
Block a user