From fb9a630795657b409c2f3a14afa68c1669bd9cdb Mon Sep 17 00:00:00 2001 From: user Date: Sat, 25 Dec 2021 16:35:52 +0800 Subject: [PATCH] don't break on sp500 http err --- main.py | 69 +++++++++++++++++++++++++++++++-------------------------- run.sh | 4 ++-- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/main.py b/main.py index 2bdb041..1f2cc6a 100644 --- a/main.py +++ b/main.py @@ -19,6 +19,8 @@ from settings import settings as option import subprocess +from urllib.error import URLError + LOG_TRACE = True ENABLE_EXMO = True @@ -135,40 +137,43 @@ def fetch_sp500_index(self): session = Session() session.headers.update(headers) response = session.get(url, params=parameters) - df = pd.read_excel(response.content) - print(f"{__name__}: df:\n{df}") - print(f"{__name__}: df.keys():\n{df.keys()}") - print(f"{__name__}: col count:\n{len(df.keys())}") - price_row_found = False - price_col_found = False - price_row_index = None - price_col_index = None - col_index = 0 - for col_name in df.keys(): - col = df[col_name] - row_index = 0 - for cell in col: - value = f'{cell}'.strip() - if value == 'Price Return\nS&P 500': - price_row_found = True - price_row_index = row_index - print("price_row_index: ", price_row_index) - else: - # print(f"val: '{value}'") - if value == 'Index Level': - price_col_found = True - price_col_index = col_index - print("price_col_index: ", price_col_index) + if response.status_code == 200: + df = pd.read_excel(response.content) + print(f"{__name__}: df:\n{df}") + print(f"{__name__}: df.keys():\n{df.keys()}") + print(f"{__name__}: col count:\n{len(df.keys())}") + price_row_found = False + price_col_found = False + price_row_index = None + price_col_index = None + col_index = 0 + for col_name in df.keys(): + col = df[col_name] + row_index = 0 + for cell in col: + value = f'{cell}'.strip() + if value == 'Price Return\nS&P 500': + price_row_found = True + price_row_index = row_index + print("price_row_index: ", price_row_index) + else: + # print(f"val: '{value}'") + if value == 'Index Level': + price_col_found = True + price_col_index = col_index + print("price_col_index: ", price_col_index) + if price_row_found and price_col_found: + break + row_index = row_index + 1 if price_row_found and price_col_found: break - row_index = row_index + 1 - if price_row_found and price_col_found: - break - col_index = col_index + 1 - if not price_row_found or not price_col_found: - return f'S&P500 Fetch Error: Unrecognized Format' + col_index = col_index + 1 + if not price_row_found or not price_col_found: + return f'S&P500 Error: Unknown Format' + else: + return f'S&P500 Index: {"{:0,.2f}".format(float(df.iloc[price_row_index, price_col_index]))}' else: - return f'S&P500 Index: {"{:0,.2f}".format(float(df.iloc[price_row_index, price_col_index]))}' + return f'S&P500 Error: HTTP {response.status_code} {response.reason[:10] if response.reason else "No message"}' except (ConnectionError, Timeout, TooManyRedirects) as e: print(__name__, e, flush=True) return f'S&P500 Fetch Error: {e}' @@ -1752,7 +1757,7 @@ def login_and_loop(self): tons = self.format_currency(exmo_TONCOIN_USD_json["sell_price"]) tonb = self.format_currency(exmo_TONCOIN_USD_json["buy_price"]) ircProtocolDisplayText_exmo = f'Exmo.me: TONCOIN/USDT S {tons} B {tonb}.' - except (ConnectionError, Timeout, TooManyRedirects) as e: + except (ConnectionError, Timeout, TooManyRedirects, URLError) as e: print(__name__, e, flush=True) ircProtocolDisplayText_exmo = f"Can't fetch exmo.me: {e}." diff --git a/run.sh b/run.sh index c6909c1..ab8a256 100755 --- a/run.sh +++ b/run.sh @@ -1,4 +1,4 @@ -cd /home/user/git/ircbot_greenbich/ +cd /home/user/vcs/ircbot_greenbich/ echo pwd pwd @@ -8,7 +8,7 @@ whoami . pythonvars.sh # export CL="strace ./venv/bin/python ./launch_all.py" -export CL="./venv/bin/python ./launch_all.py" +export CL="./venv3sn2/bin/python ./launch_all.py" echo "launching $CL" $CL