From 6407190ae07663e84640663477c94e97269069fb Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 20 Apr 2020 16:38:18 +0000 Subject: [PATCH] Soupify requests responses properly --- PFERD/ilias/authenticators.py | 5 +++-- PFERD/utils.py | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/PFERD/ilias/authenticators.py b/PFERD/ilias/authenticators.py index 4e058d8..7701092 100644 --- a/PFERD/ilias/authenticators.py +++ b/PFERD/ilias/authenticators.py @@ -10,6 +10,7 @@ import bs4 import requests from ..authenticators import UserPassAuthenticator +from ..utils import soupify LOGGER = logging.getLogger(__name__) @@ -55,7 +56,7 @@ class KitShibbolethAuthenticator(IliasAuthenticator): "target": "/shib_login.php", "home_organization_selection": "Mit KIT-Account anmelden", } - soup = bs4.BeautifulSoup(sess.post(url, data=data)) + soup = soupify(sess.post(url, data=data)) # Attempt to login using credentials, if necessary while not self._login_successful(soup): @@ -73,7 +74,7 @@ class KitShibbolethAuthenticator(IliasAuthenticator): "j_username": self._auth.username, "j_password": self._auth.password, } - soup = bs4.BeautifulSoup(sess.post(url, data=data)) + soup = soupify(sess.post(url, data=data)) if not self._login_successful(soup): print("Incorrect credentials.") diff --git a/PFERD/utils.py b/PFERD/utils.py index 8233ad1..b29f8cb 100644 --- a/PFERD/utils.py +++ b/PFERD/utils.py @@ -6,6 +6,7 @@ import logging from pathlib import Path, PurePath from typing import Optional, Tuple +import bs4 import requests from colorama import Fore, Style @@ -30,6 +31,10 @@ def rename(path: PurePath, to_name: str) -> PurePath: return PurePath(*path.parts[:-1], to_name) +def soupify(response: requests.Response) -> bs4.BeautifulSoup: + return bs4.BeautifulSoup(response.text, "html.parser") + + def stream_to_path(response: requests.Response, to_path: Path, chunk_size: int = 1024 ** 2) -> None: """ Download a requests response content to a file by streaming it. This