auto-schedule-pro-v2: hotfixing many issues at once to make the module work properly

This commit is contained in:
dymik739 2023-09-11 22:29:52 +03:00
parent 1381ddb007
commit a22fb2b4b1
3 changed files with 36 additions and 32 deletions

View File

@ -60,7 +60,7 @@ class ModuleV1:
return self.RESPONSE, self.FORMAT return self.RESPONSE, self.FORMAT
except Exception as e: except Exception as e:
print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"") print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"")
print(f"[ERROR] module v1: traceback:\ntraceback.format_exc()") print(f"[ERROR] module v1: traceback:\n{traceback.format_exc()}")
return "", None return "", None

View File

@ -1,6 +1,6 @@
from datetime import datetime from datetime import datetime
import json import json
import os
def readfile(filename): def readfile(filename):
with open(module_path + filename) as f: with open(module_path + filename) as f:
@ -44,14 +44,14 @@ def get_preference_by_id(user_id, name):
if not name in preferences: if not name in preferences:
return None return None
return preferences['name'] return preferences[name]
def get_all_preferences_by_id(user_id): def get_all_preferences_by_id(user_id):
user_preferences = {"output-style": "legacy-vibrant"} user_preferences = {"output-style": "legacy-vibrant"}
for i in default_preferences: for i in user_preferences:
override = get_preferences_by_id(i) override = get_preference_by_id(user_id, i)
if override != None: if override != None:
user_preferences[i] = override user_preferences[i] = override
@ -62,6 +62,8 @@ def set_preference_by_id(user_id, name, value):
if not os.path.exists(module_path + "preference-db/"): if not os.path.exists(module_path + "preference-db/"):
os.mkdir(module_path + "preference-db/") os.mkdir(module_path + "preference-db/")
preferences = {}
if os.path.exists(module_path + f"preference-db/{user_id}.json"): if os.path.exists(module_path + f"preference-db/{user_id}.json"):
try: try:
raw_prefs = readfile(f"preference-db/{user_id}.json") raw_prefs = readfile(f"preference-db/{user_id}.json")
@ -74,7 +76,7 @@ def set_preference_by_id(user_id, name, value):
preferences[name] = value preferences[name] = value
final_data = json.dumps(preferences) final_data = json.dumps(preferences)
writefile(final_data) writefile(f"preference-db/{user_id}.json", final_data)
def load_template(template, part): def load_template(template, part):
@ -166,7 +168,7 @@ def generate_lesson_description(lesson, start_datetime, end_datetime, current_da
if lesson.__class__ == dict: if lesson.__class__ == dict:
active_template = load_template(template, "single") active_template = load_template(template, "single")
for i in ['name', 'teacher', 'link', 'comment']: for i in ['name', 'teacher', 'link']:
active_template = active_template.replace(f"%{i.upper()}%", lesson[i]) active_template = active_template.replace(f"%{i.upper()}%", lesson[i])
@ -188,11 +190,11 @@ def generate_lesson_description(lesson, start_datetime, end_datetime, current_da
for l in lesson: for l in lesson:
active_template = load_template(template, "multiple") active_template = load_template(template, "multiple")
for i in ['name', 'teacher', 'link', 'comment']: for i in ['name', 'teacher', 'link']:
active_template = active_template.replace(f"%{i.upper()}%", escaped_string(lesson[i])) active_template = active_template.replace(f"%{i.upper()}%", l[i])
active_template = active_template.replace("%DATE%", human_readable_date) active_template = active_template.replace("%DATE%", human_readable_date)
active_template = active_template.replace("%TYPE%", get_name_of_lesson_type(lesson['type'])) active_template = active_template.replace("%TYPE%", get_name_of_lesson_type(l['type']))
active_template = active_template.replace("%NAME_PREFIX%", custom_name_prefix) active_template = active_template.replace("%NAME_PREFIX%", custom_name_prefix)
total_result += active_template + "\n" total_result += active_template + "\n"
@ -288,7 +290,7 @@ def get_lesson_description(schedule, reference_time, lesson_time, current_day, c
''' '''
return generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime, current_day, return generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime, current_day,
current_week, overrides=overrides, custom_name_prefix=custom_name_prefix) current_week, overrides=overrides, custom_name_prefix=custom_name_prefix, template=template)
def process(message, path): def process(message, path):
@ -319,7 +321,9 @@ def process(message, path):
reference_time = int(current_time.strftime("%s")) - current_seconds reference_time = int(current_time.strftime("%s")) - current_seconds
output_style_preference = get_preference_by_id(message['from']['id'], "output-style") output_style_preference = get_preference_by_id(message.from_user.id, "output-style")
if output_style_preference == None:
output_style_preference = "legacy-vibrant"
if base_command == "!пара": if base_command == "!пара":
# easter egg # easter egg
@ -360,34 +364,34 @@ def process(message, path):
current_week, overrides=preferences, custom_name_prefix="Назва", template=output_style_preference) current_week, overrides=preferences, custom_name_prefix="Назва", template=output_style_preference)
for lesson_time in lesson_list] for lesson_time in lesson_list]
return f"<b><u>Пари у {WEEKDAYS_ACCUSATIVE[selected_day % 7]}</u></b>:\n\n\n" + "\n\n".join(lesson_descriptions_list), "HTML" return f"<b><u>Пари у {WEEKDAYS_ACCUSATIVE[selected_day % 7]}</u></b>:\n\n\n" + "\n".join(lesson_descriptions_list), "HTML"
elif base_command == "!schedule-ctl": elif base_command == "!schedule-ctl":
if base_command[1] == "list": if full_command[1] == "list":
prefs = get_all_preferences_by_id(message['from']['id']) prefs = get_all_preferences_by_id(message.from_user.id)
return "Ваші персональні налаштування:\n" + [f"- {k} = {v}" for k, v in prefs.items()] return "Ваші персональні налаштування:\n" + '\n'.join([f"- {k} = {v}" for k, v in prefs.items()]), "HTML"
elif base_command[1] == "set": elif full_command[1] == "set":
prefs = get_all_preferences_by_id(message['from']['id']) prefs = get_all_preferences_by_id(message.from_user.id)
if base_command[2] in prefs: if full_command[2] in prefs:
if base_command[2] == "output-style": if full_command[2] == "output-style":
if base_command[3] not in os.listdir(module_path + "templates/"): if full_command[3] not in os.listdir(module_path + "templates/"):
return f"Стилю {base_command[3]} не існує; доступні варіанти: " \ return f"Стилю {full_command[3]} не існує; доступні варіанти: " \
+ ', '.join(os.listdir(module_path + "templates/")), "HTML" + ', '.join(os.listdir(module_path + "templates/")), "HTML"
previous_value = prefs[base_command[2]] previous_value = prefs[full_command[2]]
prefs[base_command[2]] = base_command[3] prefs[full_command[2]] = full_command[3]
set_preference_by_id(message['from']['id'], base_command[2], base_command[3]) set_preference_by_id(message.from_user.id, full_command[2], full_command[3])
return f"Змінено значення {base_command[2]}: {previous_value} -> {base_command[3]}", "HTML" return f"Змінено значення {full_command[2]}: {previous_value} -> {full_command[3]}", "HTML"
else: else:
return f"Такого налаштування не існує; переглянути наявні налаштування можна за допомогою команди <u>!schedule-ctl list</u>", "HTML" return f"Такого налаштування не існує; переглянути наявні налаштування можна за допомогою команди <u>!schedule-ctl list</u>", "HTML"
elif base_command[1] == "get": elif full_command[1] == "get":
requested_preference = get_preference_by_id(message['from']['id'], base_command[2]) requested_preference = get_preference_by_id(message.from_user.id, full_command[2])
return f"Налаштування {base_command[2]} має значення {requested_preference}", "HTML" return f"Налаштування {full_command[2]} має значення {requested_preference}", "HTML"
else: else:
return "Такої команди не існує", "HTML" return "Такої команди не існує", "HTML"

View File

@ -1 +1 @@
%DATE%: <b><u>%DATE%</u></b>: