mirror of
https://github.com/Garmelon/PFERD.git
synced 2025-09-09 14:12:26 +02:00
Use a strategy to decide conflict resolution
This commit is contained in:
34
sync_url.py
34
sync_url.py
@@ -5,24 +5,35 @@ A simple script to download a course by name from ILIAS.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
from pathlib import Path, PurePath
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from PFERD import Pferd
|
||||
from PFERD.cookie_jar import CookieJar
|
||||
from PFERD.ilias import (IliasCrawler, IliasElementType,
|
||||
KitShibbolethAuthenticator)
|
||||
from PFERD.organizer import FileConflictResolution, resolve_prompt_user
|
||||
from PFERD.transform import sanitize_windows_path
|
||||
from PFERD.utils import to_path
|
||||
|
||||
|
||||
def _resolve_overwrite(_path: PurePath) -> FileConflictResolution:
|
||||
return FileConflictResolution.OVERWRITE_EXISTING
|
||||
|
||||
|
||||
def _resolve_default(_path: PurePath) -> FileConflictResolution:
|
||||
return FileConflictResolution.DEFAULT
|
||||
|
||||
|
||||
def main() -> None:
|
||||
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('--no-videos', nargs='?', default=None, help="Don't download videos")
|
||||
parser.add_argument('-p', '--passive', action="store_true",
|
||||
parser.add_argument('-d', '--default', action="store_true",
|
||||
help="Don't prompt for confirmations and use sane defaults")
|
||||
parser.add_argument('-r', '--remove', action="store_true",
|
||||
help="Remove and overwrite files without prompting for confirmation")
|
||||
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()
|
||||
@@ -39,13 +50,17 @@ def main() -> None:
|
||||
|
||||
folder = Path(args.folder)
|
||||
if args.folder is None:
|
||||
folder = Path(crawler.find_element_name(args.url))
|
||||
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
|
||||
folder = Path(element_name)
|
||||
cookie_jar.save_cookies()
|
||||
|
||||
# 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
|
||||
folder = folder.name
|
||||
target = folder.name
|
||||
pferd = Pferd(pferd_root, test_run=args.test_run)
|
||||
|
||||
def dir_filter(_: Path, element: IliasElementType) -> bool:
|
||||
@@ -53,15 +68,22 @@ def main() -> None:
|
||||
return element not in [IliasElementType.VIDEO_FILE, IliasElementType.VIDEO_FOLDER]
|
||||
return True
|
||||
|
||||
if args.default:
|
||||
file_confilict_resolver = _resolve_default
|
||||
elif args.remove:
|
||||
file_confilict_resolver = _resolve_overwrite
|
||||
else:
|
||||
file_confilict_resolver = resolve_prompt_user
|
||||
|
||||
pferd.enable_logging()
|
||||
# fetch
|
||||
pferd.ilias_kit_folder(
|
||||
target=folder,
|
||||
target=target,
|
||||
full_url=args.url,
|
||||
cookies=args.cookies,
|
||||
dir_filter=dir_filter,
|
||||
transform=sanitize_windows_path,
|
||||
no_prompt=args.passive
|
||||
file_conflict_resolver=file_confilict_resolver
|
||||
)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user