Last active
November 18, 2019 19:35
-
-
Save jgasteiz/ac668a30c66b8c673aede0ce033bb9c2 to your computer and use it in GitHub Desktop.
Given a path, look for subdirectories and create a .cbz file per each of them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
black $1 | |
isort $1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import zipfile | |
def create_cbzs(base_dir): | |
""" | |
Goes through the given `base_dir` and creates a .cbz file per directory | |
found. | |
""" | |
comic_list = [name for name in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, name))] | |
print("{} comics found".format(len(comic_list))) | |
for comic in comic_list: | |
path = os.path.join(base_dir, comic) | |
file_name = '{}/{}.cbz'.format(base_dir, comic) | |
cbz = zipfile.ZipFile(file_name, mode='w') | |
print('Creating {}'.format(file_name)) | |
for root, dirs, files in os.walk(path): | |
for filename in files: | |
cbz.write(os.path.join(root, filename)) | |
cbz.close() | |
def rename_to_cb(base_dir): | |
""" | |
Goes through the given `base_dir` and renames all .zip and .rar files | |
to .cbz and .cbr respectively. | |
""" | |
for comic in os.listdir(base_dir): | |
file_name = '{}/{}'.format(base_dir, comic) | |
if file_name.endswith('.rar'): | |
new_file_name = '{}.cbr'.format(file_name[:-4]) | |
print(new_file_name) | |
os.rename(file_name, new_file_name) | |
if file_name.endswith('.zip'): | |
new_file_name = '{}.cbz'.format(file_name[:-4]) | |
print(new_file_name) | |
os.rename(file_name, new_file_name) | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print('You must provide a function (`create_cbzs` or `rename_to_cb`) and a base directory.') | |
exit(-1) | |
function_name = sys.argv[1] | |
base_dir = sys.argv[2] | |
if function_name == 'create_cbzs': | |
create_cbzs(base_dir) | |
elif function_name == 'rename_to_cb': | |
rename_to_cb(base_dir) | |
else: | |
print('The available functions are `create_cbzs` and `rename_to_cb`.') | |
exit(-1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import shutil | |
CONFIG = { | |
"group_covers": True, | |
"2000AD 2100": {"cover": 0, "story_start": 2, "story_end": 8}, | |
"2000AD 2109 (2018) (Digital-Empire)": { | |
"cover": 0, | |
"story_start": 2, | |
"story_end": 9, | |
}, | |
"2000AD 2111 (2018) (digital) (Minutemen-juvecube)": { | |
"cover": 0, | |
"story_start": 4, | |
"story_end": 15, | |
}, | |
"2000AD 2130": {"cover": 0, "story_start": 2, "story_end": 13}, | |
"2000AD 2145 (2019) (digital) (Minutemen-juvecube)": { | |
"cover": 0, | |
"story_start": 2, | |
"story_end": 9, | |
}, | |
"default": {"cover": 0, "story_start": 2, "story_end": 7}, | |
} | |
FULL_COMPILATION_CONFIG = { | |
"group_covers": False, | |
"default": {"cover": None, "story_start": 0, "story_end": 1000}, | |
} | |
def _create_compilation(*, source_dir, destination_dir, config): | |
if source_dir == destination_dir: | |
raise Exception("Base and result directories must be different") | |
covers_dir = f"{destination_dir}/_covers" | |
# Clean and create results dir. | |
try: | |
shutil.rmtree(destination_dir) | |
except Exception: | |
pass | |
finally: | |
os.mkdir(destination_dir) | |
if config["group_covers"]: | |
os.mkdir(covers_dir) | |
# Fetch list of comic files. | |
comic_list = [ | |
name | |
for name in os.listdir(source_dir) | |
if os.path.isdir(os.path.join(source_dir, name)) | |
] | |
comic_list = sorted(comic_list) | |
print("{} comics found".format(len(comic_list))) | |
# Create compilation | |
page_counter = 1 | |
for comic in comic_list: | |
comic_name = "-".join(comic.split(" ")[:2]) | |
comic_path = os.path.join(source_dir, comic) | |
print(f"Extracting {comic_name}") | |
page_list = sorted(os.listdir(comic_path)) | |
page_list = [p for p in page_list if not p.startswith(".")] | |
try: | |
cfg = config[comic] | |
except KeyError: | |
cfg = config["default"] | |
cover = cfg["cover"] | |
story_start = cfg["story_start"] | |
story_end = min(cfg["story_end"], len(page_list) - 1) | |
if cover is not None: | |
cover_page = os.path.join(comic_path, page_list[cover]) | |
shutil.copy(cover_page, f"{covers_dir}/{comic_name}.jpg") | |
# Copy the pages. | |
for i in range(story_start, story_end + 1): | |
page = os.path.join(comic_path, page_list[i]) | |
shutil.copy(page, f"{destination_dir}/{page_counter}.jpg") | |
page_counter += 1 | |
def create_full_compilation(source_dir, destination_dir): | |
_create_compilation( | |
source_dir=source_dir, | |
destination_dir=destination_dir, | |
config=FULL_COMPILATION_CONFIG, | |
) | |
def create_partial_compilation(source_dir, destination_dir): | |
_create_compilation( | |
source_dir=source_dir, destination_dir=destination_dir, config=CONFIG | |
) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Create a comic compilation") | |
parser.add_argument("source") | |
parser.add_argument("destination") | |
parser.add_argument("--mode", choices=["default", "partial"], default="default") | |
args = parser.parse_args() | |
if args.mode == "default": | |
create_full_compilation(args.source, args.destination) | |
else: | |
create_partial_compilation(args.source, args.destination) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment