2022-09-10 16:26:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2020 monkey
|
|
|
|
#
|
|
|
|
# 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/>.
|
2022-09-14 15:03:48 +00:00
|
|
|
|
2022-09-10 16:26:52 +00:00
|
|
|
import os
|
|
|
|
from lxml import etree
|
2023-03-21 19:02:57 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
from cps import config, db, gdriveutils, logger, app
|
2023-02-25 15:31:48 +00:00
|
|
|
from cps.services.worker import CalibreTask
|
2022-09-10 16:26:52 +00:00
|
|
|
from flask_babel import lazy_gettext as N_
|
|
|
|
|
2024-01-13 09:53:46 +00:00
|
|
|
from ..epub_helper import create_new_metadata_backup
|
2022-09-10 16:26:52 +00:00
|
|
|
|
2024-06-18 18:13:26 +00:00
|
|
|
|
2022-09-10 16:26:52 +00:00
|
|
|
class TaskBackupMetadata(CalibreTask):
|
|
|
|
|
2022-09-19 16:56:22 +00:00
|
|
|
def __init__(self, export_language="en",
|
2022-09-19 20:39:40 +00:00
|
|
|
translated_title="Cover",
|
2022-09-19 16:56:22 +00:00
|
|
|
set_dirty=False,
|
|
|
|
task_message=N_('Backing up Metadata')):
|
2022-09-10 16:26:52 +00:00
|
|
|
super(TaskBackupMetadata, self).__init__(task_message)
|
|
|
|
self.log = logger.create()
|
2024-11-26 05:13:10 +00:00
|
|
|
# self.calibre_db = db.CalibreDB(expire_on_commit=False, init=True)
|
2022-09-14 15:03:48 +00:00
|
|
|
self.export_language = export_language
|
|
|
|
self.translated_title = translated_title
|
2022-09-19 20:39:40 +00:00
|
|
|
self.set_dirty = set_dirty
|
2022-09-10 16:26:52 +00:00
|
|
|
|
|
|
|
def run(self, worker_thread):
|
2022-09-19 16:56:22 +00:00
|
|
|
if self.set_dirty:
|
|
|
|
self.set_all_books_dirty()
|
|
|
|
else:
|
|
|
|
self.backup_metadata()
|
|
|
|
|
|
|
|
def set_all_books_dirty(self):
|
2024-11-26 05:13:10 +00:00
|
|
|
with app.app_context():
|
2024-11-26 19:22:20 +00:00
|
|
|
calibre_dbb = db.CalibreDB(app)
|
2024-11-26 05:13:10 +00:00
|
|
|
try:
|
2024-11-26 19:22:20 +00:00
|
|
|
books = calibre_dbb.session.query(db.Books).all()
|
2024-11-26 05:13:10 +00:00
|
|
|
for book in books:
|
2024-11-26 19:22:20 +00:00
|
|
|
calibre_dbb.set_metadata_dirty(book.id)
|
|
|
|
calibre_dbb.session.commit()
|
2024-11-26 05:13:10 +00:00
|
|
|
self._handleSuccess()
|
|
|
|
except Exception as ex:
|
|
|
|
self.log.debug('Error adding book for backup: ' + str(ex))
|
|
|
|
self._handleError('Error adding book for backup: ' + str(ex))
|
2024-11-26 19:22:20 +00:00
|
|
|
calibre_dbb.session.rollback()
|
2024-11-26 05:13:10 +00:00
|
|
|
# self.calibre_db.session.close()
|
2022-09-19 16:56:22 +00:00
|
|
|
|
|
|
|
def backup_metadata(self):
|
2024-11-26 05:13:10 +00:00
|
|
|
with app.app_context():
|
|
|
|
try:
|
2024-11-26 19:22:20 +00:00
|
|
|
calibre_dbb = db.CalibreDB(app)
|
|
|
|
metadata_backup = calibre_dbb.session.query(db.Metadata_Dirtied).all()
|
|
|
|
custom_columns = (calibre_dbb.session.query(db.CustomColumns)
|
2024-11-26 05:13:10 +00:00
|
|
|
.filter(db.CustomColumns.mark_for_delete == 0)
|
|
|
|
.filter(db.CustomColumns.datatype.notin_(db.cc_exceptions))
|
|
|
|
.order_by(db.CustomColumns.label).all())
|
|
|
|
count = len(metadata_backup)
|
|
|
|
i = 0
|
|
|
|
for backup in metadata_backup:
|
2024-11-26 19:22:20 +00:00
|
|
|
book = calibre_dbb.session.query(db.Books).filter(db.Books.id == backup.book).one_or_none()
|
|
|
|
calibre_dbb.session.query(db.Metadata_Dirtied).filter(
|
2024-11-26 05:13:10 +00:00
|
|
|
db.Metadata_Dirtied.book == backup.book).delete()
|
2024-11-26 19:22:20 +00:00
|
|
|
calibre_dbb.session.commit()
|
2024-11-26 05:13:10 +00:00
|
|
|
if book:
|
|
|
|
self.open_metadata(book, custom_columns)
|
|
|
|
else:
|
|
|
|
self.log.error("Book {} not found in database".format(backup.book))
|
|
|
|
i += 1
|
|
|
|
self.progress = (1.0 / count) * i
|
|
|
|
self._handleSuccess()
|
|
|
|
# self.calibre_db.session.close()
|
2022-09-10 16:26:52 +00:00
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
except Exception as ex:
|
|
|
|
b = "NaN" if not hasattr(book, 'id') else book.id
|
|
|
|
self.log.debug('Error creating metadata backup for book {}: '.format(b) + str(ex))
|
|
|
|
self._handleError('Error creating metadata backup: ' + str(ex))
|
2024-11-26 19:22:20 +00:00
|
|
|
calibre_dbb.session.rollback()
|
2024-11-26 05:13:10 +00:00
|
|
|
# self.calibre_db.session.close()
|
2022-09-10 16:26:52 +00:00
|
|
|
|
|
|
|
def open_metadata(self, book, custom_columns):
|
2024-01-13 09:53:46 +00:00
|
|
|
# package = self.create_new_metadata_backup(book, custom_columns)
|
2024-01-14 13:28:08 +00:00
|
|
|
package = create_new_metadata_backup(book, custom_columns, self.export_language, self.translated_title)
|
2022-09-10 16:26:52 +00:00
|
|
|
if config.config_use_google_drive:
|
|
|
|
if not gdriveutils.is_gdrive_ready():
|
|
|
|
raise Exception('Google Drive is configured but not ready')
|
|
|
|
|
2023-03-21 19:02:57 +00:00
|
|
|
gdriveutils.uploadFileToEbooksFolder(os.path.join(book.path, 'metadata.opf').replace("\\", "/"),
|
|
|
|
etree.tostring(package,
|
|
|
|
xml_declaration=True,
|
|
|
|
encoding='utf-8',
|
|
|
|
pretty_print=True).decode('utf-8'),
|
|
|
|
True)
|
2022-09-10 16:26:52 +00:00
|
|
|
else:
|
2022-09-14 15:03:48 +00:00
|
|
|
# ToDo: Handle book folder not found or not readable
|
2023-11-07 18:30:13 +00:00
|
|
|
book_metadata_filepath = os.path.join(config.get_book_path(), book.path, 'metadata.opf')
|
2023-03-21 19:02:57 +00:00
|
|
|
# prepare finalize everything and output
|
|
|
|
doc = etree.ElementTree(package)
|
|
|
|
try:
|
|
|
|
with open(book_metadata_filepath, 'wb') as f:
|
|
|
|
doc.write(f, xml_declaration=True, encoding='utf-8', pretty_print=True)
|
|
|
|
except Exception as ex:
|
|
|
|
raise Exception('Writing Metadata failed with error: {} '.format(ex))
|
|
|
|
|
2022-09-10 16:26:52 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2022-09-19 20:39:40 +00:00
|
|
|
return "Metadata backup"
|
|
|
|
|
|
|
|
# needed for logging
|
|
|
|
def __str__(self):
|
|
|
|
if self.set_dirty:
|
|
|
|
return "Queue all books for metadata backup"
|
|
|
|
else:
|
|
|
|
return "Perform metadata backup"
|
2022-09-10 16:26:52 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_cancellable(self):
|
|
|
|
return True
|