From 19c1e3ac6fe6cbe5d2531ea216c2563854036461 Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Fri, 8 May 2020 23:47:05 +0200 Subject: [PATCH] Fail on invalid ILIAS course ids --- PFERD/errors.py | 2 ++ PFERD/ilias/crawler.py | 11 ++++++++++- PFERD/pferd.py | 9 ++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/PFERD/errors.py b/PFERD/errors.py index c12d23a..dee3d5a 100644 --- a/PFERD/errors.py +++ b/PFERD/errors.py @@ -5,6 +5,8 @@ An error logging decorator. import logging from typing import Any, Callable +from rich.console import Console + from .logging import FatalException, PrettyLogger LOGGER = logging.getLogger(__name__) diff --git a/PFERD/ilias/crawler.py b/PFERD/ilias/crawler.py index 6ed527c..e58f444 100644 --- a/PFERD/ilias/crawler.py +++ b/PFERD/ilias/crawler.py @@ -14,7 +14,7 @@ from urllib.parse import (parse_qs, urlencode, urljoin, urlparse, urlsplit, import bs4 import requests -from ..logging import PrettyLogger +from ..logging import FatalException, PrettyLogger from ..utils import soupify from .authenticators import IliasAuthenticator from .date_demangler import demangle_date @@ -80,9 +80,18 @@ class IliasCrawler: self._base_url + "/goto.php", "target", f"crs_{self._course_id}" ) + if not self._is_course_id_valid(root_url): + raise FatalException( + "Invalid course id? The URL the server returned did not contain my id." + ) + # And treat it as a folder return self._crawl_folder(Path(""), root_url) + def _is_course_id_valid(self, root_url: str) -> bool: + response: requests.Response = self._session.get(root_url) + return self._course_id in response.url + def _switch_on_crawled_type( self, path: Path, diff --git a/PFERD/pferd.py b/PFERD/pferd.py index 74e54c5..1f9e431 100644 --- a/PFERD/pferd.py +++ b/PFERD/pferd.py @@ -14,7 +14,7 @@ from .ilias import (IliasAuthenticator, IliasCrawler, IliasDirectoryFilter, IliasDownloader, IliasDownloadStrategy, KitShibbolethAuthenticator, download_modified_or_new) from .location import Location -from .logging import PrettyLogger +from .logging import FatalException, PrettyLogger from .organizer import Organizer from .tmp_dir import TmpDir from .transform import TF, Transform, apply_transform @@ -168,11 +168,18 @@ class Pferd(Location): clean {bool} -- Whether to clean up when the method finishes. """ tmp_dir = self._tmp_dir.new_subdir() + + if target is None: + PRETTY.starting_synchronizer("None", "DIVA", playlist_id) + raise FatalException("Got 'None' as target directory, aborting") + if isinstance(target, Organizer): organizer = target else: organizer = Organizer(self.resolve(to_path(target))) + PRETTY.starting_synchronizer(organizer.path, "DIVA", playlist_id) + crawler = DivaPlaylistCrawler(playlist_id) downloader = DivaDownloader(tmp_dir, organizer, download_strategy)