97 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import sys
if len(sys.argv) != 2:
sys.exit(1)
class Lesson:
def __init__(self):
pass
def read_from_schedule_pair(self, p):
self.time = p["time"].rsplit(':', 1)[0]
self.name = p["name"]
self.teacher = p["teacherName"]
self.type = p["tag"]
self.selectable = False
self.link = ""
self.nolink = True
def get_packable_object(self):
return {
"name": self.name,
"teacher": self.teacher,
"link": self.link,
"type": self.type,
"selectable": self.selectable,
"nolink": self.nolink
}
def day_to_lesson_pool(pairs):
pool = []
for p in pairs:
l = Lesson()
l.read_from_schedule_pair(p)
pool.append(l)
return pool
def lesson_pool_to_objs(pool):
times = []
for i in (list(map(int, (i.time.split(':')))) for i in pool):
if i not in times:
times.append(i)
times.sort(key = lambda x: x[0] * 60 + x[1])
result = {}
for i in times:
lesson_timestamp = f"{i[0]:02}:{i[1]:02}"
required_timestamp = f"{i[0]}:{i[1]}"
objs_dup = (j.get_packable_object() for j in pool if j.time == lesson_timestamp)
objs = []
for i in objs_dup:
if i not in objs:
objs.append(i)
if len(objs) == 0:
continue
elif len(objs) == 1:
result[required_timestamp] = list(objs)[0]
else:
result[required_timestamp] = list(objs)
return result
def process_week(week):
result = []
for weekday in ["Пн", "Вв", "Ср", "Чт", "Пт"]:
day_pool = day_to_lesson_pool(next(filter(lambda l: l['day'] == weekday, week))['pairs'])
result.append(lesson_pool_to_objs(day_pool))
result.append({})
result.append({})
return result
ext_schedule = json.load(open(sys.argv[1], 'r'))
int_schedule = []
int_schedule += process_week(ext_schedule["scheduleFirstWeek"])
int_schedule += process_week(ext_schedule["scheduleSecondWeek"])
print(json.dumps(int_schedule, ensure_ascii = False, indent = 4))