mirror of
https://github.com/jo1gi/grawlix.git
synced 2026-06-06 22:44:58 -06:00
Add output location template argument
This commit is contained in:
parent
d386cdcd88
commit
aa1eacfd43
@ -54,24 +54,27 @@ def main() -> None:
|
|||||||
result = source.download(url)
|
result = source.download(url)
|
||||||
if isinstance(result, Book):
|
if isinstance(result, Book):
|
||||||
with logging.progress(result.metadata.title, source.name) as progress:
|
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):
|
elif isinstance(result, Series):
|
||||||
with logging.progress(result.title, source.name, len(result.book_ids)) as progress:
|
with logging.progress(result.title, source.name, len(result.book_ids)) as progress:
|
||||||
for book_id in result.book_ids:
|
for book_id in result.book_ids:
|
||||||
book = source.download_book_from_id(book_id)
|
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
|
Download book with progress bar in cli
|
||||||
|
|
||||||
:param book: Book to download
|
:param book: Book to download
|
||||||
:param progress: Progress object
|
:param progress: Progress object
|
||||||
|
:param template: Output template
|
||||||
"""
|
"""
|
||||||
task = logging.add_book(progress, book)
|
task = logging.add_book(progress, book)
|
||||||
update_function = partial(progress.advance, task)
|
update_function = partial(progress.advance, task)
|
||||||
download_book(book, update_function)
|
download_book(book, update_function, template)
|
||||||
progress.advance(task, 1)
|
progress.advance(task, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -34,4 +34,10 @@ def parse_arguments():
|
|||||||
dest = "password",
|
dest = "password",
|
||||||
)
|
)
|
||||||
# Outputs
|
# Outputs
|
||||||
|
parser.add_argument(
|
||||||
|
'-o',
|
||||||
|
'--output',
|
||||||
|
help = "Output destination",
|
||||||
|
dest = "output"
|
||||||
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|||||||
@ -10,6 +10,14 @@ class Metadata:
|
|||||||
publisher: Optional[str] = None
|
publisher: Optional[str] = None
|
||||||
identifier: 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)
|
@dataclass(slots=True)
|
||||||
class OnlineFile:
|
class OnlineFile:
|
||||||
|
|||||||
@ -9,14 +9,14 @@ from typing import Callable
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import os
|
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
|
Download and write book to disk
|
||||||
|
|
||||||
:param book: Book to download
|
:param book: Book to download
|
||||||
"""
|
"""
|
||||||
output_format = get_default_format(book.data)
|
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
|
parent = Path(location).parent
|
||||||
if not parent.exists():
|
if not parent.exists():
|
||||||
os.makedirs(parent)
|
os.makedirs(parent)
|
||||||
@ -28,15 +28,17 @@ def download_book(book: Book, update_func: Callable) -> None:
|
|||||||
raise NotImplementedError
|
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
|
Create path to output location of book
|
||||||
|
|
||||||
:param book: Book to download
|
:param book: Book to download
|
||||||
:param output_format: Output format of book
|
:param output_format: Output format of book
|
||||||
|
:param template: Template for output path
|
||||||
|
:returns: Output path
|
||||||
"""
|
"""
|
||||||
series = book.metadata.series or "UNKNOWN"
|
values = book.metadata.as_dict()
|
||||||
return f"{series}/{book.metadata.title}.{output_format.extension}"
|
return template.format(**values, ext = output_format.extension)
|
||||||
|
|
||||||
|
|
||||||
def get_default_format(bookdata: BookData) -> OutputFormat:
|
def get_default_format(bookdata: BookData) -> OutputFormat:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user