Skip to content

Commit

Permalink
Add arguments to filter announcements
Browse files Browse the repository at this point in the history
  • Loading branch information
aditeyabaral committed Apr 22, 2024
1 parent be409b5 commit f6b708a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
17 changes: 14 additions & 3 deletions pesuacademy/pages/announcements.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import re
from typing import Optional

import urllib3

Expand Down Expand Up @@ -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 = {
Expand All @@ -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
12 changes: 10 additions & 2 deletions pesuacademy/pesuacademy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 13 additions & 2 deletions pesuacademy/util/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit f6b708a

Please sign in to comment.