2020-04-23 20:31:32 +02:00
|
|
|
"""
|
|
|
|
Convenience functions for using PFERD.
|
|
|
|
"""
|
|
|
|
|
2020-04-23 19:55:37 +02:00
|
|
|
import logging
|
2020-04-23 11:44:13 +02:00
|
|
|
from pathlib import Path
|
2020-04-24 20:00:21 +02:00
|
|
|
from typing import List, Optional, Union
|
2020-04-23 11:44:13 +02:00
|
|
|
|
|
|
|
from .cookie_jar import CookieJar
|
2020-04-23 12:33:38 +02:00
|
|
|
from .ilias import (IliasAuthenticator, IliasCrawler, IliasDirectoryFilter,
|
2020-04-24 16:26:20 +02:00
|
|
|
IliasDownloader, IliasDownloadStrategy,
|
|
|
|
KitShibbolethAuthenticator, download_modified_or_new)
|
2020-04-23 19:38:28 +02:00
|
|
|
from .location import Location
|
2020-04-23 11:44:13 +02:00
|
|
|
from .organizer import Organizer
|
|
|
|
from .tmp_dir import TmpDir
|
2020-04-24 20:00:21 +02:00
|
|
|
from .transform import Transform, Transformable, apply_transform
|
2020-04-23 19:55:37 +02:00
|
|
|
from .utils import PrettyLogger
|
2020-04-23 11:44:13 +02:00
|
|
|
|
2020-04-23 12:49:01 +02:00
|
|
|
# TODO save known-good cookies as soon as possible
|
2020-04-23 19:55:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
PRETTY = PrettyLogger(LOGGER)
|
2020-04-23 12:49:01 +02:00
|
|
|
|
|
|
|
|
2020-04-23 11:44:13 +02:00
|
|
|
class Pferd(Location):
|
|
|
|
# pylint: disable=too-many-arguments
|
2020-04-23 20:31:32 +02:00
|
|
|
"""
|
|
|
|
The main entrypoint in your Pferd usage: This class combines a number of
|
|
|
|
useful shortcuts for running synchronizers in a single interface.
|
|
|
|
"""
|
2020-04-23 11:44:13 +02:00
|
|
|
|
2020-04-24 20:00:21 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
base_dir: Path,
|
|
|
|
tmp_dir: Path = Path(".tmp"),
|
|
|
|
test_run: bool = False
|
|
|
|
):
|
2020-04-23 11:44:13 +02:00
|
|
|
super().__init__(Path(base_dir))
|
|
|
|
|
|
|
|
self._tmp_dir = TmpDir(self.resolve(tmp_dir))
|
2020-04-24 20:00:21 +02:00
|
|
|
self._test_run = test_run
|
|
|
|
|
|
|
|
def _print_transformables(self, transformables: List[Transformable]) -> None:
|
|
|
|
LOGGER.info("")
|
|
|
|
LOGGER.info("Results of the test run:")
|
|
|
|
for transformable in transformables:
|
|
|
|
LOGGER.info(transformable.path)
|
2020-04-23 11:44:13 +02:00
|
|
|
|
|
|
|
def _ilias(
|
|
|
|
self,
|
2020-04-24 13:50:27 +02:00
|
|
|
target: Union[Path, str],
|
2020-04-23 11:44:13 +02:00
|
|
|
base_url: str,
|
|
|
|
course_id: str,
|
|
|
|
authenticator: IliasAuthenticator,
|
|
|
|
cookies: Optional[Path],
|
2020-04-23 12:33:38 +02:00
|
|
|
dir_filter: IliasDirectoryFilter,
|
2020-04-23 11:44:13 +02:00
|
|
|
transform: Transform,
|
2020-04-24 16:26:20 +02:00
|
|
|
download_strategy: IliasDownloadStrategy,
|
2020-04-23 11:44:13 +02:00
|
|
|
) -> None:
|
2020-04-23 20:31:32 +02:00
|
|
|
# pylint: disable=too-many-locals
|
2020-04-23 11:44:13 +02:00
|
|
|
cookie_jar = CookieJar(cookies)
|
|
|
|
session = cookie_jar.create_session()
|
|
|
|
tmp_dir = self._tmp_dir.new_subdir()
|
2020-04-24 13:50:27 +02:00
|
|
|
organizer = Organizer(self.resolve(Path(target)))
|
2020-04-23 11:44:13 +02:00
|
|
|
|
2020-04-23 12:33:38 +02:00
|
|
|
crawler = IliasCrawler(base_url, course_id, session, authenticator, dir_filter)
|
2020-04-24 16:26:20 +02:00
|
|
|
downloader = IliasDownloader(tmp_dir, organizer, session, authenticator, download_strategy)
|
2020-04-23 11:44:13 +02:00
|
|
|
|
|
|
|
cookie_jar.load_cookies()
|
|
|
|
info = crawler.crawl()
|
|
|
|
cookie_jar.save_cookies()
|
2020-04-24 20:00:21 +02:00
|
|
|
|
|
|
|
transformed = apply_transform(transform, info)
|
|
|
|
if self._test_run:
|
|
|
|
self._print_transformables(transformed)
|
|
|
|
return
|
|
|
|
|
|
|
|
downloader.download_all(transformed)
|
2020-04-23 11:44:13 +02:00
|
|
|
cookie_jar.save_cookies()
|
2020-04-24 16:17:23 +02:00
|
|
|
organizer.cleanup()
|
2020-04-23 11:44:13 +02:00
|
|
|
|
|
|
|
def ilias_kit(
|
|
|
|
self,
|
2020-04-24 13:50:27 +02:00
|
|
|
target: Union[Path, str],
|
2020-04-23 11:44:13 +02:00
|
|
|
course_id: str,
|
2020-04-23 12:33:38 +02:00
|
|
|
dir_filter: IliasDirectoryFilter = lambda x: True,
|
2020-04-23 11:44:13 +02:00
|
|
|
transform: Transform = lambda x: x,
|
|
|
|
cookies: Optional[Path] = None,
|
|
|
|
username: Optional[str] = None,
|
|
|
|
password: Optional[str] = None,
|
2020-04-24 16:26:20 +02:00
|
|
|
download_strategy: IliasDownloadStrategy = download_modified_or_new,
|
2020-04-23 11:44:13 +02:00
|
|
|
) -> None:
|
2020-04-23 20:31:32 +02:00
|
|
|
"""
|
|
|
|
Synchronizes a folder with the ILIAS instance of the KIT.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
target {Path} -- the target path to write the data to
|
|
|
|
course_id {str} -- the id of the main course page (found in the URL after ref_id
|
|
|
|
when opening the course homepage)
|
|
|
|
|
|
|
|
Keyword Arguments:
|
|
|
|
dir_filter {IliasDirectoryFilter} -- A filter for directories. Will be applied on the
|
|
|
|
crawler level, these directories and all of their content is skipped.
|
|
|
|
(default: {lambdax:True})
|
|
|
|
transform {Transform} -- A transformation function for the output paths. Return None
|
|
|
|
to ignore a file. (default: {lambdax:x})
|
|
|
|
cookies {Optional[Path]} -- The path to store and load cookies from.
|
|
|
|
(default: {None})
|
|
|
|
username {Optional[str]} -- The SCC username. If none is given, it will prompt
|
|
|
|
the user. (default: {None})
|
|
|
|
password {Optional[str]} -- The SCC password. If none is given, it will prompt
|
|
|
|
the user. (default: {None})
|
|
|
|
download_strategy {DownloadStrategy} -- A function to determine which files need to
|
|
|
|
be downloaded. Can save bandwidth and reduce the number of requests.
|
2020-04-24 13:48:06 +02:00
|
|
|
(default: {download_modified_or_new})
|
2020-04-23 20:31:32 +02:00
|
|
|
"""
|
2020-04-23 11:44:13 +02:00
|
|
|
# This authenticator only works with the KIT ilias instance.
|
|
|
|
authenticator = KitShibbolethAuthenticator(username=username, password=password)
|
2020-04-23 19:55:37 +02:00
|
|
|
PRETTY.starting_synchronizer(target, "ILIAS", course_id)
|
2020-04-23 11:44:13 +02:00
|
|
|
self._ilias(
|
|
|
|
target=target,
|
|
|
|
base_url="https://ilias.studium.kit.edu/",
|
|
|
|
course_id=course_id,
|
|
|
|
authenticator=authenticator,
|
|
|
|
cookies=cookies,
|
2020-04-23 12:33:38 +02:00
|
|
|
dir_filter=dir_filter,
|
2020-04-23 11:44:13 +02:00
|
|
|
transform=transform,
|
2020-04-23 18:29:20 +02:00
|
|
|
download_strategy=download_strategy,
|
2020-04-23 11:44:13 +02:00
|
|
|
)
|