diff --git a/pesuacademy/pages/announcements.py b/pesuacademy/pages/announcements.py index c9cf5aa..e9c4588 100644 --- a/pesuacademy/pages/announcements.py +++ b/pesuacademy/pages/announcements.py @@ -1,5 +1,6 @@ import datetime import re +from typing import Optional import urllib3 @@ -85,7 +86,11 @@ def get_announcement_by_id( ) def get_page( - self, session: requests_html.HTMLSession, csrf_token: str + self, + session: requests_html.HTMLSession, + csrf_token: str, + start_date: Optional[datetime.date] = None, + end_date: Optional[datetime.date] = None, ) -> list[Announcement]: url = "https://www.pesuacademy.com/Academy/s/studentProfilePESUAdmin" query = { @@ -107,7 +112,13 @@ def get_page( announcements = list() for announcement_id in announcement_ids: - announcements.append( - self.get_announcement_by_id(session, csrf_token, announcement_id) + announcement = self.get_announcement_by_id( + session, csrf_token, announcement_id ) + if start_date and announcement.date < start_date: + continue + if end_date and announcement.date > end_date: + continue + announcements.append(announcement) + announcements.sort(key=lambda x: x.date, reverse=True) return announcements diff --git a/pesuacademy/pesuacademy.py b/pesuacademy/pesuacademy.py index e066dfb..1675a50 100644 --- a/pesuacademy/pesuacademy.py +++ b/pesuacademy/pesuacademy.py @@ -156,11 +156,19 @@ def attendance(self, semester: Optional[int] = None) -> dict[int, list[Course]]: attendance_info = self.page_handler.get_attendance(semester) return attendance_info - def announcements(self) -> list[Announcement]: + def announcements( + self, start_date: Optional[str] = None, end_date: Optional[str] = None + ) -> list[Announcement]: """ Get the announcements from the PESU Academy website. + :param start_date: The start date of the announcements to fetch in "yyyy-mm-dd" format. If not provided, all + announcements from the beginning are fetched. + :param end_date: The end date of the announcements to fetch in "yyyy-mm-dd" format. If not provided, all + announcements till the end are fetched. :return: The list of announcements. """ - announcements = self.page_handler.get_announcements(self._csrf_token) + announcements = self.page_handler.get_announcements( + self._csrf_token, start_date, end_date + ) return announcements diff --git a/pesuacademy/util/page.py b/pesuacademy/util/page.py index 8e77678..626cec5 100644 --- a/pesuacademy/util/page.py +++ b/pesuacademy/util/page.py @@ -82,5 +82,16 @@ def get_attendance(self, semester: Optional[int] = None): semester_ids = self.get_semester_ids_from_semester_number(semester) return self.attendance_page_handler.get_page(self.__session, semester_ids) - def get_announcements(self, csrf_token: str): - return self.announcement_handler.get_page(self.__session, csrf_token) + def get_announcements( + self, + csrf_token: str, + start_date: Optional[str] = None, + end_date: Optional[str] = None, + ): + if start_date is not None: + start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d").date() + if end_date is not None: + end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d").date() + return self.announcement_handler.get_page( + self.__session, csrf_token, start_date, end_date + )