Fix currency API endpoint accepting only lowercase currency strings
Signed-off-by: Lukáš Kucharczyk <lukas@kucharczyk.xyz>
This commit is contained in:
parent
2116cfc219
commit
d936fdc60d
|
@ -32,21 +32,29 @@ def convert_prices():
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
if not exchange_rate:
|
if not exchange_rate:
|
||||||
|
print(
|
||||||
|
f"Getting exchange rate from {currency_from} to {currency_to} for {year}..."
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
|
# this API endpoint only accepts lowercase currency string
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
f"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@{year}-01-01/v1/currencies/{currency_from}.json"
|
f"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@{year}-01-01/v1/currencies/{currency_from.lower()}.json"
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
rate = data[currency_from].get(currency_to)
|
currency_from_data = data.get(currency_from.lower())
|
||||||
|
rate = currency_from_data.get(currency_to.lower())
|
||||||
|
|
||||||
if rate:
|
if rate:
|
||||||
|
print(f"Got {rate}, saving...")
|
||||||
exchange_rate = ExchangeRate.objects.create(
|
exchange_rate = ExchangeRate.objects.create(
|
||||||
currency_from=currency_from,
|
currency_from=currency_from,
|
||||||
currency_to=currency_to,
|
currency_to=currency_to,
|
||||||
year=year,
|
year=year,
|
||||||
rate=rate,
|
rate=rate,
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
print("Could not get an exchange rate.")
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
print(
|
print(
|
||||||
f"Failed to fetch exchange rate for {currency_from}->{currency_to} in {year}: {e}"
|
f"Failed to fetch exchange rate for {currency_from}->{currency_to} in {year}: {e}"
|
||||||
|
|
Loading…
Reference in New Issue