From 319077aea9356aa03fa95ff9541040eec0c555d5 Mon Sep 17 00:00:00 2001 From: OzzieIsaacs Date: Sat, 2 Dec 2017 17:33:55 +0100 Subject: [PATCH] Import covers from comic files during upload --- cps/book_formats.py | 4 ++++ cps/cli.py | 3 +++ cps/comic.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 cps/comic.py diff --git a/cps/book_formats.py b/cps/book_formats.py index 5cff0232..bc9e889e 100644 --- a/cps/book_formats.py +++ b/cps/book_formats.py @@ -5,6 +5,7 @@ import logging import uploader import os from flask_babel import gettext as _ +import comic __author__ = 'lemmsh' @@ -49,6 +50,9 @@ def process(tmp_file_path, original_file_name, original_file_extension): meta = epub.get_epub_info(tmp_file_path, original_file_name, original_file_extension) if ".FB2" == original_file_extension.upper() and use_fb2_meta is True: meta = fb2.get_fb2_info(tmp_file_path, original_file_extension) + if original_file_extension.upper() in ['.CBZ', '.CBT']: + meta = comic.get_comic_info(tmp_file_path, original_file_name, original_file_extension) + except Exception as ex: logger.warning('cannot parse metadata, using default: %s', ex) diff --git a/cps/cli.py b/cps/cli.py index d75d54c7..bd57d654 100644 --- a/cps/cli.py +++ b/cps/cli.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + import argparse import os diff --git a/cps/comic.py b/cps/comic.py new file mode 100644 index 00000000..98343ae7 --- /dev/null +++ b/cps/comic.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import zipfile +import tarfile +import os +import uploader + + +def extractCover(tmp_file_name, original_file_extension): + if original_file_extension.upper() == '.CBZ': + cf = zipfile.ZipFile(tmp_file_name) + compressed_name = cf.namelist()[0] + cover_data = cf.read(compressed_name) + elif original_file_extension.upper() == '.CBT': + cf = tarfile.TarFile(tmp_file_name) + compressed_name = cf.getnames()[0] + cover_data = cf.extractfile(compressed_name).read() + + prefix = os.path.dirname(tmp_file_name) + + tmp_cover_name = prefix + '/cover' + os.path.splitext(compressed_name)[1] + image = open(tmp_cover_name, 'wb') + image.write(cover_data) + image.close() + return tmp_cover_name + + +def get_comic_info(tmp_file_path, original_file_name, original_file_extension): + + coverfile = extractCover(tmp_file_path, original_file_extension) + + return uploader.BookMeta( + file_path=tmp_file_path, + extension=original_file_extension, + title=original_file_name, + author=u"Unknown", + cover=coverfile, + description="", + tags="", + series="", + series_id="", + languages="")