TelegramAktienBot/telegram_bot/bot_updates.py
Florian Kellermann 813231449e Mistake corrected
2022-04-06 14:26:52 +02:00

149 lines
3.8 KiB
Python

"""
script for regularly sending updates on shares and news based on user interval
"""
__author__ = "Florian Kellermann, Linus Eickhoff"
__date__ = "05.04.2022"
__version__ = "0.0.1"
__license__ = "None"
from shares.share_fetcher import get_share_price
import time
import datetime
from bot import bot
import sys
from multiprocessing import Process
'''
* * * * * code
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── weekday (0->Monday, 7->Sunday)
│ │ │ └────── Month (1-12)
│ │ └──────── Day (1-31)
│ └────────── Hour (0-23)
└──────────── Minute (0-59)
example 0 8 * * * -> daily update at 8am
'''
user_ids = []
user_crontab = []
def main_loop():
""" main loop for regularly sending updates
:raises: none
:rtype: none
"""
current_time_datetime = datetime.datetime.now()
print(time.ctime()) # Debug
p1 = Process(target= update_crontab, args=(current_time_datetime, ))
p1.start()
p3 = Process(target= update_based_on_crontab, args=(current_time_datetime, ) )
p3.daemon = True
p3.start()
p1.join()
p3.terminate()
p1.terminate()
def update_crontab(pCurrent_Time):
""" Updating crontab lists every hour
:type pCurrent_Time: time when starting crontab update
:param pCurrent_Time: datetime
:raises: none
:rtype: none
"""
# Update user info now
print('in update_crontab')
user_ids.clear() # Example for me (Florian Kellermann)
user_crontab.clear()
user_ids.append(1770205310)
user_crontab.append("0 8 * * *")
while True:
time_difference = datetime.datetime.now() - pCurrent_Time
if float(str(time_difference).split(':')[0]) >=1:
update_crontab(datetime.datetime.now())
break
def update_based_on_crontab(pCurrent_Time):
current_time_ctime = time.ctime()
ctime_split = str(current_time_ctime).split(' ')
if ctime_split[0] == "Mon":
current_day = 0
elif ctime_split[0] == "Tue":
current_day = 1
elif ctime_split[0] == "Wed":
current_day = 3
elif ctime_split[0] == "Thu":
current_day = 4
elif ctime_split[0] == "Fri":
current_day = 5
elif ctime_split[0] == "Sat":
current_day = 6
elif ctime_split[0] == "Sun":
current_day = 7
else:
print('Error with time code')
sys.exit(-1)
for i in range(len(user_crontab)):
day_match = False
hour_match = False
user_crontab[i] = user_crontab.strip()
contrab_split = str(user_crontab[i]).split(' ')
if ',' in contrab_split[4]: # if the user wants to be alerted on multiple specific days
contrab_days = contrab_split[4].split(',')
else:
contrab_days = contrab_days
for element in contrab_days:
if str(current_day) == element or element=='*':
hour_match = True
if hour_match and day_match:
send_to_user("regular update", pUser_id=user_crontab[i])
def send_to_user(pText, pUser_id = 1770205310):
""" 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)
:raises: none
:rtype: none
"""
bot.send_message(chat_id=pUser_id, text=pText)
if __name__ == "__main__":
print('This script shall not be run directly. Starting main_loop for debugging purposes.')
try:
main_loop()
sys.exit(-1)
except KeyboardInterrupt:
print("Ending")
sys.exit(-1)