Not working fully yet, still problems with scheduler, might try other module later

This commit is contained in:
Florian Kellermann 2022-04-17 13:26:12 +02:00
parent 26b41f2c94
commit 1803bba222
3 changed files with 87 additions and 55 deletions

View File

@ -6,12 +6,16 @@ __date__ = "05.04.2022"
__version__ = "0.0.1" __version__ = "0.0.1"
__license__ = "None" __license__ = "None"
from calendar import month
from shares.share_fetcher import get_share_price from shares.share_fetcher import get_share_price
import time import time
import datetime import datetime
from bot import bot from bot import bot
import sys import sys
from multiprocessing import Process from multiprocessing import Process
from apscheduler.schedulers.blocking import BlockingScheduler
from api_handling.api_handler import API_Handler
''' '''
* * * * * code * * * * * code
@ -25,7 +29,6 @@ from multiprocessing import Process
example 0 8 * * * -> daily update at 8am example 0 8 * * * -> daily update at 8am
''' '''
user_ids = [] user_ids = []
user_crontab = [] user_crontab = []
@ -37,20 +40,24 @@ def main_loop():
""" """
current_time_datetime = datetime.datetime.now() current_time_datetime = datetime.datetime.now()
my_handler = API_Handler("https://gruppe1.testsites.info/api", "bot@example.com", "bot")
print(time.ctime()) # Debug 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() 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.daemon = True
p3.start() p3.start()
p1.join() p1.join()
p3.terminate() 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 """ Updating crontab lists every hour
:type pCurrent_Time: time when starting crontab update :type pCurrent_Time: time when starting crontab update
:param pCurrent_Time: datetime :param pCurrent_Time: datetime
@ -60,14 +67,23 @@ def update_crontab(pCurrent_Time):
:rtype: none :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) all_users = p_my_handler.get_all_users()
user_crontab.clear()
user_ids.append(1770205310) user_ids = []
user_crontab.append("0 8 * * *") 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: while True:
time_difference = datetime.datetime.now() - pCurrent_Time time_difference = datetime.datetime.now() - pCurrent_Time
@ -76,52 +92,26 @@ def update_crontab(pCurrent_Time):
break break
def update_based_on_crontab(pCurrent_Time): def update_based_on_crontab():
current_time_ctime = time.ctime()
ctime_split = str(current_time_ctime).split(' ')
if ctime_split[0] == "Mon": my_scheduler = BlockingScheduler()
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)): print("update based on cron")
day_match = False
hour_match = False
user_crontab[i] = user_crontab.strip() print(len(user_ids)) #Debug
contrab_split = str(user_crontab[i]).split(' ') 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], ))
if ',' in contrab_split[4]: # if the user wants to be alerted on multiple specific days my_scheduler.start()
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 crontab")
def update_for_user(p_user_id):
print("updating for {p_user_id}")
def send_to_user(pText, pUser_id = 1770205310): def send_to_user(pText, pUser_id = 1770205310):

View File

@ -5,4 +5,5 @@ newsapi-python~=0.2.6
python-dotenv~=0.20.0 python-dotenv~=0.20.0
requests~=2.27.1 requests~=2.27.1
APScheduler~=3.9.1 APScheduler~=3.9.1
cronitor~=1.3.4 croniter~=1.3.4
tzlocal==2.1

41
telegram_bot/testfile.py Normal file
View File

@ -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()