2020-09-28 17:49:36 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2020-09-28 19:42:59 +02:00
|
|
|
"""
|
|
|
|
A simple script to download a course by name from ILIAS.
|
|
|
|
"""
|
|
|
|
|
2020-09-28 17:49:36 +02:00
|
|
|
import argparse
|
2020-11-04 21:18:48 +01:00
|
|
|
import logging
|
|
|
|
import sys
|
2020-12-02 19:29:52 +01:00
|
|
|
from pathlib import Path, PurePath
|
2020-11-04 00:18:27 +01:00
|
|
|
from typing import Optional
|
2020-10-06 17:15:10 +02:00
|
|
|
from urllib.parse import urlparse
|
2020-09-28 17:49:36 +02:00
|
|
|
|
|
|
|
from PFERD import Pferd
|
2020-11-04 00:18:27 +01:00
|
|
|
from PFERD.authenticators import KeyringAuthenticator, UserPassAuthenticator
|
2020-09-28 17:49:36 +02:00
|
|
|
from PFERD.cookie_jar import CookieJar
|
2020-10-06 17:15:10 +02:00
|
|
|
from PFERD.ilias import (IliasCrawler, IliasElementType,
|
|
|
|
KitShibbolethAuthenticator)
|
2020-11-04 21:18:48 +01:00
|
|
|
from PFERD.logging import PrettyLogger, enable_logging
|
2020-12-05 13:47:53 +01:00
|
|
|
from PFERD.organizer import (ConflictType, FileConflictResolution,
|
|
|
|
FileConflictResolver, resolve_prompt_user)
|
2020-11-12 20:52:46 +01:00
|
|
|
from PFERD.transform import sanitize_windows_path
|
2020-09-28 19:42:59 +02:00
|
|
|
from PFERD.utils import to_path
|
|
|
|
|
2020-11-04 21:18:48 +01:00
|
|
|
_LOGGER = logging.getLogger("sync_url")
|
|
|
|
_PRETTY = PrettyLogger(_LOGGER)
|
|
|
|
|
|
|
|
|
2020-11-04 00:18:27 +01:00
|
|
|
def _extract_credentials(file_path: Optional[str]) -> UserPassAuthenticator:
|
2020-11-04 21:18:48 +01:00
|
|
|
if not file_path:
|
2020-11-04 00:18:27 +01:00
|
|
|
return UserPassAuthenticator("KIT ILIAS Shibboleth", None, None)
|
2020-11-04 21:18:48 +01:00
|
|
|
|
|
|
|
if not Path(file_path).exists():
|
|
|
|
_PRETTY.error("Credential file does not exist")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
with open(file_path, "r") as file:
|
|
|
|
first_line = file.read().splitlines()[0]
|
|
|
|
read_name, *read_password = first_line.split(":", 1)
|
|
|
|
|
|
|
|
name = read_name if read_name else None
|
|
|
|
password = read_password[0] if read_password else None
|
2020-11-04 00:18:27 +01:00
|
|
|
return UserPassAuthenticator("KIT ILIAS Shibboleth", username=name, password=password)
|
2020-11-04 21:18:48 +01:00
|
|
|
|
2020-09-28 17:49:36 +02:00
|
|
|
|
2020-12-05 13:47:53 +01:00
|
|
|
def _resolve_remote_first(_path: PurePath, _conflict: ConflictType) -> FileConflictResolution:
|
|
|
|
return FileConflictResolution.DESTROY_EXISTING
|
2020-12-02 19:29:52 +01:00
|
|
|
|
|
|
|
|
2020-12-05 13:47:53 +01:00
|
|
|
def _resolve_local_first(_path: PurePath, _conflict: ConflictType) -> FileConflictResolution:
|
|
|
|
return FileConflictResolution.KEEP_EXISTING
|
|
|
|
|
|
|
|
|
|
|
|
def _resolve_no_delete(_path: PurePath, conflict: ConflictType) -> FileConflictResolution:
|
|
|
|
# Update files
|
|
|
|
if conflict == ConflictType.FILE_OVERWRITTEN:
|
|
|
|
return FileConflictResolution.DESTROY_EXISTING
|
2020-12-05 14:03:43 +01:00
|
|
|
if conflict == ConflictType.MARKED_FILE_OVERWRITTEN:
|
|
|
|
return FileConflictResolution.DESTROY_EXISTING
|
2020-12-05 13:47:53 +01:00
|
|
|
# But do not delete them
|
|
|
|
return FileConflictResolution.KEEP_EXISTING
|
2020-12-02 19:29:52 +01:00
|
|
|
|
|
|
|
|
2020-09-28 17:49:36 +02:00
|
|
|
def main() -> None:
|
2020-11-04 21:18:48 +01:00
|
|
|
enable_logging(name="sync_url")
|
|
|
|
|
2020-09-28 17:49:36 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--test-run", action="store_true")
|
|
|
|
parser.add_argument('-c', '--cookies', nargs='?', default=None, help="File to store cookies in")
|
2020-11-03 13:38:33 +01:00
|
|
|
parser.add_argument('-u', '--username', nargs='?', default=None, help="Username for Ilias")
|
|
|
|
parser.add_argument('-p', '--password', nargs='?', default=None, help="Password for Ilias")
|
2020-11-04 21:18:48 +01:00
|
|
|
parser.add_argument('--credential-file', nargs='?', default=None,
|
|
|
|
help="Path to a file containing credentials for Ilias. The file must have "
|
|
|
|
"one line in the following format: '<user>:<password>'")
|
2020-11-04 00:18:27 +01:00
|
|
|
parser.add_argument("-k", "--keyring", action="store_true",
|
|
|
|
help="Use the system keyring service for authentication")
|
2021-04-08 18:12:27 +02:00
|
|
|
parser.add_argument('--no-videos', action="store_true", help="Don't download videos")
|
2020-12-05 13:47:53 +01:00
|
|
|
parser.add_argument('--local-first', action="store_true",
|
|
|
|
help="Don't prompt for confirmation, keep existing files")
|
|
|
|
parser.add_argument('--remote-first', action="store_true",
|
|
|
|
help="Don't prompt for confirmation, delete and overwrite local files")
|
|
|
|
parser.add_argument('--no-delete', action="store_true",
|
|
|
|
help="Don't prompt for confirmation, overwrite local files, don't delete")
|
2020-09-28 17:49:36 +02:00
|
|
|
parser.add_argument('url', help="URL to the course page")
|
|
|
|
parser.add_argument('folder', nargs='?', default=None, help="Folder to put stuff into")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-09-28 20:00:01 +02:00
|
|
|
cookie_jar = CookieJar(to_path(args.cookies) if args.cookies else None)
|
|
|
|
session = cookie_jar.create_session()
|
2020-11-04 21:18:48 +01:00
|
|
|
|
2020-11-04 00:18:27 +01:00
|
|
|
if args.keyring:
|
|
|
|
if not args.username:
|
|
|
|
_PRETTY.error("Keyring auth selected but no --username passed!")
|
|
|
|
return
|
|
|
|
inner_auth: UserPassAuthenticator = KeyringAuthenticator(
|
|
|
|
"KIT ILIAS Shibboleth", username=args.username, password=args.password
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
inner_auth = _extract_credentials(args.credential_file)
|
|
|
|
|
|
|
|
username, password = inner_auth.get_credentials()
|
|
|
|
authenticator = KitShibbolethAuthenticator(inner_auth)
|
2020-11-04 21:18:48 +01:00
|
|
|
|
|
|
|
url = urlparse(args.url)
|
2020-11-04 00:18:27 +01:00
|
|
|
|
2020-09-28 20:00:01 +02:00
|
|
|
crawler = IliasCrawler(url.scheme + '://' + url.netloc, session,
|
|
|
|
authenticator, lambda x, y: True)
|
|
|
|
|
|
|
|
cookie_jar.load_cookies()
|
2020-09-28 17:49:36 +02:00
|
|
|
|
2020-12-02 16:50:30 +01:00
|
|
|
if args.folder is None:
|
2020-12-02 19:29:52 +01:00
|
|
|
element_name = crawler.find_element_name(args.url)
|
|
|
|
if not element_name:
|
|
|
|
print("Error, could not get element name. Please specify a folder yourself.")
|
|
|
|
return
|
2021-02-06 22:51:08 +01:00
|
|
|
folder = sanitize_windows_path(Path(element_name.replace("/", "-").replace("\\", "-")))
|
2020-09-28 17:49:36 +02:00
|
|
|
cookie_jar.save_cookies()
|
2020-12-05 13:50:46 +01:00
|
|
|
else:
|
|
|
|
folder = Path(args.folder)
|
2020-09-28 17:49:36 +02:00
|
|
|
|
2020-12-02 16:50:30 +01:00
|
|
|
# files may not escape the pferd_root with relative paths
|
|
|
|
# note: Path(Path.cwd, Path(folder)) == Path(folder) if it is an absolute path
|
|
|
|
pferd_root = Path(Path.cwd(), Path(folder)).parent
|
2020-12-02 19:29:52 +01:00
|
|
|
target = folder.name
|
2020-12-02 16:50:30 +01:00
|
|
|
pferd = Pferd(pferd_root, test_run=args.test_run)
|
2020-09-28 19:42:59 +02:00
|
|
|
|
2020-10-06 17:15:10 +02:00
|
|
|
def dir_filter(_: Path, element: IliasElementType) -> bool:
|
|
|
|
if args.no_videos:
|
|
|
|
return element not in [IliasElementType.VIDEO_FILE, IliasElementType.VIDEO_FOLDER]
|
|
|
|
return True
|
|
|
|
|
2020-12-06 13:28:08 +01:00
|
|
|
if args.local_first:
|
|
|
|
file_confilict_resolver: FileConflictResolver = _resolve_local_first
|
2020-12-05 13:47:53 +01:00
|
|
|
elif args.no_delete:
|
|
|
|
file_confilict_resolver = _resolve_no_delete
|
2020-12-06 13:28:08 +01:00
|
|
|
elif args.remote_first:
|
|
|
|
file_confilict_resolver = _resolve_remote_first
|
2020-12-02 19:29:52 +01:00
|
|
|
else:
|
|
|
|
file_confilict_resolver = resolve_prompt_user
|
|
|
|
|
2020-09-28 19:42:59 +02:00
|
|
|
pferd.enable_logging()
|
2020-11-04 00:18:27 +01:00
|
|
|
|
2020-09-28 17:49:36 +02:00
|
|
|
# fetch
|
2020-10-06 17:15:10 +02:00
|
|
|
pferd.ilias_kit_folder(
|
2020-12-02 19:29:52 +01:00
|
|
|
target=target,
|
2020-10-06 17:15:10 +02:00
|
|
|
full_url=args.url,
|
|
|
|
cookies=args.cookies,
|
2020-11-10 15:27:12 +01:00
|
|
|
dir_filter=dir_filter,
|
2020-11-04 21:18:48 +01:00
|
|
|
username=username,
|
|
|
|
password=password,
|
|
|
|
file_conflict_resolver=file_confilict_resolver,
|
|
|
|
transform=sanitize_windows_path
|
2020-10-06 17:15:10 +02:00
|
|
|
)
|
2020-09-28 19:42:59 +02:00
|
|
|
|
2020-12-05 14:08:00 +01:00
|
|
|
pferd.print_summary()
|
|
|
|
|
2020-09-28 17:49:36 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|