Use credential file

This commit is contained in:
I-Al-Istannen 2020-11-04 21:18:48 +01:00
parent 1e0343bba6
commit 75471c46d1

View File

@ -5,18 +5,42 @@ A simple script to download a course by name from ILIAS.
"""
import argparse
import logging
import sys
from pathlib import Path, PurePath
from typing import Optional, Tuple
from urllib.parse import urlparse
from PFERD import Pferd
from PFERD.cookie_jar import CookieJar
from PFERD.ilias import (IliasCrawler, IliasElementType,
KitShibbolethAuthenticator)
from PFERD.logging import PrettyLogger, enable_logging
from PFERD.organizer import (ConflictType, FileConflictResolution,
FileConflictResolver, resolve_prompt_user)
from PFERD.transform import sanitize_windows_path
from PFERD.utils import to_path
_LOGGER = logging.getLogger("sync_url")
_PRETTY = PrettyLogger(_LOGGER)
def _extract_credentials(file_path: Optional[str]) -> Tuple[Optional[str], Optional[str]]:
if not file_path:
return (None, None)
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
return (name, password)
def _resolve_remote_first(_path: PurePath, _conflict: ConflictType) -> FileConflictResolution:
return FileConflictResolution.DESTROY_EXISTING
@ -37,11 +61,16 @@ def _resolve_no_delete(_path: PurePath, conflict: ConflictType) -> FileConflictR
def main() -> None:
enable_logging(name="sync_url")
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")
parser.add_argument('-u', '--username', nargs='?', default=None, help="Username for Ilias")
parser.add_argument('-p', '--password', nargs='?', default=None, help="Password for Ilias")
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>'")
parser.add_argument('--no-videos', nargs='?', default=None, help="Don't download videos")
parser.add_argument('--local-first', action="store_true",
help="Don't prompt for confirmation, keep existing files")
@ -53,11 +82,13 @@ def main() -> None:
parser.add_argument('folder', nargs='?', default=None, help="Folder to put stuff into")
args = parser.parse_args()
url = urlparse(args.url)
cookie_jar = CookieJar(to_path(args.cookies) if args.cookies else None)
session = cookie_jar.create_session()
authenticator = KitShibbolethAuthenticator(username=args.username, password=args.password)
username, password = _extract_credentials(args.credential_file)
authenticator = KitShibbolethAuthenticator(username=username, password=password)
url = urlparse(args.url)
crawler = IliasCrawler(url.scheme + '://' + url.netloc, session,
authenticator, lambda x, y: True)
@ -100,10 +131,10 @@ def main() -> None:
full_url=args.url,
cookies=args.cookies,
dir_filter=dir_filter,
username=args.username,
password=args.password,
transform=sanitize_windows_path,
file_conflict_resolver=file_confilict_resolver
username=username,
password=password,
file_conflict_resolver=file_confilict_resolver,
transform=sanitize_windows_path
)
pferd.print_summary()