From c1d9160cf87658f857bbe326dd41f5877e3ef803 Mon Sep 17 00:00:00 2001 From: Frederik Kriewitz Date: Wed, 27 Dec 2023 18:26:38 +0100 Subject: [PATCH 1/3] add new expcetions --- .../assets/errors/book_has_no_audiobook.txt | 3 +++ audiobookdl/assets/errors/book_not_found.txt | 3 +++ .../assets/errors/book_not_released.txt | 3 +++ audiobookdl/assets/errors/download_error.txt | 4 ++++ audiobookdl/assets/errors/generic.txt | 3 +++ audiobookdl/exceptions.py | 18 ++++++++++++++++++ 6 files changed, 34 insertions(+) create mode 100644 audiobookdl/assets/errors/book_has_no_audiobook.txt create mode 100644 audiobookdl/assets/errors/book_not_found.txt create mode 100644 audiobookdl/assets/errors/book_not_released.txt create mode 100644 audiobookdl/assets/errors/download_error.txt create mode 100644 audiobookdl/assets/errors/generic.txt diff --git a/audiobookdl/assets/errors/book_has_no_audiobook.txt b/audiobookdl/assets/errors/book_has_no_audiobook.txt new file mode 100644 index 0000000..2d03a78 --- /dev/null +++ b/audiobookdl/assets/errors/book_has_no_audiobook.txt @@ -0,0 +1,3 @@ +[red]ERROR: Book has no audiobook[/red] + +Only audiobooks are supported. diff --git a/audiobookdl/assets/errors/book_not_found.txt b/audiobookdl/assets/errors/book_not_found.txt new file mode 100644 index 0000000..62f8125 --- /dev/null +++ b/audiobookdl/assets/errors/book_not_found.txt @@ -0,0 +1,3 @@ +[red]ERROR: Book doesn't exist[/red] + +Please check the URL. diff --git a/audiobookdl/assets/errors/book_not_released.txt b/audiobookdl/assets/errors/book_not_released.txt new file mode 100644 index 0000000..9b7e4cd --- /dev/null +++ b/audiobookdl/assets/errors/book_not_released.txt @@ -0,0 +1,3 @@ +[red]ERROR: Book is not released yet[/red] + +Your don't have access to the book you are trying to download because it's not yet released. diff --git a/audiobookdl/assets/errors/download_error.txt b/audiobookdl/assets/errors/download_error.txt new file mode 100644 index 0000000..d9194b1 --- /dev/null +++ b/audiobookdl/assets/errors/download_error.txt @@ -0,0 +1,4 @@ +[red]Download failed[/red] + +Expected status code {expected_status_code}, got {status_code} +Expected content-type {expected_content_type}, got {content_type} \ No newline at end of file diff --git a/audiobookdl/assets/errors/generic.txt b/audiobookdl/assets/errors/generic.txt new file mode 100644 index 0000000..8f222db --- /dev/null +++ b/audiobookdl/assets/errors/generic.txt @@ -0,0 +1,3 @@ +[red]{heading}[/red] + +{body} \ No newline at end of file diff --git a/audiobookdl/exceptions.py b/audiobookdl/exceptions.py index e039664..d7e0d43 100644 --- a/audiobookdl/exceptions.py +++ b/audiobookdl/exceptions.py @@ -41,5 +41,23 @@ class UserNotAuthorized(AudiobookDLException): class MissingBookAccess(AudiobookDLException): error_description = "book_access" +class BookNotFound(AudiobookDLException): + error_description = "book_not_found" + +class BookNotReleased(AudiobookDLException): + error_description = "book_not_released" + +class BookHasNoAudiobook(AudiobookDLException): + error_description = "book_has_no_audiobook" + class ConfigNotFound(AudiobookDLException): error_description = "config_not_found" + +class GenericAudiobookDLException(AudiobookDLException): + error_description: str = "generic" + + def __init__(self, heading: str, body: Optional[int] = None) -> None: + self.data = {'heading': heading, 'body': body if body else ""} + +class DownloadError(AudiobookDLException): + error_description: str = "download_error" \ No newline at end of file From 0c50ab91ffc0e37166d461c0e5f57785012505b5 Mon Sep 17 00:00:00 2001 From: Frederik Kriewitz Date: Wed, 27 Dec 2023 18:31:56 +0100 Subject: [PATCH 2/3] download: support status code and content type checking --- audiobookdl/output/download.py | 14 +++++++++++++- audiobookdl/utils/audiobook.py | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/audiobookdl/output/download.py b/audiobookdl/output/download.py index d5a5f99..a1d07e6 100644 --- a/audiobookdl/output/download.py +++ b/audiobookdl/output/download.py @@ -1,5 +1,5 @@ from audiobookdl import AudiobookFile, Source, logging, Audiobook -from audiobookdl.exceptions import UserNotAuthorized, NoFilesFound +from audiobookdl.exceptions import UserNotAuthorized, NoFilesFound, DownloadError from . import metadata, output, encryption import os @@ -164,7 +164,19 @@ def download_file(args: Tuple[Audiobook, str, int, Any]) -> str: filepath = create_filepath(audiobook, output_dir, index) logging.debug(f"Starting downloading file: {file.url}") request = audiobook.session.get(file.url, headers=file.headers, stream=True) + content_type: Optional[str] = request.headers.get("Content-type", None) + if ((file.expected_content_type and file.expected_content_type != content_type) + or (file.expected_status_code and file.expected_status_code != request.status_code)): + raise DownloadError(status_code=request.status_code, + content_type=content_type, + expected_status_code=file.expected_status_code, + expected_content_type=file.expected_content_type, + ) total_filesize = int(request.headers["Content-length"]) + if not file.expected_status_code: + logging.debug(f"expected_status_code not set by source, status-code is {request.status_code}, please update the source implementation") + if not file.expected_content_type: + logging.debug(f"expected_content_type not set by source, content-type is {content_type}, please update the source implementation") # Download file with open(filepath, "wb") as f: for chunk in request.iter_content(chunk_size=1024): diff --git a/audiobookdl/utils/audiobook.py b/audiobookdl/utils/audiobook.py index d20bd28..04aeb31 100644 --- a/audiobookdl/utils/audiobook.py +++ b/audiobookdl/utils/audiobook.py @@ -41,7 +41,10 @@ class AudiobookFile: headers: Dict[str, str] = Factory(dict) # Encryption method encryption_method: Optional[AudiobookFileEncryption] = None - + # Expected content-type of the download request + expected_content_type: Optional[str] = None + # Expected status code of the download request + expected_status_code: Optional[int] = None @define From 3a5daed54c32c1119740de434a24341f50cd68bb Mon Sep 17 00:00:00 2001 From: Frederik Kriewitz Date: Thu, 28 Dec 2023 11:05:32 +0100 Subject: [PATCH 3/3] fix GenericAudiobookDLException typing --- audiobookdl/exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/audiobookdl/exceptions.py b/audiobookdl/exceptions.py index d7e0d43..e9add22 100644 --- a/audiobookdl/exceptions.py +++ b/audiobookdl/exceptions.py @@ -56,7 +56,7 @@ class ConfigNotFound(AudiobookDLException): class GenericAudiobookDLException(AudiobookDLException): error_description: str = "generic" - def __init__(self, heading: str, body: Optional[int] = None) -> None: + def __init__(self, heading: str, body: Optional[str] = None) -> None: self.data = {'heading': heading, 'body': body if body else ""} class DownloadError(AudiobookDLException):