Skip to content

Commit

Permalink
sort json info
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasJoKuJonas committed Oct 21, 2024
1 parent 69316b4 commit 62e981a
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions custom_components/webuntis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def __init__(
self.calendar_homework_ids = []
self.calendar_homework_ids_setup = False
self.next_day_json = None
self.day_json = None
self.today = [None, None]

self.subjects = []
Expand Down Expand Up @@ -318,6 +319,7 @@ async def _async_status_request(self) -> None:
self.calendar_events = []
self.calendar_homework = []
self.next_day_json = None
self.day_json = None
self.today = [None, None]

# Inform user once about failed update if necessary.
Expand Down Expand Up @@ -405,6 +407,18 @@ async def _async_status_request(self) -> None:
error,
)

try:
self.day_json = await self._hass.async_add_executor_job(self._day_json)
except OSError as error:
self.day_json = None

_LOGGER.warning(
"Updating the property day_json of '%s@%s' failed - OSError: %s",
self.school,
self.username,
error,
)

try:
self.calendar_events = await self._hass.async_add_executor_job(
self._get_events
Expand Down Expand Up @@ -546,6 +560,7 @@ def webuntis_login(self):
self.calendar_homework = []
self.calendar_exams = []
self.next_day_json = None
self.day_json = None

# Inform user once about failed update if necessary.
if not self._last_status_request_failed:
Expand Down Expand Up @@ -575,11 +590,10 @@ def webuntis_logout(self):
# _LOGGER.debug("Logout successful")
self._loged_in = False

def get_timetable(self, start, end: datetime):
def get_timetable(self, start, end: datetime, sort=False):
"""Get the timetable for the given time period"""
if self.timetable_source == "personal":
return self.session.my_timetable(start=start, end=end)
else:
timetable_object = None
if self.timetable_source != "personal":
timetable_object = get_timetable_object(
self.timetable_source_id, self.timetable_source, self.session
)
Expand All @@ -590,11 +604,20 @@ def get_timetable(self, start, end: datetime):
if start_schoolyear.end.date() < end:
end = start_schoolyear.end.date()

if self.extended_timetable:
return self.session.timetable_extended(
result = []
if self.timetable_source == "personal":
result = self.session.my_timetable(start=start, end=end)
elif self.extended_timetable:
result = self.session.timetable_extended(
start=start, end=end, **timetable_object
)
return self.session.timetable(start=start, end=end, **timetable_object)
else:
result = self.session.timetable(start=start, end=end, **timetable_object)

if sort:
result = sorted(result, key=lambda x: x.start)

return result

def _is_class(self):
"""return if is class"""
Expand Down Expand Up @@ -689,7 +712,7 @@ def _next_day_json(self):
return "JSON data is disabled - activate it in the options"
day = self.next_lesson_to_wake_up.date()

table = self.get_timetable(start=day, end=day)
table = self.get_timetable(start=day, end=day, sort=True)

lessons = []
for lesson in table:
Expand All @@ -700,6 +723,25 @@ def _next_day_json(self):

return json_str

def _day_json(self):
if self.next_lesson_to_wake_up is None:
return None
if not self.generate_json:
return "JSON data is disabled - activate it in the options"
day = date.today()

table = self.get_timetable(start=day, end=day, sort=True)

lessons = []
for lesson in table:
_LOGGER.info(str(lesson.subjects))
if self.check_lesson(lesson):
lessons.append(str(self.get_lesson_json(lesson)))

json_str = "[" + ", ".join(lessons) + "]"

return json_str

def _get_events(self):
today = date.today()
week_start = today - timedelta(days=today.weekday())
Expand Down Expand Up @@ -964,6 +1006,9 @@ def get_lesson_json(self, lesson, force=False, output_str=True) -> str:
]
except:
pass

dic["name"] = get_lesson_name(self, lesson)

if output_str:
return str(json.dumps(dic))
return dic
Expand Down

0 comments on commit 62e981a

Please sign in to comment.