auto-schedule-pro: add schedule lookup function

This commit is contained in:
dymik739 2023-03-04 16:44:44 +02:00
parent c3a9e002b9
commit f52757a8f1
2 changed files with 110 additions and 15 deletions

View File

@ -86,12 +86,6 @@ if self.MESSAGE["text"].lower() == "!пара":
print("test3.1.2") print("test3.1.2")
'''
if int(dt_pair.strftime("%H")) == 11:
human_readable_date += "об "
else:
human_readable_date += "о "
'''
human_readable_date += "з " human_readable_date += "з "
print("test3.1.3") print("test3.1.3")
@ -112,8 +106,102 @@ if self.MESSAGE["text"].lower() == "!пара":
dt_pair = datetime.datetime.fromtimestamp(actual_pair_ts) dt_pair = datetime.datetime.fromtimestamp(actual_pair_ts)
dt_pair_finish = datetime.datetime.fromtimestamp(actual_pair_ts + 5400) dt_pair_finish = datetime.datetime.fromtimestamp(actual_pair_ts + 5400)
print(f"Debug: selected pair at {next_pair_time}") human_readable_date = ""
print(f"{current_day} == 6 && {dt_pair.strftime('%u')} == 1") if ((current_day + 2) == int(dt_pair.strftime("%u"))) or ((str(current_day) == "6") and (dt_pair.strftime("%u") == "1")):
human_readable_date += "завтра "
elif current_week != int(dt_pair.strftime("%W")) % 2:
human_readable_date += "{} ".format(self.WEEKDAY_NAMES_ROD_WITH_NEXT[int(dt_pair.strftime("%u")) - 1])
elif current_day != (int(dt_pair.strftime("%u")) - 1):
human_readable_date += "{} ".format(self.WEEKDAY_NAMES_ROD_WITH_THIS[int(dt_pair.strftime("%u")) - 1])
else:
human_readable_date += "сьогодні "
human_readable_date += "з "
human_readable_date += dt_pair.strftime("%H:%M")
human_readable_date += " до "
human_readable_date += dt_pair_finish.strftime("%H:%M")
self.RESPONCE = "Актуальна пара: {}\nДата: {}\nВикладач: {}\nПосилання на пару: {}".format(p['name'], human_readable_date, p['teacher'], p['link'])
if self.MESSAGE["text"].lower().split()[0] == "!пари":
command = self.MESSAGE["text"].lower().split()
current_time = datetime.datetime.now()
current_week = current_time.isocalendar()[1] % 2
current_day = current_time.weekday()
current_seconds = current_week*604800 + current_day*86400 + current_time.hour*3600 + current_time.minute*60 + current_time.second
reference_time = int(current_time.strftime("%s")) - current_seconds
# baking defined schedule
raw_schedule = json.loads( readfile(self.path + "schedule.json") )
schedule = {}
for day in range(len(raw_schedule)):
for i in raw_schedule[day]:
ts = day*86400 + int(i.split(":")[0])*3600 + int(i.split(":")[1])*60
new_item = dict(raw_schedule[day][i])
new_item["source"] = "schedule"
schedule[ts] = new_item
# baking additions (extra pairs)
raw_additions = json.loads( readfile(self.path + "additions.json") )
additions = {}
for day in range(len(raw_additions)):
for i in raw_additions[day]:
ts = day*86400 + int(i.split(":")[0])*3600 + int(i.split(":")[1])*60
new_item = dict(raw_additions[day][i])
new_item["source"] = "additions"
schedule[ts] = new_item
full_schedule = dict(list(schedule.items()) + list(additions.items()))
preferences = {"name": True, "date": True, "teacher": True, "link": False}
selected_day = current_week*7 + current_day
if len(command) >= 2 and len(command[1]) > 0:
if command[1][0] == "+":
try:
selected_day += int(command[1][1:])
except Exception as e:
print(f"[auto-schedule-pro:error] Got exception '{e}' while parsing {command[1]}")
elif command[1][0] == "-":
try:
selected_day -= int(command[1][1:])
except Exception as e:
print(f"[auto-schedule-pro:error] Got exception '{e}' while parsing {command[1]}")
else:
try:
selected_day = int(command[1])
except Exception as e:
print(f"[auto-schedule-pro:error] Got exception '{e}' while parsing {command[1]}")
# keeping day in bounds
while selected_day > 13:
selected_day -= 14
while selected_day < 0:
selected_day += 14
if len(command) > 2:
for i in command[2:]:
if len(i) >= 2:
if i[1:] in preferences:
if i[0] == "+":
preferences[i[1:]] = True
elif i[0] == "-":
preferences[i[1:]] = False
found_pairs = {}
for i in full_schedule:
if selected_day*86400 <= i < (selected_day+1)*86400:
found_pairs[i] = dict(full_schedule[i])
result_text = f"Пари у {self.WEEKDAY_NAMES_ZNAH[selected_day%7]}:\n\n"
for i in found_pairs:
actual_pair_ts = reference_time + i
dt_pair = datetime.datetime.fromtimestamp(actual_pair_ts)
dt_pair_finish = datetime.datetime.fromtimestamp(actual_pair_ts + 5400)
p = found_pairs[i]
human_readable_date = "" human_readable_date = ""
if ((current_day + 2) == int(dt_pair.strftime("%u"))) or ((str(current_day) == "6") and (dt_pair.strftime("%u") == "1")): if ((current_day + 2) == int(dt_pair.strftime("%u"))) or ((str(current_day) == "6") and (dt_pair.strftime("%u") == "1")):
@ -125,16 +213,22 @@ if self.MESSAGE["text"].lower() == "!пара":
else: else:
human_readable_date += "сьогодні " human_readable_date += "сьогодні "
'''
if int(dt_pair.strftime("%H")) == 11:
human_readable_date += "об "
else:
human_readable_date += "о "
'''
human_readable_date += "з " human_readable_date += "з "
human_readable_date += dt_pair.strftime("%H:%M") human_readable_date += dt_pair.strftime("%H:%M")
human_readable_date += " до " human_readable_date += " до "
human_readable_date += dt_pair_finish.strftime("%H:%M") human_readable_date += dt_pair_finish.strftime("%H:%M")
self.RESPONCE = "Актуальна пара: {}\nДата: {}\nВикладач: {}\nПосилання на пару: {}".format(p['name'], human_readable_date, p['teacher'], p['link'])
if preferences['name']:
result_text += f"Назва: {p['name']}\n"
if preferences['date']:
result_text += f"Дата: {human_readable_date}\n"
if preferences['teacher']:
result_text += f"Викладач: {p['teacher']}\n"
if preferences['link']:
result_text += f"Посилання на пару: {p['link']}\n"
result_text += "\n"
self.RESPONCE = result_text

View File

@ -1,3 +1,4 @@
self.WEEKDAY_NAMES_ZNAH = ["понедулок", "вівторок", "середу", "четвер", "п'ятницю", "суботу", "неділю"]
self.WEEKDAY_NAMES_ROD_WITH_NEXT = ["наступного понеділка", "наступного вівторка", "наступної середи", "наступного четверга", "наступної п'ятниці", "наступної суботи", "наступної неділі"] self.WEEKDAY_NAMES_ROD_WITH_NEXT = ["наступного понеділка", "наступного вівторка", "наступної середи", "наступного четверга", "наступної п'ятниці", "наступної суботи", "наступної неділі"]
self.WEEKDAY_NAMES_ROD_WITH_THIS = ["цього понеділка", "цього вівторка", "цієї середи", "цього четверга", "цієї п'ятниці", "цієї суботи", "цієї неділі"] self.WEEKDAY_NAMES_ROD_WITH_THIS = ["цього понеділка", "цього вівторка", "цієї середи", "цього четверга", "цієї п'ятниці", "цієї суботи", "цієї неділі"]
self.current_seconds = 0 self.current_seconds = 0