diff --git a/grawlix/__main__.py b/grawlix/__main__.py index 70f2b52..f56577d 100644 --- a/grawlix/__main__.py +++ b/grawlix/__main__.py @@ -54,24 +54,27 @@ def main() -> None: result = source.download(url) if isinstance(result, Book): with logging.progress(result.metadata.title, source.name) as progress: - download_with_progress(result, progress) + template = args.output or "{title}.{ext}" + download_with_progress(result, progress, template) elif isinstance(result, Series): with logging.progress(result.title, source.name, len(result.book_ids)) as progress: for book_id in result.book_ids: book = source.download_book_from_id(book_id) - download_with_progress(book, progress) + template = args.output or "{series}/{title}.{ext}" + download_with_progress(book, progress, template) -def download_with_progress(book: Book, progress: Progress): +def download_with_progress(book: Book, progress: Progress, template: str): """ Download book with progress bar in cli :param book: Book to download :param progress: Progress object + :param template: Output template """ task = logging.add_book(progress, book) update_function = partial(progress.advance, task) - download_book(book, update_function) + download_book(book, update_function, template) progress.advance(task, 1) diff --git a/grawlix/arguments.py b/grawlix/arguments.py index 26bb922..a941729 100644 --- a/grawlix/arguments.py +++ b/grawlix/arguments.py @@ -34,4 +34,10 @@ def parse_arguments(): dest = "password", ) # Outputs + parser.add_argument( + '-o', + '--output', + help = "Output destination", + dest = "output" + ) return parser.parse_args() diff --git a/grawlix/book.py b/grawlix/book.py index 618ccc2..9177769 100644 --- a/grawlix/book.py +++ b/grawlix/book.py @@ -10,6 +10,14 @@ class Metadata: publisher: Optional[str] = None identifier: Optional[str] = None + def as_dict(self) -> dict: + return { + "title": self.title, + "series": self.series or "UNKNOWN", + "publisher": self.publisher or "UNKNOWN", + "identifier": self.identifier or "UNKNOWN", + } + @dataclass(slots=True) class OnlineFile: diff --git a/grawlix/output/__init__.py b/grawlix/output/__init__.py index f69e1c1..aecb7f0 100644 --- a/grawlix/output/__init__.py +++ b/grawlix/output/__init__.py @@ -9,14 +9,14 @@ from typing import Callable from pathlib import Path import os -def download_book(book: Book, update_func: Callable) -> None: +def download_book(book: Book, update_func: Callable, template: str) -> None: """ Download and write book to disk :param book: Book to download """ output_format = get_default_format(book.data) - location = format_output_location(book, output_format) + location = format_output_location(book, output_format, template) parent = Path(location).parent if not parent.exists(): os.makedirs(parent) @@ -28,15 +28,17 @@ def download_book(book: Book, update_func: Callable) -> None: raise NotImplementedError -def format_output_location(book: Book, output_format: OutputFormat) -> str: +def format_output_location(book: Book, output_format: OutputFormat, template: str) -> str: """ Create path to output location of book :param book: Book to download :param output_format: Output format of book + :param template: Template for output path + :returns: Output path """ - series = book.metadata.series or "UNKNOWN" - return f"{series}/{book.metadata.title}.{output_format.extension}" + values = book.metadata.as_dict() + return template.format(**values, ext = output_format.extension) def get_default_format(bookdata: BookData) -> OutputFormat: