143 lines
No EOL
6 KiB
Python
143 lines
No EOL
6 KiB
Python
import json
|
|
import datetime
|
|
import config as _config
|
|
|
|
dayStrings = ['Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота', 'Воскресенье']
|
|
|
|
class Lesson:
|
|
#EDIT: фулл переделать потому-что формат поменялся сильно
|
|
def __init__(self, start_end: str, data: dict) -> None:
|
|
self.start: list[int] = [
|
|
int(start_end.split(' - ')[0].split(':')[0]),
|
|
int(start_end.split(' - ')[0].split(':')[1])
|
|
]
|
|
|
|
self.end: list[int] = [
|
|
int(start_end.split(' - ')[1].split(':')[0]),
|
|
int(start_end.split(' - ')[1].split(':')[1])
|
|
]
|
|
self.strTime: str = start_end
|
|
|
|
self.raw: str = data['raw']
|
|
self.ok: bool = data['ok']
|
|
if not self.ok: return
|
|
|
|
self.name: str = data['name']
|
|
self.type: str = data['type']
|
|
self.info: list[str] = data['info']
|
|
|
|
def text(self, isToday: bool = False) -> str:
|
|
date = datetime.datetime.now()
|
|
lessonStart = datetime.datetime(date.year, date.month, date.day, self.start[0], self.start[1], 10)
|
|
|
|
|
|
|
|
if self.ok:
|
|
dateStr: str = f'{self.type.capitalize()} <i>в {self.start[0]}:{self.start[1] if self.start[1] > 9 else "0"+str(self.start[1])}</i>'
|
|
if isToday:
|
|
if lessonStart > date and (lessonStart-date).seconds < 30*60:
|
|
dateStr = f'{self.type.capitalize()} <i>начнется через {max((lessonStart-date).seconds//60, 1)} мин.</i>'
|
|
elif lessonStart < date and (date-lessonStart).seconds < 31*60:
|
|
dateStr = f'<i>Сейчас идет</i> {self.type}'
|
|
elif lessonStart < date and (date-lessonStart).seconds >= 31*60 and (date-lessonStart).seconds < 89*60:
|
|
dateStr = f'{self.type.capitalize()} <i>закончится через {max(90-(date-lessonStart).seconds//60, 1)} мин.</i>'
|
|
wrb = ''
|
|
if self.info:
|
|
nd: list[str] = []
|
|
for data in self.info:
|
|
if data in self.type: continue
|
|
if 'подгр' in data:
|
|
nd.append(data.split('подгр')[0])
|
|
nd.append('\n<b>подгр'+data.split('подгр')[1]+f'</b>\n')
|
|
|
|
else: nd.append(data)
|
|
|
|
|
|
for data in nd:
|
|
# print()
|
|
# print(data, wrb)
|
|
if ('\n' in data or '\n' in wrb[-2:]):
|
|
wrb += data
|
|
elif 'ауд' in data.lower():
|
|
audn = '.'.join(data.replace(' ', '').split('.')[1:])
|
|
wrb += ', '+f'<a href="https://rasp.pgups.ru/schedule/room?room={audn}">{data}</a>'
|
|
else:
|
|
wrb += ', '+data
|
|
|
|
|
|
return f'''\
|
|
<blockquote>\
|
|
<b>{self.name}</b>
|
|
{dateStr}
|
|
{wrb[2:] if wrb else 'Дополнительная информация отсуствует'}</blockquote>'''
|
|
|
|
else:
|
|
return f'''\
|
|
<pre language="Error">\
|
|
Пара в {self.start[0]}:{self.start[1] if self.start[1] > 9 else '0'+str(self.start[1])}
|
|
{self.raw}</pre>'''
|
|
|
|
|
|
class Schedule:
|
|
def __init__(self, responce: dict) -> None:
|
|
self.weekNumber: int = responce['date']['weekNumber']
|
|
self.weekDay: int = responce['date']['weekDay']
|
|
|
|
self.raw: dict = responce['schedule']
|
|
self.days: list[list[list[Lesson]]] = []
|
|
|
|
for dayInfo in self.raw.values():
|
|
currentDay = []
|
|
for start_end, lessons in dayInfo.items():
|
|
if lessons == [None] or lessons == [None, None]:
|
|
continue
|
|
|
|
lessonList: list[Lesson | None] = []
|
|
for lesson in lessons:
|
|
if lesson: lessonList.append(Lesson(start_end, lesson))
|
|
else: lessonList.append(None)
|
|
|
|
currentDay.append(lessonList)
|
|
|
|
self.days.append(currentDay)
|
|
|
|
def today(self) -> list[list[Lesson]]:
|
|
return self.days[self.weekDay] if self.weekDay < 5 else []
|
|
|
|
def getFacCourseByGroup(group: str) -> tuple[str | None, str | None]:
|
|
for faculty, courses in _config.LOAD.items():
|
|
for course in courses:
|
|
path = f'./timetables/{faculty} {course}.json'.replace(' ', '_')
|
|
data: dict[str, dict] = json.load(
|
|
open(
|
|
path,
|
|
'r',
|
|
encoding = 'utf-8'
|
|
)
|
|
)
|
|
if group.lower() in [_.lower() for _ in data.keys()]:
|
|
return faculty, course
|
|
return None, None
|
|
|
|
def getSchedulePathByGroup(group: str) -> str | None:
|
|
faculty, course = getFacCourseByGroup(group)
|
|
return None if not faculty else f'./timetables/{faculty} {course}.json'.replace(' ', '_')
|
|
|
|
def generateDaySchedule(schedule: Schedule, showOddWeekText: bool = True, isToday: bool = False) -> str:
|
|
isWeekOdd = schedule.weekNumber%2 == 0
|
|
weekDay = schedule.weekDay
|
|
schedule_today = schedule.today()
|
|
lastLessonEnds = [0,0]
|
|
scheduleResult = f'<b>{dayStrings[weekDay]}, {"четная" if isWeekOdd else "нечетная"} неделя</b>\n' if showOddWeekText else f'<b>{dayStrings[weekDay]}</b>'
|
|
for lessonList in schedule_today:
|
|
if len(lessonList) == 1:
|
|
scheduleResult += lessonList[0].text(isToday) + '\n'
|
|
lastLessonEnds = lessonList[0].end
|
|
else:
|
|
lesson = lessonList[1 if isWeekOdd else 0]
|
|
if lesson:
|
|
scheduleResult += lesson.text(isToday) + '\n'
|
|
lastLessonEnds = lesson.end
|
|
|
|
scheduleResult += f'Пары закончатся в <i>{lastLessonEnds[0]}:{lastLessonEnds[1] if lastLessonEnds[1] > 9 else "0"+str(lastLessonEnds[1])}</i>'
|
|
return scheduleResult |