From e0327db08f8cbb9068530ca057fda0b3383bf23d Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 8 Jan 2024 20:51:38 +0100 Subject: [PATCH] Enable embedd metadata of kepub file during conversion --- cps/constants.py | 2 -- cps/helper.py | 8 ++++---- cps/tasks/convert.py | 23 +++++++++++++++-------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cps/constants.py b/cps/constants.py index b512d1ea..ef207e02 100644 --- a/cps/constants.py +++ b/cps/constants.py @@ -179,8 +179,6 @@ STABLE_VERSION = {'version': '0.6.22b'} NIGHTLY_VERSION = dict() NIGHTLY_VERSION[0] = '$Format:%H$' NIGHTLY_VERSION[1] = '$Format:%cI$' -# NIGHTLY_VERSION[0] = 'bb7d2c6273ae4560e83950d36d64533343623a57' -# NIGHTLY_VERSION[1] = '2018-09-09T10:13:08+02:00' # CACHE CACHE_TYPE_THUMBNAILS = 'thumbnails' diff --git a/cps/helper.py b/cps/helper.py index c4e8b6b3..42580be7 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -948,7 +948,7 @@ def do_download_file(book, book_format, client, data, headers): os.makedirs(output_path) output = os.path.join(config.config_calibre_dir, book.path, book_name + "." + book_format) gd.downloadFile(book.path, book_name + "." + book_format, output) - filename, download_name = do_calibre_export(book, book_format) + filename, download_name = do_calibre_export(book.id, book_format) else: return gd.do_gdrive_download(df, headers) else: @@ -963,7 +963,7 @@ def do_download_file(book, book_format, client, data, headers): headers["Content-Disposition"] = headers["Content-Disposition"].replace(".kepub", ".kepub.epub") if config.config_binariesdir and config.config_embed_metadata: - filename, download_name = do_calibre_export(book, book_format) + filename, download_name = do_calibre_export(book.id, book_format) else: download_name = book_name @@ -975,7 +975,7 @@ def do_download_file(book, book_format, client, data, headers): return response -def do_calibre_export(book, book_format): +def do_calibre_export(book_id, book_format): try: quotes = [3, 5, 7, 9] tmp_dir = get_temp_dir() @@ -983,7 +983,7 @@ def do_calibre_export(book, book_format): temp_file_name = str(uuid4()) opf_command = [calibredb_binarypath, 'export', '--dont-write-opf', '--with-library', config.config_calibre_dir, '--to-dir', tmp_dir, '--formats', book_format, "--template", "{}".format(temp_file_name), - str(book.id)] + str(book_id)] p = process_open(opf_command, quotes) _, err = p.communicate() if err: diff --git a/cps/tasks/convert.py b/cps/tasks/convert.py index da1cfc7b..3b0ee2ea 100644 --- a/cps/tasks/convert.py +++ b/cps/tasks/convert.py @@ -22,6 +22,7 @@ from glob import glob from shutil import copyfile, copyfileobj from markupsafe import escape from time import time +from uuid import uuid4 from sqlalchemy.exc import SQLAlchemyError from flask_babel import lazy_gettext as N_ @@ -36,7 +37,7 @@ from cps.ub import init_db_thread from cps.file_helper import get_temp_dir from cps.tasks.mail import TaskEmail -from cps import gdriveutils +from cps import gdriveutils, helper from cps.constants import SUPPORTED_CALIBRE_BINARIES log = logger.create() @@ -124,7 +125,7 @@ class TaskConvert(CalibreTask): # check to see if destination format already exists - or if book is in database # if it does - mark the conversion task as complete and return a success - # this will allow send to E-Reader workflow to continue to work + # this will allow to send to E-Reader workflow to continue to work if os.path.isfile(file_path + format_new_ext) or\ local_db.get_book_format(self.book_id, self.settings['new_book_format']): log.info("Book id %d already converted to %s", book_id, format_new_ext) @@ -207,8 +208,15 @@ class TaskConvert(CalibreTask): return def _convert_kepubify(self, file_path, format_old_ext, format_new_ext): + if config.config_embed_metadata: + tmp_dir, temp_file_name = helper.do_calibre_export(self.book_id, format_old_ext[1:]) + filename = os.path.join(tmp_dir, temp_file_name + format_old_ext) + temp_file_path = tmp_dir + else: + filename = file_path + format_old_ext + temp_file_path = os.path.dirname(file_path) quotes = [1, 3] - command = [config.config_kepubifypath, (file_path + format_old_ext), '-o', os.path.dirname(file_path)] + command = [config.config_kepubifypath, filename, '-o', temp_file_path, '-i'] try: p = process_open(command, quotes) except OSError as e: @@ -222,13 +230,12 @@ class TaskConvert(CalibreTask): if p.poll() is not None: break - # ToD Handle # process returncode check = p.returncode # move file if check == 0: - converted_file = glob(os.path.join(os.path.dirname(file_path), "*.kepub.epub")) + converted_file = glob(os.path.splitext(filename)[0] + "*.kepub.epub") if len(converted_file) == 1: copyfile(converted_file[0], (file_path + format_new_ext)) os.unlink(converted_file[0]) @@ -238,17 +245,17 @@ class TaskConvert(CalibreTask): return check, None def _convert_calibre(self, file_path, format_old_ext, format_new_ext, has_cover): - book_id = self.book_id try: + # path_tmp_opf = self._embed_metadata() if config.config_embed_metadata: quotes = [3, 5] tmp_dir = get_temp_dir() calibredb_binarypath = os.path.join(config.config_binariesdir, SUPPORTED_CALIBRE_BINARIES["calibredb"]) - opf_command = [calibredb_binarypath, 'show_metadata', '--as-opf', str(book_id), + opf_command = [calibredb_binarypath, 'show_metadata', '--as-opf', str(self.book_id), '--with-library', config.config_calibre_dir] p = process_open(opf_command, quotes) p.wait() - path_tmp_opf = os.path.join(tmp_dir, "metadata_" + str(current_milli_time()) + ".opf") + path_tmp_opf = os.path.join(tmp_dir, "metadata_" + str(uuid4()) + ".opf") with open(path_tmp_opf, 'w') as fd: copyfileobj(p.stdout, fd)