31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
# File: /home/code/projects/manga-organizer-1/cbz-volume-combiner/cbz_volume_combiner/core.py
|
|
import os
|
|
from collections import defaultdict
|
|
from .parsing import parse_manga_filename
|
|
|
|
def organize_by_volume(cbz_files, extra_verbose=False):
|
|
"""Group CBZ files by manga name and volume."""
|
|
volumes = defaultdict(lambda: defaultdict(list))
|
|
unparsed_files = []
|
|
|
|
for cbz_file in cbz_files:
|
|
info = parse_manga_filename(cbz_file)
|
|
if info:
|
|
manga_key = info['manga_name'].lower()
|
|
volumes[manga_key][info['volume']].append(info)
|
|
else:
|
|
unparsed_files.append(cbz_file)
|
|
|
|
# Sort chapters within each volume
|
|
for manga in volumes:
|
|
for volume in volumes[manga]:
|
|
volumes[manga][volume].sort(key=lambda x: x['chapter'])
|
|
|
|
if extra_verbose and unparsed_files:
|
|
print(f"\nWARNING: Could not parse {len(unparsed_files)} files:")
|
|
for file in unparsed_files[:10]: # Show first 10 only to avoid spam
|
|
print(f" - {os.path.basename(file)}")
|
|
if len(unparsed_files) > 10:
|
|
print(f" ... and {len(unparsed_files) - 10} more")
|
|
|
|
return volumes |