Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aditeyabaral committed Apr 16, 2024
1 parent ddd819c commit f98872d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
16 changes: 7 additions & 9 deletions pesu_academy/pages/attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from bs4 import BeautifulSoup

from pesu_academy.models import Course, Attendance
from pesu_academy.pages.utils import get_semester_list


def get_attendance_in_semester(session: requests_html.HTMLSession, semester_value: Optional[int] = None):
Expand Down Expand Up @@ -46,14 +45,13 @@ def get_attendance_in_semester(session: requests_html.HTMLSession, semester_valu
return attendance


def get_attendance_page(session: requests_html.HTMLSession, csrf_token: str, semester: Optional[int] = None) -> dict[
int, list[Course]]:
semesters = get_semester_list(session, csrf_token, semester)
def get_attendance_page(
session: requests_html.HTMLSession,
semester_ids: dict
) -> dict[int, list[Course]]:
attendance = dict()
for current_semester in semesters:
if semester is None or current_semester == semester:
attendance_in_semester = get_attendance_in_semester(session, semesters[current_semester])
attendance[current_semester] = attendance_in_semester

for semester_number in semester_ids:
attendance_in_semester = get_attendance_in_semester(session, semester_ids[semester_number])
attendance[semester_number] = attendance_in_semester
attendance = dict(sorted(attendance.items()))
return attendance
16 changes: 7 additions & 9 deletions pesu_academy/pages/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from bs4 import BeautifulSoup

from pesu_academy.models import Course
from pesu_academy.pages.utils import get_semester_list


def get_courses_in_semester(session: requests_html.HTMLSession, semester_id: Optional[int] = None):
Expand Down Expand Up @@ -41,14 +40,13 @@ def get_courses_in_semester(session: requests_html.HTMLSession, semester_id: Opt
return courses


def get_courses_page(session: requests_html.HTMLSession, csrf_token: str, semester: Optional[int] = None) -> dict[
int, list[Course]]:
semesters = get_semester_list(session, csrf_token, semester)
def get_courses_page(
session: requests_html.HTMLSession,
semester_ids: dict
) -> dict[int, list[Course]]:
courses = dict()
for current_semester in semesters:
if semester is None or current_semester == semester:
courses_in_semester = get_courses_in_semester(session, semesters[current_semester])
courses[current_semester] = courses_in_semester

for semester_number in semester_ids:
courses_in_semester = get_courses_in_semester(session, semester_ids[semester_number])
courses[semester_number] = courses_in_semester
courses = dict(sorted(courses.items()))
return courses
31 changes: 24 additions & 7 deletions pesu_academy/pesu_academy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from bs4 import BeautifulSoup

from .exceptions import CSRFTokenError, AuthenticationError
from .models import Profile
from .pages import profile, courses, attendance
from .models import Profile, Course
from .pages import profile, courses, attendance, utils


class PESUAcademy:
Expand All @@ -21,6 +21,7 @@ def __init__(self, username: Optional[str] = None, password: Optional[str] = Non
"""
self.__session = requests_html.HTMLSession()
self._authenticated: bool = False
self._semester_ids = dict()
self._csrf_token: str = self.generate_csrf_token(username, password)

@property
Expand All @@ -31,6 +32,19 @@ def csrf_token(self):
def session(self):
return self.__session

@property
def authenticated(self):
return self._authenticated

def get_semester_ids_from_semester_number(self, semester: Optional[int] = None) -> dict:
"""
Get the semester ids from the semester number. If semester is not provided, all semester ids are returned.
:param semester: The semester number.
:return:
"""
assert semester is None or 1 <= semester <= 8, "Semester number should be between 1 and 8."
return self._semester_ids if semester is None else {semester: self._semester_ids[semester]}

def generate_csrf_token(self, username: Optional[str] = None, password: Optional[str] = None) -> str:
"""
Generate a CSRF token. If username and password are provided, authenticate and get the CSRF token.
Expand Down Expand Up @@ -69,6 +83,7 @@ def generate_csrf_token(self, username: Optional[str] = None, password: Optional
# if login is successful, update the CSRF token
csrf_token = soup.find("meta", attrs={"name": "csrf-token"})["content"]
self._authenticated = True
self._semester_ids = utils.get_semester_list(self.__session, csrf_token)

return csrf_token

Expand Down Expand Up @@ -113,7 +128,7 @@ def know_your_class_and_section(self, username: str) -> Profile:

return profile

def profile(self):
def profile(self) -> Profile:
"""
Get the private profile information of the currently authenticated user.
:return: The profile information.
Expand All @@ -123,24 +138,26 @@ def profile(self):
profile_info = profile.get_profile_page(self.__session)
return profile_info

def courses(self, semester: Optional[int] = None):
def courses(self, semester: Optional[int] = None) -> dict[int, list[Course]]:
"""
Get the courses of the currently authenticated user.
:param semester: The semester number. If not provided, all courses across all semesters are returned.
:return: The course information for the given semester.
"""
if not self._authenticated:
raise AuthenticationError("You need to authenticate first.")
courses_info = courses.get_courses_page(self.__session, self._csrf_token, semester)
semester_ids = self.get_semester_ids_from_semester_number(semester)
courses_info = courses.get_courses_page(self.__session, semester_ids)
return courses_info

def attendance(self, semester: Optional[int] = None):
def attendance(self, semester: Optional[int] = None) -> dict[int, list[Course]]:
"""
Get the attendance in courses of the currently authenticated user.
:param semester: The semester number. If not provided, attendance across all semesters are returned.
:return: The attendance information for the given semester.
"""
if not self._authenticated:
raise AuthenticationError("You need to authenticate first.")
attendance_info = attendance.get_attendance_page(self.__session, self._csrf_token, semester)
semester_ids = self.get_semester_ids_from_semester_number(semester)
attendance_info = attendance.get_attendance_page(self.__session, semester_ids)
return attendance_info

0 comments on commit f98872d

Please sign in to comment.