Some work. Too confused to work today.😵

This commit is contained in:
Boyan 2024-04-05 19:27:24 +02:00
parent a9b0764cba
commit 078034faf2
5 changed files with 78 additions and 57 deletions

30
Folder.py Normal file
View File

@ -0,0 +1,30 @@
# Module to handle each assignment (most difficult part)
from Base import Base
from Exercise import Exercise
from requests import Session
class Assignment(Base):
def __init__(self, url:str, name:str, session:Session, parent):
super().__init__(url, name, session, parent)
self.download = Downloadable(name, session, self)
def __str__(self):
return f"Assignment {self.name} in course {self.parent.name}"
def getExercises(self) -> list[Exercise]:
# Find li large
ul = self.soup.find('ul', class_='round')
# Turn each li to an exercise instance
return self.liLargeToExercises(ul, self.session, self)
def getExercise(self, name:str) -> Exercise:
# Get the exercise
r = self.session.get(self.url)
soup = BeautifulSoup(r.text, 'lxml')
# Search by name
exercise = soup.find('a', text=name)
# Get the url and transform it into an exercise object
return Exercise(url=exercise['href'], name=name, session=self.session, assignment=self)

View File

@ -70,24 +70,3 @@ class Base:
submissions.append(self.__parseCfgBlock(div)) submissions.append(self.__parseCfgBlock(div))
return self.__parseCfgBlock(divs[0]), submissions return self.__parseCfgBlock(divs[0]), submissions
def liLargeToAssignments(self, ul:BeautifulSoup) -> list:
# Assume that ul is the block surrounding the li elements
# Get all the li elements
lis = ul.find_all('li', class_='large')
# Turn each to an assignment instance
assignments = []
for li in lis:
assignments.append(Base(li.a['href'], li.a.text, self.session, self.parent))
return assignments
def liLargeToExercises(self, ul:BeautifulSoup) -> list:
# Assume that ul is the block surrounding the li elements
# Get all the li elements
lis = ul.find_all('li', class_='large')
# Turn each to an exercise instance
exercises = []
for li in lis:
exercises.append(Base(li.a['href'], li.a.text, self.session, self.parent))
return exercises

View File

@ -1,7 +1,7 @@
# Class to handle courses # Class to handle courses
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from requests import Session from requests import Session
from Assignment import Assignment from ExerciseGroup import ExerciseGroup
import re import re
from Base import Base from Base import Base
from exceptions.CourseUnavailable import CourseUnavailable from exceptions.CourseUnavailable import CourseUnavailable
@ -24,12 +24,12 @@ class Course(Base):
def __courseAvailable(self, r): def __courseAvailable(self, r):
# Check if we got an error # Check if we got an error
print(self.url) # print(self.url)
if "Something went wrong" in r.text: if "Something went wrong" in r.text:
raise CourseUnavailable() raise CourseUnavailable()
@property @property
def courseInfo(self): def info(self):
return { return {
"name": self.name, "name": self.name,
"year": self.parent.year, "year": self.parent.year,
@ -37,36 +37,9 @@ class Course(Base):
"assignments": [x.name for x in self.assignments] "assignments": [x.name for x in self.assignments]
} }
def getAssignment(self, name:str) -> Assignment: def getExerciseGroups(self):
# Optimization: if we already have the assignments, don't get them again
try:
if name in [x.name for x in self.assignments]:
return name
except AttributeError:
pass
# Get the assignment
r = self.session.get(self.url) r = self.session.get(self.url)
soup = BeautifulSoup(r.text, 'lxml') soup = BeautifulSoup(r.text, 'lxml')
# Search by name
assignment = soup.find('a', text=name)
# Get the url and transform it into an assignment object
return Assignment(url=assignment['href'], name=name, session=self.session, parent=self)
def getAssignments(self) -> list[Assignment]:
# For each link in the course page, get the assignment
r = self.session.get(self.url)
soup = BeautifulSoup(r.text, 'lxml')
# Find the big ul
# print(soup)
section = soup.find('div', class_="ass-children") section = soup.find('div', class_="ass-children")
ul = section.find('ul', class_='round') entries = section.find_all('a', href=True)
return [ExerciseGroup(f"https://themis.housing.rug.nl{x['href']}", x.text, self.session, self) for x in entries]
# IDEA: They sometimes put other stuff in these li's, so we have to filter them out
# print(ul)
# print(type(ul))
# Transform them into Assignment objects
# I want to call the __liLargeToAssignments method from the Base class
return self.liLargeToAssignments(ul)

View File

@ -13,9 +13,9 @@ class Exercise(Base):
def __str__(self): def __str__(self):
return f"Exercise {self.name} in assignment {self.parent.name}" return f"Exercise {self.name} in assignment {self.parent.name}"
# IDEA: Implement test case downloading def getTests(self) -> list[str]:
pass
# IDEA : Make this async, so we don't have to wait for the whole output to load
def submit(self, file:str, comment:str) -> str: def submit(self, file:str, comment:str) -> str:
# Submit a file # Submit a file
# The form is in the page with class "cfg-container round" # The form is in the page with class "cfg-container round"

39
src/ExerciseGroup.py Normal file
View File

@ -0,0 +1,39 @@
from Base import Base
from bs4 import BeautifulSoup\
class ExerciseGroup(Base):
# I can't tell if I'm already an exercise :C
def __init__(self, url:str, name:str, session, parent):
super().__init__(url, name, session, parent)
self.exercises = self.getExercises()
self.folders = self.getFolders()
def __str__(self):
return f"ExerciseGroup {self.name} in course {self.parent.name}"
def getExercises(self) -> list:
r = self.session.get(self.url)
soup = BeautifulSoup(r.text, 'lxml')
section = soup.find('div', class_="ass-children")
try:
submittables = section.find_all('a', class_="ass-submitable")
except AttributeError:
return None
return submittables
# Returns a list of names of the folders
def getFolders(self) -> list:
r = self.session.get(self.url)
soup = BeautifulSoup(r.text, 'lxml')
section = soup.find('div', class_="ass-children")
try:
folders = section.find_all('a', class_="ass-group")
except AttributeError:
return None
return [x.text for x in folders]
def recurse(self, folder:str):
print(self.url)