Soupify requests responses properly

This commit is contained in:
Joscha 2020-04-20 16:38:18 +00:00
parent 87395faac2
commit 6407190ae0
2 changed files with 8 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import bs4
import requests import requests
from ..authenticators import UserPassAuthenticator from ..authenticators import UserPassAuthenticator
from ..utils import soupify
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -55,7 +56,7 @@ class KitShibbolethAuthenticator(IliasAuthenticator):
"target": "/shib_login.php", "target": "/shib_login.php",
"home_organization_selection": "Mit KIT-Account anmelden", "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 # Attempt to login using credentials, if necessary
while not self._login_successful(soup): while not self._login_successful(soup):
@ -73,7 +74,7 @@ class KitShibbolethAuthenticator(IliasAuthenticator):
"j_username": self._auth.username, "j_username": self._auth.username,
"j_password": self._auth.password, "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): if not self._login_successful(soup):
print("Incorrect credentials.") print("Incorrect credentials.")

View File

@ -6,6 +6,7 @@ import logging
from pathlib import Path, PurePath from pathlib import Path, PurePath
from typing import Optional, Tuple from typing import Optional, Tuple
import bs4
import requests import requests
from colorama import Fore, Style from colorama import Fore, Style
@ -30,6 +31,10 @@ def rename(path: PurePath, to_name: str) -> PurePath:
return PurePath(*path.parts[:-1], to_name) 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: 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 Download a requests response content to a file by streaming it. This