From 3a08b91ffa686c6c8e23ccd1e14d1d2142e8b7d6 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Wed, 16 Aug 2023 18:44:03 +0200 Subject: [PATCH] Added cb7 to supported comic files for upload and metadata extraction --- cps/__init__.py | 3 ++- cps/comic.py | 22 ++++++++++++++++++++-- cps/uploader.py | 2 +- optional-requirements.txt | 1 + 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cps/__init__.py b/cps/__init__.py index 8b0b86ad..f4f8dbf2 100644 --- a/cps/__init__.py +++ b/cps/__init__.py @@ -64,7 +64,8 @@ mimetypes.add_type('application/x-mobi8-ebook', '.azw3') mimetypes.add_type('application/x-cbr', '.cbr') mimetypes.add_type('application/x-cbz', '.cbz') mimetypes.add_type('application/x-cbt', '.cbt') -mimetypes.add_type('image/vnd.djvu', '.djvu') +mimetypes.add_type('application/x-cb7', '.cb7') +mimetypes.add_type('image/vnd.djv', '.djv') mimetypes.add_type('application/mpeg', '.mpeg') mimetypes.add_type('application/mpeg', '.mp3') mimetypes.add_type('application/mp4', '.m4a') diff --git a/cps/comic.py b/cps/comic.py index 13774756..4242bb2f 100644 --- a/cps/comic.py +++ b/cps/comic.py @@ -52,6 +52,12 @@ except (ImportError, LookupError) as e: except (ImportError, SyntaxError) as e: log.debug('Cannot import rarfile, extracting cover files from rar files will not work: %s', e) use_rarfile = False + try: + import py7zr + use_7zip = True + except (ImportError, SyntaxError) as e: + log.debug('Cannot import py7zr, extracting cover files from CB7 files will not work: %s', e) + use_7zip = False use_comic_meta = False @@ -84,10 +90,22 @@ def _extract_cover_from_archive(original_file_extension, tmp_file_name, rar_exec if len(ext) > 1: extension = ext[1].lower() if extension in cover.COVER_EXTENSIONS: - cover_data = cf.read(name) + cover_data = cf.read([name]) break except Exception as ex: - log.debug('Rarfile failed with error: {}'.format(ex)) + log.error('Rarfile failed with error: {}'.format(ex)) + elif original_file_extension.upper() == '.CB7' and use_7zip: + cf = py7zr.SevenZipFile(tmp_file_name) + for name in cf.getnames(): + ext = os.path.splitext(name) + if len(ext) > 1: + extension = ext[1].lower() + if extension in cover.COVER_EXTENSIONS: + try: + cover_data = cf.read(name)[name].read() + except (py7zr.Bad7zFile, OSError) as ex: + log.error('7Zip file failed with error: {}'.format(ex)) + break return cover_data, extension diff --git a/cps/uploader.py b/cps/uploader.py index bf30094d..23dfc4a6 100644 --- a/cps/uploader.py +++ b/cps/uploader.py @@ -79,7 +79,7 @@ def process(tmp_file_path, original_file_name, original_file_extension, rar_exec meta = epub.get_epub_info(tmp_file_path, original_file_name, original_file_extension) elif ".FB2" == extension_upper and use_fb2_meta is True: meta = fb2.get_fb2_info(tmp_file_path, original_file_extension) - elif extension_upper in ['.CBZ', '.CBT', '.CBR']: + elif extension_upper in ['.CBZ', '.CBT', '.CBR', ".CB7"]: meta = comic.get_comic_info(tmp_file_path, original_file_name, original_file_extension, diff --git a/optional-requirements.txt b/optional-requirements.txt index ff02d04f..d34d09aa 100644 --- a/optional-requirements.txt +++ b/optional-requirements.txt @@ -35,6 +35,7 @@ html2text>=2020.1.16,<2022.1.1 python-dateutil>=2.1,<2.9.0 beautifulsoup4>=4.0.1,<4.12.0 faust-cchardet>=2.1.18 +py7zr>=0.15.0,<0.21.0 # Comics natsort>=2.2.0,<8.4.0