2017-12-02 16:33:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 18:37:45 +00:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
2022-03-12 17:01:11 +00:00
|
|
|
# Copyright (C) 2018-2022 OzzieIsaacs
|
2019-01-20 18:37:45 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-12-02 16:33:55 +00:00
|
|
|
import os
|
|
|
|
|
2022-03-12 17:01:11 +00:00
|
|
|
from . import logger, isoLanguages, cover
|
2019-07-13 18:45:48 +00:00
|
|
|
from .constants import BookMeta
|
|
|
|
|
2020-12-09 13:18:39 +00:00
|
|
|
try:
|
|
|
|
from wand.image import Image
|
|
|
|
use_IM = True
|
|
|
|
except (ImportError, RuntimeError) as e:
|
|
|
|
use_IM = False
|
|
|
|
|
2022-03-12 17:01:11 +00:00
|
|
|
log = logger.create()
|
2020-12-09 13:18:39 +00:00
|
|
|
|
2019-05-30 08:43:28 +00:00
|
|
|
try:
|
|
|
|
from comicapi.comicarchive import ComicArchive, MetaDataStyle
|
|
|
|
use_comic_meta = True
|
2020-05-23 14:20:19 +00:00
|
|
|
try:
|
|
|
|
from comicapi import __version__ as comic_version
|
2020-11-30 17:32:21 +00:00
|
|
|
except ImportError:
|
2020-05-23 14:20:19 +00:00
|
|
|
comic_version = ''
|
2023-06-25 12:39:45 +00:00
|
|
|
try:
|
|
|
|
from comicapi.comicarchive import load_archive_plugins
|
|
|
|
import comicapi.utils
|
|
|
|
comicapi.utils.add_rar_paths()
|
|
|
|
except ImportError:
|
|
|
|
load_archive_plugins = None
|
2020-06-07 13:41:53 +00:00
|
|
|
except (ImportError, LookupError) as e:
|
2020-05-23 14:20:19 +00:00
|
|
|
log.debug('Cannot import comicapi, extracting comic metadata will not work: %s', e)
|
2019-05-30 08:43:28 +00:00
|
|
|
import zipfile
|
|
|
|
import tarfile
|
2020-04-28 14:50:08 +00:00
|
|
|
try:
|
|
|
|
import rarfile
|
|
|
|
use_rarfile = True
|
2020-11-30 17:32:21 +00:00
|
|
|
except (ImportError, SyntaxError) as e:
|
2020-05-23 14:20:19 +00:00
|
|
|
log.debug('Cannot import rarfile, extracting cover files from rar files will not work: %s', e)
|
2020-04-28 14:50:08 +00:00
|
|
|
use_rarfile = False
|
2023-08-16 16:44:03 +00:00
|
|
|
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
|
2019-05-30 08:43:28 +00:00
|
|
|
use_comic_meta = False
|
2017-12-02 16:33:55 +00:00
|
|
|
|
2020-05-02 09:24:30 +00:00
|
|
|
|
2022-03-12 17:01:11 +00:00
|
|
|
def _extract_cover_from_archive(original_file_extension, tmp_file_name, rar_executable):
|
2022-01-22 09:31:18 +00:00
|
|
|
cover_data = extension = None
|
2021-03-14 12:28:52 +00:00
|
|
|
if original_file_extension.upper() == '.CBZ':
|
|
|
|
cf = zipfile.ZipFile(tmp_file_name)
|
|
|
|
for name in cf.namelist():
|
|
|
|
ext = os.path.splitext(name)
|
|
|
|
if len(ext) > 1:
|
|
|
|
extension = ext[1].lower()
|
2022-03-12 17:01:11 +00:00
|
|
|
if extension in cover.COVER_EXTENSIONS:
|
2021-03-14 12:28:52 +00:00
|
|
|
cover_data = cf.read(name)
|
|
|
|
break
|
|
|
|
elif original_file_extension.upper() == '.CBT':
|
|
|
|
cf = tarfile.TarFile(tmp_file_name)
|
|
|
|
for name in cf.getnames():
|
|
|
|
ext = os.path.splitext(name)
|
|
|
|
if len(ext) > 1:
|
|
|
|
extension = ext[1].lower()
|
2022-03-12 17:01:11 +00:00
|
|
|
if extension in cover.COVER_EXTENSIONS:
|
2021-03-14 12:28:52 +00:00
|
|
|
cover_data = cf.extractfile(name).read()
|
|
|
|
break
|
|
|
|
elif original_file_extension.upper() == '.CBR' and use_rarfile:
|
|
|
|
try:
|
2022-03-12 17:01:11 +00:00
|
|
|
rarfile.UNRAR_TOOL = rar_executable
|
2021-03-14 12:28:52 +00:00
|
|
|
cf = rarfile.RarFile(tmp_file_name)
|
2022-02-19 09:04:21 +00:00
|
|
|
for name in cf.namelist():
|
2021-03-14 12:28:52 +00:00
|
|
|
ext = os.path.splitext(name)
|
|
|
|
if len(ext) > 1:
|
|
|
|
extension = ext[1].lower()
|
2022-03-12 17:01:11 +00:00
|
|
|
if extension in cover.COVER_EXTENSIONS:
|
2023-08-16 16:44:03 +00:00
|
|
|
cover_data = cf.read([name])
|
2021-03-14 12:28:52 +00:00
|
|
|
break
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
2023-08-16 16:44:03 +00:00
|
|
|
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:
|
2024-05-10 07:05:31 +00:00
|
|
|
cover_data = cf.read([name])[name].read()
|
2023-08-16 16:44:03 +00:00
|
|
|
except (py7zr.Bad7zFile, OSError) as ex:
|
|
|
|
log.error('7Zip file failed with error: {}'.format(ex))
|
|
|
|
break
|
2022-01-22 09:31:18 +00:00
|
|
|
return cover_data, extension
|
2021-03-14 12:28:52 +00:00
|
|
|
|
2019-05-30 08:43:28 +00:00
|
|
|
|
2022-03-12 17:01:11 +00:00
|
|
|
def _extract_cover(tmp_file_name, original_file_extension, rar_executable):
|
2020-04-27 10:03:54 +00:00
|
|
|
cover_data = extension = None
|
2019-05-30 08:43:28 +00:00
|
|
|
if use_comic_meta:
|
2023-06-25 12:39:45 +00:00
|
|
|
try:
|
|
|
|
archive = ComicArchive(tmp_file_name, rar_exe_path=rar_executable)
|
|
|
|
except TypeError:
|
|
|
|
archive = ComicArchive(tmp_file_name)
|
2023-05-04 17:23:02 +00:00
|
|
|
name_list = archive.getPageNameList if hasattr(archive, "getPageNameList") else archive.get_page_name_list
|
|
|
|
for index, name in enumerate(name_list()):
|
2019-08-03 12:09:54 +00:00
|
|
|
ext = os.path.splitext(name)
|
|
|
|
if len(ext) > 1:
|
|
|
|
extension = ext[1].lower()
|
2022-03-12 17:01:11 +00:00
|
|
|
if extension in cover.COVER_EXTENSIONS:
|
2023-05-04 17:23:02 +00:00
|
|
|
get_page = archive.getPage if hasattr(archive, "getPageNameList") else archive.get_page
|
|
|
|
cover_data = get_page(index)
|
2019-08-03 12:09:54 +00:00
|
|
|
break
|
2019-05-30 08:43:28 +00:00
|
|
|
else:
|
2022-03-12 17:01:11 +00:00
|
|
|
cover_data, extension = _extract_cover_from_archive(original_file_extension, tmp_file_name, rar_executable)
|
|
|
|
return cover.cover_processing(tmp_file_name, cover_data, extension)
|
2017-12-02 16:33:55 +00:00
|
|
|
|
|
|
|
|
2024-08-18 08:12:04 +00:00
|
|
|
def get_comic_info(tmp_file_path, original_file_name, original_file_extension, rar_executable, no_cover_processing):
|
2019-05-30 08:43:28 +00:00
|
|
|
if use_comic_meta:
|
2023-06-25 12:39:45 +00:00
|
|
|
try:
|
|
|
|
archive = ComicArchive(tmp_file_path, rar_exe_path=rar_executable)
|
|
|
|
except TypeError:
|
|
|
|
load_archive_plugins(force=True, rar=rar_executable)
|
|
|
|
archive = ComicArchive(tmp_file_path)
|
2023-05-04 17:23:02 +00:00
|
|
|
if hasattr(archive, "seemsToBeAComicArchive"):
|
|
|
|
seems_archive = archive.seemsToBeAComicArchive
|
|
|
|
else:
|
|
|
|
seems_archive = archive.seems_to_be_a_comic_archive
|
|
|
|
if seems_archive():
|
|
|
|
has_metadata = archive.hasMetadata if hasattr(archive, "hasMetadata") else archive.has_metadata
|
|
|
|
if has_metadata(MetaDataStyle.CIX):
|
2019-05-30 08:43:28 +00:00
|
|
|
style = MetaDataStyle.CIX
|
2023-05-04 17:23:02 +00:00
|
|
|
elif has_metadata(MetaDataStyle.CBI):
|
2019-05-30 08:43:28 +00:00
|
|
|
style = MetaDataStyle.CBI
|
|
|
|
else:
|
|
|
|
style = None
|
2017-12-02 16:33:55 +00:00
|
|
|
|
2023-05-04 17:23:02 +00:00
|
|
|
read_metadata = archive.readMetadata if hasattr(archive, "readMetadata") else archive.read_metadata
|
|
|
|
loaded_metadata = read_metadata(style)
|
2017-12-02 16:33:55 +00:00
|
|
|
|
2022-03-12 17:01:11 +00:00
|
|
|
lang = loaded_metadata.language or ""
|
|
|
|
loaded_metadata.language = isoLanguages.get_lang3(lang)
|
2024-08-18 08:12:04 +00:00
|
|
|
if not no_cover_processing:
|
|
|
|
cover_file = _extract_cover(tmp_file_path, original_file_extension, rar_executable)
|
|
|
|
else:
|
|
|
|
cover_file = None
|
2020-04-28 14:50:08 +00:00
|
|
|
return BookMeta(
|
2019-05-30 08:43:28 +00:00
|
|
|
file_path=tmp_file_path,
|
|
|
|
extension=original_file_extension,
|
2022-03-12 17:01:11 +00:00
|
|
|
title=loaded_metadata.title or original_file_name,
|
2021-03-14 12:28:52 +00:00
|
|
|
author=" & ".join([credit["person"]
|
2022-03-12 17:01:11 +00:00
|
|
|
for credit in loaded_metadata.credits if credit["role"] == "Writer"]) or 'Unknown',
|
2024-08-18 08:12:04 +00:00
|
|
|
cover=cover_file,
|
2022-03-12 17:01:11 +00:00
|
|
|
description=loaded_metadata.comments or "",
|
2019-05-30 08:43:28 +00:00
|
|
|
tags="",
|
2022-03-12 17:01:11 +00:00
|
|
|
series=loaded_metadata.series or "",
|
|
|
|
series_id=loaded_metadata.issue or "",
|
|
|
|
languages=loaded_metadata.language,
|
2022-04-05 00:26:35 +00:00
|
|
|
publisher="",
|
|
|
|
pubdate="",
|
|
|
|
identifiers=[])
|
2024-08-18 08:12:04 +00:00
|
|
|
if not no_cover_processing:
|
|
|
|
cover_file = _extract_cover(tmp_file_path, original_file_extension, rar_executable)
|
|
|
|
else:
|
|
|
|
cover_file = None
|
2017-12-02 16:33:55 +00:00
|
|
|
|
2020-04-28 14:50:08 +00:00
|
|
|
return BookMeta(
|
|
|
|
file_path=tmp_file_path,
|
|
|
|
extension=original_file_extension,
|
|
|
|
title=original_file_name,
|
2023-01-21 14:23:18 +00:00
|
|
|
author='Unknown',
|
2024-08-18 08:12:04 +00:00
|
|
|
cover=cover_file,
|
2020-04-28 14:50:08 +00:00
|
|
|
description="",
|
|
|
|
tags="",
|
|
|
|
series="",
|
|
|
|
series_id="",
|
2021-03-17 18:06:51 +00:00
|
|
|
languages="",
|
2022-04-05 00:26:35 +00:00
|
|
|
publisher="",
|
|
|
|
pubdate="",
|
|
|
|
identifiers=[])
|