136 lines
4.0 KiB
Python
Executable File
136 lines
4.0 KiB
Python
Executable File
# Created on iPad.
|
|
# Comic to PDF Converter
|
|
|
|
# version 1.0
|
|
|
|
import os, re, argparse, tqdm, img2pdf, pathlib, tempfile, patoolib
|
|
from tqdm import tqdm
|
|
from send2trash import send2trash
|
|
|
|
parser = argparse.ArgumentParser(
|
|
description="Converts comic book zipfiles to PDF.",
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
)
|
|
parser.add_argument("-src", help="location of file(s) to convert")
|
|
parser.add_argument("-dest", help="destination of converted files")
|
|
parser.add_argument(
|
|
"-t",
|
|
"--trash-old",
|
|
action="store_true",
|
|
help="places converted cbz and cbr to the trash",
|
|
)
|
|
parser.add_argument(
|
|
"-d",
|
|
"--delete-old",
|
|
action="store_true",
|
|
help="delete cbz and cbr that are converted",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
src = args.src
|
|
dest = args.dest
|
|
delete_old = args.delete_old
|
|
trash_old = args.trash_old
|
|
|
|
|
|
class Comic2PDF:
|
|
def __init__(self, src, dest):
|
|
# huh? this needs to be fixed. I can't read it
|
|
# horrible use of OOP
|
|
src = self.check_src(src, dest)
|
|
dest = self.check_dest(src, dest)
|
|
|
|
if src.endswith(".cbz") or src.endswith(".cbr"):
|
|
self.convert_single_file(src, dest)
|
|
else:
|
|
self.batch_convert(src, dest)
|
|
|
|
def check_src(self, src, dest):
|
|
if (src is None and dest is None) or (src is None and dest is not None):
|
|
src = os.path.realpath(__file__)
|
|
src = os.path.dirname(src)
|
|
return src
|
|
else:
|
|
return src
|
|
|
|
def check_dest(self, src, dest):
|
|
if dest is None and src is None:
|
|
dest = os.path.realpath(__file__)
|
|
dest = os.path.dirname(dest)
|
|
return dest
|
|
elif dest is None and src is not None:
|
|
if src.endswith(".cbz") or src.endswith(".cbr"):
|
|
dest = pathlib.Path(src).parents[0]
|
|
else:
|
|
dest = pathlib.Path(src)
|
|
return dest
|
|
else:
|
|
return dest
|
|
|
|
def scan_folder(self, src):
|
|
filesIn_folder = []
|
|
|
|
for root, dirs, files in os.walk(src):
|
|
for name in files:
|
|
if name.endswith((".png", ".jpg", ".jpeg", ".cbz", ".cbr")):
|
|
filesIn_folder.append(os.path.join(root, name))
|
|
|
|
filesIn_folder.sort(key=natural_keys)
|
|
return filesIn_folder
|
|
|
|
def convert_to_pdf(self, src, dest, name):
|
|
imgs = []
|
|
imgs = self.scan_folder(src)
|
|
pdf_path = f"{dest}/{name}.pdf"
|
|
with open(pdf_path, "wb") as f:
|
|
f.write(img2pdf.convert(imgs))
|
|
|
|
def convert_single_file(self, src, dest):
|
|
with tempfile.TemporaryDirectory() as temp_dict:
|
|
folder_parents = pathlib.Path(temp_dict)
|
|
name = pathlib.Path(src).stem
|
|
patoolib.extract_archive(src, outdir=temp_dict)
|
|
list_folderInside = next(os.walk(folder_parents))[1]
|
|
|
|
try:
|
|
storage = f"{temp_dict}/{list_folderInside[0]}"
|
|
except:
|
|
storage = f"{temp_dict}/"
|
|
|
|
self.convert_to_pdf(src=storage, dest=dest, name=name)
|
|
|
|
if trash_old is True:
|
|
send2trash(f"{src}")
|
|
|
|
if delete_old is True:
|
|
os.remove(f"{src}")
|
|
|
|
def batch_convert(self, src, dest):
|
|
for root, dir, files in os.walk(src):
|
|
pstat = [f for f in files if f.endswith(".cbr") or f.endswith(".cbz")]
|
|
pbar = tqdm(total=len(pstat))
|
|
queue = 0
|
|
|
|
for name in files:
|
|
if name.endswith(".cbz") or name.endswith(".cbr"):
|
|
pbar.set_description(f"Converting {name}")
|
|
self.convert_single_file(os.path.join(root, name), dest)
|
|
pbar.update(1)
|
|
queue += 1
|
|
else:
|
|
pass
|
|
break
|
|
|
|
# exported from the internet;
|
|
# sorts folder name to human order
|
|
def atoi(text):
|
|
return int(text) if text.isdigit() else text
|
|
|
|
|
|
def natural_keys(text):
|
|
return [atoi(c) for c in re.split(r"(d )", text)]
|
|
|
|
|
|
Comic2PDF(src, dest)
|