Files
2023-05-16 17:14:16 +03:00

52 lines
1.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import json
from datetime import datetime, timedelta
from BaseModels.mailSender import techSendMail
from GeneralApp.temp_data_funcs import *
def get_rate_nb_by_currency_code(code, date=None):
from .nbrb.nbrb_currency_exchange import get_nbrb_rate_by_currency_code
from .alfabank_api.alfabank_api_funcs import get_alfabank_nb_rate_by_currency_code
if code == 'BYN':
return 1
rate = None
request_required = True
try:
tmp_rec = get_tmp_data('currency_rate', code)
if tmp_rec and tmp_rec.json_data:
if 'rate' in tmp_rec.json_data:
# если с момента последнего импорта прошло меньше 30 минут - забираем курс из базы
if datetime.strptime(tmp_rec.json_data['DT'], '%d.%m.%Y %H:%M') + timedelta(
minutes=30) > datetime.now():
rate = tmp_rec.json_data['rate']
if not rate:
# если с последней попытки меньше минуты - отдаем старый курс или None
if tmp_rec.modifiedDT + timedelta(minutes=5) > datetime.now():
if 'rate' in tmp_rec.json_data:
rate = tmp_rec.json_data['rate']
else:
request_required = False
if request_required:
if not rate:
rate = get_alfabank_nb_rate_by_currency_code(code)
# if not rate:
# rate = get_nbrb_rate_by_currency_code(code)
tmp_rec.modifiedDT = datetime.now()
tmp_rec.save()
except Exception as e:
msg = f'<b style="color : red;">!!!!! --- get_rate_nb_by_currency_code error={str(e)}</b>'
print(msg)
techSendMail(msg, 'tE get_rate_nb_by_currency_code error')
return rate