From 88c5977bc98b845c6e93fcc5ae8d272083ab4809 Mon Sep 17 00:00:00 2001 From: Markil 3 Date: Wed, 8 Oct 2025 11:07:00 -0600 Subject: [PATCH] Adds supports for ComicInfo.xml files --- comic_download/comic_strip.py | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/comic_download/comic_strip.py b/comic_download/comic_strip.py index 0eca7d0..c1c027c 100644 --- a/comic_download/comic_strip.py +++ b/comic_download/comic_strip.py @@ -33,6 +33,7 @@ from pathlib import Path from tempfile import NamedTemporaryFile import requests +from bs4 import BeautifulSoup from PIL import Image, ImageDraw, ImageFont def file_safe_string(string): @@ -537,6 +538,64 @@ class ComicStrip(ImageRepo, ABC): """ self.transformed_images = self.images + def create_comic_info(self) -> str: + """ + Creates the contents of a ComicInfo.xml file. + """ + soup = BeautifulSoup('', features="xml") + info_tag = soup.find("ComicInfo") + info_tag.append(soup.new_tag("Title", string=self.title)) + info_tag.append(soup.new_tag("Series", string=self.comic.title)) + number_el = soup.new_tag("Number") + if type(self.identifier) == int: + number_el.string = str(self.identifier) + elif (type(self.identifier) == list or type(self.identifier) == tuple) and type(self.identifier[-1]) == int: + number_el.string = str(self.identifier[-1]) + if number_el.string: + info_tag.append(number_el) + if self.date: + info_tag.append(soup.new_tag("Year", string=str(self.date.year))) + info_tag.append(soup.new_tag("Month", string=str(self.date.month))) + info_tag.append(soup.new_tag("Day", string=str(self.date.day))) + if self.comic.author: + if type(self.comic.author) == str: + info_tag.append(soup.new_tag("Writer", string=self.comic.author)) + elif type(self.comic.author) == list: + authors = {} + writer_string = "" + for author in self.comic.author: + author_class = None + if type(author) == str: + author_name = author + author_class = "Writer" + elif type(author) == dict: + author_name = author["name"] + if author["role"].lower() in ("writer", "author", "creator"): + author_class = "Writer" + elif author["role"].lower() in ("penciller"): + author_class = "Penciller" + elif author["role"].lower() in ("inker"): + author_class = "Inker" + elif author["role"].lower() in ("colorist"): + author_class = "Colorist" + elif author["role"].lower() in ("letterer"): + author_class = "Letterer" + elif author["role"].lower() in ("coverartist", "conver_artist"): + author_class = "CoverArtist" + elif author["role"].lower() in ("editor"): + author_class = "Editor" + elif author["role"].lower() in ("translator"): + author_class = "Translator" + if author_class not in authors: + authors[author_class] = author_name + else: + authors[author_class] += "," + author_name + for author_class, author_list in authors.items(): + info_tag.append(soup.new_tag(author_class, string=author_list)) + info_tag.append(soup.new_tag("Format", string="Web")) + info_tag.append(soup.new_tag("PageCount", string=str(len(self.transformed_images)))) + return str(soup) + def package_data(self, base_path: Path): """ Compresses all the data into a cbz file. @@ -569,6 +628,11 @@ class ComicStrip(ImageRepo, ABC): logging.info("Packaging %s" % image_path.suffix) with open(image.name, 'rb') as image_stream, comic_zip.open(str(i + 1) + (image_path.suffix or '.png'), 'w') as zip_stream: zip_stream.write(image_stream.read()) + # Add a ComicInfo file + comic_info = self.create_comic_info() + if comic_info: + with comic_zip.open("ComicInfo.xml", 'w') as zip_stream: + zip_stream.write(comic_info.encode("utf-8")); # Delete the base images, as they are no longer needed. # We cannot touch the transformed images, since some # of them may come directly from the base comic, and