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)
|
|
|
|
# Copyright (C) 2018 OzzieIsaacs
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
from . import logger, isoLanguages
|
|
|
|
from .constants import BookMeta
|
|
|
|
|
|
|
|
|
|
|
|
log = logger.create()
|
2017-12-02 16:33:55 +00:00
|
|
|
|
2019-05-30 08:43:28 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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 = ''
|
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
|
2019-05-30 08:43:28 +00:00
|
|
|
use_comic_meta = False
|
2017-12-02 16:33:55 +00:00
|
|
|
|
2020-12-09 18:02:10 +00:00
|
|
|
NO_JPEG_EXTENSIONS = ['.png', '.webp', '.bmp']
|
|
|
|
COVER_EXTENSIONS = ['.png', '.webp', '.bmp', '.jpg', '.jpeg']
|
2020-12-09 13:18:39 +00:00
|
|
|
|
2020-05-02 09:24:30 +00:00
|
|
|
def _cover_processing(tmp_file_name, img, extension):
|
2020-12-09 13:18:39 +00:00
|
|
|
tmp_cover_name = os.path.join(os.path.dirname(tmp_file_name), 'cover.jpg')
|
2022-01-22 09:31:18 +00:00
|
|
|
if extension in NO_JPEG_EXTENSIONS:
|
|
|
|
if use_IM:
|
|
|
|
with Image(blob=img) as imgc:
|
2020-12-09 13:18:39 +00:00
|
|
|
imgc.format = 'jpeg'
|
|
|
|
imgc.transform_colorspace('rgb')
|
2022-01-22 09:31:18 +00:00
|
|
|
imgc.save(filename=tmp_cover_name)
|
2020-12-09 13:18:39 +00:00
|
|
|
return tmp_cover_name
|
2022-01-22 09:31:18 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
if img:
|
|
|
|
with open(tmp_cover_name, 'wb') as f:
|
|
|
|
f.write(img)
|
|
|
|
return tmp_cover_name
|
|
|
|
else:
|
2020-10-31 21:28:24 +00:00
|
|
|
return None
|
|
|
|
|
2020-05-02 09:24:30 +00:00
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
def _extract_Cover_from_archive(original_file_extension, tmp_file_name, rarExecutable):
|
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()
|
|
|
|
if extension in COVER_EXTENSIONS:
|
|
|
|
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()
|
|
|
|
if extension in COVER_EXTENSIONS:
|
|
|
|
cover_data = cf.extractfile(name).read()
|
|
|
|
break
|
|
|
|
elif original_file_extension.upper() == '.CBR' and use_rarfile:
|
|
|
|
try:
|
|
|
|
rarfile.UNRAR_TOOL = rarExecutable
|
|
|
|
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()
|
|
|
|
if extension in COVER_EXTENSIONS:
|
|
|
|
cover_data = cf.read(name)
|
|
|
|
break
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
log.debug('Rarfile failed with error: %s', ex)
|
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
|
|
|
|
2020-09-20 13:03:25 +00:00
|
|
|
def _extractCover(tmp_file_name, original_file_extension, rarExecutable):
|
2020-04-27 10:03:54 +00:00
|
|
|
cover_data = extension = None
|
2019-05-30 08:43:28 +00:00
|
|
|
if use_comic_meta:
|
2020-09-20 13:03:25 +00:00
|
|
|
archive = ComicArchive(tmp_file_name, rar_exe_path=rarExecutable)
|
2019-08-03 12:09:54 +00:00
|
|
|
for index, name in enumerate(archive.getPageNameList()):
|
|
|
|
ext = os.path.splitext(name)
|
|
|
|
if len(ext) > 1:
|
|
|
|
extension = ext[1].lower()
|
2020-12-09 13:18:39 +00:00
|
|
|
if extension in COVER_EXTENSIONS:
|
2019-08-03 12:09:54 +00:00
|
|
|
cover_data = archive.getPage(index)
|
|
|
|
break
|
2019-05-30 08:43:28 +00:00
|
|
|
else:
|
2022-01-22 09:31:18 +00:00
|
|
|
cover_data, extension = _extract_Cover_from_archive(original_file_extension, tmp_file_name, rarExecutable)
|
2020-05-02 09:24:30 +00:00
|
|
|
return _cover_processing(tmp_file_name, cover_data, extension)
|
2017-12-02 16:33:55 +00:00
|
|
|
|
|
|
|
|
2020-09-20 13:03:25 +00:00
|
|
|
def get_comic_info(tmp_file_path, original_file_name, original_file_extension, rarExecutable):
|
2019-05-30 08:43:28 +00:00
|
|
|
if use_comic_meta:
|
2020-09-20 13:03:25 +00:00
|
|
|
archive = ComicArchive(tmp_file_path, rar_exe_path=rarExecutable)
|
2019-05-30 08:43:28 +00:00
|
|
|
if archive.seemsToBeAComicArchive():
|
|
|
|
if archive.hasMetadata(MetaDataStyle.CIX):
|
|
|
|
style = MetaDataStyle.CIX
|
|
|
|
elif archive.hasMetadata(MetaDataStyle.CBI):
|
|
|
|
style = MetaDataStyle.CBI
|
|
|
|
else:
|
|
|
|
style = None
|
2017-12-02 16:33:55 +00:00
|
|
|
|
2019-08-03 12:09:54 +00:00
|
|
|
# if style is not None:
|
|
|
|
loadedMetadata = archive.readMetadata(style)
|
2017-12-02 16:33:55 +00:00
|
|
|
|
2020-09-20 13:03:25 +00:00
|
|
|
lang = loadedMetadata.language or ""
|
2020-09-20 09:44:03 +00:00
|
|
|
loadedMetadata.language = isoLanguages.get_lang3(lang)
|
2019-04-02 22:52:45 +00:00
|
|
|
|
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,
|
|
|
|
title=loadedMetadata.title or original_file_name,
|
2021-03-14 12:28:52 +00:00
|
|
|
author=" & ".join([credit["person"]
|
|
|
|
for credit in loadedMetadata.credits if credit["role"] == "Writer"]) or u'Unknown',
|
2020-09-20 13:03:25 +00:00
|
|
|
cover=_extractCover(tmp_file_path, original_file_extension, rarExecutable),
|
2019-05-30 08:43:28 +00:00
|
|
|
description=loadedMetadata.comments or "",
|
|
|
|
tags="",
|
|
|
|
series=loadedMetadata.series or "",
|
|
|
|
series_id=loadedMetadata.issue or "",
|
2021-03-17 18:06:51 +00:00
|
|
|
languages=loadedMetadata.language,
|
|
|
|
publisher="")
|
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,
|
|
|
|
author=u'Unknown',
|
2020-09-20 13:03:25 +00:00
|
|
|
cover=_extractCover(tmp_file_path, original_file_extension, rarExecutable),
|
2020-04-28 14:50:08 +00:00
|
|
|
description="",
|
|
|
|
tags="",
|
|
|
|
series="",
|
|
|
|
series_id="",
|
2021-03-17 18:06:51 +00:00
|
|
|
languages="",
|
|
|
|
publisher="")
|