From 5b966c87f83e3ad39f4707b1ead36ec5a13ee3ed Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Wed, 14 Aug 2024 22:41:40 +0200 Subject: [PATCH] =?UTF-8?q?fix(api):=20extract=20course=20("forl=C3=B8b")?= =?UTF-8?q?=20from=20lessons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/lectio/_modul.py | 78 +++++++++++++++++++++------------------ apps/api/lectio/utils.py | 3 ++ 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/apps/api/lectio/_modul.py b/apps/api/lectio/_modul.py index 6c9fa556..27c61d7f 100644 --- a/apps/api/lectio/_modul.py +++ b/apps/api/lectio/_modul.py @@ -20,6 +20,7 @@ def modul(self, absid): "præsentation": "", "grupper": {}, "øvrigtIndhold": "", + "forløb": "", } with contextlib.suppress(Exception): @@ -34,46 +35,51 @@ def modul(self, absid): modulContent = soup.find( "div", {"id": "s_m_Content_Content_tocAndToolbar_inlineHomeworkDiv"} ) - last = "" - for div in modulContent.find_all("div"): - if div.get("id") is None: - if ( - divText := div.text.lstrip().rstrip() - ) != "" and divText != "Vis fuld skærm": - last = divText.lower().title().replace(" ", "") - last = last[0].lower() + last[1:] - else: - article = div.find("article") - if ( - article - and article.find("h1") is not None - and article.find("h1").text == "Groups" - ): - groupNames = list(map(lambda x: x.text, article.find_all("p"))) - groupParticipants = article.find_all("ul") - for i, group in enumerate(groupNames): - modulDetaljer["grupper"][group] = list( - map(lambda x: x.text, groupParticipants[i].find_all("li")) - ) - elif article: - for child in article.find_all(recursive=False): - if child.name == "h1": - child.name = "h3" - elif child.name == "h2": - child.name = "h4" - modulDetaljer[last] += markdownify.markdownify( - str(child), bullets="-" - ) - elif last == "præsentation" and ( - anchor := div.find("a", attrs={"data-lc-display-linktype": "file"}) - ): - if modulDetaljer[last] != f"[{anchor.text}]({anchor.get('href')})": - modulDetaljer[last] += f"[{anchor.text}]({anchor.get('href')})" + if modulContent: + last = "" + for div in modulContent.find_all("div"): + if div.get("id") is None: + if ( + divText := div.text.lstrip().rstrip() + ) != "" and divText != "Vis fuld skærm": + last = divText.lower().title().replace(" ", "") + last = last[0].lower() + last[1:] else: - modulDetaljer[last] += markdownify.markdownify(str(div), bullets="-") + article = div.find("article") + if ( + article + and article.find("h1") is not None + and article.find("h1").text == "Groups" + ): + groupNames = list(map(lambda x: x.text, article.find_all("p"))) + groupParticipants = article.find_all("ul") + for i, group in enumerate(groupNames): + modulDetaljer["grupper"][group] = list( + map(lambda x: x.text, groupParticipants[i].find_all("li")) + ) + elif article: + for child in article.find_all(recursive=False): + if child.name == "h1": + child.name = "h3" + elif child.name == "h2": + child.name = "h4" + modulDetaljer[last] += markdownify.markdownify( + str(child), bullets="-" + ) + elif last == "præsentation" and ( + anchor := div.find("a", attrs={"data-lc-display-linktype": "file"}) + ): + if modulDetaljer[last] != f"[{anchor.text}]({anchor.get('href')})": + modulDetaljer[last] += f"[{anchor.text}]({anchor.get('href')})" + else: + modulDetaljer[last] += markdownify.markdownify(str(div), bullets="-") modulDetaljer["aktivitet"] = skemaBrikExtract(soup.find("a", class_="s2skemabrik"), modul_id=absid) + course = soup.find("a", {"id": "s_m_Content_Content_tocAndToolbar_phaseRepeater_ctl01_phaseLB"}) + if course: + modulDetaljer["forløb"] = course.text + return modulDetaljer diff --git a/apps/api/lectio/utils.py b/apps/api/lectio/utils.py index 4f9d63c1..2a65dc09 100644 --- a/apps/api/lectio/utils.py +++ b/apps/api/lectio/utils.py @@ -51,6 +51,9 @@ def skemaBrikExtract(skemabrik, modul_id=None): "andet": None, } + if skemabrik is None: + return modulDict + modulDetaljer = skemabrik statusClass = modulDetaljer.get("class")[2] if statusClass in statusDictionary: