Formatted, debugging. Added exception.

This commit is contained in:
2024-02-13 19:24:25 +01:00
parent 5e975cead1
commit 99e356d96a
12 changed files with 294 additions and 157 deletions

View File

@ -1,29 +1,52 @@
# Year class to represent an academic year
from bs4 import BeautifulSoup
import selenium
from login import login
from Course import Course
from Themis import Themis
from Base import Base
from requests import Session
from exceptions.CourseUnavailable import CourseUnavailable
class Year(Base):
def __init__(name:str, session:Session, parent:Themis, end_year:int):
super().__init__()
self.start = end_year - 1
class Year:
def __init__(self, session:Session, parent, start_year:int, end_year:int):
self.start = start_year
self.year = end_year
self.url = __constructUrl()
self.session = session
self.url = self.__constructUrl()
# Method to set the url
def __constructUrl(self):
return f"https://themis.housing.rug.nl/{self.start}-{self.year}"
return f"https://themis.housing.rug.nl/course/{self.start}-{self.year}"
# Method to get the courses of the year
def getCourses(self) -> list[Course]:
def getCourses(self, errors:bool=False) -> list[Course]:
# lis in a big ul
r = self.session.get(self.url)
soup = BeautifulSoup(r.text, 'lxml')
lis = soup.find_all('li', class_='large')
courses = []
# TODO: Logic to get all courses
for li in lis:
try:
courses.append(
Course(
self.url + li.a['href'],
li.a.text,
self.session,
self
)
)
except CourseUnavailable:
if errors:
raise CourseUnavailable(f"Course {li.a.text} in year {self.start}-{self.year} is not available")
else:
pass
return courses
def getCourse(self, name:str) -> Course:
#TODO: Implement
pass
# Get the course
r = self.session.get(self.url)
soup = BeautifulSoup(r.text, 'lxml')
# Search by name
course = soup.find('a', text=name)
# Get the url and transform it into a course object
return Course(url=course['href'], name=name, session=self.session, year=self)