diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 821de90..7ed1bf2 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -6,12 +6,16 @@ __date__ = "05.04.2022" __version__ = "0.0.1" __license__ = "None" +from calendar import month from shares.share_fetcher import get_share_price import time import datetime from bot import bot import sys from multiprocessing import Process +from apscheduler.schedulers.blocking import BlockingScheduler +from api_handling.api_handler import API_Handler + ''' * * * * * code @@ -25,7 +29,6 @@ from multiprocessing import Process example 0 8 * * * -> daily update at 8am ''' - user_ids = [] user_crontab = [] @@ -37,20 +40,24 @@ def main_loop(): """ current_time_datetime = datetime.datetime.now() + my_handler = API_Handler("https://gruppe1.testsites.info/api", "bot@example.com", "bot") print(time.ctime()) # Debug - p1 = Process(target= update_crontab, args=(current_time_datetime, )) + """p1 = Process(target= update_crontab, args=(current_time_datetime, my_handler )) p1.start() - p3 = Process(target= update_based_on_crontab, args=(current_time_datetime, ) ) + time.sleep(5) + p3 = Process(target= update_based_on_crontab, args=() ) p3.daemon = True p3.start() p1.join() p3.terminate() - p1.terminate() + p1.terminate()""" + + update_crontab(current_time_datetime, my_handler) -def update_crontab(pCurrent_Time): +def update_crontab(pCurrent_Time, p_my_handler): """ Updating crontab lists every hour :type pCurrent_Time: time when starting crontab update :param pCurrent_Time: datetime @@ -60,14 +67,23 @@ def update_crontab(pCurrent_Time): :rtype: none """ - # Update user info now + global user_crontab + global user_ids - print('in update_crontab') + p_my_handler.set_cron_interval(user_id = 1770205310, cron_interval = "50 11 * * *") - user_ids.clear() # Example for me (Florian Kellermann) - user_crontab.clear() - user_ids.append(1770205310) - user_crontab.append("0 8 * * *") + all_users = p_my_handler.get_all_users() + + user_ids = [] + user_crontab = [] + + for element in all_users: + if element["cron"] != '' and element["telegram_user_id"] != '': + user_ids.append(int(element["telegram_user_id"])) + user_crontab.append(str(element["cron"])) + + print(user_ids) + update_based_on_crontab() while True: time_difference = datetime.datetime.now() - pCurrent_Time @@ -76,52 +92,26 @@ def update_crontab(pCurrent_Time): break -def update_based_on_crontab(pCurrent_Time): - current_time_ctime = time.ctime() - ctime_split = str(current_time_ctime).split(' ') +def update_based_on_crontab(): - 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) + my_scheduler = BlockingScheduler() - 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]) + print("update based on cron") + + print(len(user_ids)) #Debug + + for i in range(len(user_ids)): + print("in loop") + cron_split = user_crontab[i].split(" ") + 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=(user_ids[i], )) + + my_scheduler.start() + + print("update based on crontab") - +def update_for_user(p_user_id): + print("updating for {p_user_id}") def send_to_user(pText, pUser_id = 1770205310): diff --git a/telegram_bot/requirements.txt b/telegram_bot/requirements.txt index 146877f..6620615 100644 --- a/telegram_bot/requirements.txt +++ b/telegram_bot/requirements.txt @@ -5,4 +5,5 @@ newsapi-python~=0.2.6 python-dotenv~=0.20.0 requests~=2.27.1 APScheduler~=3.9.1 -cronitor~=1.3.4 +croniter~=1.3.4 +tzlocal==2.1 diff --git a/telegram_bot/testfile.py b/telegram_bot/testfile.py new file mode 100644 index 0000000..4369096 --- /dev/null +++ b/telegram_bot/testfile.py @@ -0,0 +1,41 @@ +from multiprocessing import Process +import time + +class Array_Class: + def __init__(self): + self.array = [] + + def SetArray(self, parray): + array = parray + + def GetArray(self): + return self.array + +my_array = Array_Class() + + +def update_crontab(): + global my_array + array = [] + time.sleep(5) + array.append(1) + my_array.SetArray(array) + print("set done") + +def update_based_on_crontab(): + time.sleep(5) + print(my_array.GetArray()) + + +"""update_crontab() +update_based_on_crontab()""" + +if __name__ == "__main__": + p1 = Process(target= update_crontab, args=()) + p1.start() + p3 = Process(target= update_based_on_crontab, args=() ) + p3.daemon = True + p3.start() + p1.join() + p3.terminate() + p1.terminate() \ No newline at end of file