Added methods to create an exercise group from a link

This commit is contained in:
Boyan 2024-11-18 15:45:19 +01:00
parent de66e9762e
commit 0ff3f28978
2 changed files with 43 additions and 1 deletions

View File

@ -15,12 +15,35 @@ class ExerciseGroup(Group):
super().__init__(url, name, session, parent=parent, full=full, classes=classes)
self.am_exercise = "ass-submitable" in self.classes
def create_group(self, url: str, name: str, session, parent, full: bool, classes=None):
def create_group(self, url: str, name: str, session, parent, full: bool, classes=None) -> ExerciseGroup:
"""
Create an instance of ExerciseGroup for subgroups.
"""
return ExerciseGroup(url, name, session, parent, full, classes)
def create_group_from_url(self, url: str, full: bool) -> ExerciseGroup:
"""
Create an instance of ExerciseGroup from a full URL of a Themis group.
This method will retrieve the name of the group from the URL.
Args:
url (str): URL of the Themis group.
full (bool): Whether to traverse the whole group.
Returns:
ExerciseGroup: An instance of ExerciseGroup.
"""
if "https://themis.housing.rug.nl/course/" not in url:
url = "https://themis.housing.rug.nl/course/" + url
# Find name of group (last of a with class fill accent large)
r = self._session.get(url)
soup = BeautifulSoup(r.text, "lxml")
group_links = soup.find_all("a", class_="fill accent large")
name = group_links[-1].text
return self.create_group(url, name, self._session, self, full)
@property
def test_cases(self) -> list[str]:
"""

View File

@ -50,3 +50,22 @@ class Year:
suffix = course_link["href"].replace(f"course/{self.start}-{self.year}", "")
course_url = self.url + suffix
return Course(course_url, name, self._session, self)
def get_course_by_url(self, url: str) -> Course:
"""
Gets a course by url.
"""
r = self._session.get(url)
soup = BeautifulSoup(r.text, "lxml")
# <a class="fill accent large" href="https://themis.housing.rug.nl/course/2023-2024/adinc-cs">Algorithms and Data Structures for CS</a>
course_link = soup.find_all("a", class_="fill accent large")
name = None
for link in course_link:
if url in link["href"]:
name = link.text
break
if not name:
raise CourseUnavailable(f"No such course found: {url}")
return Course(url, name, self._session, self)