pferd/PFERD/pferd.py

78 lines
2.7 KiB
Python
Raw Normal View History

2020-04-23 11:44:13 +02:00
from pathlib import Path
from typing import Optional
from .cookie_jar import CookieJar
from .download_strategies import DownloadEverythingStrategy, DownloadStrategy
from .ilias import (IliasAuthenticator, IliasCrawler, IliasDirectoryFilter,
IliasDownloader, KitShibbolethAuthenticator)
2020-04-23 11:44:13 +02:00
from .organizer import Organizer
from .tmp_dir import TmpDir
from .transform import Transform, apply_transform
from .utils import Location
2020-04-23 12:49:01 +02:00
# TODO save known-good cookies as soon as possible
# TODO print synchronizer name before beginning synchronization
2020-04-23 11:44:13 +02:00
class Pferd(Location):
# pylint: disable=too-many-arguments
def __init__(self, base_dir: Path, tmp_dir: Path = Path(".tmp")):
super().__init__(Path(base_dir))
self._tmp_dir = TmpDir(self.resolve(tmp_dir))
def _ilias(
self,
target: Path,
base_url: str,
course_id: str,
authenticator: IliasAuthenticator,
cookies: Optional[Path],
dir_filter: IliasDirectoryFilter,
2020-04-23 11:44:13 +02:00
transform: Transform,
download_strategy: DownloadStrategy,
2020-04-23 11:44:13 +02:00
) -> None:
cookie_jar = CookieJar(cookies)
session = cookie_jar.create_session()
tmp_dir = self._tmp_dir.new_subdir()
organizer = Organizer(self.resolve(target))
crawler = IliasCrawler(base_url, course_id, session, authenticator, dir_filter)
2020-04-23 11:44:13 +02:00
downloader = IliasDownloader(tmp_dir, organizer, session, authenticator)
cookie_jar.load_cookies()
info = crawler.crawl()
cookie_jar.save_cookies()
downloader.download_all(
[
info for info in apply_transform(transform, info)
if download_strategy.should_download(organizer, info)
]
)
2020-04-23 11:44:13 +02:00
cookie_jar.save_cookies()
def ilias_kit(
self,
target: Path,
course_id: str,
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,
download_strategy: DownloadStrategy = DownloadEverythingStrategy(),
2020-04-23 11:44:13 +02:00
) -> None:
# This authenticator only works with the KIT ilias instance.
authenticator = KitShibbolethAuthenticator(username=username, password=password)
self._ilias(
target=target,
base_url="https://ilias.studium.kit.edu/",
course_id=course_id,
authenticator=authenticator,
cookies=cookies,
dir_filter=dir_filter,
2020-04-23 11:44:13 +02:00
transform=transform,
download_strategy=download_strategy,
2020-04-23 11:44:13 +02:00
)