2022-05-09 21:07:37 +02:00
|
|
|
import re
|
|
|
|
from pathlib import Path
|
2021-12-22 10:10:37 +01:00
|
|
|
from zipfile import ZipFile
|
2022-05-09 21:07:37 +02:00
|
|
|
|
2021-12-22 10:10:37 +01:00
|
|
|
|
2022-05-16 16:09:17 +02:00
|
|
|
# create an archive of the chapter images
|
2022-05-19 00:06:35 +02:00
|
|
|
def make_archive(chapter_path: Path, file_format: str) -> None:
|
2022-05-04 19:17:12 +02: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
|
|
|
|
zip_path.rename(zip_path.with_suffix(file_format))
|
|
|
|
except:
|
|
|
|
raise IOError
|
2021-12-22 10:10:37 +01:00
|
|
|
|
2022-05-10 21:59:11 +02:00
|
|
|
|
2022-05-19 00:06:35 +02:00
|
|
|
def make_pdf(chapter_path: Path) -> None:
|
2022-05-16 16:09:17 +02:00
|
|
|
try:
|
|
|
|
import img2pdf
|
|
|
|
except:
|
|
|
|
print("Cant import img2pdf. Please install it first")
|
|
|
|
raise ImportError
|
2021-12-22 10:10:37 +01:00
|
|
|
|
2022-05-16 16:09:17 +02:00
|
|
|
pdf_path = Path(f"{chapter_path}.pdf")
|
|
|
|
images = []
|
|
|
|
for file in chapter_path.iterdir():
|
|
|
|
images.append(str(file))
|
|
|
|
try:
|
|
|
|
pdf_path.write_bytes(img2pdf.convert(images))
|
|
|
|
except:
|
|
|
|
print("ERR: Can't create '.pdf' archive")
|
|
|
|
raise IOError
|
2021-12-22 10:10:37 +01:00
|
|
|
|
|
|
|
|
2022-05-09 15:42:13 +02:00
|
|
|
# create a list of chapters
|
2022-05-19 00:06:35 +02:00
|
|
|
def get_chapter_list(chapters: str, available_chapters: list = None) -> list:
|
2022-05-04 19:17:12 +02:00
|
|
|
chapter_list = []
|
|
|
|
for chapter in chapters.split(","):
|
2022-05-13 22:34:25 +02:00
|
|
|
# check if chapter list is with volumes and ranges
|
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-05-19 00:06:35 +02:00
|
|
|
lower_num = chapter.split("-")[0].split(":")
|
|
|
|
upper_num = chapter.split("-")[1].split(":")
|
|
|
|
vol = lower_num[0]
|
|
|
|
chap_beg = int(lower_num[1])
|
|
|
|
chap_end = int(upper_num[1])
|
2022-05-17 13:16:29 +02:00
|
|
|
# generate range inbetween start and end --> 1:1-1:3 == 1:1,1:2,1:3
|
2022-05-19 00:06:35 +02:00
|
|
|
for chap in range(chap_beg, chap_end + 1):
|
|
|
|
chapter_list.append(str(f"{vol}:{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-05-19 00:06:35 +02:00
|
|
|
lower_num = int(chapter.split("-")[0])
|
|
|
|
upper_num = int(chapter.split("-")[1])
|
2022-05-13 22:34:25 +02:00
|
|
|
# generate range inbetween start and end --> 1-3 == 1,2,3
|
2022-05-19 00:06:35 +02:00
|
|
|
for chap in range(lower_num, upper_num + 1):
|
|
|
|
chapter_list.append(str(chap))
|
|
|
|
# check if full volume should be downloaded
|
|
|
|
elif ":" in chapter:
|
|
|
|
vol = chapter.split(":")[0]
|
|
|
|
chap = chapter.split(":")[1]
|
|
|
|
# select all chapters from the volume --> 1: == 1:1,1:2,1:3...
|
|
|
|
if vol and not chap:
|
|
|
|
regex = re.compile(f"{vol}:[0-9]{{1,4}}")
|
|
|
|
vol_list = [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)
|
2021-12-22 10:10:37 +01:00
|
|
|
|
2022-05-04 19:17:12 +02:00
|
|
|
return chapter_list
|
2021-12-22 10:10:37 +01:00
|
|
|
|
|
|
|
|
2022-05-09 15:42:13 +02:00
|
|
|
# remove illegal characters etc
|
2022-05-19 00:06:35 +02:00
|
|
|
def fix_name(filename: str) -> str:
|
2022-05-04 19:17:12 +02:00
|
|
|
# remove illegal characters
|
2022-05-17 22:10:16 +02:00
|
|
|
filename = re.sub(r'[/\\<>:;|?*!@"]', "", filename)
|
2022-05-09 15:42:13 +02:00
|
|
|
# remove multiple dots
|
2022-05-17 22:10:16 +02:00
|
|
|
filename = re.sub(r"([.]{2,})", ".", filename)
|
2022-05-09 15:42:13 +02:00
|
|
|
# remove dot(s) at the beginning and end of the filename
|
2022-05-17 22:10:16 +02:00
|
|
|
filename = re.sub(r"(^[.]+)|([.]+$)", "", filename)
|
2022-05-09 15:42:13 +02:00
|
|
|
# remove trailing and beginning spaces
|
|
|
|
filename = re.sub("([ \t]+$)|(^[ \t]+)", "", filename)
|
2021-12-22 10:10:37 +01:00
|
|
|
|
2022-05-09 15:42:13 +02:00
|
|
|
return filename
|
2021-12-22 10:10:37 +01:00
|
|
|
|
|
|
|
|
2022-05-09 15:42:13 +02:00
|
|
|
# create name for chapter
|
2022-05-19 00:06:35 +02:00
|
|
|
def get_filename(
|
|
|
|
chapter_name: str, chapter_vol: str, chapter_num: str, forcevol: bool
|
|
|
|
) -> str:
|
2022-05-13 22:34:25 +02:00
|
|
|
# if chapter is a oneshot
|
2022-05-04 19:17:12 +02:00
|
|
|
if chapter_name == "Oneshot" or chapter_num == "Oneshot":
|
2022-05-13 22:34:25 +02:00
|
|
|
return "Oneshot"
|
|
|
|
# if the chapter has no name
|
|
|
|
if not chapter_name:
|
|
|
|
return (
|
|
|
|
f"Vol. {chapter_vol} Ch. {chapter_num}"
|
|
|
|
if forcevol
|
|
|
|
else f"Ch. {chapter_num}"
|
|
|
|
)
|
|
|
|
# if the chapter has a name
|
|
|
|
# return with volume if option is set, else just the chapter num and name
|
|
|
|
return (
|
|
|
|
f"Vol. {chapter_vol} Ch. {chapter_num} - {chapter_name}"
|
|
|
|
if forcevol
|
|
|
|
else f"Ch. {chapter_num} - {chapter_name}"
|
|
|
|
)
|
2022-05-13 16:43:50 +02:00
|
|
|
|
|
|
|
|
2022-05-19 00:06:35 +02:00
|
|
|
def progress_bar(progress: float, total: float) -> None:
|
2022-05-13 16:43:50 +02:00
|
|
|
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
|
|
|
|
print(f"\r❙{full_bar}❙ 100%", end="\n")
|
2022-05-13 16:43:50 +02:00
|
|
|
else:
|
|
|
|
print(f"\r❙{bar_texture}{whitespace_texture}❙ {percent}%", end="\r")
|