From b83653bc4244a830fafe602c6bce52eb4396539f Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 10 May 2022 19:16:44 +0200 Subject: [PATCH] bigger fixes --- telegram_bot/bot.py | 15 +++++++++++++-- telegram_bot/helper_functions.py | 1 + telegram_bot/news/news_fetcher.py | 6 ++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 5366f29..36915f5 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -579,9 +579,20 @@ def send_transactions(message): else: for transaction in transactions: - comment = hf.make_markdown_proof(transaction['comment']) + comment = hf.make_markdown_proof(transaction['comment']) or "\(no desc\)" # if comment is empty, make it "no desc" isin = hf.make_markdown_proof(transaction['isin']) - bot.send_message(chat_id=user_id, text=f'_{comment}_\n{isin}\namount: {transaction["count"]}\nprice: {transaction["price"]}\ntime: {transaction["time"]}', parse_mode="MARKDOWNV2") + amount = hf.make_markdown_proof(transaction['count']) + price = hf.make_markdown_proof(transaction['price']) + time = hf.make_markdown_proof(transaction['time']) + """ + comment = transaction['comment'] + isin = transaction['isin'] + amount = transaction['count'] + price = transaction['price'] + time = transaction['time'] + """ + print(f'_{comment}_\n{isin}\namount: {amount}\nprice: {price}\ntime: {time}') + bot.send_message(chat_id=user_id, text=f'_{comment}_\n{isin}\namount: {amount}\nprice: {price}\ntime: {time}', parse_mode="MARKDOWNV2") @bot.message_handler(commands=['shares', 'Shares']) diff --git a/telegram_bot/helper_functions.py b/telegram_bot/helper_functions.py index 92c9d77..a7bfc63 100644 --- a/telegram_bot/helper_functions.py +++ b/telegram_bot/helper_functions.py @@ -32,6 +32,7 @@ def make_markdown_proof(text): # used to avoid errors related to markdown parsem :rtype: string """ + text = str(text) text = text.replace("_", "\\_") text = text.replace("*", "\\*") diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index d29d0a2..3f8d953 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -20,14 +20,16 @@ load_dotenv() # loads environment vars # Init api_key = os.getenv('NEWS_API_KEY') # get API Key from .env file newsapi = NewsApiClient(api_key=api_key) # news api from https://newsapi.org/ + try: # get all available news sources (e.g BBC, New York Times, etc.) source_json = requests.get(f"https://newsapi.org/v2/top-headlines/sources?apiKey={api_key}&language=en").json() sources = source_json["sources"] str_sources = ",".join([source["id"] for source in sources]) + except KeyError: - print("Error: Could not get sources") - sys.exit(1) + print("Error: Could not get sources, may be blocked because of too many requests (free newsapi is limited to 100 reqs per day)") + str_sources = str("Reuters, bbc, cnn, fox-news, google-news, hacker-news, nytimes, the-huffington-post, the-new-york-times, business-insider, bbc-news, cbc-news, ESPN, fox-sports, google-news-uk, independent, the-wall-street-journal, the-washington-times, time, usa-today") def get_all_news_by_keyword(keyword, from_date="2000-01-01"):