2020-04-20 18:36:40 +02:00
|
|
|
"""Contains a downloader for ILIAS."""
|
|
|
|
|
2020-04-21 13:31:50 +02:00
|
|
|
import datetime
|
2020-04-24 16:26:20 +02:00
|
|
|
import logging
|
2020-04-22 01:37:34 +02:00
|
|
|
from dataclasses import dataclass
|
2020-04-20 18:36:40 +02:00
|
|
|
from pathlib import Path
|
2020-04-24 16:26:20 +02:00
|
|
|
from typing import Callable, List, Optional
|
2020-04-20 18:36:40 +02:00
|
|
|
|
|
|
|
import bs4
|
|
|
|
import requests
|
|
|
|
|
2020-04-25 19:59:58 +02:00
|
|
|
from ..logging import PrettyLogger
|
2020-04-20 18:50:23 +02:00
|
|
|
from ..organizer import Organizer
|
2020-04-20 18:36:40 +02:00
|
|
|
from ..tmp_dir import TmpDir
|
2020-04-22 20:25:09 +02:00
|
|
|
from ..transform import Transformable
|
2020-04-25 19:59:58 +02:00
|
|
|
from ..utils import soupify, stream_to_path
|
2020-04-20 18:36:40 +02:00
|
|
|
from .authenticators import IliasAuthenticator
|
|
|
|
|
2020-04-24 16:26:20 +02:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
PRETTY = PrettyLogger(LOGGER)
|
|
|
|
|
2020-04-20 18:36:40 +02:00
|
|
|
|
|
|
|
class ContentTypeException(Exception):
|
|
|
|
"""Thrown when the content type of the ilias element can not be handled."""
|
|
|
|
|
|
|
|
|
2020-04-20 20:06:21 +02:00
|
|
|
@dataclass
|
2020-04-22 20:25:09 +02:00
|
|
|
class IliasDownloadInfo(Transformable):
|
2020-04-20 20:06:21 +02:00
|
|
|
"""
|
|
|
|
This class describes a single file to be downloaded.
|
|
|
|
"""
|
|
|
|
|
|
|
|
url: str
|
2020-04-23 18:29:20 +02:00
|
|
|
modification_date: Optional[datetime.datetime]
|
2020-04-21 13:31:50 +02:00
|
|
|
# parameters: Dict[str, Any] = field(default_factory=dict)
|
2020-04-20 20:06:21 +02:00
|
|
|
|
|
|
|
|
2020-04-24 16:26:20 +02:00
|
|
|
IliasDownloadStrategy = Callable[[Organizer, IliasDownloadInfo], bool]
|
|
|
|
|
|
|
|
|
|
|
|
def download_everything(organizer: Organizer, info: IliasDownloadInfo) -> bool:
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
"""
|
|
|
|
Accepts everything.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def download_modified_or_new(organizer: Organizer, info: IliasDownloadInfo) -> bool:
|
|
|
|
"""
|
|
|
|
Accepts new files or files with a more recent modification date.
|
|
|
|
"""
|
|
|
|
resolved_file = organizer.resolve(info.path)
|
|
|
|
if not resolved_file.exists() or info.modification_date is None:
|
|
|
|
return True
|
|
|
|
resolved_mod_time_seconds = resolved_file.stat().st_mtime
|
|
|
|
|
|
|
|
# Download if the info is newer
|
|
|
|
if info.modification_date.timestamp() > resolved_mod_time_seconds:
|
|
|
|
return True
|
|
|
|
|
2020-04-24 20:24:44 +02:00
|
|
|
PRETTY.ignored_file(info.path, "local file has newer or equal modification time")
|
2020-04-24 16:26:20 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-04-20 20:06:21 +02:00
|
|
|
class IliasDownloader:
|
2020-04-24 16:26:20 +02:00
|
|
|
# pylint: disable=too-many-arguments
|
2020-04-20 18:36:40 +02:00
|
|
|
"""A downloader for ILIAS."""
|
|
|
|
|
2020-04-23 11:44:13 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
tmp_dir: TmpDir,
|
|
|
|
organizer: Organizer,
|
|
|
|
session: requests.Session,
|
|
|
|
authenticator: IliasAuthenticator,
|
2020-04-24 16:26:20 +02:00
|
|
|
strategy: IliasDownloadStrategy,
|
2020-04-23 11:44:13 +02:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Create a new IliasDownloader.
|
|
|
|
"""
|
|
|
|
|
2020-04-20 18:36:40 +02:00
|
|
|
self._tmp_dir = tmp_dir
|
|
|
|
self._organizer = organizer
|
2020-04-23 11:44:13 +02:00
|
|
|
self._session = session
|
|
|
|
self._authenticator = authenticator
|
2020-04-24 16:26:20 +02:00
|
|
|
self._strategy = strategy
|
2020-04-20 18:36:40 +02:00
|
|
|
|
2020-04-20 20:06:21 +02:00
|
|
|
def download_all(self, infos: List[IliasDownloadInfo]) -> None:
|
|
|
|
"""
|
|
|
|
Download multiple files one after the other.
|
|
|
|
"""
|
2020-04-20 18:36:40 +02:00
|
|
|
|
2020-04-20 20:06:21 +02:00
|
|
|
for info in infos:
|
|
|
|
self.download(info)
|
|
|
|
|
|
|
|
def download(self, info: IliasDownloadInfo) -> None:
|
|
|
|
"""
|
|
|
|
Download a file from ILIAS.
|
|
|
|
|
|
|
|
Retries authentication until eternity if it could not fetch the file.
|
2020-04-20 18:36:40 +02:00
|
|
|
"""
|
2020-04-20 20:06:21 +02:00
|
|
|
|
2020-04-25 13:02:07 +02:00
|
|
|
LOGGER.debug("Downloading %r", info)
|
2020-04-24 16:26:20 +02:00
|
|
|
if not self._strategy(self._organizer, info):
|
|
|
|
self._organizer.mark(info.path)
|
|
|
|
return
|
|
|
|
|
2020-04-23 11:44:13 +02:00
|
|
|
tmp_file = self._tmp_dir.new_path()
|
2020-04-20 18:36:40 +02:00
|
|
|
|
2020-04-20 20:06:21 +02:00
|
|
|
while not self._try_download(info, tmp_file):
|
2020-04-20 18:36:40 +02:00
|
|
|
self._authenticator.authenticate(self._session)
|
|
|
|
|
2020-04-20 20:06:21 +02:00
|
|
|
self._organizer.accept_file(tmp_file, info.path)
|
2020-04-20 18:36:40 +02:00
|
|
|
|
2020-04-20 20:06:21 +02:00
|
|
|
def _try_download(self, info: IliasDownloadInfo, target: Path) -> bool:
|
2020-04-21 13:31:50 +02:00
|
|
|
with self._session.get(info.url, stream=True) as response:
|
2020-04-20 19:27:26 +02:00
|
|
|
content_type = response.headers["content-type"]
|
2020-04-20 18:36:40 +02:00
|
|
|
|
|
|
|
if content_type.startswith("text/html"):
|
|
|
|
# Dangit, we're probably not logged in.
|
2020-04-20 20:06:21 +02:00
|
|
|
if self._is_logged_in(soupify(response)):
|
2020-04-20 19:27:26 +02:00
|
|
|
raise ContentTypeException("Attempting to download a web page, not a file")
|
2020-04-20 18:36:40 +02:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
2020-04-20 19:27:26 +02:00
|
|
|
# Yay, we got the file :)
|
2020-05-08 00:26:33 +02:00
|
|
|
stream_to_path(response, target, info.path.name)
|
2020-04-20 19:27:26 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _is_logged_in(soup: bs4.BeautifulSoup) -> bool:
|
2020-04-20 18:36:40 +02:00
|
|
|
userlog = soup.find("li", {"id": "userlog"})
|
|
|
|
return userlog is not None
|