Add output location template argument

This commit is contained in:
Joakim Holm 2023-04-07 21:43:21 +02:00
parent d386cdcd88
commit aa1eacfd43
4 changed files with 28 additions and 9 deletions

View File

@ -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)

View File

@ -34,4 +34,10 @@ def parse_arguments():
dest = "password",
)
# Outputs
parser.add_argument(
'-o',
'--output',
help = "Output destination",
dest = "output"
)
return parser.parse_args()

View File

@ -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:

View File

@ -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: