Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Chapters from ComicInfo.txt #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions comicon/inputs/cbz.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from typing import Iterator, TypedDict

from lxml import etree
from slugify import slugify

from ..base import Chapter, Comic, Metadata
from ..base import Chapter, Comic, Metadata, SLUGIFY_ARGS
from ..cirtools import IR_DATA_FILE
from ..image import ACCEPTED_IMAGE_EXTENSIONS

Expand Down Expand Up @@ -35,6 +36,8 @@ def create_cir(path: Path, dest: Path) -> Iterator[str | int]:
"extra_metadata": {},
}

chapters = []
chapter_index = {}
found_comicon_metadata = False
image_paths: list[Path] = []
with zipfile.ZipFile(path, "r", zipfile.ZIP_DEFLATED) as z:
Expand All @@ -53,6 +56,15 @@ def create_cir(path: Path, dest: Path) -> Iterator[str | int]:
data_dict["authors"] = str(el.text).split(", ")
case "Genre":
data_dict["genres"] = str(el.text).split(", ")
if el.tag == "Pages":
for page in el.iter():
if page.tag == "Page" and "Image" in page.attrib:
if "Bookmark" in page.attrib:
chapters.append(Chapter(page.attrib["Bookmark"], slugify(page.attrib["Bookmark"], **SLUGIFY_ARGS)))
chapter_index[page.attrib["Bookmark"]] = int(page.attrib["Image"])
elif "Type" in page.attrib:
chapters.append(Chapter(page.attrib["Type"], slugify(page.attrib["Type"], **SLUGIFY_ARGS)))
chapter_index[page.attrib["Type"]] = int(page.attrib["Image"])
elif name.endswith(IR_DATA_FILE):
with z.open(name) as file:
data = file.read()
Expand Down Expand Up @@ -80,7 +92,11 @@ def create_cir(path: Path, dest: Path) -> Iterator[str | int]:
# ignore all other file types
...
else:
comic = Comic(Metadata(**data_dict), [Chapter("Chapter 1", "chapter-1")])
if not chapters:
# TODO: remove hardcoded "chapter-1" and make it variable
chapters.append(Chapter("Chapter 1", "chapter-1"))
chapter_index["Chapter 1"] = 0
comic = Comic(Metadata(**data_dict), chapters)

with open(dest / IR_DATA_FILE, "w", encoding="utf-8") as file:
json.dump(comic.to_dict(), file, indent=2)
Expand Down Expand Up @@ -110,14 +126,23 @@ def create_cir(path: Path, dest: Path) -> Iterator[str | int]:
file.write(data)
yield str(filename)
else:
# copy all image folders into a single folder
# TODO: remove hardcoded "chapter-1" and make it variable
(dest / "chapter-1").mkdir(exist_ok=True)
for image_path in image_paths:
with z.open(str(image_path)) as file:
data = file.read()

new_path = dest / "chapter-1" / image_path.name
with open(new_path, "wb") as file:
file.write(data)
yield str(new_path)
# create chapters from the ComicInfo.xml Metadata or put them into a single folder if none are given
image_paths = sorted(image_paths)
chapters = sorted(chapters, key=lambda c: chapter_index[c.title])
chapters.append(Chapter("END", "end"))
chapter_index["END"] = len(image_paths)
for i, chapter in enumerate(chapters[:-1]):
(dest / chapter.slug).mkdir(exist_ok=True)

start_page = chapter_index[chapter.title]
end_page = chapter_index[chapters[i + 1].title]

for j, image_path in enumerate(image_paths[start_page:end_page], start=1):
with z.open(str(image_path)) as file:
data = file.read()

ext = Path(image_path).suffix
new_path = dest / chapter.slug / f"{j:05}{ext}"
with open(new_path, "wb") as file:
file.write(data)
yield str(new_path)