Converting books from gdrive implemented

pull/570/head
Ozzie Isaacs 6 years ago
parent ff1b479188
commit 82fe282e9b

@ -86,6 +86,8 @@ def convert_kindlegen(file_path, book):
uncompressed_size=os.path.getsize(file_path + ".mobi") uncompressed_size=os.path.getsize(file_path + ".mobi")
)) ))
db.session.commit() db.session.commit()
if ub.config.config_use_google_drive:
os.remove(file_path + u".epub")
return file_path + ".mobi", RET_SUCCESS return file_path + ".mobi", RET_SUCCESS
else: else:
web.app.logger.info("convert_kindlegen: kindlegen failed with error while converting book") web.app.logger.info("convert_kindlegen: kindlegen failed with error while converting book")
@ -101,8 +103,8 @@ def convert_calibre(file_path, book):
web.app.logger.error("convert_calibre: " + error_message) web.app.logger.error("convert_calibre: " + error_message)
return error_message, RET_FAIL return error_message, RET_FAIL
try: try:
command = ("\""+ub.config.config_converterpath + "\" " + ub.config.config_calibre + command = ("\""+ub.config.config_converterpath + "\" \"" + file_path + u".epub\" \""
" \"" + file_path + u".epub\" \"" + file_path + u".mobi\"").encode(sys.getfilesystemencoding()) + file_path + u".mobi\" " + ub.config.config_calibre).encode(sys.getfilesystemencoding())
p = subprocess.Popen(command,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) p = subprocess.Popen(command,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
except Exception as e: except Exception as e:
error_message = _(u"Ebook-convert failed, no execution permissions") error_message = _(u"Ebook-convert failed, no execution permissions")
@ -124,6 +126,9 @@ def convert_calibre(file_path, book):
uncompressed_size=os.path.getsize(file_path + ".mobi") uncompressed_size=os.path.getsize(file_path + ".mobi")
)) ))
db.session.commit() db.session.commit()
if ub.config.config_use_google_drive:
os.remove(file_path + u".epub")
return file_path + ".mobi", RET_SUCCESS return file_path + ".mobi", RET_SUCCESS
else: else:
web.app.logger.info("convert_calibre: Ebook-convert failed with error while converting book") web.app.logger.info("convert_calibre: Ebook-convert failed with error while converting book")

@ -74,10 +74,25 @@ def make_mobi(book_id, calibrepath):
error_message = _(u"epub format not found for book id: %(book)d", book=book_id) error_message = _(u"epub format not found for book id: %(book)d", book=book_id)
app.logger.error("make_mobi: " + error_message) app.logger.error("make_mobi: " + error_message)
return error_message, RET_FAIL return error_message, RET_FAIL
if ub.config.config_use_google_drive:
df = gd.getFileFromEbooksFolder(book.path, data.name + u".epub")
if df:
datafile = os.path.join(calibrepath, book.path, data.name + u".epub")
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 = "make_mobi: epub not found on gdrive: %s.epub" % data.name
return error_message, RET_FAIL
# else:
file_path = os.path.join(calibrepath, book.path, data.name) file_path = os.path.join(calibrepath, book.path, data.name)
if os.path.exists(file_path + u".epub"): if os.path.exists(file_path + u".epub"):
return converter.convert_mobi(file_path, book) # convert book, and upload in case of google drive
res = converter.convert_mobi(file_path, book)
if ub.config.config_use_google_drive:
gd.updateGdriveCalibreFromLocal()
# time.sleep(10)
return res
else: else:
error_message = "make_mobi: epub not found: %s.epub" % file_path error_message = "make_mobi: epub not found: %s.epub" % file_path
return error_message, RET_FAIL return error_message, RET_FAIL
@ -104,17 +119,18 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
msg.attach(MIMEText(text.encode('UTF-8'), 'plain', 'UTF-8')) msg.attach(MIMEText(text.encode('UTF-8'), 'plain', 'UTF-8'))
book = db.session.query(db.Books).filter(db.Books.id == book_id).first() 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) data = db.session.query(db.Data).filter(db.Data.book == book.id).all()
formats = {} formats = {}
index = 0
for entry in data: for indx,entry in enumerate(data):
if entry.format == "MOBI": if entry.format == "MOBI":
formats["mobi"] = entry.name + ".mobi" # os.path.join(calibrepath, book.path, entry.name + ".mobi") formats["mobi"] = entry.name + ".mobi"
if entry.format == "EPUB": if entry.format == "EPUB":
formats["epub"] = entry.name + ".epub" # os.path.join(calibrepath, book.path, entry.name + ".epub") formats["epub"] = entry.name + ".epub"
index = indx
if entry.format == "PDF": if entry.format == "PDF":
formats["pdf"] = entry.name + ".pdf" # os.path.join(calibrepath, book.path, entry.name + ".pdf") formats["pdf"] = entry.name + ".pdf"
if len(formats) == 0: if len(formats) == 0:
return _("Could not find any formats suitable for sending by email") return _("Could not find any formats suitable for sending by email")
@ -124,14 +140,15 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
if result: if result:
msg.attach(result) msg.attach(result)
elif 'epub' in formats: elif 'epub' in formats:
# returns filename if sucess, otherwise errormessage
data, resultCode = make_mobi(book.id, calibrepath) data, resultCode = make_mobi(book.id, calibrepath)
if resultCode == RET_SUCCESS: if resultCode == RET_SUCCESS:
result = get_attachment(calibrepath, book.path, data) # toDo check data result = get_attachment(calibrepath, book.path, os.path.basename(data))
if result: if result:
msg.attach(result) msg.attach(result)
else: else:
app.logger.error = data app.logger.error(data)
return data # _("Could not convert epub to mobi") return data
elif 'pdf' in formats: elif 'pdf' in formats:
result = get_attachment(calibrepath, book.path, formats['pdf']) result = get_attachment(calibrepath, book.path, formats['pdf'])
if result: if result:
@ -144,22 +161,25 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
else: else:
return _('The requested file could not be read. Maybe wrong permissions?') return _('The requested file could not be read. Maybe wrong permissions?')
# For gdrive download book from gdrive to calibredir (temp dir for books), read contents in both cases and append
# it in MIME Base64 encoded to
def get_attachment(calibrepath, bookpath, filename): def get_attachment(calibrepath, bookpath, filename):
"""Get file as MIMEBase message""" """Get file as MIMEBase message"""
if ub.config.config_use_google_drive: if ub.config.config_use_google_drive:
df = gd.getFileFromEbooksFolder(bookpath, filename) df = gd.getFileFromEbooksFolder(bookpath, filename)
if df: if df:
# tmpDir = gettempdir()
datafile = os.path.join(calibrepath, bookpath, filename) datafile = os.path.join(calibrepath, bookpath, filename)
if not os.path.exists(os.path.join(calibrepath, bookpath)): if not os.path.exists(os.path.join(calibrepath, bookpath)):
os.makedirs(os.path.join(calibrepath, bookpath)) os.makedirs(os.path.join(calibrepath, bookpath))
df.GetContentFile(datafile) df.GetContentFile(datafile)
file_ = open(datafile, 'rb')
data = file_.read()
file_.close()
os.remove(datafile)
else: else:
return None return None
file_ = open(datafile, 'rb')
data = file_.read()
file_.close()
os.remove(datafile)
else: else:
try: try:
file_ = open(os.path.join(calibrepath, bookpath, filename), 'rb') file_ = open(os.path.join(calibrepath, bookpath, filename), 'rb')

Loading…
Cancel
Save