2017-12-02 18:33:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 20:37:45 +02: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/>.
|
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
from __future__ import division, print_function, unicode_literals
|
2017-12-02 18:33:55 +02:00
|
|
|
import os
|
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
from . import logger, isoLanguages
|
|
|
|
from .constants import BookMeta
|
|
|
|
|
|
|
|
|
|
|
|
log = logger.create()
|
2017-12-02 18:33:55 +02:00
|
|
|
|
2019-05-30 10:43:28 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
from comicapi.comicarchive import ComicArchive, MetaDataStyle
|
|
|
|
use_comic_meta = True
|
|
|
|
except ImportError as e:
|
2019-07-21 08:10:23 +02:00
|
|
|
log.debug('cannot import comicapi, extracting comic metadata will not work: %s', e)
|
2019-05-30 10:43:28 +02:00
|
|
|
import zipfile
|
|
|
|
import tarfile
|
2020-04-28 16:50:08 +02:00
|
|
|
try:
|
|
|
|
import rarfile
|
|
|
|
use_rarfile = True
|
|
|
|
except ImportError as e:
|
|
|
|
log.debug('cannot import rarfile, extracting cover files from rar files will not work: %s', e)
|
|
|
|
use_rarfile = False
|
2019-05-30 10:43:28 +02:00
|
|
|
use_comic_meta = False
|
2017-12-02 18:33:55 +02:00
|
|
|
|
2019-05-30 10:43:28 +02:00
|
|
|
|
2020-04-28 16:50:08 +02:00
|
|
|
def _extractCover(tmp_file_name, original_file_extension, rarExceutable):
|
2020-04-27 12:03:54 +02:00
|
|
|
cover_data = extension = None
|
2019-05-30 10:43:28 +02:00
|
|
|
if use_comic_meta:
|
|
|
|
archive = ComicArchive(tmp_file_name)
|
2019-08-03 14:09:54 +02:00
|
|
|
for index, name in enumerate(archive.getPageNameList()):
|
|
|
|
ext = os.path.splitext(name)
|
|
|
|
if len(ext) > 1:
|
|
|
|
extension = ext[1].lower()
|
|
|
|
if extension == '.jpg' or extension == '.jpeg':
|
|
|
|
cover_data = archive.getPage(index)
|
|
|
|
break
|
2019-05-30 10:43:28 +02:00
|
|
|
else:
|
|
|
|
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()
|
2019-08-03 14:09:54 +02:00
|
|
|
if extension == '.jpg' or extension == '.jpeg':
|
2019-05-30 10:43:28 +02: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()
|
2019-08-03 14:09:54 +02:00
|
|
|
if extension == '.jpg' or extension == '.jpeg':
|
2019-05-30 10:43:28 +02:00
|
|
|
cover_data = cf.extractfile(name).read()
|
|
|
|
break
|
2020-04-28 16:50:08 +02:00
|
|
|
elif original_file_extension.upper() == '.CBR' and use_rarfile:
|
|
|
|
try:
|
|
|
|
rarfile.UNRAR_TOOL = rarExceutable
|
|
|
|
cf = rarfile.RarFile(tmp_file_name)
|
|
|
|
for name in cf.getnames():
|
|
|
|
ext = os.path.splitext(name)
|
|
|
|
if len(ext) > 1:
|
|
|
|
extension = ext[1].lower()
|
|
|
|
if extension == '.jpg' or extension == '.jpeg':
|
|
|
|
cover_data = cf.read(name)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
log.debug('Rarfile failed with error: %s', e)
|
|
|
|
|
2017-12-02 18:33:55 +02:00
|
|
|
prefix = os.path.dirname(tmp_file_name)
|
2019-03-05 22:28:55 +02:00
|
|
|
if cover_data:
|
|
|
|
tmp_cover_name = prefix + '/cover' + extension
|
|
|
|
image = open(tmp_cover_name, 'wb')
|
|
|
|
image.write(cover_data)
|
|
|
|
image.close()
|
|
|
|
else:
|
|
|
|
tmp_cover_name = None
|
2017-12-02 18:33:55 +02:00
|
|
|
return tmp_cover_name
|
|
|
|
|
|
|
|
|
2020-04-28 16:50:08 +02:00
|
|
|
def get_comic_info(tmp_file_path, original_file_name, original_file_extension, rarExceutable):
|
2019-05-30 10:43:28 +02:00
|
|
|
if use_comic_meta:
|
2020-04-28 16:50:08 +02:00
|
|
|
archive = ComicArchive(tmp_file_path, rar_exe_path=rarExceutable)
|
2019-05-30 10:43:28 +02: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 18:33:55 +02:00
|
|
|
|
2019-08-03 14:09:54 +02:00
|
|
|
# if style is not None:
|
|
|
|
loadedMetadata = archive.readMetadata(style)
|
2017-12-02 18:33:55 +02:00
|
|
|
|
2019-08-03 14:09:54 +02:00
|
|
|
lang = loadedMetadata.language
|
|
|
|
if lang:
|
|
|
|
if len(lang) == 2:
|
|
|
|
loadedMetadata.language = isoLanguages.get(part1=lang).name
|
|
|
|
elif len(lang) == 3:
|
|
|
|
loadedMetadata.language = isoLanguages.get(part3=lang).name
|
|
|
|
else:
|
|
|
|
loadedMetadata.language = ""
|
2019-04-03 00:52:45 +02:00
|
|
|
|
2020-04-28 16:50:08 +02:00
|
|
|
return BookMeta(
|
2019-05-30 10:43:28 +02:00
|
|
|
file_path=tmp_file_path,
|
|
|
|
extension=original_file_extension,
|
|
|
|
title=loadedMetadata.title or original_file_name,
|
2019-12-09 21:53:16 +02:00
|
|
|
author=" & ".join([credit["person"] for credit in loadedMetadata.credits if credit["role"] == "Writer"]) or u'Unknown',
|
2020-04-28 16:50:08 +02:00
|
|
|
cover=_extractCover(tmp_file_path, original_file_extension, rarExceutable),
|
2019-05-30 10:43:28 +02:00
|
|
|
description=loadedMetadata.comments or "",
|
|
|
|
tags="",
|
|
|
|
series=loadedMetadata.series or "",
|
|
|
|
series_id=loadedMetadata.issue or "",
|
|
|
|
languages=loadedMetadata.language)
|
2017-12-02 18:33:55 +02:00
|
|
|
|
2020-04-28 16:50:08 +02:00
|
|
|
return BookMeta(
|
|
|
|
file_path=tmp_file_path,
|
|
|
|
extension=original_file_extension,
|
|
|
|
title=original_file_name,
|
|
|
|
author=u'Unknown',
|
|
|
|
cover=_extractCover(tmp_file_path, original_file_extension, rarExceutable),
|
|
|
|
description="",
|
|
|
|
tags="",
|
|
|
|
series="",
|
|
|
|
series_id="",
|
|
|
|
languages="")
|