-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepack.py
74 lines (64 loc) · 2.47 KB
/
repack.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
import collections
import dataclasses
import importlib
import json
import os
import re
import sys
import zipfile
meta_module = sys.argv[1]
metadata = importlib.import_module(f"works.{meta_module}")
page_regex = re.compile(metadata.page_regex)
chapter_overrides = getattr(metadata, "chapter_overrides", {})
repack_dir = meta_module
os.makedirs(repack_dir, exist_ok=True)
with open(os.path.join(repack_dir, "details.json"), "w") as f:
json.dump(metadata.details, f)
@dataclasses.dataclass
class Chapter:
title: str = None
pages: list[str] = dataclasses.field(default_factory=list)
for vol_num, vol_fn in enumerate(metadata.volumes, 1):
print(f"Volume {vol_num}")
with zipfile.ZipFile(vol_fn) as volfile:
if vol_num == metadata.cover_volume:
cover = volfile.read(metadata.cover_file)
with open(os.path.join(repack_dir, "cover.jpg"), "wb") as f:
f.write(cover)
chapters = collections.defaultdict(Chapter)
for page in volfile.namelist():
if not (matches := page_regex.match(page)):
raise Exception(page)
matches = matches.groupdict()
chap_num = chapter_overrides.get(
(vol_num, matches["page"]),
matches["chapter"],
)
chapter = chapters[chap_num]
chapter.pages.append(page)
if not chapter.title:
if title := matches.get("chap_title"):
chapter.title = title
for chap_num, chapter in chapters.items():
chap_num = chap_num.lstrip("0").replace(metadata.subchapter_separator, ".")
print(f" Chapter {chap_num}")
fn = []
if len(metadata.volumes) > 1:
fn.append(f"Vol.{vol_num}")
fn.append(f"Ch.{chap_num}")
if title := metadata.toc.get(chap_num, chapter.title):
fn.append("-")
fn.append(title)
fn = " ".join(fn) + ".cbz"
path = os.path.join(repack_dir, fn)
with zipfile.ZipFile(path, "w") as chapfile:
for page in chapter.pages:
data = volfile.read(page)
chapfile.writestr(
page,
data,
# Saves a little bit even though the pages are already
# compressed
compress_type=zipfile.ZIP_DEFLATED,
)