Files
exchange-rates/app/sources/unicredit.py
T
JiriUhlir e39f52f027 Dalsi banky, prehled ve Swaggeru, /status a prazdne promenne
- zdroje rb, unicredit a air
- nahore ve Swaggeru prehled nabizenych a nenabizenych bank s duvody
- GET /status s prehledem vypadku stahovani u jednotlivych bank
- prazdna promenna prostredi znamena vychozi hodnotu
- popis stahovani v documentation/stahovani.md
2026-11-04 15:33:21 +01:00

89 lines
2.6 KiB
Python

"""UniCredit Bank: the JSON endpoint used by the bank's own page, one request per currency."""
import json
from datetime import datetime
import httpx
from app.models import ParsedRates
from app.sources.base import PRAGUE_ZONE, make_rate
URL = "https://www.unicreditbank.cz/cwa/GetExchangeRates"
# Headers and date format are what the bank's page sends, the endpoint rejects anything else.
REQUEST_HEADERS = {
"EntityCode": "CZ",
"UserID": "",
"Language": "CS",
"SourceSystem": "PWS",
"Product": "PWS",
}
REQUEST_DATE_FORMAT = "%Y%m%dT%H:%M:%S.000%z"
# Currencies listed on the bank's page with the units the prices refer to. The API does not
# return the units, its page keeps them in the jsonCurrencyAmounts variable.
CURRENCY_UNITS = {
"AUD": 1,
"BAM": 1,
"CAD": 1,
"CHF": 1,
"CNY": 1,
"DKK": 1,
"EUR": 1,
"GBP": 1,
"HKD": 1,
"HUF": 100,
"JPY": 100,
"NOK": 1,
"NZD": 1,
"PLN": 1,
"RON": 1,
"RSD": 100,
"SEK": 1,
"SGD": 1,
"TRY": 1,
"USD": 1,
}
async def fetch(client: httpx.AsyncClient) -> str:
"""Any failed currency fails the source, so a partial list never replaces a complete one."""
request_date = datetime.now(PRAGUE_ZONE).strftime(REQUEST_DATE_FORMAT)
items = []
for currency in CURRENCY_UNITS:
response = await client.post(
URL,
headers=REQUEST_HEADERS,
json={
"Currency": currency,
"DateFrom": request_date,
"DateTo": request_date,
"multiplier": "0",
},
)
response.raise_for_status()
items += response.json()
return json.dumps(items)
def parse(document: str) -> ParsedRates:
items = json.loads(document)
if not items:
raise ValueError("UniCredit rate list is empty")
# "Cards" is the non-cash rate, the page shows it in the Devizy tab. "Cash" is in Valuty.
# MiddleRate is the CNB rate, so the middle of the non-cash prices is used instead.
rates = [
make_rate(
currency=item["CurrencyCode"],
amount=CURRENCY_UNITS[item["CurrencyCode"]],
buy=item["CardsPurchaseRate"],
sell=item["CardsSaleRate"],
middle=item["CardsMiddleRate"],
)
for item in items
if item["CardsPurchaseRate"] and item["CardsSaleRate"]
]
latest_update = max(item["ExchangeRateUpdatedTimestamp"] for item in items)
valid_from = datetime.fromisoformat(latest_update)
return ParsedRates(valid_date=valid_from.astimezone(PRAGUE_ZONE).date(), rates=rates)