0.0.1 init, main page prepare

This commit is contained in:
SDE
2023-11-16 14:38:44 +03:00
parent 894ee6d750
commit 74f7bbb24b
78 changed files with 6198 additions and 21 deletions

View File

View File

@@ -0,0 +1,108 @@
import requests
import json
from datetime import datetime, timedelta
from BaseModels.mailSender import techSendMail
from GeneralApp.temp_data_funcs import *
def get_alfabank_nb_rate_by_currency_code(code, date=None):
rate = None
res = None
req_str = None
try:
msg = f'get_alfabank_nb_rate_by_currency_code'
print(msg)
int_code = None
if code == 'USD':
int_code = 840
elif code == 'EUR':
int_code = 978
elif code == 'RUB':
int_code = 643
code_str = ''
if int_code:
code_str = f'?currencyCode={int_code}'
date_str = ''
if date:
date_str = f'date={datetime.now().strftime("%d.%m.%Y")}'
if int_code:
date_str = f'&{date_str}'
else:
date_str = f'?{date_str}'
req_str = f'https://developerhub.alfabank.by:8273/partner/1.0.1/public/nationalRates{code_str}{date_str}'
try:
msg = f'GET {req_str}'
print(msg)
res = requests.get(req_str)
msg = f'answer received = {str(res)}'
print(msg)
except Exception as e:
msg = f'Exception GET {req_str} = {str(e)} ({str(res)})'
print(msg)
res = None
if res:
# if not res and res != 200:
# if tmp_rec:
# rate = tmp_rec.json_data['rate']
# else:
# rate_Dict = {
# 'rate': 1,
# 'DT': datetime.now().strftime('%d.%m.%Y %H:%M')
# }
# create_or_update_tmp_data('currency_rate', code, rate_Dict)
# rate = 1
#
# msg = '<b style="color : red;">!!!!! --- get_alfabank_nbrb_rate_by_currency_code requests GET error={0}</b><br>{1}<br>{2}<br>rate set = {3}'.format(
# str(e),
# str(res),
# str(req_str),
# str(rate)
# )
# print(msg)
# techSendMail(msg, 'tE get_alfabank_nbrb_rate_by_currency_code error')
data = json.loads(res.content)
for item in data['rates']:
if item['iso'].upper() == code.upper():
rate = item['rate'] / item['quantity']
rate_Dict = {
'rate': rate,
'DT': datetime.now().strftime('%d.%m.%Y %H:%M')
}
create_or_update_tmp_data('currency_rate', code, rate_Dict)
break
except Exception as e:
msg = '<b style="color : red;">!!!!! --- get_alfabank_nb_rate_by_currency_code error={0}</b><br>{1}<br>{2}'.format(
str(e),
str(res),
str(req_str)
)
print(msg)
techSendMail(msg, 'tE get_alfabank_nb_rate_by_currency_code error')
# if not res:
# rate_Dict = {
# 'rate': 1,
# 'DT': datetime.now().strftime('%d.%m.%Y %H:%M')
# }
# create_or_update_tmp_data('currency_rate', code, rate_Dict)
# return 1
# if rate:
msg = f'get alfabank nb {code} rate = {str(rate)}'
print(msg)
return rate

View File

@@ -0,0 +1,51 @@
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

View File

@@ -0,0 +1,115 @@
import requests
import json
from datetime import datetime, timedelta
from BaseModels.mailSender import techSendMail
from GeneralApp.temp_data_funcs import *
def get_nbrb_currency_id_by_currency_code(code):
data = requests.get('https://www.nbrb.by/api/exrates/currencies')
json_data = json.loads(data.content)
for item in json_data:
if 'Cur_Abbreviation' in item and item['Cur_Abbreviation'] == code:
return item['Cur_Code']
return None
def get_nbrb_rate_by_currency_code(code, date=None):
# if code == 'BYN':
# return 1
#
# tmp_rec = get_tmp_data('currency_rate', code)
# if tmp_rec and tmp_rec.json_data:
# # если с момента последнего импорта прошло меньше 30 минут - забираем курс из базы
# if datetime.strptime(tmp_rec.json_data['DT'], '%d.%m.%Y %H:%M') + timedelta(minutes=30) > datetime.now():
# return tmp_rec.json_data['rate']
# currency_id = get_nbrb_currency_id_by_currency_code('USD')
rate = None
res = None
req_str = None
try:
msg = f'get_nbrb_rate_by_currency_code'
print(msg)
if not date:
# data = requests.get('https://www.nbrb.by/API/ExRates/Rates/{0}?Periodicity=0'.format(str(currency_id)))
req_str = 'https://www.nbrb.by/api/exrates/rates/{0}?parammode=2'.format(str(code))
else:
date_str = datetime.now().strftime('%Y-%m-%d')
date_str = date_str.replace('-0', '-')
req_str = 'https://www.nbrb.by/api/exrates/rates/{0}?parammode=2&ondate={1}'.format(
str(code),
date_str
)
e = None
try:
msg = f'GET {req_str}'
print(msg)
res = requests.get(req_str, timeout=3)
msg = f'answer received = {str(res)}'
print(msg)
except Exception as e:
msg = f'Exception GET {req_str} = {str(e)} ({str(res)})'
print(msg)
res = None
if not res and res != 200:
# if tmp_rec:
# rate = tmp_rec.json_data['rate']
# else:
# rate_Dict = {
# 'rate': 1,
# 'DT': datetime.now().strftime('%d.%m.%Y %H:%M')
# }
# create_or_update_tmp_data('currency_rate', code, rate_Dict)
# rate = 1
msg = '<b style="color : red;">!!!!! --- get_nbrb_rate_by_currency_code requests GET error={0}</b><br>{1}<br>{2}<br>rate set = {3}'.format(
str(e),
str(res),
str(req_str),
str(rate)
)
print(msg)
techSendMail(msg, 'tE get_nbrb_rate_by_currency_code error')
data = json.loads(res.content)
if data and 'Cur_OfficialRate' in data and 'Cur_Scale' in data:
rate = data['Cur_OfficialRate'] / data['Cur_Scale']
rate_Dict = {
'rate': rate,
'DT': datetime.now().strftime('%d.%m.%Y %H:%M')
}
create_or_update_tmp_data('currency_rate', code, rate_Dict)
except Exception as e:
msg = '<b style="color : red;">!!!!! --- get_nbrb_rate_by_currency_code error={0}</b><br>{1}<br>{2}'.format(
str(e),
str(res),
str(req_str)
)
print(msg)
techSendMail(msg, 'tE get_nbrb_rate_by_currency_code error')
# if not res:
# rate_Dict = {
# 'rate': 1,
# 'DT': datetime.now().strftime('%d.%m.%Y %H:%M')
# }
# create_or_update_tmp_data('currency_rate', code, rate_Dict)
# return 1
if rate:
msg = f'get nbrb nb rate = {rate}'
print(msg)
return rate