36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
from MoodleClasses.MoodleCourseSection import MoodleCourseSection
|
|
from bs4 import BeautifulSoup
|
|
from urllib.parse import unquote
|
|
import requests
|
|
import os
|
|
|
|
|
|
class MoodleCourse:
|
|
def __init__(self, name_with_code: str, url: str):
|
|
self.url = url
|
|
self.code = unquote(name_with_code.split()[0])
|
|
self.name = unquote(" ".join(name_with_code.split()[1:]).strip())
|
|
self.soup = None
|
|
self.sections = None
|
|
|
|
def __repr__(self):
|
|
return f"{self.code} {self.name}: {len(self.sections)} sections"
|
|
|
|
def init(self, s: requests.Session) -> None:
|
|
self.soup = self._get_soup(s)
|
|
self.sections = self._get_sections()
|
|
|
|
def _get_soup(self, s: requests.Session) -> BeautifulSoup:
|
|
r = s.get(self.url)
|
|
return BeautifulSoup(r.text, 'html.parser')
|
|
|
|
def _get_sections(self) -> list[MoodleCourseSection]:
|
|
sections_raw = self.soup.find_all("li", {"class": "course-section"})
|
|
return list(map(lambda x: MoodleCourseSection(x), sections_raw))
|
|
|
|
def set_parent_dir(self, parent_dir: [str, os.path], year: str = None) -> None:
|
|
if year is None:
|
|
year = self.url.split("/")[3]
|
|
for i, sec in enumerate(self.sections):
|
|
sec.set_parent_dir(os.path.join(parent_dir, f"{self.code} - {self.name}", year), i)
|