add --lean flag and change verbosity configuration

This commit is contained in:
Ivan Schaller 2022-06-26 15:23:19 +02:00
parent 6b3eceae93
commit 26d8043fe4
8 changed files with 119 additions and 82 deletions

View file

@ -15,12 +15,12 @@ class Mangadex:
img_base_url = "https://uploads.mangadex.org"
# get infos to initiate class
def __init__(self, url_uuid: str, language: str, forcevol: bool, verbose: bool):
def __init__(self, url_uuid: str, language: str, forcevol: bool, verbosity: int):
# static info
self.url_uuid = url_uuid
self.language = language
self.forcevol = forcevol
self.verbose = verbose
self.verbosity = verbosity
# api stuff
self.api_content_ratings = "contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&contentRating[]=pornographic"
@ -36,7 +36,7 @@ class Mangadex:
# make initial request
def get_manga_data(self) -> requests.Response:
if self.verbose:
if self.verbosity >= 2:
print(f"INFO: Getting manga data for: {self.manga_uuid}")
counter = 1
while counter <= 3:
@ -76,7 +76,7 @@ class Mangadex:
# get the title of the manga (and fix the filename)
def get_manga_title(self) -> str:
if self.verbose:
if self.verbosity >= 2:
print(f"INFO: Getting manga title for: {self.manga_uuid}")
manga_data = self.manga_data.json()
try:
@ -95,7 +95,7 @@ class Mangadex:
# check if chapters are available in requested language
def check_chapter_lang(self) -> int:
if self.verbose:
if self.verbosity >= 2:
print(
f"INFO: Checking for chapters in specified language for: {self.manga_uuid}"
)
@ -118,7 +118,7 @@ class Mangadex:
# get chapter data like name, uuid etc
def get_chapter_data(self) -> dict:
if self.verbose:
if self.verbosity >= 2:
print(f"INFO: Getting chapter data for: {self.manga_uuid}")
api_sorting = "order[chapter]=asc&order[volume]=asc"
# check for chapters in specified lang
@ -177,7 +177,7 @@ class Mangadex:
# get images for the chapter (mangadex@home)
def get_chapter_images(self, chapter: str, wait_time: float) -> list:
if self.verbose:
if self.verbosity >= 2:
print(f"INFO: Getting chapter images for: {self.manga_uuid}")
athome_url = f"{self.api_base_url}/at-home/server"
chapter_uuid = self.manga_chapter_data[chapter][0]
@ -224,7 +224,7 @@ class Mangadex:
# create list of chapters
def create_chapter_list(self) -> list:
if self.verbose:
if self.verbosity >= 2:
print(f"INFO: Creating chapter list for: {self.manga_uuid}")
chapter_list = []
for chapter in self.manga_chapter_data.items():
@ -240,7 +240,7 @@ class Mangadex:
# create easy to access chapter infos
def get_chapter_infos(self, chapter: str) -> dict:
if self.verbose:
if self.verbosity >= 3:
print(
f"INFO: Getting chapter infos for: {self.manga_chapter_data[chapter][0]}"
)

View file

