From 653e049f2e4a20aee48a6fb0c4a633746ab00591 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Wed, 11 May 2022 09:26:42 +0200 Subject: [PATCH 1/3] More markdown proof --- telegram_bot/bot_updates.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 01b8543..434ece5 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -148,7 +148,7 @@ def update_for_user(p_user_id, p_my_handler): if len(share_symbols) != 0: for i in range(len(share_symbols)): my_price = share_fetcher.get_share_price_no_currency(share_symbols[i]) - my_update_message = f'{share_fetcher.get_share_information_markdown(share_symbols[i])}\ncount: {share_amounts[i]}\nTotal: {hf.make_markdown_proof(round(float(my_price) * float(share_amounts[i]), 2))} EUR' + my_update_message = f'{share_fetcher.get_share_information_markdown(share_symbols[i])}\ncount: {hf.make_markdown_proof(share_amounts[i])}\nTotal: {hf.make_markdown_proof(round(float(my_price) * float(share_amounts[i]), 2))} EUR' bot.send_message(chat_id=p_user_id, text=my_update_message, parse_mode="MARKDOWNV2") else: send_to_user("No shares found for your account. Check https://gruppe1.testsites.info to change your settings and add shares.", pUser_id=p_user_id) @@ -169,10 +169,10 @@ def update_for_user(p_user_id, p_my_handler): keyword = hf.make_markdown_proof(keyword) if not news: # if empty news array - send_to_user(f"No news found for keyword _{keyword}_.", pUser_id=p_user_id, md_mode=True) + send_to_user(f"No news found for keyword _{keyword}_\.", pUser_id=p_user_id, md_mode=True) elif news == None: # if news is none - send_to_user(f"Server error for keyword _{keyword}_.", pUser_id=p_user_id, md_mode=True) + send_to_user(f"Server error for keyword _{keyword}_\.", pUser_id=p_user_id, md_mode=True) else: news_formatted = news_fetcher.format_article(news[0]) # format for message, only use the most popular article send_to_user(f"_keyword: {keyword}_\n\n{news_formatted}", pUser_id=p_user_id, md_mode=True) # send news with related keyword in Markdown From f87f8cbf4c551ad3b29735c5662fdd9cc8f9e147 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Wed, 11 May 2022 09:40:08 +0200 Subject: [PATCH 2/3] added comments --- telegram_bot/bot_updates.py | 23 +++++++++++------------ telegram_bot/shares/share_fetcher.py | 16 +++++++++------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 434ece5..ce14699 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -63,19 +63,19 @@ def update_crontab(p_my_handler): global user_crontab global user_ids - all_users = p_my_handler.get_all_users() + all_users = p_my_handler.get_all_users() # get all users so crontabs can update for everybody user_ids = [] user_crontab = [] for element in all_users: - if element["cron"] != '' and element["telegram_user_id"] != '': + if element["cron"] != '' and element["telegram_user_id"] != '': # check if both values are existing so I have consistent data try: user_ids.append(int(element["telegram_user_id"])) try: user_crontab.append(str(element["cron"])) except: - user_ids.pop() + user_ids.pop() # if something goes wrong with cron I have to delete matching user id except: continue @@ -83,7 +83,7 @@ def update_crontab(p_my_handler): update_based_on_crontab(user_ids, user_crontab, p_my_handler) - update_crontab(p_my_handler) + update_crontab(p_my_handler) # restart the update after time sleep def update_based_on_crontab(p_user_ids, p_user_crontab, p_my_handler): @@ -103,17 +103,17 @@ def update_based_on_crontab(p_user_ids, p_user_crontab, p_my_handler): :rtype: none """ - my_scheduler = BackgroundScheduler() + my_scheduler = BackgroundScheduler() # schedule sends based on cron for i in range(len(p_user_ids)): - cron_split = p_user_crontab[i].split(" ") + cron_split = p_user_crontab[i].split(" ") # split it up to use in scheduler print(cron_split[4], cron_split[1], cron_split[0], cron_split[3], cron_split[2]) my_scheduler.add_job(update_for_user, 'cron', day_of_week = cron_split[4] , hour= cron_split[1] , minute = cron_split[0], month= cron_split[3] , day=cron_split[2], args=(p_user_ids[i], p_my_handler )) my_scheduler.start() - time.sleep( 600 ) - my_scheduler.shutdown() + time.sleep( 600 ) # scheduler runs in background and I wait 10mins + my_scheduler.shutdown() # after this the new crontabs will be loaded def update_for_user(p_user_id, p_my_handler): @@ -130,9 +130,8 @@ def update_for_user(p_user_id, p_my_handler): """ share_symbols = [] share_amounts = [] - share_courses = [] - my_portfolio = p_my_handler.get_user_portfolio(p_user_id) + my_portfolio = p_my_handler.get_user_portfolio(p_user_id) # get all existing shares for user for element in my_portfolio: if element["count"] != '' and element["isin"]!= '': @@ -143,9 +142,9 @@ def update_for_user(p_user_id, p_my_handler): my_user = p_my_handler.get_user(p_user_id) send_to_user("Hello %s this is your share update for today:"%str(my_user["username"]), pUser_id=p_user_id) - shares = p_my_handler.get_user_shares(p_user_id) + shares = p_my_handler.get_user_shares(p_user_id) # all interest shares - if len(share_symbols) != 0: + if len(share_symbols) != 0: # iterate through all shares for i in range(len(share_symbols)): my_price = share_fetcher.get_share_price_no_currency(share_symbols[i]) my_update_message = f'{share_fetcher.get_share_information_markdown(share_symbols[i])}\ncount: {hf.make_markdown_proof(share_amounts[i])}\nTotal: {hf.make_markdown_proof(round(float(my_price) * float(share_amounts[i]), 2))} EUR' diff --git a/telegram_bot/shares/share_fetcher.py b/telegram_bot/shares/share_fetcher.py index 5d209a0..f56c3fb 100644 --- a/telegram_bot/shares/share_fetcher.py +++ b/telegram_bot/shares/share_fetcher.py @@ -22,21 +22,22 @@ def get_share_price(str_search_for): try: search_result = investpy.search_quotes(text=str_search_for, products=['stocks'], n_results=1, countries=['germany']) - currency = str(search_result.retrieve_currency()) + currency = str(search_result.retrieve_currency()) # retrieve currency from data + # should always be Euro because of countries=['germany'] - recent_data = pandas.DataFrame(search_result.retrieve_recent_data()) + recent_data = pandas.DataFrame(search_result.retrieve_recent_data()) # stock prices of last few days - stock_price = recent_data.iloc[-1]["Close"] + stock_price = recent_data.iloc[-1]["Close"] # retrieve latest stock price stock_price = round(float(stock_price), 2) - str_return =str(stock_price) + " " + str(currency) + str_return =str(stock_price) + " " + str(currency) # return + currency return str_return - except RuntimeError: + except RuntimeError: # if no shares are found for germany (e.g. isin: US.....) try: - my_Converter = CurrencyConverter() + my_Converter = CurrencyConverter() # need a currency converter search_result = investpy.search_quotes(text=str_search_for, products=['stocks'], n_results=1) @@ -46,7 +47,8 @@ def get_share_price(str_search_for): stock_price = recent_data.iloc[-1]["Close"] - stock_price = my_Converter.convert(float(stock_price), str(currency), 'EUR') + #convert stock price from currency to EUR + stock_price = my_Converter.convert(float(stock_price), str(currency), 'EUR') stock_price = round(float(stock_price), 2) From 11e856db24627bc05da917aef26bf437be481f8b Mon Sep 17 00:00:00 2001 From: Rripped Date: Wed, 11 May 2022 17:01:46 +0200 Subject: [PATCH 3/3] enhanced /share --- telegram_bot/shares/share_fetcher.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/telegram_bot/shares/share_fetcher.py b/telegram_bot/shares/share_fetcher.py index f56c3fb..179a478 100644 --- a/telegram_bot/shares/share_fetcher.py +++ b/telegram_bot/shares/share_fetcher.py @@ -106,8 +106,16 @@ def get_share_information(str_search_for): return str_return def get_share_information_markdown(str_search_for): - search_result = investpy.search_quotes(text=str_search_for, products=['stocks'], - countries=['germany'], n_results=1) + + try: + search_result = investpy.search_quotes(text=str_search_for, products=['stocks'], + countries=['germany'], n_results=1) + + except RuntimeError as e: + return hf.make_markdown_proof(f"no shares found for \"{str_search_for}\"") # if no shares are found, make error message markdown proof and return + + except ConnectionError as e: + return hf.make_markdown_proof(f"connection not possible. Try again later.") # if no connection, make error message markdown proof and return str_return = f'*{hf.make_markdown_proof(search_result.name)}*\n_{hf.make_markdown_proof(search_result.symbol)}_\nworth: {hf.make_markdown_proof(get_share_price(str_search_for))}' return str_return