- detect whether authenticating is really necessary when attempting to
download a file
- add a get_website_refid() function
- move often-used goto.php url into constant
- and some comments
This commit is contained in:
Joscha 2018-11-23 17:45:07 +00:00
parent 2afcd38f1c
commit 5732268084

View File

@ -1,3 +1,12 @@
# This file is called IliasAuthenticators because there are multiple mechanisms
# for authenticating with Ilias (even though only the Shibboleth is currently
# implemented). Most of what the ShibbolethAuthenticator currently does is
# not Shibboleth specific; this mess would have to be cleaned up before
# actually implementing any other authentication method.
#
# I think the only other method is the password prompt when clicking the log in
# button.
import aiohttp import aiohttp
import asyncio import asyncio
import bs4 import bs4
@ -23,6 +32,8 @@ class UnknownFileTypeException(Exception):
class ShibbolethAuthenticator: class ShibbolethAuthenticator:
ILIAS_GOTO = "https://ilias.studium.kit.edu/goto.php"
RETRY_ATTEMPTS = 5 RETRY_ATTEMPTS = 5
RETRY_DELAY = 1 # seconds RETRY_DELAY = 1 # seconds
CHUNK_SIZE = 1024**2 CHUNK_SIZE = 1024**2
@ -160,14 +171,16 @@ class ShibbolethAuthenticator:
userlog = soup.find("li", {"id": "userlog"}) userlog = soup.find("li", {"id": "userlog"})
return userlog is not None return userlog is not None
async def get_webpage(self, ref_id): async def get_webpage_refid(self, ref_id):
url = "https://ilias.studium.kit.edu/goto.php" return await self.get_webpage(f"fold_{ref_id}")
params = {"target": f"fold_{ref_id}"}
async def get_webpage(self, object_id):
params = {"target": object_id}
while True: while True:
async with self._lock.read(): async with self._lock.read():
logger.debug(f"Getting {url} {params}") logger.debug(f"Getting {url} {params}")
_, text = await self._get(url, params=params) _, text = await self._get(self.ILIAS_GOTO, params=params)
soup = bs4.BeautifulSoup(text, "html.parser") soup = bs4.BeautifulSoup(text, "html.parser")
if self._is_logged_in(soup): if self._is_logged_in(soup):
@ -193,6 +206,10 @@ class ShibbolethAuthenticator:
return True return True
elif resp.content_type == "text/html": elif resp.content_type == "text/html":
# Dangit, we're probably not logged in. # Dangit, we're probably not logged in.
text = await resp.text()
soup = bs4.BeautifulSoup(text, "html.parser")
if self._is_logged_in(soup):
raise UnknownFileTypeException(f"Attempting to download a web page (use get_webpage() instead).")
return False return False
else: else:
# What *did* we get? # What *did* we get?
@ -206,12 +223,11 @@ class ShibbolethAuthenticator:
raise OutOfTriesException(f"Try {self.RETRY_ATTEMPTS} out of {self.RETRY_ATTEMPTS} failed.") raise OutOfTriesException(f"Try {self.RETRY_ATTEMPTS} out of {self.RETRY_ATTEMPTS} failed.")
async def download_file(self, file_id, to_path): async def download_file(self, file_id, to_path):
url = "https://ilias.studium.kit.edu/goto.php"
params = {"target": file_id} params = {"target": file_id}
while True: while True:
async with self._lock.read(): async with self._lock.read():
success = await self._download(url, params, to_path) success = await self._download(self.ILIAS_GOTO, params, to_path)
if success: if success:
return return