2023-05-06 18:03:01 +03:00
|
|
|
|
from datetime import datetime
|
|
|
|
|
import json
|
2023-09-11 22:29:52 +03:00
|
|
|
|
import os
|
2023-05-08 10:22:23 +03:00
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
def readfile(filename):
|
|
|
|
|
with open(module_path + filename) as f:
|
|
|
|
|
return f.read()
|
|
|
|
|
|
2023-09-11 21:36:24 +03:00
|
|
|
|
def writefile(filename, data):
|
|
|
|
|
with open(module_path + filename, 'w') as f:
|
|
|
|
|
f.write(data)
|
2023-05-08 10:22:23 +03:00
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
# global constants
|
|
|
|
|
# Accusative - znahidnyj
|
2023-05-08 10:22:23 +03:00
|
|
|
|
WEEKDAYS_ACCUSATIVE = ["понеділок", "вівторок", "середу", "четвер", "п'ятницю", "суботу", "неділю"]
|
2023-09-05 20:40:57 +03:00
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
# Genitive - rodovyj
|
2023-05-08 10:22:23 +03:00
|
|
|
|
WEEKDAYS_GENITIVE_NEXT = ["наступного понеділка", "наступного вівторка", "наступної середи", "наступного четверга",
|
|
|
|
|
"наступної п'ятниці", "наступної суботи", "наступної неділі"]
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
2023-05-08 10:22:23 +03:00
|
|
|
|
WEEKDAYS_GENITIVE_THIS = ["цього понеділка", "цього вівторка", "цієї середи", "цього четверга", "цієї п'ятниці",
|
|
|
|
|
"цієї суботи", "цієї неділі"]
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
2023-09-05 20:40:57 +03:00
|
|
|
|
lesson_types_to_strings = {
|
2023-09-12 10:04:00 +03:00
|
|
|
|
"lec": "лекція",
|
|
|
|
|
"prac": "практика",
|
2023-11-06 18:39:31 +02:00
|
|
|
|
"lab": "лабораторна",
|
|
|
|
|
"con": "консультація"
|
2023-09-05 20:40:57 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
# global variables
|
|
|
|
|
module_path = ""
|
|
|
|
|
|
2023-09-11 21:36:24 +03:00
|
|
|
|
def get_preference_by_id(user_id, name):
|
|
|
|
|
if not os.path.exists(module_path + f"preference-db/{user_id}.json"):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
raw_prefs = readfile(f"preference-db/{user_id}.json")
|
|
|
|
|
try:
|
|
|
|
|
preferences = json.loads(raw_prefs)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
if not name in preferences:
|
|
|
|
|
return None
|
2023-09-11 22:29:52 +03:00
|
|
|
|
|
|
|
|
|
return preferences[name]
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_preferences_by_id(user_id):
|
2023-09-12 12:24:33 +03:00
|
|
|
|
user_preferences = {
|
|
|
|
|
"output-style": "legacy-vibrant",
|
|
|
|
|
"output-style-lesson": "None",
|
|
|
|
|
"output-style-lookup": "None"
|
|
|
|
|
}
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-12 12:24:33 +03:00
|
|
|
|
# label defaults as defaults and let custom settings override these labels
|
2023-09-12 10:04:00 +03:00
|
|
|
|
for i in user_preferences:
|
|
|
|
|
user_preferences[i] += " <i>(default)</i>"
|
|
|
|
|
|
2023-09-11 22:29:52 +03:00
|
|
|
|
for i in user_preferences:
|
|
|
|
|
override = get_preference_by_id(user_id, i)
|
2023-09-11 21:36:24 +03:00
|
|
|
|
if override != None:
|
|
|
|
|
user_preferences[i] = override
|
|
|
|
|
|
|
|
|
|
return user_preferences
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_preference_by_id(user_id, name, value):
|
|
|
|
|
if not os.path.exists(module_path + "preference-db/"):
|
|
|
|
|
os.mkdir(module_path + "preference-db/")
|
|
|
|
|
|
2023-09-11 22:29:52 +03:00
|
|
|
|
preferences = {}
|
|
|
|
|
|
2023-09-11 21:36:24 +03:00
|
|
|
|
if os.path.exists(module_path + f"preference-db/{user_id}.json"):
|
|
|
|
|
try:
|
|
|
|
|
raw_prefs = readfile(f"preference-db/{user_id}.json")
|
|
|
|
|
preferences = json.loads(raw_prefs)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
preferences = {}
|
|
|
|
|
else:
|
|
|
|
|
preferences = {}
|
|
|
|
|
|
|
|
|
|
preferences[name] = value
|
|
|
|
|
|
|
|
|
|
final_data = json.dumps(preferences)
|
2023-09-11 22:29:52 +03:00
|
|
|
|
writefile(f"preference-db/{user_id}.json", final_data)
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
|
|
|
|
|
2023-09-12 10:04:00 +03:00
|
|
|
|
def clear_preference_by_id(user_id, name):
|
2023-09-12 09:52:38 +03:00
|
|
|
|
if not os.path.exists(module_path + "preference-db/"):
|
|
|
|
|
os.mkdir(module_path + "preference-db/")
|
|
|
|
|
|
|
|
|
|
preferences = {}
|
|
|
|
|
|
|
|
|
|
if os.path.exists(module_path + f"preference-db/{user_id}.json"):
|
|
|
|
|
try:
|
|
|
|
|
raw_prefs = readfile(f"preference-db/{user_id}.json")
|
|
|
|
|
preferences = json.loads(raw_prefs)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
preferences = {}
|
|
|
|
|
else:
|
|
|
|
|
preferences = {}
|
|
|
|
|
|
|
|
|
|
if name in preferences:
|
|
|
|
|
del preferences[name]
|
|
|
|
|
|
|
|
|
|
final_data = json.dumps(preferences)
|
|
|
|
|
writefile(f"preference-db/{user_id}.json", final_data)
|
|
|
|
|
|
|
|
|
|
|
2023-09-11 21:36:24 +03:00
|
|
|
|
def load_template(template, part):
|
|
|
|
|
return readfile(f"templates/{template}/{part}.msg")
|
|
|
|
|
|
|
|
|
|
|
2023-09-06 17:22:26 +03:00
|
|
|
|
def escaped_string_markdownV2(input_string):
|
2023-09-05 20:40:57 +03:00
|
|
|
|
result_string = input_string
|
|
|
|
|
|
|
|
|
|
symbols_to_escape = ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!']
|
|
|
|
|
|
|
|
|
|
for symbol in symbols_to_escape:
|
|
|
|
|
result_string = result_string.replace(symbol, f"\\{symbol}")
|
|
|
|
|
|
|
|
|
|
return result_string
|
2023-05-08 10:22:23 +03:00
|
|
|
|
|
2023-09-06 17:22:26 +03:00
|
|
|
|
|
|
|
|
|
def escaped_string_html(input_string):
|
|
|
|
|
result_string = input_string
|
|
|
|
|
|
2023-09-06 17:36:18 +03:00
|
|
|
|
symbols_to_escape = ['<', '>']
|
2023-09-06 17:22:26 +03:00
|
|
|
|
|
|
|
|
|
for symbol in symbols_to_escape:
|
|
|
|
|
result_string = result_string.replace(symbol, f"\\{symbol}")
|
|
|
|
|
|
|
|
|
|
return result_string
|
|
|
|
|
|
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
def get_human_readable_date(start_datetime, end_datetime,
|
|
|
|
|
current_day, current_week):
|
|
|
|
|
human_readable_date = ""
|
2023-05-08 12:27:39 +03:00
|
|
|
|
if ((current_day + 2) == int(start_datetime.strftime("%u"))) or \
|
|
|
|
|
((current_day == 6) and (start_datetime.strftime("%u") == "1")):
|
2023-05-06 18:03:01 +03:00
|
|
|
|
human_readable_date += "завтра "
|
|
|
|
|
elif current_week != int(start_datetime.strftime("%W")) % 2:
|
|
|
|
|
human_readable_date += f"{WEEKDAYS_GENITIVE_NEXT[int(start_datetime.strftime('%u')) - 1]} "
|
|
|
|
|
elif current_day != (int(start_datetime.strftime("%u")) - 1):
|
|
|
|
|
human_readable_date += f"{WEEKDAYS_GENITIVE_THIS[int(start_datetime.strftime('%u')) - 1]} "
|
|
|
|
|
else:
|
|
|
|
|
human_readable_date += "сьогодні "
|
|
|
|
|
|
|
|
|
|
human_readable_date += "з "
|
|
|
|
|
human_readable_date += start_datetime.strftime("%H:%M")
|
|
|
|
|
|
|
|
|
|
human_readable_date += " до "
|
|
|
|
|
human_readable_date += end_datetime.strftime("%H:%M")
|
|
|
|
|
|
|
|
|
|
return human_readable_date
|
|
|
|
|
|
|
|
|
|
|
2023-09-05 20:40:57 +03:00
|
|
|
|
def get_name_of_lesson_type(lesson_type):
|
|
|
|
|
if lesson_type in lesson_types_to_strings:
|
|
|
|
|
return lesson_types_to_strings[lesson_type]
|
|
|
|
|
|
2023-05-08 10:22:23 +03:00
|
|
|
|
|
2023-09-11 21:36:24 +03:00
|
|
|
|
def generate_lesson_description(lesson, start_datetime, end_datetime, current_day, current_week, overrides={},
|
2023-09-12 11:44:03 +03:00
|
|
|
|
custom_name_prefix="Назва", template="legacy-vibrant", force_date_at_top=False):
|
2023-09-11 21:36:24 +03:00
|
|
|
|
# temporarily not supported
|
|
|
|
|
#output_settings = {"name": True, "date": True, "teacher": True, "link": True, "comment": True}
|
|
|
|
|
#output_settings.update(overrides)
|
2023-09-11 22:29:52 +03:00
|
|
|
|
|
2023-09-11 21:36:24 +03:00
|
|
|
|
if lesson.__class__ == dict:
|
2023-09-12 11:44:03 +03:00
|
|
|
|
if force_date_at_top:
|
|
|
|
|
total_result = load_template(template, "date")
|
|
|
|
|
human_readable_date = get_human_readable_date(start_datetime, end_datetime,
|
|
|
|
|
current_day, current_week)
|
|
|
|
|
total_result = total_result.replace("%DATE%", human_readable_date)
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-12 11:44:03 +03:00
|
|
|
|
total_result += load_template(template, "multiple")
|
|
|
|
|
for i in ['name', 'teacher', 'link']:
|
|
|
|
|
total_result = total_result.replace(f"%{i.upper()}%", lesson[i])
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-12 11:44:03 +03:00
|
|
|
|
total_result = total_result.replace("%DATE%", human_readable_date)
|
|
|
|
|
total_result = total_result.replace("%TYPE%", get_name_of_lesson_type(lesson['type']))
|
|
|
|
|
total_result = total_result.replace("%NAME_PREFIX%", custom_name_prefix)
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-12 11:44:03 +03:00
|
|
|
|
return total_result + "\n"
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
active_template = load_template(template, "single")
|
|
|
|
|
|
|
|
|
|
for i in ['name', 'teacher', 'link']:
|
|
|
|
|
active_template = active_template.replace(f"%{i.upper()}%", lesson[i])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
human_readable_date = get_human_readable_date(start_datetime, end_datetime,
|
2023-09-11 21:36:24 +03:00
|
|
|
|
current_day, current_week)
|
|
|
|
|
|
2023-09-12 11:44:03 +03:00
|
|
|
|
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("%NAME_PREFIX%", custom_name_prefix)
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-12 11:44:03 +03:00
|
|
|
|
return active_template
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
|
|
|
|
elif lesson.__class__ == list:
|
|
|
|
|
total_result = load_template(template, "date")
|
|
|
|
|
human_readable_date = get_human_readable_date(start_datetime, end_datetime,
|
|
|
|
|
current_day, current_week)
|
|
|
|
|
total_result = total_result.replace("%DATE%", human_readable_date)
|
|
|
|
|
|
|
|
|
|
for l in lesson:
|
|
|
|
|
active_template = load_template(template, "multiple")
|
|
|
|
|
|
2023-09-11 22:29:52 +03:00
|
|
|
|
for i in ['name', 'teacher', 'link']:
|
|
|
|
|
active_template = active_template.replace(f"%{i.upper()}%", l[i])
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
|
|
|
|
active_template = active_template.replace("%DATE%", human_readable_date)
|
2023-09-11 22:29:52 +03:00
|
|
|
|
active_template = active_template.replace("%TYPE%", get_name_of_lesson_type(l['type']))
|
2023-09-11 21:36:24 +03:00
|
|
|
|
active_template = active_template.replace("%NAME_PREFIX%", custom_name_prefix)
|
|
|
|
|
|
|
|
|
|
total_result += active_template + "\n"
|
|
|
|
|
|
|
|
|
|
return total_result
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
2023-09-12 09:52:38 +03:00
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
def get_schedule_data_from(filename):
|
|
|
|
|
raw_schedule = json.loads(readfile(filename))
|
|
|
|
|
|
|
|
|
|
baked_schedule = {}
|
|
|
|
|
|
2023-05-08 10:22:23 +03:00
|
|
|
|
for day_number, lesson_times in enumerate(raw_schedule):
|
2023-05-06 18:03:01 +03:00
|
|
|
|
for lesson_time in lesson_times:
|
2023-05-08 10:22:23 +03:00
|
|
|
|
timestamp = day_number * 86400 + int(lesson_time.split(":")[0]) * 3600 \
|
|
|
|
|
+ int(lesson_time.split(":")[1]) * 60
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
2023-09-05 20:40:57 +03:00
|
|
|
|
new_record = raw_schedule[day_number][lesson_time]
|
|
|
|
|
item_source = filename.split(".json")[0]
|
|
|
|
|
|
|
|
|
|
if new_record.__class__ == list:
|
|
|
|
|
for item in new_record:
|
|
|
|
|
item["source"] = item_source
|
|
|
|
|
else:
|
|
|
|
|
new_record["source"] = item_source
|
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
baked_schedule[timestamp] = new_record
|
|
|
|
|
|
|
|
|
|
return baked_schedule
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_arguments(args, base_day):
|
|
|
|
|
selected_day = int(base_day)
|
|
|
|
|
preferences = {}
|
|
|
|
|
|
|
|
|
|
for arg in args:
|
|
|
|
|
if arg[0] == "-":
|
2023-05-06 18:25:00 +03:00
|
|
|
|
if arg[1:].isdigit():
|
|
|
|
|
selected_day -= int(arg[1:])
|
|
|
|
|
else:
|
|
|
|
|
preferences[arg[1:]] = False
|
2023-05-08 10:22:23 +03:00
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
elif arg[0] == "+":
|
2023-05-06 18:25:00 +03:00
|
|
|
|
if arg[1:].isdigit():
|
|
|
|
|
selected_day += int(arg[1:])
|
|
|
|
|
else:
|
|
|
|
|
preferences[arg[1:]] = True
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
|
|
|
|
selected_day = selected_day % 14
|
|
|
|
|
|
|
|
|
|
return preferences, selected_day
|
|
|
|
|
|
|
|
|
|
|
2023-09-05 20:40:57 +03:00
|
|
|
|
def get_lesson_description(schedule, reference_time, lesson_time, current_day, current_week, overrides={},
|
2023-09-12 11:44:03 +03:00
|
|
|
|
custom_name_prefix="Назва", template="legacy-vibrant", force_date_at_top=False):
|
2023-05-06 18:03:01 +03:00
|
|
|
|
lesson_record = schedule[lesson_time]
|
|
|
|
|
|
|
|
|
|
lesson_start_datetime = datetime.fromtimestamp(reference_time + lesson_time)
|
|
|
|
|
lesson_end_datetime = datetime.fromtimestamp(reference_time + lesson_time + 5400)
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
|
|
|
|
return generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime, current_day,
|
2023-09-12 11:44:03 +03:00
|
|
|
|
current_week, overrides=overrides, custom_name_prefix=custom_name_prefix, template=template,
|
|
|
|
|
force_date_at_top=force_date_at_top)
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process(message, path):
|
|
|
|
|
message_text = message["text"]
|
|
|
|
|
full_command = message_text.split()
|
2023-05-08 10:22:23 +03:00
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
# there is no need to check if the full_command list if empty as it
|
|
|
|
|
# never will be - Telegram requires all messages to have at least
|
|
|
|
|
# one printable symbol, so this is already protected
|
|
|
|
|
base_command = full_command[0].lower()
|
|
|
|
|
|
2023-09-11 21:36:24 +03:00
|
|
|
|
if base_command not in ["!пара", "!пари", "!schedule-ctl"]:
|
2023-09-06 17:22:26 +03:00
|
|
|
|
return None, None
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
|
|
|
|
global module_path
|
|
|
|
|
module_path = path
|
|
|
|
|
|
2023-09-05 20:40:57 +03:00
|
|
|
|
schedule = get_schedule_data_from("schedule-v2.json")
|
|
|
|
|
schedule.update(get_schedule_data_from("additions-v2.json"))
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
|
|
|
|
current_time = datetime.now()
|
2023-05-08 10:22:23 +03:00
|
|
|
|
|
2023-05-06 18:03:01 +03:00
|
|
|
|
current_week = current_time.isocalendar()[1] % 2
|
|
|
|
|
current_day = current_time.weekday()
|
|
|
|
|
|
2023-05-08 10:22:23 +03:00
|
|
|
|
current_seconds = current_week * 604800 + current_day * 86400 + current_time.hour * 3600 + current_time.minute \
|
|
|
|
|
* 60 + current_time.second
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
|
|
|
|
reference_time = int(current_time.strftime("%s")) - current_seconds
|
|
|
|
|
|
2023-05-06 18:14:53 +03:00
|
|
|
|
if base_command == "!пара":
|
2023-09-11 21:36:24 +03:00
|
|
|
|
# easter egg
|
2023-08-07 23:32:45 +03:00
|
|
|
|
study_begin_ts = int(datetime(year=2023, month=9, day=4).strftime("%s"))
|
|
|
|
|
current_ts = int(datetime.now().strftime("%s"))
|
|
|
|
|
|
|
|
|
|
if -3600*4 < study_begin_ts - current_ts < 0:
|
2023-09-11 21:36:24 +03:00
|
|
|
|
return "Навчання от-от розпочнеться!", None
|
2023-08-07 23:32:45 +03:00
|
|
|
|
elif 0 <= study_begin_ts - current_ts < 1209600:
|
2023-09-06 17:22:26 +03:00
|
|
|
|
return f"До навчання залишилося {study_begin_ts - current_ts} секунд...", None
|
2023-08-07 23:32:45 +03:00
|
|
|
|
elif study_begin_ts - current_ts >= 1209600:
|
2023-09-06 17:22:26 +03:00
|
|
|
|
return "Ви маєте законне право відпочити, пари почнуться не скоро", None
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
|
|
|
|
# actual lesson finding code
|
2023-05-08 10:22:23 +03:00
|
|
|
|
upcoming_lessons = [timestamp for timestamp in schedule if timestamp > current_seconds - 5400]
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
|
|
|
|
if len(upcoming_lessons) > 0:
|
|
|
|
|
closest_lesson_time = min(upcoming_lessons)
|
|
|
|
|
else:
|
|
|
|
|
closest_lesson_time = min(schedule)
|
2023-05-08 10:22:23 +03:00
|
|
|
|
|
2023-09-12 12:24:33 +03:00
|
|
|
|
# shifting lesson pointer if requested to do so
|
|
|
|
|
if len(full_command) >= 2:
|
|
|
|
|
possible_times = list(schedule.keys())
|
|
|
|
|
current_pointer_position = possible_times.index(closest_lesson_time)
|
|
|
|
|
total_list_length = len(possible_times)
|
|
|
|
|
|
|
|
|
|
if len(full_command[1]) > 1 and full_command[1][1:].isdigit():
|
|
|
|
|
if full_command[1][0] == "+":
|
|
|
|
|
current_pointer_position = (current_pointer_position + int(full_command[1][1:])) % total_list_length
|
|
|
|
|
else:
|
|
|
|
|
current_pointer_position = (current_pointer_position - int(full_command[1][1:])) % total_list_length
|
|
|
|
|
|
|
|
|
|
closest_lesson_time = possible_times[current_pointer_position]
|
|
|
|
|
|
|
|
|
|
# getting corrent style
|
|
|
|
|
output_style_preference = "legacy-vibrant"
|
|
|
|
|
|
|
|
|
|
general_output_style_preference = get_preference_by_id(message.from_user.id, "output-style")
|
|
|
|
|
if general_output_style_preference != None:
|
|
|
|
|
output_style_preference = general_output_style_preference
|
|
|
|
|
|
|
|
|
|
specific_output_style_preference = get_preference_by_id(message.from_user.id, "output-style-lesson")
|
|
|
|
|
if specific_output_style_preference != None:
|
|
|
|
|
output_style_preference = specific_output_style_preference
|
|
|
|
|
|
|
|
|
|
# returning generated pair description
|
2023-09-05 20:40:57 +03:00
|
|
|
|
return get_lesson_description(schedule, reference_time, closest_lesson_time, current_day,
|
2023-09-11 21:36:24 +03:00
|
|
|
|
current_week, custom_name_prefix="Актуальна пара", template=output_style_preference), "HTML"
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
2023-05-06 18:14:53 +03:00
|
|
|
|
elif base_command == "!пари":
|
2023-05-08 10:22:23 +03:00
|
|
|
|
base_day = current_week * 7 + current_day
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
|
|
|
|
if len(full_command) >= 2:
|
|
|
|
|
args = [i for i in full_command[1:] if len(i) > 1]
|
|
|
|
|
preferences, selected_day = process_arguments(args, base_day)
|
|
|
|
|
else:
|
|
|
|
|
preferences = {}
|
|
|
|
|
selected_day = base_day
|
|
|
|
|
|
2023-05-08 10:22:23 +03:00
|
|
|
|
lesson_list = [i for i in schedule if selected_day * 86400 <= i < (selected_day + 1) * 86400]
|
2023-05-06 18:03:01 +03:00
|
|
|
|
|
2023-09-12 12:24:33 +03:00
|
|
|
|
output_style_preference = "legacy-vibrant"
|
|
|
|
|
|
|
|
|
|
general_output_style_preference = get_preference_by_id(message.from_user.id, "output-style")
|
|
|
|
|
if general_output_style_preference != None:
|
|
|
|
|
output_style_preference = general_output_style_preference
|
|
|
|
|
|
|
|
|
|
specific_output_style_preference = get_preference_by_id(message.from_user.id, "output-style-lookup")
|
|
|
|
|
if specific_output_style_preference != None:
|
|
|
|
|
output_style_preference = specific_output_style_preference
|
|
|
|
|
|
2023-09-05 20:40:57 +03:00
|
|
|
|
lesson_descriptions_list = [get_lesson_description(schedule, reference_time, lesson_time, current_day,
|
2023-09-12 11:44:03 +03:00
|
|
|
|
current_week, overrides=preferences, custom_name_prefix="Назва", template=output_style_preference, force_date_at_top=True)
|
2023-05-06 18:03:01 +03:00
|
|
|
|
for lesson_time in lesson_list]
|
|
|
|
|
|
2023-09-11 22:29:52 +03:00
|
|
|
|
return f"<b><u>Пари у {WEEKDAYS_ACCUSATIVE[selected_day % 7]}</u></b>:\n\n\n" + "\n".join(lesson_descriptions_list), "HTML"
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-12 09:52:38 +03:00
|
|
|
|
elif base_command == "!schedule-ctl" and len(full_command) >= 2:
|
2023-09-11 22:29:52 +03:00
|
|
|
|
if full_command[1] == "list":
|
|
|
|
|
prefs = get_all_preferences_by_id(message.from_user.id)
|
|
|
|
|
return "Ваші персональні налаштування:\n" + '\n'.join([f"- {k} = {v}" for k, v in prefs.items()]), "HTML"
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-12 09:52:38 +03:00
|
|
|
|
elif full_command[1] == "set" and len(full_command) == 4:
|
2023-09-11 22:29:52 +03:00
|
|
|
|
prefs = get_all_preferences_by_id(message.from_user.id)
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-11 22:29:52 +03:00
|
|
|
|
if full_command[2] in prefs:
|
2023-09-12 12:24:33 +03:00
|
|
|
|
if full_command[2] in ["output-style", "output-style-lesson", "output-style-lookup"]:
|
2023-09-11 22:29:52 +03:00
|
|
|
|
if full_command[3] not in os.listdir(module_path + "templates/"):
|
|
|
|
|
return f"Стилю {full_command[3]} не існує; доступні варіанти: " \
|
2023-09-11 21:36:24 +03:00
|
|
|
|
+ ', '.join(os.listdir(module_path + "templates/")), "HTML"
|
|
|
|
|
|
2023-09-11 22:29:52 +03:00
|
|
|
|
previous_value = prefs[full_command[2]]
|
|
|
|
|
prefs[full_command[2]] = full_command[3]
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-11 22:29:52 +03:00
|
|
|
|
set_preference_by_id(message.from_user.id, full_command[2], full_command[3])
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-11 22:29:52 +03:00
|
|
|
|
return f"Змінено значення {full_command[2]}: {previous_value} -> {full_command[3]}", "HTML"
|
2023-09-11 21:36:24 +03:00
|
|
|
|
else:
|
|
|
|
|
return f"Такого налаштування не існує; переглянути наявні налаштування можна за допомогою команди <u>!schedule-ctl list</u>", "HTML"
|
|
|
|
|
|
2023-09-12 09:52:38 +03:00
|
|
|
|
elif full_command[1] == "get" and len(full_command) == 3:
|
2023-09-11 22:29:52 +03:00
|
|
|
|
requested_preference = get_preference_by_id(message.from_user.id, full_command[2])
|
|
|
|
|
return f"Налаштування {full_command[2]} має значення {requested_preference}", "HTML"
|
2023-09-11 21:36:24 +03:00
|
|
|
|
|
2023-09-12 09:52:38 +03:00
|
|
|
|
elif full_command[1] == "clear" and len(full_command) == 3:
|
2023-09-12 10:04:00 +03:00
|
|
|
|
clear_preference_by_id(message.from_user.id, full_command[2])
|
2023-09-12 09:52:38 +03:00
|
|
|
|
return f"Очищено значення для налаштування {full_command[2]}, надалі для нього використовуватиметься стандартне значення", "HTML"
|
|
|
|
|
|
2023-09-11 21:36:24 +03:00
|
|
|
|
else:
|
2023-09-12 09:52:38 +03:00
|
|
|
|
return "Такої команди не існує (або був використаний помилковий синтаксис)", "HTML"
|