|
|
|
@ -17,10 +17,9 @@
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
from lxml import objectify
|
|
|
|
|
from urllib.request import urlopen
|
|
|
|
|
from lxml import etree
|
|
|
|
|
from html import escape
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from cps import config, db, gdriveutils, logger
|
|
|
|
|
from cps.services.worker import CalibreTask
|
|
|
|
@ -102,50 +101,29 @@ class TaskBackupMetadata(CalibreTask):
|
|
|
|
|
self.calibre_db.session.close()
|
|
|
|
|
|
|
|
|
|
def open_metadata(self, book, custom_columns):
|
|
|
|
|
package = self.create_new_metadata_backup(book, custom_columns)
|
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
|
if not gdriveutils.is_gdrive_ready():
|
|
|
|
|
raise Exception('Google Drive is configured but not ready')
|
|
|
|
|
|
|
|
|
|
web_content_link = gdriveutils.get_metadata_backup_via_gdrive(book.path)
|
|
|
|
|
if not web_content_link:
|
|
|
|
|
raise Exception('Google Drive cover url not found')
|
|
|
|
|
|
|
|
|
|
stream = None
|
|
|
|
|
try:
|
|
|
|
|
stream = urlopen(web_content_link)
|
|
|
|
|
except Exception as ex:
|
|
|
|
|
# Bubble exception to calling function
|
|
|
|
|
self.log.debug('Error reading metadata.opf: ' + str(ex)) # ToDo Check whats going on
|
|
|
|
|
raise ex
|
|
|
|
|
finally:
|
|
|
|
|
if stream is not None:
|
|
|
|
|
stream.close()
|
|
|
|
|
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)
|
|
|
|
|
else:
|
|
|
|
|
# ToDo: Handle book folder not found or not readable
|
|
|
|
|
book_metadata_filepath = os.path.join(config.config_calibre_dir, book.path, 'metadata.opf')
|
|
|
|
|
#if not os.path.isfile(book_metadata_filepath):
|
|
|
|
|
self.create_new_metadata_backup(book, custom_columns, book_metadata_filepath)
|
|
|
|
|
# else:
|
|
|
|
|
'''namespaces = {'dc': PURL_NAMESPACE, 'opf': OPF_NAMESPACE}
|
|
|
|
|
test = etree.parse(book_metadata_filepath)
|
|
|
|
|
root = test.getroot()
|
|
|
|
|
for i in root.iter():
|
|
|
|
|
self.log.info(i)
|
|
|
|
|
title = root.find("dc:metadata", namespaces)
|
|
|
|
|
pass
|
|
|
|
|
with open(book_metadata_filepath, "rb") as f:
|
|
|
|
|
xml = f.read()
|
|
|
|
|
|
|
|
|
|
root = objectify.fromstring(xml)
|
|
|
|
|
# root.metadata['{http://purl.org/dc/elements/1.1/}title']
|
|
|
|
|
# root.metadata[PURL + 'title']
|
|
|
|
|
# getattr(root.metadata, PURL +'title')
|
|
|
|
|
# test = objectify.parse()
|
|
|
|
|
pass
|
|
|
|
|
# backup not found has to be created
|
|
|
|
|
#raise Exception('Book cover file not found')'''
|
|
|
|
|
|
|
|
|
|
def create_new_metadata_backup(self, book, custom_columns, book_metadata_filepath):
|
|
|
|
|
# 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))
|
|
|
|
|
|
|
|
|
|
def create_new_metadata_backup(self, book, custom_columns):
|
|
|
|
|
# generate root package element
|
|
|
|
|
package = etree.Element(OPF + "package", nsmap=OPF_NS)
|
|
|
|
|
package.set("unique-identifier", "uuid_id")
|
|
|
|
@ -230,14 +208,7 @@ class TaskBackupMetadata(CalibreTask):
|
|
|
|
|
guide = etree.SubElement(package, "guide")
|
|
|
|
|
etree.SubElement(guide, "reference", type="cover", title=self.translated_title, href="cover.jpg")
|
|
|
|
|
|
|
|
|
|
# prepare finalize everything and output
|
|
|
|
|
doc = etree.ElementTree(package)
|
|
|
|
|
# doc = etree.tostring(package, xml_declaration=True, encoding='utf-8', pretty_print=True) # .replace(b"&quot;", b""")
|
|
|
|
|
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))
|
|
|
|
|
return package
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def name(self):
|
|
|
|
|