Last active
November 15, 2022 01:51
-
-
Save sebassdc/95ec02605ff9ec20a763fd84a0523c73 to your computer and use it in GitHub Desktop.
A little script to fix numbering on cbz (comic book zip files)
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
#!/usr/bin/python | |
import os, sys, zipfile | |
import shutil | |
def renamed(string): | |
fp, sp = string.split('.') | |
new_fp = fp.zfill(4) | |
return new_fp + '.' + sp | |
def cbzfix_folder(path): | |
files = os.listdir(path) | |
for filename in files: | |
os.rename(path + '/' + filename, path + '/' + renamed(filename)) | |
def unzip(path, aux_path): | |
comic_book = zipfile.ZipFile(path) | |
os.mkdir(aux_path) | |
comic_book.extractall(path=aux_path) | |
def cbzfix_zip(path, aux_path): | |
shutil.rmtree(aux_path, ignore_errors=True) | |
unzip(path, aux_path) | |
cbzfix_folder(aux_path) | |
shutil.make_archive(path.split('.zip')[0], 'zip', aux_path) | |
print path + " FIXED!!" | |
shutil.rmtree(aux_path, ignore_errors=True) | |
if __name__ == '__main__': | |
aux_path = ".cbzfix/" | |
arg_filename = '' | |
option = '-z' # -fo (folder) | |
if (len(sys.argv)) == 1: | |
sys.exit("You must enter a file argument") | |
elif(len(sys.argv)) == 2: | |
arg_filename = sys.argv[1] | |
else: | |
option = sys.argv[1] | |
if(option != '-z' and option != '-fo' and option != '-mz' and option != '-mfo'): | |
sys.exit("You Must enter a valid option (-z, -fo, -mz, -mfo)") | |
arg_filename = sys.argv[2] | |
if (option == '-z'): | |
cbzfix_zip(arg_filename, aux_path) | |
if (option == '-fo'): | |
cbzfix_folder(arg_filename) | |
shutil.make_archive(arg_filename, 'zip', arg_filename) | |
print arg_filename + ".zip " + "CREATED!!" | |
shutil.rmtree(arg_filename, ignore_errors=True) | |
if (option == '-mz'): | |
print arg_filename | |
files = os.listdir(arg_filename) | |
for filename in files: | |
cbzfix_zip(arg_filename + '/' + filename, aux_path) | |
if (option == '-mfo'): | |
print arg_filename | |
files = os.listdir(arg_filename) | |
for filename in files: | |
cbzfix_folder(arg_filename + '/' + filename) | |
shutil.make_archive(arg_filename + '/' + filename, 'zip', arg_filename + '/' + filename) | |
print arg_filename + filename + ".zip " + "CREATED!!" | |
shutil.rmtree(arg_filename + '/' + filename , ignore_errors=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment