|
|
@ -17,14 +17,34 @@
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
import tarfile
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import uploader
|
|
|
|
import uploader
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from iso639 import languages as isoLanguages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger("book_formats")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
from comicapi.comicarchive import ComicArchive, MetaDataStyle
|
|
|
|
|
|
|
|
use_comic_meta = True
|
|
|
|
|
|
|
|
except ImportError as e:
|
|
|
|
|
|
|
|
logger.warning('cannot import comicapi, extracting comic metadata will not work: %s', e)
|
|
|
|
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
import tarfile
|
|
|
|
|
|
|
|
use_comic_meta = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extractCover(tmp_file_name, original_file_extension):
|
|
|
|
def extractCover(tmp_file_name, original_file_extension):
|
|
|
|
|
|
|
|
if use_comic_meta:
|
|
|
|
|
|
|
|
archive = ComicArchive(tmp_file_name)
|
|
|
|
cover_data = None
|
|
|
|
cover_data = None
|
|
|
|
|
|
|
|
ext = os.path.splitext(archive.getPageName(0))
|
|
|
|
|
|
|
|
if len(ext) > 1:
|
|
|
|
|
|
|
|
extension = ext[1].lower()
|
|
|
|
|
|
|
|
if extension == '.jpg' or extension == '.jpeg':
|
|
|
|
|
|
|
|
cover_data = archive.getPage(0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
if original_file_extension.upper() == '.CBZ':
|
|
|
|
if original_file_extension.upper() == '.CBZ':
|
|
|
|
cf = zipfile.ZipFile(tmp_file_name)
|
|
|
|
cf = zipfile.ZipFile(tmp_file_name)
|
|
|
|
for name in cf.namelist():
|
|
|
|
for name in cf.namelist():
|
|
|
@ -43,7 +63,6 @@ def extractCover(tmp_file_name, original_file_extension):
|
|
|
|
if extension == '.jpg':
|
|
|
|
if extension == '.jpg':
|
|
|
|
cover_data = cf.extractfile(name).read()
|
|
|
|
cover_data = cf.extractfile(name).read()
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
prefix = os.path.dirname(tmp_file_name)
|
|
|
|
prefix = os.path.dirname(tmp_file_name)
|
|
|
|
if cover_data:
|
|
|
|
if cover_data:
|
|
|
|
tmp_cover_name = prefix + '/cover' + extension
|
|
|
|
tmp_cover_name = prefix + '/cover' + extension
|
|
|
@ -56,15 +75,46 @@ def extractCover(tmp_file_name, original_file_extension):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_comic_info(tmp_file_path, original_file_name, original_file_extension):
|
|
|
|
def get_comic_info(tmp_file_path, original_file_name, original_file_extension):
|
|
|
|
|
|
|
|
if use_comic_meta:
|
|
|
|
|
|
|
|
archive = ComicArchive(tmp_file_path)
|
|
|
|
|
|
|
|
if archive.seemsToBeAComicArchive():
|
|
|
|
|
|
|
|
if archive.hasMetadata(MetaDataStyle.CIX):
|
|
|
|
|
|
|
|
style = MetaDataStyle.CIX
|
|
|
|
|
|
|
|
elif archive.hasMetadata(MetaDataStyle.CBI):
|
|
|
|
|
|
|
|
style = MetaDataStyle.CBI
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
style = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if style is not None:
|
|
|
|
|
|
|
|
loadedMetadata = archive.readMetadata(style)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lang = loadedMetadata.language
|
|
|
|
|
|
|
|
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 = ""
|
|
|
|
|
|
|
|
|
|
|
|
coverfile = extractCover(tmp_file_path, original_file_extension)
|
|
|
|
return uploader.BookMeta(
|
|
|
|
|
|
|
|
file_path=tmp_file_path,
|
|
|
|
|
|
|
|
extension=original_file_extension,
|
|
|
|
|
|
|
|
title=loadedMetadata.title or original_file_name,
|
|
|
|
|
|
|
|
author=" & ".join([credit["person"] for credit in loadedMetadata.credits if credit["role"] == "Writer"]) or u"Unknown",
|
|
|
|
|
|
|
|
cover=extractCover(tmp_file_path, original_file_extension),
|
|
|
|
|
|
|
|
description=loadedMetadata.comments or "",
|
|
|
|
|
|
|
|
tags="",
|
|
|
|
|
|
|
|
series=loadedMetadata.series or "",
|
|
|
|
|
|
|
|
series_id=loadedMetadata.issue or "",
|
|
|
|
|
|
|
|
languages=loadedMetadata.language)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
|
|
return uploader.BookMeta(
|
|
|
|
return uploader.BookMeta(
|
|
|
|
file_path=tmp_file_path,
|
|
|
|
file_path=tmp_file_path,
|
|
|
|
extension=original_file_extension,
|
|
|
|
extension=original_file_extension,
|
|
|
|
title=original_file_name,
|
|
|
|
title=original_file_name,
|
|
|
|
author=u"Unknown",
|
|
|
|
author=u"Unknown",
|
|
|
|
cover=coverfile,
|
|
|
|
cover=extractCover(tmp_file_path, original_file_extension),
|
|
|
|
description="",
|
|
|
|
description="",
|
|
|
|
tags="",
|
|
|
|
tags="",
|
|
|
|
series="",
|
|
|
|
series="",
|
|
|
|