@ -6,6 +6,9 @@ def readfile(filename):
with open ( module_path + filename ) as f :
return f . read ( )
def writefile ( filename , data ) :
with open ( module_path + filename , ' w ' ) as f :
f . write ( data )
# global constants
# Accusative - znahidnyj
@ -28,6 +31,56 @@ lesson_types_to_strings = {
# global variables
module_path = " "
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
return preferences [ ' name ' ]
def get_all_preferences_by_id ( user_id ) :
user_preferences = { " output-style " : " legacy-vibrant " }
for i in default_preferences :
override = get_preferences_by_id ( i )
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/ " )
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 )
writefile ( final_data )
def load_template ( template , part ) :
return readfile ( f " templates/ { template } / { part } .msg " )
def escaped_string_markdownV2 ( input_string ) :
result_string = input_string
@ -76,7 +129,7 @@ def get_name_of_lesson_type(lesson_type):
if lesson_type in lesson_types_to_strings :
return lesson_types_to_strings [ lesson_type ]
'''
def generate_lesson_description ( lesson , start_datetime , end_datetime , current_day , current_week , overrides = { } ,
custom_name_prefix = " <b>Назва</b> " ) :
output_settings = { " name " : True , " date " : True , " teacher " : True , " link " : True , " comment " : True }
@ -102,7 +155,49 @@ def generate_lesson_description(lesson, start_datetime, end_datetime, current_da
result + = f " <b>Примітка</b>: { escaped_string_html ( lesson [ ' comment ' ] ) } \n "
return result
'''
def generate_lesson_description ( lesson , start_datetime , end_datetime , current_day , current_week , overrides = { } ,
custom_name_prefix = " Назва " , template = " legacy-vibrant " ) :
# temporarily not supported
#output_settings = {"name": True, "date": True, "teacher": True, "link": True, "comment": True}
#output_settings.update(overrides)
if lesson . __class__ == dict :
active_template = load_template ( template , " single " )
for i in [ ' name ' , ' teacher ' , ' link ' , ' comment ' ] :
active_template = active_template . replace ( f " % { i . upper ( ) } % " , lesson [ i ] )
human_readable_date = get_human_readable_date ( start_datetime , end_datetime ,
current_day , current_week )
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 )
return active_template
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 " )
for i in [ ' name ' , ' teacher ' , ' link ' , ' comment ' ] :
active_template = active_template . replace ( f " % { i . upper ( ) } % " , escaped_string ( lesson [ i ] ) )
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 )
total_result + = active_template + " \n "
return total_result
def get_schedule_data_from ( filename ) :
raw_schedule = json . loads ( readfile ( filename ) )
@ -151,12 +246,12 @@ def process_arguments(args, base_day):
def get_lesson_description ( schedule , reference_time , lesson_time , current_day , current_week , overrides = { } ,
custom_name_prefix = " <b>Назва</b>" , force_date_at_top = False ) :
custom_name_prefix = " Назва" , template = " legacy-vibrant " ) :
lesson_record = schedule [ lesson_time ]
lesson_start_datetime = datetime . fromtimestamp ( reference_time + lesson_time )
lesson_end_datetime = datetime . fromtimestamp ( reference_time + lesson_time + 5400 )
'''
if lesson_record . __class__ == dict :
if force_date_at_top :
user_defined_overrides = dict ( overrides )
@ -190,6 +285,10 @@ def get_lesson_description(schedule, reference_time, lesson_time, current_day, c
human_readable_date = get_human_readable_date ( lesson_start_datetime , lesson_end_datetime ,
current_day , current_week )
return f " <b><u> { human_readable_date . capitalize ( ) } </u></b>: \n " + " \n " . join ( descriptions )
'''
return generate_lesson_description ( lesson_record , lesson_start_datetime , lesson_end_datetime , current_day ,
current_week , overrides = overrides , custom_name_prefix = custom_name_prefix )
def process ( message , path ) :
@ -201,7 +300,7 @@ def process(message, path):
# one printable symbol, so this is already protected
base_command = full_command [ 0 ] . lower ( )
if base_command not in [ " !пара " , " !пари " ]:
if base_command not in [ " !пара " , " !пари " , " !schedule-ctl " ]:
return None , None
global module_path
@ -220,17 +319,21 @@ def process(message, path):
reference_time = int ( current_time . strftime ( " %s " ) ) - current_seconds
output_style_preference = get_preference_by_id ( message [ ' from ' ] [ ' id ' ] , " output-style " )
if base_command == " !пара " :
# easter egg
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 :
return " Навчання незабаром розпочнеться!" , None
return " Навчання от-от розпочнеться!" , None
elif 0 < = study_begin_ts - current_ts < 1209600 :
return f " До навчання залишилося { study_begin_ts - current_ts } секунд... " , None
elif study_begin_ts - current_ts > = 1209600 :
return " Ви маєте законне право відпочити, пари почнуться не скоро " , None
# actual lesson finding code
upcoming_lessons = [ timestamp for timestamp in schedule if timestamp > current_seconds - 5400 ]
if len ( upcoming_lessons ) > 0 :
@ -239,7 +342,7 @@ def process(message, path):
closest_lesson_time = min ( schedule )
return get_lesson_description ( schedule , reference_time , closest_lesson_time , current_day ,
current_week , custom_name_prefix = " <b> Актуальна пара</b> " ) , " HTML "
current_week , custom_name_prefix = " Актуальна пара" , template = output_style_preference ) , " HTML "
elif base_command == " !пари " :
base_day = current_week * 7 + current_day
@ -254,7 +357,37 @@ def process(message, path):
lesson_list = [ i for i in schedule if selected_day * 86400 < = i < ( selected_day + 1 ) * 86400 ]
lesson_descriptions_list = [ get_lesson_description ( schedule , reference_time , lesson_time , current_day ,
current_week , overrides = preferences , custom_name_prefix = " <b>Назва</b>" , force_date_at_top = Tru e)
current_week , overrides = preferences , custom_name_prefix = " Назва" , template = output_style_preferenc e)
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 "
elif base_command == " !schedule-ctl " :
if base_command [ 1 ] == " list " :
prefs = get_all_preferences_by_id ( message [ ' from ' ] [ ' id ' ] )
return " Ваші персональні налаштування: \n " + [ f " - { k } = { v } " for k , v in prefs . items ( ) ]
elif base_command [ 1 ] == " set " :
prefs = get_all_preferences_by_id ( message [ ' from ' ] [ ' id ' ] )
if base_command [ 2 ] in prefs :
if base_command [ 2 ] == " output-style " :
if base_command [ 3 ] not in os . listdir ( module_path + " templates/ " ) :
return f " Стилю { base_command [ 3 ] } не існує; доступні варіанти: " \
+ ' , ' . join ( os . listdir ( module_path + " templates/ " ) ) , " HTML "
previous_value = prefs [ base_command [ 2 ] ]
prefs [ base_command [ 2 ] ] = base_command [ 3 ]
set_preference_by_id ( message [ ' from ' ] [ ' id ' ] , base_command [ 2 ] , base_command [ 3 ] )
return f " Змінено значення { base_command [ 2 ] } : { previous_value } -> { base_command [ 3 ] } " , " HTML "
else :
return f " Такого налаштування не існує; переглянути наявні налаштування можна за допомогою команди <u>!schedule-ctl list</u> " , " HTML "
elif base_command [ 1 ] == " get " :
requested_preference = get_preference_by_id ( message [ ' from ' ] [ ' id ' ] , base_command [ 2 ] )
return f " Налаштування { base_command [ 2 ] } має значення { requested_preference } " , " HTML "
else :
return " Такої команди не існує " , " HTML "