Refactored make_mobi and convert_ebook functions

This commit is contained in:
OzzieIsaacs 2018-08-31 15:19:48 +02:00
parent 4a95404e17
commit c3b0492834

View File

@ -49,34 +49,40 @@ def update_download(book_id, user_id):
ub.session.add(new_download)
ub.session.commit()
def make_mobi(book_id, calibrepath, user_id, kindle_mail):
# Convert existing book entry to new format
def convert_book_format(book_id, calibrepath, old_book_format, new_book_format, user_id, kindle_mail=None):
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == 'EPUB').first()
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == old_book_format).first()
if not data:
error_message = _(u"Epub format not found for book id: %(book)d", book=book_id)
app.logger.error("make_mobi: " + error_message)
error_message = _(u"%(format)s format not found for book id: %(book)d", format=old_book_format, book=book_id)
app.logger.error("convert_book_format: " + error_message)
return error_message
if ub.config.config_use_google_drive:
df = gd.getFileFromEbooksFolder(book.path, data.name + u".epub")
df = gd.getFileFromEbooksFolder(book.path, data.name + "." + old_book_format.lower())
if df:
datafile = os.path.join(calibrepath, book.path, data.name + u".epub")
datafile = os.path.join(calibrepath, book.path, data.name + u"." + old_book_format.lower())
if not os.path.exists(os.path.join(calibrepath, book.path)):
os.makedirs(os.path.join(calibrepath, book.path))
df.GetContentFile(datafile)
else:
error_message = (u"Epub not found on Google Drive: %s.epub" % data.name)
error_message = _(u"%(format)s not found on Google Drive: %(fn)s",
format=old_book_format, fn=data.name + "." + old_book_format.lower())
return error_message
file_path = os.path.join(calibrepath, book.path, data.name)
if os.path.exists(file_path + u".epub"):
if os.path.exists(file_path + "." + old_book_format.lower()):
# read settings and append converter task to queue
settings = ub.get_mail_settings()
if kindle_mail:
settings = ub.get_mail_settings()
text = _(u"Convert: %s" % book.title)
else:
text = _(u"Convert to %(format)s: %(book)s", format=new_book_format, book=book.title)
settings['old_book_format'] = u'EPUB'
settings['new_book_format'] = u'MOBI'
global_WorkerThread.add_convert(file_path, book.id, user_id, _(u"Convert: %s" % book.title), settings,
kindle_mail)
global_WorkerThread.add_convert(file_path, book.id, user_id, text, settings, kindle_mail)
return None
else:
error_message = (u"Epub not found: %s.epub" % file_path)
error_message = _(u"%(format)s not found: %(fn)s",
format=old_book_format, fn=data.name + "." + old_book_format.lower())
return error_message
@ -127,7 +133,7 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
result = formats['mobi']
elif 'epub' in formats:
# returns None if sucess, otherwise errormessage
return make_mobi(book.id, calibrepath, user_id, kindle_mail)
return convert_book_format(book_id, calibrepath, u'epub', u'mobi', user_id, kindle_mail)
elif 'pdf' in formats:
result = formats['pdf'] # worker.get_attachment()
else:
@ -139,52 +145,6 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
return _(u"The requested file could not be read. Maybe wrong permissions?")
# Convert existing book entry to new format
def convert_book_format(book_id, calibrepath, old_book_format, new_book_format, user_id):
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == old_book_format).first()
if not data:
error_message = _(u"%(format)s format not found for book id: %(book)d", format=old_book_format, book=book_id)
app.logger.error("convert_book_format: " + error_message)
return error_message
if ub.config.config_use_google_drive:
df = gd.getFileFromEbooksFolder(book.path, data.name + "." + old_book_format.lower())
if df:
datafile = os.path.join(calibrepath, book.path, data.name + "." + old_book_format.lower())
if not os.path.exists(os.path.join(calibrepath, book.path)):
os.makedirs(os.path.join(calibrepath, book.path))
df.GetContentFile(datafile)
else:
error_message = _(u"%(format)s not found on Google Drive: %(fn)s",
format=old_book_format, fn=data.name + "." + old_book_format.lower())
return error_message
file_path = os.path.join(calibrepath, book.path, data.name)
if os.path.exists(file_path + "." + old_book_format.lower()):
# append converter to queue
settings = {'old_book_format': old_book_format,
'new_book_format': new_book_format}
app.logger.debug("Creating task for worker thread:")
app.logger.debug("filepath: " + file_path + " " +
"bookid: " + str(book.id) + " " +
"userid: " + str(user_id) + " " +
"taskmsg: " + _(u"Convert to %(format)s: %(book)s",
format=new_book_format, book=book.title) + " " +
"settings:old_book_format: " + settings['old_book_format'] + " " +
"settings:new_book_format: " + settings['new_book_format']
)
global_WorkerThread.add_convert(file_path, book.id,
user_id, _(u"Convert to %(format)s: %(book)s",
format=new_book_format, book=book.title),
settings)
return None
else:
error_message = _(u"%(format)s not found: %(fn)s",
format=old_book_format, fn=data.name + "." + old_book_format.lower())
return error_message
def get_valid_filename(value, replace_whitespace=True):
"""
Returns the given string converted to a string that can be used for a clean