mirror of
https://github.com/janeczku/calibre-web
synced 2025-01-13 02:40:29 +00:00
Update metadatabackup
This commit is contained in:
parent
1210ccb43f
commit
ba7fee3918
@ -344,7 +344,7 @@ def getFileFromEbooksFolder(path, fileName):
|
|||||||
|
|
||||||
|
|
||||||
def moveGdriveFileRemote(origin_file_id, new_title):
|
def moveGdriveFileRemote(origin_file_id, new_title):
|
||||||
origin_file_id['title']= new_title
|
origin_file_id['title'] = new_title
|
||||||
origin_file_id.Upload()
|
origin_file_id.Upload()
|
||||||
|
|
||||||
|
|
||||||
@ -422,7 +422,7 @@ def copyToDrive(drive, uploadFile, createRoot, replaceFiles,
|
|||||||
driveFile.Upload()
|
driveFile.Upload()
|
||||||
|
|
||||||
|
|
||||||
def uploadFileToEbooksFolder(destFile, f):
|
def uploadFileToEbooksFolder(destFile, f, string=False):
|
||||||
drive = getDrive(Gdrive.Instance().drive)
|
drive = getDrive(Gdrive.Instance().drive)
|
||||||
parent = getEbooksFolder(drive)
|
parent = getEbooksFolder(drive)
|
||||||
splitDir = destFile.split('/')
|
splitDir = destFile.split('/')
|
||||||
@ -435,7 +435,10 @@ def uploadFileToEbooksFolder(destFile, f):
|
|||||||
else:
|
else:
|
||||||
driveFile = drive.CreateFile({'title': x,
|
driveFile = drive.CreateFile({'title': x,
|
||||||
'parents': [{"kind": "drive#fileLink", 'id': parent['id']}], })
|
'parents': [{"kind": "drive#fileLink", 'id': parent['id']}], })
|
||||||
driveFile.SetContentFile(f)
|
if not string:
|
||||||
|
driveFile.SetContentFile(f)
|
||||||
|
else:
|
||||||
|
driveFile.SetContentString(f)
|
||||||
driveFile.Upload()
|
driveFile.Upload()
|
||||||
else:
|
else:
|
||||||
existing_Folder = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
existing_Folder = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
||||||
|
@ -17,10 +17,9 @@
|
|||||||
# 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 os
|
import os
|
||||||
from lxml import objectify
|
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from html import escape
|
|
||||||
|
|
||||||
from cps import config, db, gdriveutils, logger
|
from cps import config, db, gdriveutils, logger
|
||||||
from cps.services.worker import CalibreTask
|
from cps.services.worker import CalibreTask
|
||||||
@ -102,50 +101,29 @@ class TaskBackupMetadata(CalibreTask):
|
|||||||
self.calibre_db.session.close()
|
self.calibre_db.session.close()
|
||||||
|
|
||||||
def open_metadata(self, book, custom_columns):
|
def open_metadata(self, book, custom_columns):
|
||||||
|
package = self.create_new_metadata_backup(book, custom_columns)
|
||||||
if config.config_use_google_drive:
|
if config.config_use_google_drive:
|
||||||
if not gdriveutils.is_gdrive_ready():
|
if not gdriveutils.is_gdrive_ready():
|
||||||
raise Exception('Google Drive is configured but not ready')
|
raise Exception('Google Drive is configured but not ready')
|
||||||
|
|
||||||
web_content_link = gdriveutils.get_metadata_backup_via_gdrive(book.path)
|
gdriveutils.uploadFileToEbooksFolder(os.path.join(book.path, 'metadata.opf').replace("\\", "/"),
|
||||||
if not web_content_link:
|
etree.tostring(package,
|
||||||
raise Exception('Google Drive cover url not found')
|
xml_declaration=True,
|
||||||
|
encoding='utf-8',
|
||||||
stream = None
|
pretty_print=True).decode('utf-8'),
|
||||||
try:
|
True)
|
||||||
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()
|
|
||||||
else:
|
else:
|
||||||
# ToDo: Handle book folder not found or not readable
|
# ToDo: Handle book folder not found or not readable
|
||||||
book_metadata_filepath = os.path.join(config.config_calibre_dir, book.path, 'metadata.opf')
|
book_metadata_filepath = os.path.join(config.config_calibre_dir, book.path, 'metadata.opf')
|
||||||
#if not os.path.isfile(book_metadata_filepath):
|
# prepare finalize everything and output
|
||||||
self.create_new_metadata_backup(book, custom_columns, book_metadata_filepath)
|
doc = etree.ElementTree(package)
|
||||||
# else:
|
try:
|
||||||
'''namespaces = {'dc': PURL_NAMESPACE, 'opf': OPF_NAMESPACE}
|
with open(book_metadata_filepath, 'wb') as f:
|
||||||
test = etree.parse(book_metadata_filepath)
|
doc.write(f, xml_declaration=True, encoding='utf-8', pretty_print=True)
|
||||||
root = test.getroot()
|
except Exception as ex:
|
||||||
for i in root.iter():
|
raise Exception('Writing Metadata failed with error: {} '.format(ex))
|
||||||
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)
|
def create_new_metadata_backup(self, book, custom_columns):
|
||||||
# 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):
|
|
||||||
# generate root package element
|
# generate root package element
|
||||||
package = etree.Element(OPF + "package", nsmap=OPF_NS)
|
package = etree.Element(OPF + "package", nsmap=OPF_NS)
|
||||||
package.set("unique-identifier", "uuid_id")
|
package.set("unique-identifier", "uuid_id")
|
||||||
@ -230,14 +208,7 @@ class TaskBackupMetadata(CalibreTask):
|
|||||||
guide = etree.SubElement(package, "guide")
|
guide = etree.SubElement(package, "guide")
|
||||||
etree.SubElement(guide, "reference", type="cover", title=self.translated_title, href="cover.jpg")
|
etree.SubElement(guide, "reference", type="cover", title=self.translated_title, href="cover.jpg")
|
||||||
|
|
||||||
# prepare finalize everything and output
|
return package
|
||||||
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))
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user