Skip to content

Commit

Permalink
Minor refactor and handle no seating bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aditeyabaral committed Apr 30, 2024
1 parent 204f130 commit 217ae9e
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 64 deletions.
3 changes: 1 addition & 2 deletions pesuacademy/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
AddressDetails,
QualifyingExamination,
)

from .seating_info import SeatingInfo
from .seating_information import SeatingInformation
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class SeatingInfo:
class SeatingInformation:
def __init__(
self,
name: str,
course_code: str,
date: str,
time: str,
terminal: str,
block: str
block: str,
):
self.name = name
self.course_code = course_code
Expand All @@ -16,4 +16,4 @@ def __init__(
self.block = block

def __str__(self):
return f"{self.__dict__}"
return f"{self.__dict__}"
2 changes: 1 addition & 1 deletion pesuacademy/pages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .attendance import AttendancePageHandler
from .courses import CoursesPageHandler
from .profile import ProfilePageHandler
from .seating_info import SeatingInfoHandler
from .seating_information import SeatingInformationHandler
32 changes: 0 additions & 32 deletions pesuacademy/pages/seating_info.py

This file was deleted.

58 changes: 58 additions & 0 deletions pesuacademy/pages/seating_information.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import datetime

import requests_html
from bs4 import BeautifulSoup

from pesuacademy.models import SeatingInformation


class SeatingInformationHandler:
@staticmethod
def get_seating_information_from_page(
soup: BeautifulSoup,
) -> list[SeatingInformation]:
info_table = soup.find("table", attrs={"id": "seatinginfo"})
tablebody = info_table.find("tbody")
tablerows = tablebody.find_all("tr")
seating_info = list()
for row in tablerows:
columns = row.find_all("td")
assn_name = columns[0].text.strip()
course_code = columns[1].text.strip()
date = columns[2].text.strip()
time = columns[3].text.strip()
terminal = columns[4].text.strip()
block = columns[5].text.strip()
seating_info.append(
SeatingInformation(assn_name, course_code, date, time, terminal, block)
)
return seating_info

@staticmethod
def get_page(session: requests_html.HTMLSession) -> list[SeatingInformation]:
try:
profile_url = (
"https://www.pesuacademy.com/Academy/s/studentProfilePESUAdmin"
)
query = {
"menuId": "655",
"url": "studentProfilePESUAdmin",
"controllerMode": "6404",
"actionType": "5",
"id": "0",
"selectedData": "0",
"_": str(int(datetime.datetime.now().timestamp() * 1000)),
}
response = session.get(profile_url, allow_redirects=False, params=query)
if response.status_code != 200:
raise ConnectionError("Unable to fetch seating info.")
soup = BeautifulSoup(response.text, "lxml")
if (
(no_seating_tag := soup.find("h5")) is not None
and no_seating_tag.text == "No Test Seating Info is available"
):
return []
else:
return SeatingInformationHandler.get_seating_information_from_page(soup)
except Exception:
raise ConnectionError("Unable to fetch seating info.")
6 changes: 3 additions & 3 deletions pesuacademy/pesuacademy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bs4 import BeautifulSoup

from pesuacademy import util
from pesuacademy.models.seating_info import SeatingInfo
from pesuacademy.models.seating_information import SeatingInformation
from pesuacademy.util.page import PageHandler
from .exceptions import CSRFTokenError, AuthenticationError
from .models import Profile, ClassAndSectionInfo, Course
Expand Down Expand Up @@ -157,7 +157,7 @@ def attendance(self, semester: Optional[int] = None) -> dict[int, list[Course]]:
attendance_info = self.page_handler.get_attendance(semester)
return attendance_info

def seating_info(self) -> list[SeatingInfo]:
def seating_information(self) -> list[SeatingInformation]:
"""
Get the seating information of the currently authenticated user.
Expand All @@ -166,4 +166,4 @@ def seating_info(self) -> list[SeatingInfo]:
if not self._authenticated:
raise AuthenticationError("You need to authenticate first.")
seating_info = self.page_handler.get_seating_info()
return seating_info
return seating_info
1 change: 0 additions & 1 deletion pesuacademy/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from pesuacademy.util import profile
from pesuacademy.util import seating_info
2 changes: 1 addition & 1 deletion pesuacademy/util/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ def get_attendance(self, semester: Optional[int] = None):
return self.attendance_page_handler.get_page(self.__session, semester_ids)

def get_seating_info(self):
return pages.SeatingInfoHandler.get_page(self.__session)
return pages.SeatingInformationHandler.get_page(self.__session)
21 changes: 0 additions & 21 deletions pesuacademy/util/seating_info.py

This file was deleted.

0 comments on commit 217ae9e

Please sign in to comment.