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 = '!!!!! --- get_alfabank_nbrb_rate_by_currency_code requests GET error={0}
{1}
{2}
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 = '!!!!! --- get_alfabank_nb_rate_by_currency_code error={0}
{1}
{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