mirror of
https://github.com/Garmelon/PFERD.git
synced 2025-07-12 14:12:30 +02:00
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
7291382430 | |||
1a430ad5d1 | |||
f6bdeb6b9d |
10
CHANGELOG.md
10
CHANGELOG.md
@ -22,22 +22,14 @@ ambiguous situations.
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 3.8.1 - 2025-04-17
|
||||
|
||||
## Fixed
|
||||
- Description html files now specify at UTF-8 encoding
|
||||
- Images in descriptions now always have a white background
|
||||
|
||||
## 3.8.0 - 2025-04-16
|
||||
## 3.8.0 - 2025-04-15
|
||||
|
||||
### Added
|
||||
- Support for ILIAS 9
|
||||
|
||||
### Changed
|
||||
- Added prettier CSS to forum threads
|
||||
- Downloaded forum threads now link to the forum instead of the ILIAS thread
|
||||
- Increase minimum supported Python version to 3.11
|
||||
- Do not crawl nested courses (courses linked in other courses)
|
||||
|
||||
## Fixed
|
||||
- File links in report on Windows
|
||||
|
@ -39,10 +39,6 @@ _STYLE_TAG_CONTENT = """
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
img {
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
body {
|
||||
padding: 1em;
|
||||
grid-template-columns: 1fr min(60rem, 90%) 1fr;
|
||||
@ -60,11 +56,12 @@ _ARTICLE_WORTHY_CLASSES = [
|
||||
def insert_base_markup(soup: BeautifulSoup) -> BeautifulSoup:
|
||||
head = soup.new_tag("head")
|
||||
soup.insert(0, head)
|
||||
# Force UTF-8 encoding
|
||||
head.append(soup.new_tag("meta", charset="utf-8"))
|
||||
|
||||
simplecss_link: Tag = soup.new_tag("link")
|
||||
# <link rel="stylesheet" href="https://cdn.simplecss.org/simple.css">
|
||||
head.append(soup.new_tag("link", rel="stylesheet", href="https://cdn.simplecss.org/simple.css"))
|
||||
simplecss_link["rel"] = "stylesheet"
|
||||
simplecss_link["href"] = "https://cdn.simplecss.org/simple.css"
|
||||
head.append(simplecss_link)
|
||||
|
||||
# Basic style tags for compat
|
||||
style: Tag = soup.new_tag("style")
|
||||
|
@ -216,19 +216,11 @@ instance's greatest bottleneck.
|
||||
|
||||
async def _crawl_desktop(self) -> None:
|
||||
await self._crawl_url(
|
||||
urljoin(self._base_url, "/ilias.php?baseClass=ilDashboardGUI&cmd=show"),
|
||||
crawl_nested_courses=True
|
||||
urljoin(self._base_url, "/ilias.php?baseClass=ilDashboardGUI&cmd=show")
|
||||
)
|
||||
|
||||
async def _crawl_url(
|
||||
self,
|
||||
url: str,
|
||||
expected_id: Optional[int] = None,
|
||||
crawl_nested_courses: bool = False
|
||||
) -> None:
|
||||
if awaitable := await self._handle_ilias_page(
|
||||
url, None, PurePath("."), expected_id, crawl_nested_courses
|
||||
):
|
||||
async def _crawl_url(self, url: str, expected_id: Optional[int] = None) -> None:
|
||||
if awaitable := await self._handle_ilias_page(url, None, PurePath("."), expected_id):
|
||||
await awaitable
|
||||
|
||||
async def _handle_ilias_page(
|
||||
@ -237,7 +229,6 @@ instance's greatest bottleneck.
|
||||
current_element: Optional[IliasPageElement],
|
||||
path: PurePath,
|
||||
expected_course_id: Optional[int] = None,
|
||||
crawl_nested_courses: bool = False
|
||||
) -> Optional[Coroutine[Any, Any, None]]:
|
||||
maybe_cl = await self.crawl(path)
|
||||
if not maybe_cl:
|
||||
@ -245,9 +236,7 @@ instance's greatest bottleneck.
|
||||
if current_element:
|
||||
self._ensure_not_seen(current_element, path)
|
||||
|
||||
return self._crawl_ilias_page(
|
||||
url, current_element, maybe_cl, expected_course_id, crawl_nested_courses
|
||||
)
|
||||
return self._crawl_ilias_page(url, current_element, maybe_cl, expected_course_id)
|
||||
|
||||
@anoncritical
|
||||
async def _crawl_ilias_page(
|
||||
@ -256,7 +245,6 @@ instance's greatest bottleneck.
|
||||
current_element: Optional[IliasPageElement],
|
||||
cl: CrawlToken,
|
||||
expected_course_id: Optional[int] = None,
|
||||
crawl_nested_courses: bool = False,
|
||||
) -> None:
|
||||
elements: List[IliasPageElement] = []
|
||||
# A list as variable redefinitions are not propagated to outer scopes
|
||||
@ -305,7 +293,7 @@ instance's greatest bottleneck.
|
||||
|
||||
tasks: List[Awaitable[None]] = []
|
||||
for element in elements:
|
||||
if handle := await self._handle_ilias_element(cl.path, element, crawl_nested_courses):
|
||||
if handle := await self._handle_ilias_element(cl.path, element):
|
||||
tasks.append(asyncio.create_task(handle))
|
||||
|
||||
# And execute them
|
||||
@ -321,7 +309,6 @@ instance's greatest bottleneck.
|
||||
self,
|
||||
parent_path: PurePath,
|
||||
element: IliasPageElement,
|
||||
crawl_nested_courses: bool = False
|
||||
) -> Optional[Coroutine[Any, Any, None]]:
|
||||
# element.name might contain `/` if the crawler created nested elements,
|
||||
# so we can not sanitize it here. We trust in the output dir to thwart worst-case
|
||||
@ -415,13 +402,11 @@ instance's greatest bottleneck.
|
||||
)
|
||||
return None
|
||||
elif element.type == IliasElementType.COURSE:
|
||||
if crawl_nested_courses:
|
||||
return await self._handle_ilias_page(element.url, element, element_path)
|
||||
log.status(
|
||||
"[bold bright_black]",
|
||||
"Ignored",
|
||||
fmt_path(element_path),
|
||||
"[bright_black](not descending into linked course)"
|
||||
"[bright_black](not descending into linked course, download it separately)"
|
||||
)
|
||||
return None
|
||||
elif element.type == IliasElementType.LEARNING_MODULE:
|
||||
@ -774,23 +759,70 @@ instance's greatest bottleneck.
|
||||
@_iorepeat(3, "crawling forum")
|
||||
@anoncritical
|
||||
async def _crawl_forum(self, element: IliasPageElement, cl: CrawlToken) -> None:
|
||||
elements: List[IliasForumThread] = []
|
||||
|
||||
async with cl:
|
||||
inner = IliasPage(await self._get_page(element.url), element)
|
||||
export_url = inner.get_forum_export_url()
|
||||
if not export_url:
|
||||
log.warn("Could not extract forum export url")
|
||||
next_stage_url = element.url
|
||||
page = None
|
||||
|
||||
while next_stage_url:
|
||||
log.explain_topic(f"Parsing HTML page for {fmt_path(cl.path)}")
|
||||
log.explain(f"URL: {next_stage_url}")
|
||||
|
||||
soup = await self._get_page(next_stage_url)
|
||||
page = IliasPage(soup, element)
|
||||
|
||||
if next := page.get_next_stage_element():
|
||||
next_stage_url = next.url
|
||||
else:
|
||||
break
|
||||
|
||||
forum_threads: list[tuple[IliasPageElement, bool]] = []
|
||||
for entry in cast(IliasPage, page).get_forum_entries():
|
||||
path = cl.path / (_sanitize_path_name(entry.name) + ".html")
|
||||
forum_threads.append((entry, self.should_try_download(path, mtime=entry.mtime)))
|
||||
|
||||
# Sort the ids. The forum download will *preserve* this ordering
|
||||
forum_threads.sort(key=lambda elem: elem[0].id())
|
||||
|
||||
if not forum_threads:
|
||||
log.explain("Forum had no threads")
|
||||
return
|
||||
|
||||
export = await self._post(export_url, {
|
||||
"format": "html",
|
||||
"cmd[createExportFile]": ""
|
||||
})
|
||||
download_data = cast(IliasPage, page).get_download_forum_data(
|
||||
[thread.id() for thread, download in forum_threads if download]
|
||||
)
|
||||
if not download_data:
|
||||
raise CrawlWarning("Failed to extract forum data")
|
||||
|
||||
elements = parse_ilias_forum_export(soupify(export))
|
||||
if not download_data.empty:
|
||||
html = await self._post_authenticated(download_data.url, download_data.form_data)
|
||||
elements = parse_ilias_forum_export(soupify(html))
|
||||
else:
|
||||
elements = []
|
||||
|
||||
# Verify that ILIAS does not change the order, as we depend on it later. Otherwise, we could not call
|
||||
# download in the correct order, potentially messing up duplication handling.
|
||||
expected_element_titles = [thread.name for thread, download in forum_threads if download]
|
||||
actual_element_titles = [_sanitize_path_name(thread.name) for thread in elements]
|
||||
if expected_element_titles != actual_element_titles:
|
||||
raise CrawlWarning(
|
||||
f"Forum thread order mismatch: {expected_element_titles} != {actual_element_titles}"
|
||||
)
|
||||
|
||||
tasks: List[Awaitable[None]] = []
|
||||
for thread in elements:
|
||||
tasks.append(asyncio.create_task(self._download_forum_thread(cl.path, thread, element.url)))
|
||||
for thread, download in forum_threads:
|
||||
if download:
|
||||
# This only works because ILIAS keeps the order in the export
|
||||
elem = elements.pop(0)
|
||||
tasks.append(asyncio.create_task(self._download_forum_thread(cl.path, elem, thread)))
|
||||
else:
|
||||
# We only downloaded the threads we "should_try_download"ed. This can be an
|
||||
# over-approximation and all will be fine.
|
||||
# If we selected too few, e.g. because there was a duplicate title and the mtime of the
|
||||
# original is newer than the update of the duplicate.
|
||||
# This causes stale data locally, but I consider this problem acceptable right now.
|
||||
tasks.append(asyncio.create_task(self._download_forum_thread(cl.path, thread, thread)))
|
||||
|
||||
# And execute them
|
||||
await self.gather(tasks)
|
||||
@ -801,7 +833,7 @@ instance's greatest bottleneck.
|
||||
self,
|
||||
parent_path: PurePath,
|
||||
thread: Union[IliasForumThread, IliasPageElement],
|
||||
forum_url: str
|
||||
element: IliasPageElement
|
||||
) -> None:
|
||||
path = parent_path / (_sanitize_path_name(thread.name) + ".html")
|
||||
maybe_dl = await self.download(path, mtime=thread.mtime)
|
||||
@ -811,7 +843,7 @@ instance's greatest bottleneck.
|
||||
async with maybe_dl as (bar, sink):
|
||||
rendered = forum_thread_template(
|
||||
thread.name,
|
||||
forum_url,
|
||||
element.url,
|
||||
thread.name_tag,
|
||||
await self.internalize_images(thread.content_tag)
|
||||
)
|
||||
@ -989,19 +1021,29 @@ instance's greatest bottleneck.
|
||||
)
|
||||
return soup
|
||||
|
||||
async def _post(
|
||||
async def _post_authenticated(
|
||||
self,
|
||||
url: str,
|
||||
data: dict[str, Union[str, List[str]]]
|
||||
) -> bytes:
|
||||
auth_id = await self._current_auth_id()
|
||||
|
||||
form_data = aiohttp.FormData()
|
||||
for key, val in data.items():
|
||||
form_data.add_field(key, val)
|
||||
|
||||
async with self.session.post(url, data=form_data()) as request:
|
||||
async with self.session.post(url, data=form_data(), allow_redirects=False) as request:
|
||||
if request.status == 200:
|
||||
return await request.read()
|
||||
raise CrawlError(f"post failed with status {request.status}")
|
||||
|
||||
# We weren't authenticated, so try to do that
|
||||
await self.authenticate(auth_id)
|
||||
|
||||
# Retry once after authenticating. If this fails, we will die.
|
||||
async with self.session.post(url, data=data, allow_redirects=False) as request:
|
||||
if request.status == 200:
|
||||
return await request.read()
|
||||
raise CrawlError("post_authenticated failed even after authenticating")
|
||||
|
||||
async def _get_authenticated(self, url: str) -> bytes:
|
||||
auth_id = await self._current_auth_id()
|
||||
|
@ -271,7 +271,6 @@ class IliasPageElement:
|
||||
r"mcst/(?P<id>\d+)", # mediacast
|
||||
r"pg/(?P<id>(\d|_)+)", # page?
|
||||
r"svy/(?P<id>\d+)", # survey
|
||||
r"sess/(?P<id>\d+)", # session
|
||||
r"webr/(?P<id>\d+)", # web referene (link)
|
||||
r"thr_pk=(?P<id>\d+)", # forums
|
||||
r"ref_id=(?P<id>\d+)",
|
||||
@ -490,31 +489,79 @@ class IliasPage:
|
||||
return url
|
||||
return None
|
||||
|
||||
def get_forum_export_url(self) -> Optional[str]:
|
||||
forum_link = self._soup.select_one("#tab_forums_threads > a")
|
||||
if not forum_link:
|
||||
log.explain("Found no forum link")
|
||||
def get_forum_entries(self) -> list[IliasPageElement]:
|
||||
form = self._get_forum_form()
|
||||
if not form:
|
||||
return []
|
||||
threads = []
|
||||
|
||||
for row in cast(list[Tag], form.select("table > tbody > tr")):
|
||||
url_tag = cast(
|
||||
Optional[Tag],
|
||||
row.find(name="a", attrs={"href": lambda x: x is not None and "cmd=viewthread" in x.lower()})
|
||||
)
|
||||
if url_tag is None:
|
||||
log.explain(f"Skipping row without URL: {row}")
|
||||
continue
|
||||
name = url_tag.get_text().strip()
|
||||
columns = [td.get_text().strip() for td in cast(list[Tag], row.find_all(name="td"))]
|
||||
potential_dates_opt = [IliasPage._find_date_in_text(column) for column in columns]
|
||||
potential_dates = [x for x in potential_dates_opt if x is not None]
|
||||
mtime = max(potential_dates) if potential_dates else None
|
||||
|
||||
threads.append(IliasPageElement.create_new(
|
||||
IliasElementType.FORUM_THREAD,
|
||||
self._abs_url_from_link(url_tag),
|
||||
name,
|
||||
mtime=mtime
|
||||
))
|
||||
|
||||
return threads
|
||||
|
||||
def get_download_forum_data(self, thread_ids: list[str]) -> Optional[IliasDownloadForumData]:
|
||||
form = cast(Optional[Tag], self._soup.find(
|
||||
"form",
|
||||
attrs={"action": lambda x: x is not None and "fallbackCmd=showThreads" in x}
|
||||
))
|
||||
if not form:
|
||||
return None
|
||||
post_url = self._abs_url_from_relative(cast(str, form["action"]))
|
||||
|
||||
base_url = self._abs_url_from_link(forum_link)
|
||||
base_url = re.sub(r"cmd=\w+", "cmd=post", base_url)
|
||||
base_url = re.sub(r"cmdClass=\w+", "cmdClass=ilExportGUI", base_url)
|
||||
log.explain(f"Fetching forum threads {thread_ids}")
|
||||
|
||||
rtoken_form = cast(
|
||||
Optional[Tag],
|
||||
self._soup.find("form", attrs={"action": lambda x: x is not None and "rtoken=" in x})
|
||||
)
|
||||
if not rtoken_form:
|
||||
log.explain("Found no rtoken anywhere")
|
||||
return None
|
||||
match = cast(re.Match[str], re.search(r"rtoken=(\w+)", str(rtoken_form.attrs["action"])))
|
||||
rtoken = match.group(1)
|
||||
form_data: Dict[str, Union[str, list[str]]] = {
|
||||
"thread_ids[]": cast(list[str], thread_ids),
|
||||
"selected_cmd2": "html",
|
||||
"select_cmd2": "Ausführen",
|
||||
"selected_cmd": "",
|
||||
}
|
||||
|
||||
base_url = base_url + "&rtoken=" + rtoken
|
||||
return IliasDownloadForumData(url=post_url, form_data=form_data, empty=len(thread_ids) == 0)
|
||||
|
||||
return base_url
|
||||
def _get_forum_form(self) -> Optional[Tag]:
|
||||
return cast(Optional[Tag], self._soup.find(
|
||||
"form",
|
||||
attrs={"action": lambda x: x is not None and "fallbackCmd=showThreads" in x}
|
||||
))
|
||||
|
||||
def get_next_stage_element(self) -> Optional[IliasPageElement]:
|
||||
if self._is_forum_page():
|
||||
if "trows=" in self._page_url:
|
||||
log.explain("Manual row override detected, accepting it as good")
|
||||
return None
|
||||
log.explain("Requesting *all* forum threads")
|
||||
thread_count = self._get_forum_thread_count()
|
||||
if thread_count is not None and thread_count > 400:
|
||||
log.warn(
|
||||
"Forum has more than 400 threads, fetching all threads will take a while. "
|
||||
"You might need to adjust your http_timeout config option."
|
||||
)
|
||||
|
||||
# Fetch at least 400 in case we detect it wrong
|
||||
if thread_count is not None and thread_count < 400:
|
||||
thread_count = 400
|
||||
|
||||
return self._get_show_max_forum_entries_per_page_url(thread_count)
|
||||
if self._is_ilias_opencast_embedding():
|
||||
log.explain("Unwrapping opencast embedding")
|
||||
return self.get_child_elements()[0]
|
||||
@ -532,6 +579,11 @@ class IliasPage:
|
||||
log.explain("Crawling info tab, skipping content select")
|
||||
return None
|
||||
|
||||
def _is_forum_page(self) -> bool:
|
||||
if perma_link := self.get_permalink():
|
||||
return "/frm/" in perma_link
|
||||
return False
|
||||
|
||||
def _is_video_player(self) -> bool:
|
||||
return "paella_config_file" in str(self._soup)
|
||||
|
||||
@ -1075,7 +1127,7 @@ class IliasPage:
|
||||
|
||||
videos.append(IliasPageElement.create_new(
|
||||
typ=IliasElementType.MOB_VIDEO,
|
||||
url=url,
|
||||
url=self._abs_url_from_relative(url),
|
||||
name=_sanitize_path_name(title),
|
||||
mtime=None
|
||||
))
|
||||
@ -1101,9 +1153,6 @@ class IliasPage:
|
||||
else:
|
||||
title = f"unknown video {figure}"
|
||||
|
||||
if url:
|
||||
url = self._abs_url_from_relative(url)
|
||||
|
||||
return url, title
|
||||
|
||||
def _is_in_expanded_meeting(self, tag: Tag) -> bool:
|
||||
|
@ -1,2 +1,2 @@
|
||||
NAME = "PFERD"
|
||||
VERSION = "3.8.1"
|
||||
VERSION = "3.8.0"
|
||||
|
Reference in New Issue
Block a user