def update_dir_structure_gdrive(book_id): error = False book = db.session.query(db.Books).filter(db.Books.id == book_id).first() authordir = book.path.split('/')[0] new_authordir = get_valid_filename(book.authors[0].name) titledir = book.path.split('/')[1] new_titledir = get_valid_filename(book.title) + " (" + str(book_id) + ")" if titledir != new_titledir: print(titledir) gFile = gd.getFileFromEbooksFolder(web.Gdrive.Instance().drive, os.path.dirname(book.path), titledir) gFile['title'] = new_titledir gFile.Upload() book.path = book.path.split('/')[0] + '/' + new_titledir if authordir != new_authordir: gFile = gd.getFileFromEbooksFolder(web.Gdrive.Instance().drive, None, authordir) gFile['title'] = new_authordir gFile.Upload() book.path = new_authordir + '/' + book.path.split('/')[1] return error
def update_dir_structure_gdrive(book_id): error = False book = db.session.query(db.Books).filter(db.Books.id == book_id).first() authordir = book.path.split('/')[0] new_authordir = get_valid_filename(book.authors[0].name) titledir = book.path.split('/')[1] new_titledir = get_valid_filename(book.title) + " (" + str(book_id) + ")" if titledir != new_titledir: gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) if gFile: gFile['title'] = new_titledir gFile.Upload() book.path = book.path.split('/')[0] + '/' + new_titledir gd.updateDatabaseOnEdit(gFile['id'], book.path) # only child folder affected else: error = _(u'File %(file)s not found on Google Drive', file= book.path) # file not found if authordir != new_authordir: gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) if gFile: gd.moveGdriveFolderRemote(gFile,new_authordir) book.path = new_authordir + '/' + book.path.split('/')[1] gd.updateDatabaseOnEdit(gFile['id'], book.path) else: error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found return error
def update_dir_structure_gdrive(book_id, first_author): error = False book = db.session.query(db.Books).filter(db.Books.id == book_id).first() path = book.path authordir = book.path.split('/')[0] if first_author: new_authordir = get_valid_filename(first_author) else: new_authordir = get_valid_filename(book.authors[0].name) titledir = book.path.split('/')[1] new_titledir = get_valid_filename(book.title) + u" (" + str(book_id) + u")" if titledir != new_titledir: gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) if gFile: gFile['title'] = new_titledir gFile.Upload() book.path = book.path.split('/')[0] + u'/' + new_titledir path = book.path gd.updateDatabaseOnEdit(gFile['id'], book.path) # only child folder affected else: error = _(u'File %(file)s not found on Google Drive', file=book.path) # file not found if authordir != new_authordir: gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), new_titledir) if gFile: gd.moveGdriveFolderRemote(gFile, new_authordir) book.path = new_authordir + u'/' + book.path.split('/')[1] path = book.path gd.updateDatabaseOnEdit(gFile['id'], book.path) else: error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found # Rename all files from old names to new names if authordir != new_authordir or titledir != new_titledir: new_name = get_valid_filename( book.title) + u' - ' + get_valid_filename(new_authordir) for file_format in book.data: gFile = gd.getFileFromEbooksFolder( path, file_format.name + u'.' + file_format.format.lower()) if not gFile: error = _(u'File %(file)s not found on Google Drive', file=file_format.name) # file not found break gd.moveGdriveFileRemote( gFile, new_name + u'.' + file_format.format.lower()) file_format.name = new_name return error
def update_dir_structure_gdrive(book_id, first_author): error = False book = db.session.query(db.Books).filter(db.Books.id == book_id).first() authordir = book.path.split('/')[0] if first_author: new_authordir = get_valid_filename(first_author) else: new_authordir = get_valid_filename(book.authors[0].name) titledir = book.path.split('/')[1] new_titledir = get_valid_filename(book.title) + " (" + str(book_id) + ")" if titledir != new_titledir: gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) if gFile: gFile['title'] = new_titledir gFile.Upload() book.path = book.path.split('/')[0] + '/' + new_titledir gd.updateDatabaseOnEdit(gFile['id'], book.path) # only child folder affected else: error = _(u'File %(file)s not found on Google Drive', file=book.path) # file not found if authordir != new_authordir: gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) if gFile: gd.moveGdriveFolderRemote(gFile, new_authordir) book.path = new_authordir + '/' + book.path.split('/')[1] gd.updateDatabaseOnEdit(gFile['id'], book.path) else: error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found # Rename all files from old names to new names # ToDo: Rename also all bookfiles with new author name and new title name ''' if authordir != new_authordir or titledir != new_titledir: for format in book.data: # path_name = os.path.join(calibrepath, new_authordir, os.path.basename(path)) new_name = get_valid_filename(book.title) + ' - ' + get_valid_filename(book) format.name = new_name if gFile: pass else: error = _(u'File %(file)s not found on Google Drive', file=format.name) # file not found break''' return error
def delete_book_gdrive(book, book_format): error= False if book_format: name = '' for entry in book.data: if entry.format.upper() == book_format: name = entry.name + '.' + book_format gFile = gd.getFileFromEbooksFolder(book.path, name) else: gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path),book.path.split('/')[1]) if gFile: gd.deleteDatabaseEntry(gFile['id']) gFile.Trash() else: error =_(u'Book path %(path)s not found on Google Drive', path=book.path) # file not found return error
def get_attachment(bookpath, filename): """Get file as MIMEBase message""" calibrepath = web.config.config_calibre_dir if web.ub.config.config_use_google_drive: df = gd.getFileFromEbooksFolder(bookpath, filename) if df: datafile = os.path.join(calibrepath, bookpath, filename) if not os.path.exists(os.path.join(calibrepath, bookpath)): os.makedirs(os.path.join(calibrepath, bookpath)) df.GetContentFile(datafile) else: return None file_ = open(datafile, 'rb') data = file_.read() file_.close() os.remove(datafile) else: try: file_ = open(os.path.join(calibrepath, bookpath, filename), 'rb') data = file_.read() file_.close() except IOError as e: web.app.logger.exception(e) # traceback.print_exc() web.app.logger.error(u'The requested file could not be read. Maybe wrong permissions?') return None attachment = MIMEBase('application', 'octet-stream') attachment.set_payload(data) encoders.encode_base64(attachment) attachment.add_header('Content-Disposition', 'attachment', filename=filename) return attachment
def make_mobi(book_id, calibrepath): 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() 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) 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) if os.path.exists(file_path + u".epub"): # 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: error_message = "make_mobi: epub not found: %s.epub" % file_path return error_message, RET_FAIL
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 == 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 + 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"%(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()): # read settings and append converter task to queue if kindle_mail: settings = ub.get_mail_settings() text = _(u"Convert: %(book)s" , book=book.title) else: settings = dict() text = _(u"Convert to %(format)s: %(book)s", format=new_book_format, book=book.title) settings['old_book_format'] = old_book_format settings['new_book_format'] = new_book_format global_WorkerThread.add_convert(file_path, book.id, user_id, text, settings, kindle_mail) 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 make_mobi(book_id, calibrepath, user_id, kindle_mail): 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() 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) return error_message 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 = (u"make_mobi: epub not found on gdrive: %s.epub" % data.name) return error_message file_path = os.path.join(calibrepath, book.path, data.name) if os.path.exists(file_path + u".epub"): # append converter to queue global_WorkerThread.add_convert(file_path, book.id, user_id, _(u"Convert: %s" % book.title), ub.get_mail_settings(), kindle_mail) return None else: error_message = (u"make_mobi: epub not found: %s.epub" % file_path) return error_message
def delete_book_gdrive(book): error = False gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), book.path.split('/')[1]) if gFile: gd.deleteDatabaseEntry(gFile['id']) gFile.Trash() else: error = _(u'Book path %s not found on Google Drive' % book.path) # file not found return error
def do_download_file(book, book_format, data, headers): if ub.config.config_use_google_drive: startTime = time.time() df = gd.getFileFromEbooksFolder(book.path, data.name + "." + book_format) web.app.logger.debug(time.time() - startTime) if df: return gd.do_gdrive_download(df, headers) else: abort(404) else: response = make_response(send_from_directory(os.path.join(ub.config.config_calibre_dir, book.path), data.name + "." + book_format)) response.headers = headers return response
def do_download_file(book, book_format, data, headers): if ub.config.config_use_google_drive: startTime = time.time() df = gd.getFileFromEbooksFolder(book.path, data.name + "." + book_format) web.app.logger.debug(time.time() - startTime) if df: return gd.do_gdrive_download(df, headers) else: abort(404) else: filename = os.path.join(ub.config.config_calibre_dir, book.path) if not os.path.isfile(os.path.join(filename, data.name + "." + book_format)): # ToDo: improve error handling web.app.logger.error('File not found: %s' % os.path.join(filename, data.name + "." + book_format)) response = make_response(send_from_directory(filename, data.name + "." + book_format)) response.headers = headers return response
def convert_book_format(book_id, calibrepath, old_book_format, new_book_format, user_id, kindle_mail=None): """ Convert existing book entry to new format :param book_id: :param calibrepath: :param old_book_format: :param new_book_format: :param user_id: :param kindle_mail: :return: """ 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 + 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"%(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()): # read settings and append converter task to queue if kindle_mail: settings = ub.get_mail_settings() settings['subject'] = _( 'Send to Kindle') # pretranslate Subject for e-mail settings['body'] = _(u'This e-mail has been sent via Calibre-Web.') # text = _(u"%(format)s: %(book)s", format=new_book_format, book=book.title) else: settings = dict() text = (u"%s -> %s: %s" % (old_book_format, new_book_format, book.title)) settings['old_book_format'] = old_book_format settings['new_book_format'] = new_book_format global_WorkerThread.add_convert(file_path, book.id, user_id, text, settings, kindle_mail) 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