Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download check #80

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions audiobookdl/assets/errors/book_has_no_audiobook.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[red]ERROR: Book has no audiobook[/red]

Only audiobooks are supported.
3 changes: 3 additions & 0 deletions audiobookdl/assets/errors/book_not_found.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[red]ERROR: Book doesn't exist[/red]

Please check the URL.
3 changes: 3 additions & 0 deletions audiobookdl/assets/errors/book_not_released.txt
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions audiobookdl/assets/errors/download_error.txt
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 3 additions & 0 deletions audiobookdl/assets/errors/generic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[red]{heading}[/red]

{body}
18 changes: 18 additions & 0 deletions audiobookdl/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[str] = None) -> None:
self.data = {'heading': heading, 'body': body if body else ""}

class DownloadError(AudiobookDLException):
error_description: str = "download_error"
14 changes: 13 additions & 1 deletion audiobookdl/output/download.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion audiobookdl/utils/audiobook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading