Bugfixes change author

This commit is contained in:
Ozzie Isaacs 2024-06-29 10:50:44 +02:00
parent 897188ff18
commit e02610e2f0
3 changed files with 40 additions and 19 deletions

View File

@ -264,6 +264,9 @@ def upload():
title_dir, title_dir,
meta.file_path, meta.file_path,
meta.extension.lower()) meta.extension.lower())
for file_format in db_book.data:
file_format.name = (helper.get_valid_filename(title, chars=42) + ' - '
+ helper.get_valid_filename(input_authors[0], chars=42))
else: else:
error = helper.update_dir_structure(book_id, error = helper.update_dir_structure(book_id,
config.get_book_path(), config.get_book_path(),

View File

@ -269,6 +269,7 @@ def getFile(pathId, fileName, drive):
def getFolderId(path, drive): def getFolderId(path, drive):
# drive = getDrive(drive) # drive = getDrive(drive)
log.info(f"GetFolder: {path}")
currentFolderId = None currentFolderId = None
try: try:
currentFolderId = getEbooksFolderId(drive) currentFolderId = getEbooksFolderId(drive)
@ -354,6 +355,7 @@ def moveGdriveFolderRemote(origin_file, target_folder):
fields='id, parents').execute() fields='id, parents').execute()
elif gFileTargetDir['title'] != target_folder: elif gFileTargetDir['title'] != target_folder:
deleteDatabasePath(gFileTargetDir['title'])
# Folder is not existing, create, and move folder # Folder is not existing, create, and move folder
drive.auth.service.files().patch(fileId=origin_file['id'], drive.auth.service.files().patch(fileId=origin_file['id'],
body={'title': target_folder}, body={'title': target_folder},
@ -563,6 +565,14 @@ def deleteDatabaseEntry(ID):
log.error_or_exception('Database error: {}'.format(ex)) log.error_or_exception('Database error: {}'.format(ex))
session.rollback() session.rollback()
def deleteDatabasePath(Pathname):
session.query(GdriveId).filter(GdriveId.path.contains(Pathname)).delete()
try:
session.commit()
except OperationalError as ex:
log.error_or_exception('Database error: {}'.format(ex))
session.rollback()
# Gets cover file from gdrive # Gets cover file from gdrive
# ToDo: Check is this right everyone get read permissions on cover files? # ToDo: Check is this right everyone get read permissions on cover files?

View File

@ -411,7 +411,7 @@ def rename_all_files_on_change(one_book, new_path, old_path, all_new_name, gdriv
file_format.name = all_new_name file_format.name = all_new_name
def rename_all_authors(first_author, renamed_author, calibre_path="", localbook=None, gdrive=False): '''def rename_all_authors(first_author, renamed_author, calibre_path="", localbook=None, gdrive=False):
# Create new_author_dir from parameter or from database # Create new_author_dir from parameter or from database
# Create new title_dir from database and add id # Create new title_dir from database and add id
if first_author: if first_author:
@ -424,6 +424,7 @@ def rename_all_authors(first_author, renamed_author, calibre_path="", localbook=
g_file = gd.getFileFromEbooksFolder(None, old_author_dir) g_file = gd.getFileFromEbooksFolder(None, old_author_dir)
if g_file: if g_file:
gd.moveGdriveFolderRemote(g_file, new_author_rename_dir) gd.moveGdriveFolderRemote(g_file, new_author_rename_dir)
gd.updateDatabaseOnEdit(g_file['id'], new_author_rename_dir)
else: else:
if os.path.isdir(os.path.join(calibre_path, old_author_dir)): if os.path.isdir(os.path.join(calibre_path, old_author_dir)):
old_author_path = os.path.join(calibre_path, old_author_dir) old_author_path = os.path.join(calibre_path, old_author_dir)
@ -437,7 +438,7 @@ def rename_all_authors(first_author, renamed_author, calibre_path="", localbook=
src=old_author_path, dest=new_author_path, error=str(ex)) src=old_author_path, dest=new_author_path, error=str(ex))
else: else:
new_authordir = get_valid_filename(localbook.authors[0].name, chars=96) new_authordir = get_valid_filename(localbook.authors[0].name, chars=96)
return new_authordir return new_authordir'''
def rename_author_path(first_author, old_author_dir, renamed_author, calibre_path="", gdrive=False): def rename_author_path(first_author, old_author_dir, renamed_author, calibre_path="", gdrive=False):
@ -465,7 +466,7 @@ def rename_author_path(first_author, old_author_dir, renamed_author, calibre_pat
return new_authordir return new_authordir
# Moves files in file storage during author/title rename, or from temp dir to file storage # Moves files in file storage during author/title rename, or from temp dir to file storage
def update_dir_structure_file(book_id, calibre_path, original_filepath, db_filename): def update_dir_structure_file(book_id, calibre_path, original_filepath, new_author, db_filename):
# get book database entry from id, if original path overwrite source with original_filepath # get book database entry from id, if original path overwrite source with original_filepath
local_book = calibre_db.get_book(book_id) local_book = calibre_db.get_book(book_id)
if original_filepath: if original_filepath:
@ -478,18 +479,20 @@ def update_dir_structure_file(book_id, calibre_path, original_filepath, db_filen
title_dir = local_book.path.split('/')[1] title_dir = local_book.path.split('/')[1]
new_title_dir = get_valid_filename(local_book.title, chars=96) + " (" + str(book_id) + ")" new_title_dir = get_valid_filename(local_book.title, chars=96) + " (" + str(book_id) + ")"
new_author_dir = get_valid_filename(new_author, chars=96)
if title_dir != new_title_dir or original_filepath: if title_dir != new_title_dir or author_dir != new_author_dir or original_filepath:
error = move_files_on_change(calibre_path, error = move_files_on_change(calibre_path,
author_dir, new_author_dir,
new_title_dir, new_title_dir,
local_book, local_book,
db_filename, db_filename,
original_filepath, original_filepath,
path) path)
new_path = os.path.join(calibre_path, author_dir, new_title_dir).replace('\\', '/') # old_path = os.path.join(calibre_path, author_dir, new_title_dir).replace('\\', '/')
new_path = os.path.join(calibre_path, new_author_dir, new_title_dir).replace('\\', '/')
all_new_name = get_valid_filename(local_book.title, chars=42) + ' - ' \ all_new_name = get_valid_filename(local_book.title, chars=42) + ' - ' \
+ get_valid_filename(author_dir, chars=42) + get_valid_filename(new_author, chars=42)
# Book folder already moved, only files need to be renamed # Book folder already moved, only files need to be renamed
rename_all_files_on_change(local_book, new_path, new_path, all_new_name) rename_all_files_on_change(local_book, new_path, new_path, all_new_name)
@ -511,13 +514,13 @@ def upload_new_file_gdrive(book_id, first_author, title, title_dir, original_fil
return False # rename_files_on_change(first_author, renamed_author, local_book=book, gdrive=True) return False # rename_files_on_change(first_author, renamed_author, local_book=book, gdrive=True)
def update_dir_structure_gdrive(book_id): def update_dir_structure_gdrive(book_id, first_author):
book = calibre_db.get_book(book_id) book = calibre_db.get_book(book_id)
authordir = book.path.split('/')[0] authordir = book.path.split('/')[0]
titledir = book.path.split('/')[1] titledir = book.path.split('/')[1]
# new_authordir = rename_all_authors(first_author, renamed_author, gdrive=True) # new_authordir = rename_all_authors(first_author, renamed_author, gdrive=True)
# new_authordir = get_valid_filename(book.title, chars=96) new_authordir = get_valid_filename(first_author, chars=96)
new_titledir = get_valid_filename(book.title, chars=96) + " (" + str(book_id) + ")" new_titledir = get_valid_filename(book.title, chars=96) + " (" + str(book_id) + ")"
if titledir != new_titledir: if titledir != new_titledir:
@ -529,7 +532,7 @@ def update_dir_structure_gdrive(book_id):
else: else:
return _('File %(file)s not found on Google Drive', file=book.path) # file not found return _('File %(file)s not found on Google Drive', file=book.path) # file not found
'''if authordir != new_authordir: if authordir != new_authordir:
g_file = gd.getFileFromEbooksFolder(os.path.dirname(book.path), new_titledir) g_file = gd.getFileFromEbooksFolder(os.path.dirname(book.path), new_titledir)
if g_file: if g_file:
gd.moveGdriveFolderRemote(g_file, new_authordir) gd.moveGdriveFolderRemote(g_file, new_authordir)
@ -537,25 +540,24 @@ def update_dir_structure_gdrive(book_id):
gd.updateDatabaseOnEdit(g_file['id'], book.path) gd.updateDatabaseOnEdit(g_file['id'], book.path)
else: else:
return _('File %(file)s not found on Google Drive', file=authordir) # file not found''' return _('File %(file)s not found on Google Drive', file=authordir) # file not found'''
if titledir != new_titledir: if titledir != new_titledir or authordir != new_authordir :
all_new_name = get_valid_filename(book.title, chars=42) + ' - ' \ all_new_name = get_valid_filename(book.title, chars=42) + ' - ' \
+ get_valid_filename(authordir, chars=42) + get_valid_filename(new_authordir, chars=42)
rename_all_files_on_change(book, book.path, book.path, all_new_name, gdrive=True) # todo: Move filenames on gdrive rename_all_files_on_change(book, book.path, book.path, all_new_name, gdrive=True) # todo: Move filenames on gdrive
# change location in database to new author/title path # change location in database to new author/title path
# book.path = os.path.join(authordir, new_titledir).replace('\\', '/') # book.path = os.path.join(authordir, new_titledir).replace('\\', '/')
return False return False
def move_files_on_change(calibre_path, new_authordir, new_titledir, localbook, db_filename, original_filepath, path): def move_files_on_change(calibre_path, new_author_dir, new_titledir, localbook, db_filename, original_filepath, path):
new_authordir = get_valid_filename(new_authordir, chars=96) new_path = os.path.join(calibre_path, new_author_dir, new_titledir)
new_path = os.path.join(calibre_path, new_authordir, new_titledir) # new_name = get_valid_filename(localbook.title, chars=96) + ' - ' + new_author_dir
new_name = get_valid_filename(localbook.title, chars=96) + ' - ' + new_authordir
try: try:
if original_filepath: if original_filepath:
if not os.path.isdir(new_path): if not os.path.isdir(new_path):
os.makedirs(new_path) os.makedirs(new_path)
shutil.move(os.path.normcase(original_filepath), os.path.normcase(os.path.join(new_path, db_filename))) shutil.move(os.path.normcase(original_filepath), os.path.normcase(os.path.join(new_path, db_filename)))
log.debug("Moving title: %s to %s/%s", original_filepath, new_path, new_name) log.debug("Moving title: %s to %s/%s", original_filepath, new_path)
else: else:
# Check new path is not valid path # Check new path is not valid path
if not os.path.exists(new_path): if not os.path.exists(new_path):
@ -569,8 +571,13 @@ def move_files_on_change(calibre_path, new_authordir, new_titledir, localbook, d
for file in file_list: for file in file_list:
shutil.move(os.path.normcase(os.path.join(dir_name, file)), shutil.move(os.path.normcase(os.path.join(dir_name, file)),
os.path.normcase(os.path.join(new_path + dir_name[len(path):], file))) os.path.normcase(os.path.join(new_path + dir_name[len(path):], file)))
if not os.listdir(os.path.split(path)[0]):
try:
shutil.rmtree(os.path.split(path)[0])
except (IOError, OSError) as ex:
log.error("Deleting authorpath for book %s failed: %s", localbook.id, ex)
# change location in database to new author/title path # change location in database to new author/title path
localbook.path = os.path.join(new_authordir, new_titledir).replace('\\', '/') localbook.path = os.path.join(new_author_dir, new_titledir).replace('\\', '/')
except OSError as ex: except OSError as ex:
log.error_or_exception("Rename title from {} to {} failed with error: {}".format(path, new_path, ex)) log.error_or_exception("Rename title from {} to {} failed with error: {}".format(path, new_path, ex))
return _("Rename title from: '%(src)s' to '%(dest)s' failed with error: %(error)s", return _("Rename title from: '%(src)s' to '%(dest)s' failed with error: %(error)s",
@ -729,6 +736,7 @@ def update_dir_structure(book_id,
return update_dir_structure_file(book_id, return update_dir_structure_file(book_id,
calibre_path, calibre_path,
original_filepath, original_filepath,
first_author,
db_filename) db_filename)
@ -949,7 +957,7 @@ def do_download_file(book, book_format, client, data, headers):
download_name = filename = None download_name = filename = None
if config.config_use_google_drive: if config.config_use_google_drive:
# startTime = time.time() # startTime = time.time()
df = gd.getFileFromEbooksFolder(book.path, book_name + "." + book_format) df = gd.getFileFromEbooksFolder(book.path, data.name + "." + book_format)
# log.debug('%s', time.time() - startTime) # log.debug('%s', time.time() - startTime)
if df: if df:
if config.config_embed_metadata and ( if config.config_embed_metadata and (