-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_chapters.py
More file actions
73 lines (60 loc) · 1.96 KB
/
validate_chapters.py
File metadata and controls
73 lines (60 loc) · 1.96 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
67
68
69
70
71
72
73
import os
import re
import sys
from mtlogger import logger
from _common import get_chapter, exit_with_prompt, 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 chapter folders:\n',
process_parent_folder,
{ 'log_success': False, 'loop': False }
)
def process_parent_folder(directory):
directory_name = os.path.basename(directory)
found_chapters = set()
for entry in os.scandir(directory):
chapter = get_chapter(entry.name)
if chapter:
found_chapters.add(float(chapter))
if entry.is_dir():
process_chapter_folder(entry.path, chapter)
if not found_chapters:
logger.log(f'No chapters found in "{directory_name}".')
return
expected_chapters = set(range(1, int(max(found_chapters)) + 1))
missing_chapters = expected_chapters - found_chapters
if missing_chapters:
logger.log(
f'\nChapters missing in "{directory_name}": '
f'{" ".join(f"{ch:03d}" for ch in sorted(missing_chapters))}'
)
else:
logger.log(f'\nNo chapters found missing in "{directory}".')
def process_chapter_folder(directory, chapter):
directory_name = os.path.basename(directory)
pattern = re.compile(r'(\d+)', re.IGNORECASE)
found_pages = set()
for entry in os.scandir(directory):
if entry.is_file():
match = pattern.search(entry.name)
if match:
found_pages.add(float(match.group(1)))
if not found_pages:
logger.log(f'- [Ch.{chapter}] All pages missing.')
return
expected_pages = set(range(1, int(max(found_pages)) + 1))
missing_pages = expected_pages - found_pages
if missing_pages:
logger.log(
f'- [Ch.{chapter}] {len(missing_pages)} page(s) missing: '
f'{" ".join(f"{p:02d}" for p in sorted(missing_pages))}'
)
if __name__ == '__main__':
try:
main()
except Exception as ex:
print_error(ex)
exit_with_prompt()