fixed formatting
This commit is contained in:
+35
-29
@@ -3,22 +3,28 @@
|
|||||||
|
|
||||||
# version 1.0
|
# version 1.0
|
||||||
|
|
||||||
import os, re, zipfile, argparse, tqdm, img2pdf, pathlib, tempfile, patoolib
|
import os, re, argparse, tqdm, img2pdf, pathlib, tempfile, patoolib
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
from send2trash import send2trash
|
from send2trash import send2trash
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Converts comic book zipfiles to PDF.',
|
parser = argparse.ArgumentParser(
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
description="Converts comic book zipfiles to PDF.",
|
||||||
parser.add_argument('-src',
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||||
help='location of file(s) to convert')
|
)
|
||||||
parser.add_argument('-dest',
|
parser.add_argument("-src", help="location of file(s) to convert")
|
||||||
help='destination of converted files')
|
parser.add_argument("-dest", help="destination of converted files")
|
||||||
parser.add_argument('-t','--trash-old',
|
parser.add_argument(
|
||||||
action='store_true',
|
"-t",
|
||||||
help='places converted cbz and cbr to the trash')
|
"--trash-old",
|
||||||
parser.add_argument('-d',"--delete-old",
|
action="store_true",
|
||||||
action='store_true',
|
help="places converted cbz and cbr to the trash",
|
||||||
help='delete cbz and cbr that are converted')
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-d",
|
||||||
|
"--delete-old",
|
||||||
|
action="store_true",
|
||||||
|
help="delete cbz and cbr that are converted",
|
||||||
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -27,19 +33,19 @@ dest = args.dest
|
|||||||
delete_old = args.delete_old
|
delete_old = args.delete_old
|
||||||
trash_old = args.trash_old
|
trash_old = args.trash_old
|
||||||
|
|
||||||
|
|
||||||
class Comic2PDF:
|
class Comic2PDF:
|
||||||
|
|
||||||
def __init__(self, src, dest):
|
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)
|
src = self.check_src(src, dest)
|
||||||
dest = self.check_dest(src, dest)
|
dest = self.check_dest(src, dest)
|
||||||
|
|
||||||
if src.endswith('.cbz') or src.endswith('.cbr'):
|
if src.endswith(".cbz") or src.endswith(".cbr"):
|
||||||
self.convert_single_file(src, dest)
|
self.convert_single_file(src, dest)
|
||||||
else:
|
else:
|
||||||
self.batch_convert(src, dest)
|
self.batch_convert(src, dest)
|
||||||
|
|
||||||
# check if source has no input or has input.
|
|
||||||
def check_src(self, 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):
|
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.realpath(__file__)
|
||||||
@@ -48,14 +54,13 @@ class Comic2PDF:
|
|||||||
else:
|
else:
|
||||||
return src
|
return src
|
||||||
|
|
||||||
# check if destination has no input or has input.
|
|
||||||
def check_dest(self, src, dest):
|
def check_dest(self, src, dest):
|
||||||
if dest is None and src is None:
|
if dest is None and src is None:
|
||||||
dest = os.path.realpath(__file__)
|
dest = os.path.realpath(__file__)
|
||||||
dest = os.path.dirname(dest)
|
dest = os.path.dirname(dest)
|
||||||
return dest
|
return dest
|
||||||
elif dest is None and src is not None:
|
elif dest is None and src is not None:
|
||||||
if src.endswith('.cbz') or src.endswith('.cbr'):
|
if src.endswith(".cbz") or src.endswith(".cbr"):
|
||||||
dest = pathlib.Path(src).parents[0]
|
dest = pathlib.Path(src).parents[0]
|
||||||
else:
|
else:
|
||||||
dest = pathlib.Path(src)
|
dest = pathlib.Path(src)
|
||||||
@@ -68,10 +73,9 @@ class Comic2PDF:
|
|||||||
|
|
||||||
for root, dirs, files in os.walk(src):
|
for root, dirs, files in os.walk(src):
|
||||||
for name in files:
|
for name in files:
|
||||||
if name.endswith((".png", ".jpg",".jpeg",".cbz",'.cbr')):
|
if name.endswith((".png", ".jpg", ".jpeg", ".cbz", ".cbr")):
|
||||||
filesIn_folder.append(os.path.join(root, name))
|
filesIn_folder.append(os.path.join(root, name))
|
||||||
|
|
||||||
# human sorting
|
|
||||||
filesIn_folder.sort(key=natural_keys)
|
filesIn_folder.sort(key=natural_keys)
|
||||||
return filesIn_folder
|
return filesIn_folder
|
||||||
|
|
||||||
@@ -84,33 +88,32 @@ class Comic2PDF:
|
|||||||
|
|
||||||
def convert_single_file(self, src, dest):
|
def convert_single_file(self, src, dest):
|
||||||
with tempfile.TemporaryDirectory() as temp_dict:
|
with tempfile.TemporaryDirectory() as temp_dict:
|
||||||
|
|
||||||
folder_parents = pathlib.Path(temp_dict)
|
folder_parents = pathlib.Path(temp_dict)
|
||||||
name = pathlib.Path(src).stem
|
name = pathlib.Path(src).stem
|
||||||
patoolib.extract_archive(src, outdir=temp_dict)
|
patoolib.extract_archive(src, outdir=temp_dict)
|
||||||
list_folderInside = next(os.walk(folder_parents))[1]
|
list_folderInside = next(os.walk(folder_parents))[1]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
storage = f'{temp_dict}/{list_folderInside[0]}'
|
storage = f"{temp_dict}/{list_folderInside[0]}"
|
||||||
except:
|
except:
|
||||||
storage = f'{temp_dict}/'
|
storage = f"{temp_dict}/"
|
||||||
|
|
||||||
self.convert_to_pdf(src=storage, dest=dest, name=name)
|
self.convert_to_pdf(src=storage, dest=dest, name=name)
|
||||||
|
|
||||||
if trash_old is True:
|
if trash_old is True:
|
||||||
send2trash(f'{src}')
|
send2trash(f"{src}")
|
||||||
|
|
||||||
if delete_old is True:
|
if delete_old is True:
|
||||||
os.remove(f'{src}')
|
os.remove(f"{src}")
|
||||||
|
|
||||||
def batch_convert(self, src, dest):
|
def batch_convert(self, src, dest):
|
||||||
for root, dir, files in os.walk(src):
|
for root, dir, files in os.walk(src):
|
||||||
pstat = [f for f in files if f.endswith('.cbr') or f.endswith('.cbz')]
|
pstat = [f for f in files if f.endswith(".cbr") or f.endswith(".cbz")]
|
||||||
pbar = tqdm(total=len(pstat))
|
pbar = tqdm(total=len(pstat))
|
||||||
queue = 0
|
queue = 0
|
||||||
|
|
||||||
for name in files:
|
for name in files:
|
||||||
if name.endswith('.cbz') or name.endswith('.cbr'):
|
if name.endswith(".cbz") or name.endswith(".cbr"):
|
||||||
pbar.set_description(f"Converting {name}")
|
pbar.set_description(f"Converting {name}")
|
||||||
self.convert_single_file(os.path.join(root, name), dest)
|
self.convert_single_file(os.path.join(root, name), dest)
|
||||||
pbar.update(1)
|
pbar.update(1)
|
||||||
@@ -119,11 +122,14 @@ class Comic2PDF:
|
|||||||
pass
|
pass
|
||||||
break
|
break
|
||||||
|
|
||||||
# organizes files with humanly order.
|
# exported from the internet;
|
||||||
|
# sorts folder name to human order
|
||||||
def atoi(text):
|
def atoi(text):
|
||||||
return int(text) if text.isdigit() else text
|
return int(text) if text.isdigit() else text
|
||||||
|
|
||||||
|
|
||||||
def natural_keys(text):
|
def natural_keys(text):
|
||||||
return [atoi(c) for c in re.split(r"(d )", text)]
|
return [atoi(c) for c in re.split(r"(d )", text)]
|
||||||
|
|
||||||
|
|
||||||
Comic2PDF(src, dest)
|
Comic2PDF(src, dest)
|
||||||
|
|||||||
Reference in New Issue
Block a user