-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcomvert.py
executable file
·74 lines (66 loc) · 2.9 KB
/
comvert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Version 2 for cross-platform compatability"""
import os
import tempfile
import argparse
import patoolib
def extract_archive(archive, temp):
patoolib.extract_archive(archive, outdir=temp)
def remove_blacklisted(temp):
print('comvert: Scanning ' + blacklist)
with open(blacklist, 'r') as bl:
for spam in bl.read().splitlines():
for root, _, files in os.walk(temp):
for file in files:
if file == spam:
spamfile = os.path.join(root, file)
print('comvert: Removing' + spamfile)
os.remove(spamfile)
def create_archive(source, title, type_in, type_out, temp):
newfile = os.path.join(source, title + "." + type_out)
curfile = os.path.join(source, title + "." + type_in)
bakfile = os.path.join(source, title + '.bak')
if os.path.exists(newfile):
if os.path.exists(bakfile):
os.remove(bakfile)
os.rename(newfile, bakfile)
os.chdir(temp)
patoolib.create_archive(newfile, ['.'])
os.chdir(source)
if newfile != curfile:
os.remove(curfile)
if os.path.exists(bakfile):
os.remove(bakfile)
def repack_archive(path, type_in, type_out):
for f in os.scandir(path):
if f.name.endswith(type_in):
orig_name, _ = os.path.splitext(f.name)
curfile = os.path.join(path, orig_name + "." + type_in)
newfile = os.path.join(path, orig_name + "." + type_out)
if newfile == curfile:
print("Can't repack a file to the same format")
return
patoolib.repack_archive(curfile, newfile)
os.remove(curfile)
def main(path, type_in, type_out):
for f in os.scandir(path):
if f.name.endswith(type_in):
orig_temp = tempfile.TemporaryDirectory()
orig_name, _ = os.path.splitext(f.name)
orig_full = os.path.join(path, f.name)
extract_archive(orig_full, orig_temp.name)
remove_blacklisted(orig_temp.name)
create_archive(path, orig_name, type_in, type_out, orig_temp.name)
blacklist = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'blacklist.ini')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Convert digital comic books to different formats')
parser.add_argument('-i', '--input', action='store', default='cbr', help='Input file type')
parser.add_argument('-o', '--output', action='store', default='cbz', help='Output file type')
parser.add_argument('-s', '--source', action='store', default='.', help='Source directory')
parser.add_argument('-p', '--preserve', action='store_true', help='Preserve archive')
args = parser.parse_args()
here = os.path.abspath(args.source)
if args.preserve:
repack_archive(here, args.input, args.output)
else:
main(here, args.input, args.output)