@ -23,7 +23,7 @@ class MangaDLP:
:param forcevol: Force naming of volumes. Useful for mangas where chapters reset each volume
:param download_path: Download path. Defaults to '<script_dir>/downloads'
:param download_wait: Time to wait for each picture to download in seconds
:param verbose: If verbose logging is enabled
:param verbosity: Verbosity of the output
:return: Nothing. Just the files
"""
@ -38,7 +38,7 @@ class MangaDLP:
forcevol: bool = False,
download_path: str = "downloads",
download_wait: float = 0.5,
verbose: bool = False,
verbosity: int = 0,
) -> None:
# init parameters
self.url_uuid = url_uuid
@ -49,7 +49,7 @@ class MangaDLP:
self.forcevol = forcevol
self.download_path = download_path
self.download_wait = download_wait
self.verbose = verbose
self.verbosity = verbosity
# prepare everything
self._prepare()
@ -63,7 +63,7 @@ class MangaDLP:
# init api
self.api_used = self.check_api(self.url_uuid)
self.api = self.api_used(
self.url_uuid, self.language, self.forcevol, self.verbose
self.url_uuid, self.language, self.forcevol, self.verbosity
)
# get manga title and uuid
self.manga_uuid = self.api.manga_uuid
@ -125,12 +125,15 @@ class MangaDLP:
skipped_chapters: list[Any] = []
error_chapters: list[Any] = []
# show infos
print_divider = "========================================="
print(f"\n{print_divider}")
print(f"INFO: Manga Name: {self.manga_title}")
print(f"INFO: Manga UUID: {self.manga_uuid}")
print(f"INFO: Total chapters: {len(self.manga_chapter_list)}")
# show infos
if self.verbosity == 1:
print(f"INFO: Manga Name: {self.manga_title}")
else:
print(f"{print_divider}")
print(f"INFO: Manga Name: {self.manga_title}")
print(f"INFO: Manga UUID: {self.manga_uuid}")
print(f"INFO: Total chapters: {len(self.manga_chapter_list)}")
# list chapters if list_chapters is true
if self.list_chapters:
@ -147,8 +150,11 @@ class MangaDLP:
)
# show chapters to download
print(f"INFO: Chapters selected:\n{', '.join(chapters_to_download)}")
print(f"{print_divider}\n")
if self.verbosity == 1:
print(f"INFO: Chapters selected: {', '.join(chapters_to_download)}")
else:
print(f"INFO: Chapters selected:\n{', '.join(chapters_to_download)}")
print(f"{print_divider}")
# create manga folder
self.manga_path.mkdir(parents=True, exist_ok=True)
@ -171,17 +177,25 @@ class MangaDLP:
print("INFO: Done with chapter\n")
# done with manga
print(f"{print_divider}")
if self.verbosity != 1:
print(f"{print_divider}")
print(f"INFO: Done with manga: {self.manga_title}")
# filter skipped list
skipped_chapters = list(filter(None, skipped_chapters))
if len(skipped_chapters) >= 1:
print(f"INFO: Skipped chapters:\n{', '.join(skipped_chapters)}")
if self.verbosity == 1:
print(f"INFO: Skipped chapters: {', '.join(skipped_chapters)}")
else:
print(f"INFO: Skipped chapters:\n{', '.join(skipped_chapters)}")
# filter error list
error_chapters = list(filter(None, error_chapters))
if len(error_chapters) >= 1:
print(f"INFO: Chapters with errors:\n{', '.join(error_chapters)}")
print(f"{print_divider}\n")
if self.verbosity == 1:
print(f"INFO: Chapters with errors: {', '.join(error_chapters)}")
else:
print(f"INFO: Chapters with errors:\n{', '.join(error_chapters)}")
if self.verbosity != 1:
print(f"{print_divider}\n")
# once called per chapter
def get_chapter(self, chapter: str) -> dict:
@ -225,7 +239,8 @@ class MangaDLP:
# check if chapter already exists
# check for folder, if file format is an empty string
if chapter_archive_path.exists():
print(f"INFO: '{chapter_archive_path}' already exists. Skipping")
if self.verbosity != 1:
print(f"INFO: '{chapter_archive_path}' already exists. Skipping")
# add to skipped chapters list
return (
{
@ -240,7 +255,7 @@ class MangaDLP:
chapter_path.mkdir(parents=True, exist_ok=True)
# verbose log
if self.verbose:
if self.verbosity >= 2:
print(f"INFO: Chapter UUID: {chapter_infos['uuid']}")
print(f"INFO: Filename: '{chapter_archive_path.name}'\n")
print(f"INFO: File path: '{chapter_archive_path}'\n")
@ -252,7 +267,7 @@ class MangaDLP:
# download images
try:
downloader.download_chapter(
chapter_image_urls, chapter_path, self.download_wait, self.verbose
chapter_image_urls, chapter_path, self.download_wait, self.verbosity
)
except KeyboardInterrupt:
print("ERR: Stopping")

View file

@ -14,7 +14,7 @@ def download_chapter(
image_urls: list,
chapter_path: Union[str, Path],
download_wait: float,
verbose: bool,
verbosity: int,
) -> None:
total_img = len(image_urls)
for image_num, image in enumerate(image_urls, 1):
@ -22,11 +22,11 @@ def download_chapter(
image_suffix = str(Path(image).suffix) or ".png"
# set image path
image_path = Path(f"{chapter_path}/{image_num:03d}{image_suffix}")
# show progress bar if verbose logging is not active
if verbose:
print(f"INFO: Downloading image {image_num}/{total_img}")
else:
# show progress bar or progress by image for verbose
if verbosity == 0:
utils.progress_bar(image_num, total_img)
elif verbosity >= 2:
print(f"INFO: Downloading image {image_num}/{total_img}")
counter = 1
while counter <= 3:

View file

@ -48,7 +48,7 @@ def call_app(args):
args.forcevol,
args.path,
args.wait,
args.verbose,
args.verbosity,
)
mdlp.get_manga()
@ -95,8 +95,10 @@ def get_args():
parser = argparse.ArgumentParser(
description="Script to download mangas from various sites"
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
action = parser.add_mutually_exclusive_group(required=True)
verbosity = parser.add_mutually_exclusive_group(required=False)
action.add_argument(
"-u",
"--url",
"--uuid",
@ -105,14 +107,14 @@ def get_args():
help="URL or UUID of the manga",
action="store",
)
group.add_argument(
action.add_argument(
"--read",
dest="read",
required=False,
help="Path of file with manga links to download. One per line",
action="store",
)
group.add_argument(
action.add_argument(
"-v",
"--version",
dest="version",
@ -176,12 +178,32 @@ def get_args():
default=0.5,
help="Time to wait for each picture to download in seconds(float). Defaults 0.5",
)
parser.add_argument(
verbosity.add_argument(
"--lean",
dest="verbosity",
required=False,
help="Lean logging. Defaults to false",
action="store_const",
const=1,
default=0,
)
verbosity.add_argument(
"--verbose",
dest="verbose",
dest="verbosity",
required=False,
help="Verbose logging. Defaults to false",
action="store_true",
action="store_const",
const=2,
default=0,
)
verbosity.add_argument(
"--debug",
dest="verbosity",
required=False,
help="Lean logging. Defaults to false",
action="store_const",
const=3,
default=0,
)
# parser.print_help()

View file

@ -57,7 +57,7 @@ def test_chapter_list_full():
forcevol=True,
download_path="tests",
download_wait=2,
verbose=True,
verbosity=3,
)
chap_list = utils.get_chapter_list("1:1,1:2,1:4-1:7,2:", mdlp.manga_chapter_list)
assert chap_list == [

View file

@ -13,7 +13,7 @@ def test_read_and_url():
chapters = "1"
file_format = "cbz"
download_path = "tests"
command_args = f"-u {url_uuid} --read {link_file} -l {language} -c {chapters} --path {download_path} --format {file_format} --verbose"
command_args = f"-u {url_uuid} --read {link_file} -l {language} -c {chapters} --path {download_path} --format {file_format} --debug"
script_path = "manga-dlp.py"
assert os.system(f"python3 {script_path} {command_args}") != 0
@ -25,7 +25,7 @@ def test_no_read_and_url():
chapters = "1"
file_format = "cbz"
download_path = "tests"
command_args = f"-l {language} -c {chapters} --path {download_path} --format {file_format} --verbose"
command_args = f"-l {language} -c {chapters} --path {download_path} --format {file_format} --debug"
script_path = "manga-dlp.py"
assert os.system(f"python3 {script_path} {command_args}") != 0
@ -36,7 +36,7 @@ def test_no_chaps():
chapters = ""
file_format = "cbz"
download_path = "tests"
command_args = f"-u {url_uuid} -l {language} --path {download_path} --format {file_format} --verbose"
command_args = f"-u {url_uuid} -l {language} --path {download_path} --format {file_format} --debug"
script_path = "manga-dlp.py"
assert os.system(f"python3 {script_path} {command_args}") != 0
@ -47,7 +47,7 @@ def test_no_volume():
chapters = "1"
file_format = "cbz"
download_path = "tests"
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format {file_format} --verbose --forcevol"
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format {file_format} --debug --forcevol"
script_path = "manga-dlp.py"
assert os.system(f"python3 {script_path} {command_args}") != 0

View file

@ -8,8 +8,8 @@ def test_uuid_link():
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = 3
test = Mangadex(url_uuid, language, forcevol, verbosity)
assert test.manga_uuid == "a96676e5-8ae2-425e-b549-7f15dd34a6d8"
@ -18,8 +18,8 @@ def test_uuid_pure():
url_uuid = "a96676e5-8ae2-425e-b549-7f15dd34a6d8"
language = "en"
forcevol = False
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = 3
test = Mangadex(url_uuid, language, forcevol, verbosity)
assert test.manga_uuid == "a96676e5-8ae2-425e-b549-7f15dd34a6d8"
@ -28,10 +28,10 @@ def test_uuid_link_false():
url_uuid = "https://mangadex.org/title/a966-76e-5-8a-e2-42-5e-b-549-7f15dd-34a6d8/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
verbosity = 3
with pytest.raises(SystemExit) as e:
Mangadex(url_uuid, language, forcevol, verbose)
Mangadex(url_uuid, language, forcevol, verbosity)
assert e.type == SystemExit
assert e.value.code == 1
@ -40,8 +40,8 @@ def test_title():
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = 3
test = Mangadex(url_uuid, language, forcevol, verbosity)
assert test.manga_title == "Komi-san wa Komyushou Desu"
@ -50,8 +50,8 @@ def test_chapter_infos():
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = 3
test = Mangadex(url_uuid, language, forcevol, verbosity)
chapter_infos = test.get_chapter_infos("1")
chapter_uuid = chapter_infos["uuid"]
chapter_name = chapter_infos["name"]
@ -70,10 +70,10 @@ def test_non_existing_manga():
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-999999999999/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
verbosity = 3
with pytest.raises(SystemExit) as e:
Mangadex(url_uuid, language, forcevol, verbose)
Mangadex(url_uuid, language, forcevol, verbosity)
assert e.type == SystemExit
assert e.value.code == 1
@ -86,10 +86,10 @@ def test_api_failure(monkeypatch):
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
verbosity = 3
with pytest.raises(SystemExit) as e:
Mangadex(url_uuid, language, forcevol, verbose)
Mangadex(url_uuid, language, forcevol, verbosity)
assert e.type == SystemExit
assert e.value.code == 1
@ -98,8 +98,8 @@ def test_chapter_lang_en():
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = True
test = Mangadex(url_uuid, language, forcevol, 3)
assert test.check_chapter_lang() > 0
@ -108,11 +108,11 @@ def test_empty_chapter_lang():
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "ch"
forcevol = False
verbose = True
verbosity = 3
with pytest.raises(SystemExit) as e:
Mangadex(url_uuid, language, forcevol, verbose)
Mangadex(url_uuid, language, forcevol, verbose).check_chapter_lang()
Mangadex(url_uuid, language, forcevol, verbosity)
Mangadex(url_uuid, language, forcevol, verbosity).check_chapter_lang()
assert e.type == KeyError or e.type == SystemExit
assert e.value.code == 1
@ -121,10 +121,10 @@ def test_not_existing_lang():
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "zz"
forcevol = False
verbose = True
verbosity = 3
with pytest.raises(SystemExit) as e:
Mangadex(url_uuid, language, forcevol, verbose)
Mangadex(url_uuid, language, forcevol, verbosity)
assert e.type == SystemExit
assert e.value.code == 1
@ -135,8 +135,8 @@ def test_create_chapter_list():
)
language = "en"
forcevol = False
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = 3
test = Mangadex(url_uuid, language, forcevol, verbosity)
test_list = [
"1",
"2",
@ -170,8 +170,8 @@ def test_create_chapter_list_forcevol():
)
language = "en"
forcevol = True
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = 3
test = Mangadex(url_uuid, language, forcevol, verbosity)
test_list = [
"1:1",
"1:2",
@ -203,8 +203,8 @@ def test_get_chapter_images():
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = 3
test = Mangadex(url_uuid, language, forcevol, verbosity)
img_base_url = "https://uploads.mangadex.org"
chapter_hash = "0752bc5db298beff6b932b9151dd8437"
chapter_uuid = "e86ec2c4-c5e4-4710-bfaa-7604f00939c7"
@ -235,8 +235,8 @@ def test_get_chapter_images_error(monkeypatch):
url_uuid = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
language = "en"
forcevol = False
verbose = True
test = Mangadex(url_uuid, language, forcevol, verbose)
verbosity = 3
test = Mangadex(url_uuid, language, forcevol, verbosity)
chapter_num = "1"
monkeypatch.setattr(requests, "get", fail_url)

View file

@ -33,7 +33,7 @@ def test_full_api_mangadex(wait_20s):
forcevol=False,
download_path="tests",
download_wait=2,
verbose=True,
verbosity=3,
)
mdlp.get_manga()
@ -51,7 +51,7 @@ def test_full_with_input_cbz(wait_20s):
download_path = "tests"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
chapter_path = Path("tests/Shikimori's Not Just a Cutie/Ch. 1.cbz")
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format {file_format} --verbose --wait 2"
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format {file_format} --debug --wait 2"
script_path = "manga-dlp.py"
os.system(f"python3 {script_path} {command_args}")
@ -73,7 +73,7 @@ def test_full_with_input_pdf(wait_20s):
download_path = "tests"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
chapter_path = Path("tests/Shikimori's Not Just a Cutie/Ch. 1.pdf")
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format {file_format} --verbose --wait 2"
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format {file_format} --debug --wait 2"
script_path = "manga-dlp.py"
os.system(f"python3 {script_path} {command_args}")
@ -91,7 +91,7 @@ def test_full_with_input_folder(wait_20s):
download_path = "tests"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
chapter_path = Path("tests/Shikimori's Not Just a Cutie/Ch. 1")
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format '{file_format}' --verbose --wait 2"
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format '{file_format}' --debug --wait 2"
script_path = "manga-dlp.py"
os.system(f"python3 {script_path} {command_args}")
@ -109,7 +109,7 @@ def test_full_with_input_skip_cbz(wait_10s):
download_path = "tests"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
chapter_path = Path("tests/Shikimori's Not Just a Cutie/Ch. 1.cbz")
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format {file_format} --verbose --wait 2"
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format {file_format} --debug --wait 2"
script_path = "manga-dlp.py"
manga_path.mkdir(parents=True, exist_ok=True)
chapter_path.touch()
@ -129,7 +129,7 @@ def test_full_with_input_skip_folder(wait_10s):
download_path = "tests"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
chapter_path = Path("tests/Shikimori's Not Just a Cutie/Ch. 1")
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format '{file_format}' --verbose --wait 2"
command_args = f"-u {url_uuid} -l {language} -c {chapters} --path {download_path} --format '{file_format}' --debug --wait 2"
script_path = "manga-dlp.py"
chapter_path.mkdir(parents=True, exist_ok=True)
@ -154,7 +154,7 @@ def test_full_with_read_cbz(wait_20s):
download_path = "tests"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
chapter_path = Path("tests/Shikimori's Not Just a Cutie/Ch. 1.cbz")
command_args = f"--read {str(url_list)} -l {language} -c {chapters} --path {download_path} --format {file_format} --verbose --wait 2"
command_args = f"--read {str(url_list)} -l {language} -c {chapters} --path {download_path} --format {file_format} --debug --wait 2"
script_path = "manga-dlp.py"
url_list.write_text(
"https://mangadex.org/title/0aea9f43-d4a9-4bf7-bebc-550a512f9b95/shikimori-s-not-just-a-cutie"
@ -176,7 +176,7 @@ def test_full_with_read_skip_cbz(wait_10s):
download_path = "tests"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
chapter_path = Path("tests/Shikimori's Not Just a Cutie/Ch. 1.cbz")
command_args = f"--read {str(url_list)} -l {language} -c {chapters} --path {download_path} --format {file_format} --verbose --wait 2"
command_args = f"--read {str(url_list)} -l {language} -c {chapters} --path {download_path} --format {file_format} --debug --wait 2"
script_path = "manga-dlp.py"
manga_path.mkdir(parents=True, exist_ok=True)
chapter_path.touch()