manga-dlp/mangadlp/utils.py

177 lines
6.2 KiB
Python
Raw Normal View History

import re
2022-07-06 22:19:40 +02:00
from datetime import datetime
from pathlib import Path
from typing import Any, List
from zipfile import ZipFile
from loguru import logger as log
2022-07-21 20:39:56 +02:00
2022-05-16 16:09:17 +02:00
# create an archive of the chapter images
def make_archive(chapter_path: Path, file_format: str) -> None:
2023-02-13 23:17:52 +01:00
zip_path = Path(f"{chapter_path}.zip")
2022-05-16 16:09:17 +02:00
try:
# create zip
with ZipFile(zip_path, "w") as zipfile:
for file in chapter_path.iterdir():
zipfile.write(file, file.name)
# rename zip to file format requested
2023-02-06 14:46:58 +01:00
zip_path.replace(zip_path.with_suffix(file_format))
2022-07-22 21:11:01 +02:00
except Exception as exc:
2023-02-11 15:18:56 +01:00
log.error(f"Can't create '{file_format}' archive")
2023-02-11 13:32:53 +01:00
raise exc
def make_pdf(chapter_path: Path) -> None:
2022-05-16 16:09:17 +02:00
try:
import img2pdf # pylint: disable=import-outside-toplevel # pyright:ignore
2022-07-22 21:11:01 +02:00
except Exception as exc:
2022-07-15 12:49:49 +02:00
log.error("Cant import img2pdf. Please install it first")
2023-02-11 13:32:53 +01:00
raise exc
2023-02-13 23:17:52 +01:00
pdf_path = Path(f"{chapter_path}.pdf")
images: list[str] = []
2022-05-16 16:09:17 +02:00
for file in chapter_path.iterdir():
images.append(str(file))
try:
pdf_path.write_bytes(img2pdf.convert(images)) # pyright:ignore
2022-07-22 21:11:01 +02:00
except Exception as exc:
2022-07-15 12:49:49 +02:00
log.error("Can't create '.pdf' archive")
2023-02-11 13:32:53 +01:00
raise exc
# create a list of chapters
def get_chapter_list(chapters: str, available_chapters: List[str]) -> List[str]:
2022-06-20 17:46:04 +02:00
# check if there are available chapter
chapter_list: list[str] = []
2022-05-04 19:17:12 +02:00
for chapter in chapters.split(","):
2022-06-20 17:46:04 +02:00
# check if chapter list is with volumes and ranges (forcevol)
2022-05-04 19:17:12 +02:00
if "-" in chapter and ":" in chapter:
2022-05-13 22:34:25 +02:00
# split chapters and volumes apart for list generation
2022-06-20 17:46:04 +02:00
lower_num_fv: list[str] = chapter.split("-")[0].split(":")
upper_num_fv: list[str] = chapter.split("-")[1].split(":")
vol_fv: str = lower_num_fv[0]
chap_beg_fv: int = int(lower_num_fv[1])
chap_end_fv: int = int(upper_num_fv[1])
# generate range inbetween start and end --> 1:1-1:3 == 1:1,1:2,1:3
2022-06-20 17:46:04 +02:00
for chap in range(chap_beg_fv, chap_end_fv + 1):
chapter_list.append(str(f"{vol_fv}:{chap}"))
2022-05-13 22:34:25 +02:00
# no volumes, just chapter ranges
2022-05-04 19:17:12 +02:00
elif "-" in chapter:
2022-06-20 17:46:04 +02:00
lower_num: int = int(chapter.split("-")[0])
upper_num: int = int(chapter.split("-")[1])
2022-05-13 22:34:25 +02:00
# generate range inbetween start and end --> 1-3 == 1,2,3
for chap in range(lower_num, upper_num + 1):
chapter_list.append(str(chap))
# check if full volume should be downloaded
elif ":" in chapter:
2022-06-20 17:46:04 +02:00
vol_num: str = chapter.split(":")[0]
chap_num: str = chapter.split(":")[1]
# select all chapters from the volume --> 1: == 1:1,1:2,1:3...
2022-06-20 17:46:04 +02:00
if vol_num and not chap_num:
regex: Any = re.compile(f"{vol_num}:[0-9]{{1,4}}")
vol_list: list[str] = [n for n in available_chapters if regex.match(n)]
chapter_list.extend(vol_list)
else:
chapter_list.append(chapter)
2022-05-13 22:34:25 +02:00
# single chapters without a range given
2022-05-04 19:17:12 +02:00
else:
chapter_list.append(chapter)
2022-05-04 19:17:12 +02:00
return chapter_list
# remove illegal characters etc
def fix_name(filename: str) -> str:
2023-01-21 15:36:49 +01:00
filename = filename.encode(encoding="utf8", errors="ignore").decode(encoding="utf8")
2022-05-04 19:17:12 +02:00
# remove illegal characters
2022-05-17 22:10:16 +02:00
filename = re.sub(r'[/\\<>:;|?*!@"]', "", filename)
# remove multiple dots
2022-05-17 22:10:16 +02:00
filename = re.sub(r"([.]{2,})", ".", filename)
# remove dot(s) at the beginning and end of the filename
2022-05-17 22:10:16 +02:00
filename = re.sub(r"(^[.]+)|([.]+$)", "", filename)
# remove trailing and beginning spaces
filename = re.sub("([ \t]+$)|(^[ \t]+)", "", filename)
2023-02-13 19:15:27 +01:00
log.debug(f"Input name='{filename}', Output name='{filename}'")
return filename
# create name for chapter
def get_filename(
manga_title: str,
chapter_name: str,
chapter_vol: str,
chapter_num: str,
forcevol: bool,
name_format: str,
name_format_none: str,
) -> str:
# try to apply the custom format
if name_format != "{default}":
log.debug(f"Using custom name format: '{name_format}'")
try:
filename = name_format.format(
manga_title=manga_title or name_format_none,
chapter_name=chapter_name or name_format_none,
chapter_vol=chapter_vol or name_format_none,
chapter_num=chapter_num or name_format_none,
)
except Exception:
log.warning("File format is not valid. Falling back to default")
else:
return filename
# set vol to 0 if none found
chapter_vol = chapter_vol or "0"
# use default format
log.debug("Using default name format")
2022-05-13 22:34:25 +02:00
# if chapter is a oneshot
if not chapter_num or "oneshot" in [chapter_name.lower(), chapter_num.lower()]:
2022-05-13 22:34:25 +02:00
return "Oneshot"
2022-05-13 22:34:25 +02:00
# if the chapter has no name
if not chapter_name and forcevol:
return f"Vol. {chapter_vol} Ch. {chapter_num}"
2022-05-13 22:34:25 +02:00
if not chapter_name:
return f"Ch. {chapter_num}"
2022-05-13 22:34:25 +02:00
# if the chapter has a name
# return with volume if option is set, else just the chapter num and name
if forcevol:
return f"Vol. {chapter_vol} Ch. {chapter_num} - {chapter_name}"
return f"Ch. {chapter_num} - {chapter_name}"
def get_file_format(file_format: str) -> str:
if not file_format:
return ""
if re.match(r"\.?[a-z0-9]+", file_format, flags=re.I):
if file_format[0] != ".":
file_format = f".{file_format}"
else:
log.error(f"Invalid file format: '{file_format}'")
raise ValueError
return file_format
def progress_bar(progress: float, total: float) -> None:
2022-12-30 01:46:53 +01:00
time = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
percent = int(progress / (int(total) / 100))
bar_length = 50
bar_progress = int(progress / (int(total) / bar_length))
bar_texture = "" * bar_progress
whitespace_texture = " " * (bar_length - bar_progress)
if progress == total:
2022-05-16 16:09:17 +02:00
full_bar = "" * bar_length
2022-12-30 01:46:53 +01:00
print(f"\r{time}{' '*6}| [BAR ] ❙{full_bar}❙ 100%", end="\n")
else:
2022-12-30 01:46:53 +01:00
print(
f"\r{time}{' '*6}| [BAR ] ❙{bar_texture}{whitespace_texture}{percent}%",
end="\r",
)