-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_volumes.py
More file actions
66 lines (53 loc) · 2.12 KB
/
merge_volumes.py
File metadata and controls
66 lines (53 loc) · 2.12 KB
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
import os
import shutil
import sys
from concurrent.futures import ThreadPoolExecutor
from mtlogger import LogLevel, logger
from tqdm import tqdm
from _common import delete_empty_folders, exit_with_prompt, get_volume_and_chapter, print_error, select_parent_folder
def main():
if len(sys.argv) > 1:
process_parent_folder(sys.argv[1])
else:
select_parent_folder('Enter the path to the parent folder containing the volume folders you want to merge:\n', process_parent_folder)
def process_parent_folder(root_dir):
files_to_process = []
for folder in os.listdir(root_dir):
folder_path = os.path.join(root_dir, folder)
if os.path.isdir(folder_path):
volume, chapter = get_volume_and_chapter(folder)
if not volume or not chapter:
tqdm.write(logger.format(LogLevel.WARN, f'Skipping "{folder}". Volume or chapter numbers could not be inferred.'))
continue
output_path = os.path.join(root_dir, f'Vol.{volume.zfill(2)}')
if not os.path.exists(output_path):
os.makedirs(output_path)
for item in os.listdir(folder_path):
src = os.path.join(folder_path, item)
if os.path.isfile(src):
files_to_process.append((src, output_path, get_sanitized_chapter(chapter)))
with ThreadPoolExecutor() as executor, tqdm(total = len(files_to_process), desc = f'Processing "{root_dir}"') as progress:
for _ in executor.map(process_file, files_to_process):
progress.update(1)
delete_empty_folders(root_dir)
def get_sanitized_chapter(chapter):
parts = chapter.split('.')
name = f'{int(parts[0]):03d}'
if len(parts) > 1: name += chr(ord('a') + int(parts[1]) - 1)
return name
def process_file(params):
src, target_folder, chapter = params
base, ext = os.path.splitext(os.path.basename(src))
new_name = f'ch{chapter}_p{base}{ext}'
dest = os.path.join(target_folder, new_name)
counter = 1
while os.path.exists(dest):
dest = os.path.join(target_folder, f'ch{chapter}_p{base}_{counter:02d}{ext}')
counter += 1
shutil.move(src, dest)
if __name__ == '__main__':
try:
main()
except Exception as ex:
print_error(ex)
exit_with_prompt()