2022-04-05 08:07:03 +00:00
|
|
|
"""
|
|
|
|
script for regularly sending updates on shares and news based on user interval
|
|
|
|
"""
|
|
|
|
__author__ = "Florian Kellermann, Linus Eickhoff"
|
2022-05-10 15:37:20 +00:00
|
|
|
__date__ = "10.05.2022"
|
2022-05-11 21:33:48 +00:00
|
|
|
__version__ = "1.0.2"
|
2022-04-05 10:38:20 +00:00
|
|
|
__license__ = "None"
|
|
|
|
|
2022-04-25 15:32:48 +00:00
|
|
|
import os
|
2022-04-05 12:00:33 +00:00
|
|
|
import sys
|
2022-05-11 21:33:48 +00:00
|
|
|
import time
|
|
|
|
|
2022-04-21 06:23:48 +00:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2022-05-11 21:33:48 +00:00
|
|
|
from dotenv import load_dotenv
|
2022-04-17 11:26:12 +00:00
|
|
|
|
2022-05-11 21:33:48 +00:00
|
|
|
import helper_functions as hf
|
|
|
|
import news.news_fetcher as news_fetcher
|
|
|
|
import shares.share_fetcher as share_fetcher
|
|
|
|
from api_handling.api_handler import API_Handler
|
|
|
|
from bot import bot
|
2022-04-05 12:00:33 +00:00
|
|
|
|
2022-04-05 10:38:20 +00:00
|
|
|
'''
|
|
|
|
* * * * * code
|
|
|
|
┬ ┬ ┬ ┬ ┬
|
|
|
|
│ │ │ │ │
|
2022-04-05 12:00:33 +00:00
|
|
|
│ │ │ │ └──── weekday (0->Monday, 7->Sunday)
|
2022-04-05 10:38:20 +00:00
|
|
|
│ │ │ └────── Month (1-12)
|
|
|
|
│ │ └──────── Day (1-31)
|
|
|
|
│ └────────── Hour (0-23)
|
|
|
|
└──────────── Minute (0-59)
|
|
|
|
|
|
|
|
example 0 8 * * * -> daily update at 8am
|
|
|
|
'''
|
2022-04-05 12:12:12 +00:00
|
|
|
user_ids = []
|
|
|
|
user_crontab = []
|
|
|
|
|
2022-04-25 15:32:48 +00:00
|
|
|
load_dotenv(dotenv_path='.env')
|
|
|
|
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-25 20:39:22 +00:00
|
|
|
def start_updater():
|
2022-04-26 05:39:06 +00:00
|
|
|
""" starting function for regularly sending updates
|
2022-04-05 12:00:33 +00:00
|
|
|
:raises: none
|
|
|
|
|
|
|
|
:rtype: none
|
|
|
|
"""
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-26 07:02:21 +00:00
|
|
|
print("Bot updates started")
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-25 15:32:48 +00:00
|
|
|
my_handler = API_Handler("https://gruppe1.testsites.info/api", str(os.getenv("BOT_EMAIL")), str(os.getenv("BOT_PASSWORD")))
|
2022-04-26 06:43:51 +00:00
|
|
|
|
2022-04-26 05:39:06 +00:00
|
|
|
update_crontab(my_handler)
|
2022-05-11 21:33:48 +00:00
|
|
|
|
|
|
|
|
2022-04-26 05:39:06 +00:00
|
|
|
def update_crontab(p_my_handler):
|
2022-04-05 13:07:06 +00:00
|
|
|
""" Updating crontab lists every hour
|
|
|
|
:type pCurrent_Time: time when starting crontab update
|
|
|
|
:param pCurrent_Time: datetime
|
|
|
|
|
|
|
|
:raises: none
|
|
|
|
|
|
|
|
:rtype: none
|
|
|
|
"""
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-17 11:26:12 +00:00
|
|
|
global user_crontab
|
|
|
|
global user_ids
|
2022-05-12 06:37:53 +00:00
|
|
|
<<<<<<< HEAD
|
2022-04-17 11:26:12 +00:00
|
|
|
|
2022-05-11 07:40:08 +00:00
|
|
|
all_users = p_my_handler.get_all_users() # get all users so crontabs can update for everybody
|
2022-04-17 11:26:12 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
=======
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-17 11:26:12 +00:00
|
|
|
all_users = p_my_handler.get_all_users()
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
>>>>>>> origin/main
|
2022-04-17 11:26:12 +00:00
|
|
|
user_ids = []
|
|
|
|
user_crontab = []
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-17 11:26:12 +00:00
|
|
|
for element in all_users:
|
2022-05-11 07:40:08 +00:00
|
|
|
if element["cron"] != '' and element["telegram_user_id"] != '': # check if both values are existing so I have consistent data
|
2022-05-04 07:58:54 +00:00
|
|
|
try:
|
|
|
|
user_ids.append(int(element["telegram_user_id"]))
|
|
|
|
try:
|
|
|
|
user_crontab.append(str(element["cron"]))
|
2022-05-12 06:37:53 +00:00
|
|
|
<<<<<<< HEAD
|
2022-05-09 12:37:58 +00:00
|
|
|
except:
|
2022-05-11 07:40:08 +00:00
|
|
|
user_ids.pop() # if something goes wrong with cron I have to delete matching user id
|
2022-05-04 07:58:54 +00:00
|
|
|
except: continue
|
|
|
|
|
2022-04-05 13:07:06 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
=======
|
2022-05-11 21:33:48 +00:00
|
|
|
except:
|
2022-05-09 12:37:58 +00:00
|
|
|
user_ids.pop()
|
2022-05-11 21:33:48 +00:00
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
>>>>>>> origin/main
|
2022-04-26 07:02:21 +00:00
|
|
|
print(user_ids)
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-21 06:23:48 +00:00
|
|
|
update_based_on_crontab(user_ids, user_crontab, p_my_handler)
|
2022-05-12 06:37:53 +00:00
|
|
|
<<<<<<< HEAD
|
2022-04-18 15:11:02 +00:00
|
|
|
|
2022-05-11 07:40:08 +00:00
|
|
|
update_crontab(p_my_handler) # restart the update after time sleep
|
2022-04-18 15:11:02 +00:00
|
|
|
|
2022-04-05 13:07:06 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
=======
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-26 05:39:06 +00:00
|
|
|
update_crontab(p_my_handler)
|
2022-05-11 21:33:48 +00:00
|
|
|
|
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
>>>>>>> origin/main
|
2022-04-21 06:23:48 +00:00
|
|
|
def update_based_on_crontab(p_user_ids, p_user_crontab, p_my_handler):
|
2022-04-21 08:06:08 +00:00
|
|
|
""" Check all the crontab codes and add jobs to start in time
|
|
|
|
:type p_user_ids: array
|
|
|
|
:param p_user_ids: user id array of all users
|
|
|
|
|
|
|
|
:type p_user_crontab: array
|
|
|
|
:param p_user_crontab: crontabs for all users equivalent to the user array
|
|
|
|
|
|
|
|
:type p_my_handler: Api_Handler
|
|
|
|
:param p_my_handler: get database stuff
|
|
|
|
|
|
|
|
:raises: none
|
|
|
|
|
|
|
|
:rtype: none
|
|
|
|
"""
|
2022-05-12 06:37:53 +00:00
|
|
|
<<<<<<< HEAD
|
2022-04-17 11:26:12 +00:00
|
|
|
|
2022-05-11 07:40:08 +00:00
|
|
|
my_scheduler = BackgroundScheduler() # schedule sends based on cron
|
2022-04-17 11:26:12 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
=======
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-21 08:06:08 +00:00
|
|
|
my_scheduler = BackgroundScheduler()
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
>>>>>>> origin/main
|
2022-04-18 15:11:02 +00:00
|
|
|
for i in range(len(p_user_ids)):
|
2022-05-11 07:40:08 +00:00
|
|
|
cron_split = p_user_crontab[i].split(" ") # split it up to use in scheduler
|
2022-04-26 07:02:21 +00:00
|
|
|
print(cron_split[4], cron_split[1], cron_split[0], cron_split[3], cron_split[2])
|
2022-05-11 21:33:48 +00:00
|
|
|
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))
|
|
|
|
|
2022-04-17 11:26:12 +00:00
|
|
|
my_scheduler.start()
|
2022-05-12 06:37:53 +00:00
|
|
|
<<<<<<< HEAD
|
2022-04-21 06:23:48 +00:00
|
|
|
|
2022-05-11 07:40:08 +00:00
|
|
|
time.sleep( 600 ) # scheduler runs in background and I wait 10mins
|
|
|
|
my_scheduler.shutdown() # after this the new crontabs will be loaded
|
2022-04-05 16:23:11 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
=======
|
2022-05-11 21:33:48 +00:00
|
|
|
|
|
|
|
time.sleep(600)
|
2022-04-21 06:23:48 +00:00
|
|
|
my_scheduler.shutdown()
|
2022-05-11 21:33:48 +00:00
|
|
|
|
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
>>>>>>> origin/main
|
2022-04-18 21:32:19 +00:00
|
|
|
def update_for_user(p_user_id, p_my_handler):
|
2022-04-21 08:06:08 +00:00
|
|
|
""" Pull shares and send updates for specific user id
|
|
|
|
:type p_user_id: integer
|
|
|
|
:param p_user_id: user id of user that shall receive update
|
|
|
|
|
|
|
|
:type p_my_handler: Api_Handler
|
|
|
|
:param p_my_handler: handle the api and pull from database
|
|
|
|
|
|
|
|
:raises: none
|
|
|
|
|
|
|
|
:rtype: none
|
|
|
|
"""
|
2022-04-18 21:32:19 +00:00
|
|
|
share_symbols = []
|
|
|
|
share_amounts = []
|
2022-05-12 06:37:53 +00:00
|
|
|
<<<<<<< HEAD
|
2022-04-18 21:32:19 +00:00
|
|
|
|
2022-05-11 07:40:08 +00:00
|
|
|
my_portfolio = p_my_handler.get_user_portfolio(p_user_id) # get all existing shares for user
|
2022-04-18 21:32:19 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
=======
|
2022-04-18 21:32:19 +00:00
|
|
|
share_courses = []
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-18 21:32:19 +00:00
|
|
|
my_portfolio = p_my_handler.get_user_portfolio(p_user_id)
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
>>>>>>> origin/main
|
2022-04-18 21:32:19 +00:00
|
|
|
for element in my_portfolio:
|
2022-05-11 21:33:48 +00:00
|
|
|
if element["count"] != '' and element["isin"] != '':
|
2022-04-26 07:36:25 +00:00
|
|
|
print(element["count"], element["isin"])
|
|
|
|
share_symbols.append(element["isin"])
|
2022-04-18 21:32:19 +00:00
|
|
|
share_amounts.append(element["count"])
|
|
|
|
|
|
|
|
my_user = p_my_handler.get_user(p_user_id)
|
2022-05-12 06:37:53 +00:00
|
|
|
<<<<<<< HEAD
|
2022-04-25 15:32:48 +00:00
|
|
|
send_to_user("Hello %s this is your share update for today:"%str(my_user["username"]), pUser_id=p_user_id)
|
2022-04-18 21:32:19 +00:00
|
|
|
|
2022-05-11 07:40:08 +00:00
|
|
|
shares = p_my_handler.get_user_shares(p_user_id) # all interest shares
|
2022-05-10 14:43:17 +00:00
|
|
|
|
2022-05-11 07:40:08 +00:00
|
|
|
if len(share_symbols) != 0: # iterate through all shares
|
2022-05-12 06:37:53 +00:00
|
|
|
=======
|
2022-05-11 21:33:48 +00:00
|
|
|
send_to_user("Hello %s this is your share update for today:" % str(my_user["username"]), pUser_id=p_user_id)
|
|
|
|
|
2022-05-10 14:43:17 +00:00
|
|
|
shares = p_my_handler.get_user_shares(p_user_id)
|
2022-05-11 21:33:48 +00:00
|
|
|
|
2022-04-18 21:32:19 +00:00
|
|
|
if len(share_symbols) != 0:
|
2022-05-12 06:37:53 +00:00
|
|
|
>>>>>>> origin/main
|
2022-04-18 21:32:19 +00:00
|
|
|
for i in range(len(share_symbols)):
|
2022-05-10 14:43:17 +00:00
|
|
|
my_price = share_fetcher.get_share_price_no_currency(share_symbols[i])
|
2022-05-11 07:26:42 +00:00
|
|
|
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'
|
2022-05-10 19:51:27 +00:00
|
|
|
bot.send_message(chat_id=p_user_id, text=my_update_message, parse_mode="MARKDOWNV2")
|
2022-04-18 21:32:19 +00:00
|
|
|
else:
|
2022-04-26 14:32:45 +00:00
|
|
|
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)
|
2022-04-25 15:32:48 +00:00
|
|
|
|
2022-05-11 21:33:48 +00:00
|
|
|
if len(shares) != 0: # Send updates on watchlist shares if existing
|
2022-05-10 14:43:17 +00:00
|
|
|
send_to_user("Your watchlist shares:", pUser_id=p_user_id)
|
|
|
|
for element in shares:
|
2022-05-10 19:51:27 +00:00
|
|
|
send_to_user(share_fetcher.get_share_information_markdown(element), pUser_id=p_user_id, md_mode=True)
|
2022-04-25 15:32:48 +00:00
|
|
|
|
2022-05-11 21:33:48 +00:00
|
|
|
keywords = p_my_handler.get_user_keywords(p_user_id) # get keywords as array
|
|
|
|
|
|
|
|
if (keywords): # if keywords exist and array is not empty
|
2022-04-25 15:32:48 +00:00
|
|
|
send_to_user("If you haven't read yet: \nHere are some interesting news according to your keywords:", pUser_id=p_user_id)
|
|
|
|
for keyword in keywords:
|
2022-04-26 10:07:56 +00:00
|
|
|
news = news_fetcher.get_top_news_by_keyword(keyword)["articles"]
|
2022-05-10 16:45:51 +00:00
|
|
|
keyword = hf.make_markdown_proof(keyword)
|
2022-04-26 10:07:56 +00:00
|
|
|
|
2022-05-12 06:37:53 +00:00
|
|
|
<<<<<<< HEAD
|
2022-04-26 10:07:56 +00:00
|
|
|
if not news: # if empty news array
|
2022-05-11 07:26:42 +00:00
|
|
|
send_to_user(f"No news found for keyword _{keyword}_\.", pUser_id=p_user_id, md_mode=True)
|
2022-04-26 10:07:56 +00:00
|
|
|
|
2022-05-10 15:14:53 +00:00
|
|
|
elif news == None: # if news is none
|
2022-05-11 07:26:42 +00:00
|
|
|
send_to_user(f"Server error for keyword _{keyword}_\.", pUser_id=p_user_id, md_mode=True)
|
2022-05-12 06:37:53 +00:00
|
|
|
=======
|
2022-05-11 21:33:48 +00:00
|
|
|
if not news: # if empty news array
|
2022-04-26 10:07:56 +00:00
|
|
|
send_to_user(f"No news found for keyword _{keyword}_.", pUser_id=p_user_id, md_mode=True)
|
2022-05-11 21:33:48 +00:00
|
|
|
|
|
|
|
elif news == None: # if news is none
|
2022-04-26 10:07:56 +00:00
|
|
|
send_to_user(f"Server error for keyword _{keyword}_.", pUser_id=p_user_id, md_mode=True)
|
2022-05-12 06:37:53 +00:00
|
|
|
>>>>>>> origin/main
|
2022-04-26 10:07:56 +00:00
|
|
|
else:
|
2022-05-11 21:33:48 +00:00
|
|
|
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
|
2022-04-25 15:32:48 +00:00
|
|
|
|
|
|
|
|
2022-05-11 21:33:48 +00:00
|
|
|
def send_to_user(pText, pUser_id, md_mode=False):
|
2022-04-05 12:00:33 +00:00
|
|
|
""" Send message to user
|
|
|
|
:type pText: string
|
|
|
|
:param pText: Text to send to user
|
|
|
|
|
|
|
|
:type pUser_id: int
|
|
|
|
:param pUser_id: user to send to. per default me (Florian Kellermann)
|
|
|
|
|
2022-04-25 15:32:48 +00:00
|
|
|
:type md_mode: boolean
|
|
|
|
:param md_mode: if true, parse_mode is markdown
|
|
|
|
|
2022-04-05 12:00:33 +00:00
|
|
|
:raises: none
|
|
|
|
|
|
|
|
:rtype: none
|
|
|
|
"""
|
2022-04-25 15:32:48 +00:00
|
|
|
if md_mode:
|
2022-05-10 16:45:51 +00:00
|
|
|
bot.send_message(chat_id=pUser_id, text=pText, parse_mode="MARKDOWNV2")
|
2022-04-25 15:32:48 +00:00
|
|
|
else:
|
|
|
|
bot.send_message(chat_id=pUser_id, text=pText)
|
|
|
|
|
2022-04-05 12:00:33 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
2022-04-25 20:39:22 +00:00
|
|
|
start_updater()
|
2022-04-05 12:00:33 +00:00
|
|
|
sys.exit(-1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Ending")
|
2022-05-11 21:33:48 +00:00
|
|
|
sys.exit(-1)
|