2022-05-09 21:07:37 +02:00
|
|
|
import re
|
2021-12-22 10:10:37 +01:00
|
|
|
import shutil
|
2022-05-09 21:07:37 +02:00
|
|
|
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-09 15:42:13 +02:00
|
|
|
# create a cbz archive
|
2022-05-13 22:34:25 +02:00
|
|
|
def make_archive(chapter_path, file_format):
|
|
|
|
# set manga format suffix
|
|
|
|
if file_format and "." not in file_format:
|
|
|
|
file_format = f".{file_format}"
|
2022-05-04 19:17:12 +02:00
|
|
|
image_folder = Path(chapter_path)
|
|
|
|
zip_path = Path(f"{chapter_path}.zip")
|
2022-05-10 21:59:11 +02:00
|
|
|
if not image_folder.exists():
|
|
|
|
print(f"ERR: Folder: {image_folder} does not exist")
|
|
|
|
return False
|
2022-05-04 19:17:12 +02:00
|
|
|
with ZipFile(f"{image_folder}.zip", "w") as zip_archive:
|
|
|
|
for file in image_folder.iterdir():
|
|
|
|
zip_archive.write(file, file.name)
|
2022-05-13 22:34:25 +02:00
|
|
|
zip_path.rename(zip_path.with_suffix(file_format))
|
2022-05-04 19:17:12 +02:00
|
|
|
shutil.rmtree(image_folder)
|
2021-12-22 10:10:37 +01:00
|
|
|
|
2022-05-10 21:59:11 +02:00
|
|
|
return True
|
|
|
|
|
2021-12-22 10:10:37 +01:00
|
|
|
|
2022-05-09 15:42:13 +02:00
|
|
|
# check if the file already exists
|
2022-05-13 22:34:25 +02:00
|
|
|
def check_existence(chapter_path, file_format):
|
|
|
|
# set manga format suffix
|
|
|
|
if file_format and "." not in file_format:
|
|
|
|
file_format = f".{file_format}"
|
|
|
|
# check for folder if no format is given (empty string)
|
|
|
|
# if no format is given, the folder will be overwritten if it exists
|
|
|
|
chapter_path = Path(chapter_path).with_suffix(file_format)
|
|
|
|
if chapter_path.exists():
|
2022-05-04 19:17:12 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2021-12-22 10:10:37 +01:00
|
|
|
|
|
|
|
|
2022-05-09 15:42:13 +02:00
|
|
|
# create a list of chapters
|
2021-12-22 10:10:37 +01:00
|
|
|
def get_chapter_list(chapters):
|
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-04 19:17:12 +02:00
|
|
|
lower = chapter.split("-")[0].split(":")
|
|
|
|
upper = chapter.split("-")[1].split(":")
|
2022-05-13 22:34:25 +02:00
|
|
|
# generate range inbetween start and end --> 1-3 == 1,2,3
|
2022-05-04 19:17:12 +02:00
|
|
|
for n in range(int(lower[1]), int(upper[1]) + 1):
|
|
|
|
chapter_list.append(str(f"{lower[0]}:{n}"))
|
2022-05-13 22:34:25 +02:00
|
|
|
# no volumes, just chapter ranges
|
2022-05-04 19:17:12 +02:00
|
|
|
elif "-" in chapter:
|
|
|
|
lower = chapter.split("-")[0]
|
|
|
|
upper = chapter.split("-")[1]
|
2022-05-13 22:34:25 +02:00
|
|
|
# generate range inbetween start and end --> 1-3 == 1,2,3
|
2022-05-04 19:17:12 +02:00
|
|
|
for n in range(int(lower), int(upper) + 1):
|
|
|
|
chapter_list.append(str(n))
|
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
|
|
|
|
def fix_name(filename):
|
2022-05-04 19:17:12 +02:00
|
|
|
# remove illegal characters
|
2022-05-11 18:05:26 +02:00
|
|
|
filename = re.sub('[\\\/\<\>\:\;\|\?\*\!\@"]', "", filename)
|
2022-05-09 15:42:13 +02:00
|
|
|
# remove multiple dots
|
|
|
|
filename = re.sub("([\.]{2,})", ".", filename)
|
|
|
|
# remove dot(s) at the beginning and end of the filename
|
|
|
|
filename = re.sub("(^[\.]{1,})|([\.]{1,}$)", "", filename)
|
|
|
|
# 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-13 22:34:25 +02:00
|
|
|
def get_filename(chapter_name, chapter_vol, chapter_num, forcevol):
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
def progress_bar(progress, total):
|
|
|
|
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:
|
|
|
|
print(f"\r❙{bar_texture}{whitespace_texture}❙ 100%", end="\n")
|
|
|
|
else:
|
|
|
|
print(f"\r❙{bar_texture}{whitespace_texture}❙ {percent}%", end="\r")
|