add thread command

This commit is contained in:
kr0sh512
2023-10-10 20:53:54 +03:00
parent 4560b1128f
commit c37bd01b06
5 changed files with 71 additions and 31 deletions
+23 -19
View File
@@ -3,8 +3,8 @@ import json, schedule
import schedules as bot
from datetime import datetime
path = "users.json" # Нужный путь до json файлов
path_schedule = "schedule.json"
path_users = "json/users.json" # Нужный путь до json файлов
path_schedule = "json/schedule.json"
def save_user(infos):
for i in range(len(infos)):
@@ -20,7 +20,7 @@ def save_user(infos):
}
data = {}
with open(path, 'r', encoding='utf-8') as json_file:
with open(path_users, 'r', encoding='utf-8') as json_file:
data = json.load(json_file)
data[infos["id"]] = {
@@ -29,34 +29,36 @@ def save_user(infos):
"last_name": infos["last_name"],
"group": infos["group"],
"timeout": "10",
"allow_message": "yes"
"allow_message": "yes",
"thread": "General"
}
with open(path, 'w', encoding='utf-8') as json_file:
with open(path_users, 'w', encoding='utf-8') as json_file:
json.dump(data, json_file, ensure_ascii=False, indent=4)
create_schedule_tasks()
return
def change_user_param(id, key, value):
data = {}
with open(path, 'r', encoding='utf-8') as json_file:
with open(path_users, 'r', encoding='utf-8') as json_file:
data = json.load(json_file)
data[id][key] = value
with open(path, 'w', encoding='utf-8') as json_file:
with open(path_users, 'w', encoding='utf-8') as json_file:
json.dump(data, json_file, ensure_ascii=False, indent=4)
create_schedule_tasks()
return
def create_schedule_tasks():
schedule.clear()
users = {}
schdl = {}
with open(path, 'r', encoding='utf-8') as json_file:
with open(path_users, 'r', encoding='utf-8') as json_file:
users = json.load(json_file)
with open(path_schedule, 'r', encoding='utf-8') as json_file:
schdl = json.load(json_file)
for id, params in users.items():
# print(id, '=>', params)
if params['allow_message'] != 'yes':
continue
group = params['group']
@@ -65,7 +67,7 @@ def create_schedule_tasks():
timeout = params['timeout']
for day in schdl[group]:
for i, lesson in schdl[group][day].items():
if lesson["name"] == "": #проверка на лоха
if lesson["name"] == "": # Неправильный формат schedule
continue
start, end = i.split("-")
@@ -94,6 +96,7 @@ def create_schedule_tasks():
print(lesson["name"])
continue
thread_id = params["thread"]
delta = '00:' + params["timeout"]
format = '%H:%M'
start_task = datetime.strptime(start, format) - datetime.strptime(delta, format)
@@ -102,19 +105,20 @@ def create_schedule_tasks():
tmp += (('0' + i ) if len(i) < 2 else i) + ":"
start_task = tmp[:-1]
if day == 'mon':
schedule.every().monday.at(start_task).do(bot.send_message, id, text)
schedule.every().monday.at(start_task).do(bot.send_message, id, text, thread_id)
elif day == 'tue':
schedule.every().tuesday.at(start_task).do(bot.send_message, id, text)
schedule.every().tuesday.at(start_task).do(bot.send_message, id, text, thread_id)
elif day == 'wed':
schedule.every().wednesday.at(start_task).do(bot.send_message, id, text)
schedule.every().wednesday.at(start_task).do(bot.send_message, id, text, thread_id)
elif day == 'thu':
schedule.every().thursday.at(start_task).do(bot.send_message, id, text)
schedule.every().thursday.at(start_task).do(bot.send_message, id, text, thread_id)
elif day == 'fri':
schedule.every().friday.at(start_task).do(bot.send_message, id, text)
schedule.every().friday.at(start_task).do(bot.send_message, id, text, thread_id)
elif day == 'sat':
schedule.every().saturday.at(start_task).do(bot.send_message, id, text)
schedule.every().saturday.at(start_task).do(bot.send_message, id, text, thread_id)
bot.send_message(bot.krosh, '🛑 Произошёл update')
bot.send_message(bot.admin_id, '🛑 Произошёл update')
return
def check_group_in_json(number):
@@ -125,10 +129,10 @@ def check_group_in_json(number):
def return_infos(id):
data = {}
with open(path, 'r', encoding='utf-8') as json_file:
with open(path_users, 'r', encoding='utf-8') as json_file:
data = json.load(json_file)
data = data[id]
text = '<i>Выбранная группа</i>: <b>{}</b>\
\n<i>Время напоминания до урока</i>: <b>{}</b>\
\n<i>Разрешены ли напоминания</i>: <b>{}</b>'.format(data['group'], data['timeout'], ('да' if data['allow_message'] == 'yes' else 'нет'))
return text
return text