Renaming of co-authors which are authors in another book

This commit is contained in:
Ozzie Isaacs 2022-01-30 15:16:42 +01:00
parent 3ae1b97d72
commit 1e04b51148
2 changed files with 32 additions and 31 deletions

View File

@ -964,8 +964,8 @@ def create_book_on_upload(modif_date, meta):
authr = meta.author authr = meta.author
sort_authors, input_authors, db_author, renamed_authors = prepare_authors_on_upload(title, authr) sort_authors, input_authors, db_author, renamed_authors = prepare_authors_on_upload(title, authr)
title_dir = helper.get_valid_filename(title) title_dir = helper.get_valid_filename(title, chars=96)
author_dir = helper.get_valid_filename(db_author.name) author_dir = helper.get_valid_filename(db_author.name, chars=96)
# combine path and normalize path from windows systems # combine path and normalize path from windows systems
path = os.path.join(author_dir, title_dir).replace('\\', '/') path = os.path.join(author_dir, title_dir).replace('\\', '/')
@ -1283,8 +1283,8 @@ def merge_list_book():
if to_book: if to_book:
for file in to_book.data: for file in to_book.data:
to_file.append(file.format) to_file.append(file.format)
to_name = helper.get_valid_filename(to_book.title) + ' - ' + \ to_name = helper.get_valid_filename(to_book.title, chars=96) + ' - ' + \
helper.get_valid_filename(to_book.authors[0].name) helper.get_valid_filename(to_book.authors[0].name, chars=96)
for book_id in vals: for book_id in vals:
from_book = calibre_db.get_book(book_id) from_book = calibre_db.get_book(book_id)
if from_book: if from_book:

View File

@ -220,7 +220,7 @@ def send_mail(book_id, book_format, convert, kindle_mail, calibrepath, user_id):
return _(u"The requested file could not be read. Maybe wrong permissions?") return _(u"The requested file could not be read. Maybe wrong permissions?")
def get_valid_filename(value, replace_whitespace=True): def get_valid_filename(value, replace_whitespace=True, chars=128):
""" """
Returns the given string converted to a string that can be used for a clean Returns the given string converted to a string that can be used for a clean
filename. Limits num characters to 128 max. filename. Limits num characters to 128 max.
@ -242,7 +242,7 @@ def get_valid_filename(value, replace_whitespace=True):
value = re.sub(r'[*+:\\\"/<>?]+', u'_', value, flags=re.U) value = re.sub(r'[*+:\\\"/<>?]+', u'_', value, flags=re.U)
# pipe has to be replaced with comma # pipe has to be replaced with comma
value = re.sub(r'[|]+', u',', value, flags=re.U) value = re.sub(r'[|]+', u',', value, flags=re.U)
value = value[:128].strip() value = value[:chars].strip()
if not value: if not value:
raise ValueError("Filename cannot be empty") raise ValueError("Filename cannot be empty")
return value return value
@ -332,7 +332,7 @@ def delete_book_file(book, calibrepath, book_format=None):
def clean_author_database(renamed_author, calibrepath, local_book=None, gdrive=None): def clean_author_database(renamed_author, calibrepath, local_book=None, gdrive=None):
valid_filename_authors = [get_valid_filename(r) for r in renamed_author] valid_filename_authors = [get_valid_filename(r, chars=96) for r in renamed_author]
for r in renamed_author: for r in renamed_author:
if local_book: if local_book:
all_books = [local_book] all_books = [local_book]
@ -343,10 +343,11 @@ def clean_author_database(renamed_author, calibrepath, local_book=None, gdrive=N
book_author_path = book.path.split('/')[0] book_author_path = book.path.split('/')[0]
if book_author_path in valid_filename_authors or local_book: if book_author_path in valid_filename_authors or local_book:
new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first() new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first()
all_new_authordir = get_valid_filename(new_author.name) all_new_authordir = get_valid_filename(new_author.name, chars=96)
all_titledir = book.path.split('/')[1] all_titledir = book.path.split('/')[1]
all_new_path = os.path.join(calibrepath, all_new_authordir, all_titledir) all_new_path = os.path.join(calibrepath, all_new_authordir, all_titledir)
all_new_name = get_valid_filename(book.title) + ' - ' + all_new_authordir all_new_name = get_valid_filename(book.title, chars=42) + ' - ' \
+ get_valid_filename(new_author.name, chars=42)
# change location in database to new author/title path # change location in database to new author/title path
book.path = os.path.join(all_new_authordir, all_titledir).replace('\\', '/') book.path = os.path.join(all_new_authordir, all_titledir).replace('\\', '/')
for file_format in book.data: for file_format in book.data:
@ -383,11 +384,11 @@ def update_dir_structure_file(book_id, calibrepath, first_author, orignal_filepa
# Create new_authordir from parameter or from database # Create new_authordir from parameter or from database
# Create new titledir from database and add id # Create new titledir from database and add id
if first_author: if first_author:
new_authordir = get_valid_filename(first_author) new_authordir = get_valid_filename(first_author, chars=96)
for r in renamed_author: for r in renamed_author:
new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first() new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first()
old_author_dir = get_valid_filename(r) old_author_dir = get_valid_filename(r, chars=96)
new_author_rename_dir = get_valid_filename(new_author.name) new_author_rename_dir = get_valid_filename(new_author.name, chars=96)
if os.path.isdir(os.path.join(calibrepath, old_author_dir)): if os.path.isdir(os.path.join(calibrepath, old_author_dir)):
try: try:
old_author_path = os.path.join(calibrepath, old_author_dir) old_author_path = os.path.join(calibrepath, old_author_dir)
@ -401,12 +402,12 @@ def update_dir_structure_file(book_id, calibrepath, first_author, orignal_filepa
if first_author.lower() in [r.lower() for r in renamed_author]: if first_author.lower() in [r.lower() for r in renamed_author]:
path = os.path.join(calibrepath, new_authordir, titledir) path = os.path.join(calibrepath, new_authordir, titledir)
else: else:
new_authordir = get_valid_filename(localbook.authors[0].name) new_authordir = get_valid_filename(localbook.authors[0].name, chars=96)
new_titledir = get_valid_filename(localbook.title) + " (" + str(book_id) + ")" new_titledir = get_valid_filename(localbook.title, chars=96) + " (" + str(book_id) + ")"
if titledir != new_titledir or authordir != new_authordir or orignal_filepath: if titledir != new_titledir or authordir != new_authordir or orignal_filepath:
new_path = os.path.join(calibrepath, new_authordir, new_titledir) new_path = os.path.join(calibrepath, new_authordir, new_titledir)
new_name = get_valid_filename(localbook.title) + ' - ' + new_authordir new_name = get_valid_filename(localbook.title, chars=96) + ' - ' + new_authordir
try: try:
if orignal_filepath: if orignal_filepath:
if not os.path.isdir(new_path): if not os.path.isdir(new_path):
@ -453,22 +454,22 @@ def update_dir_structure_gdrive(book_id, first_author, renamed_author):
authordir = book.path.split('/')[0] authordir = book.path.split('/')[0]
if first_author: if first_author:
new_authordir = get_valid_filename(first_author) new_authordir = get_valid_filename(first_author, chars=96)
for r in renamed_author: for r in renamed_author:
# Todo: Rename all authors on gdrive # Todo: Rename all authors on gdrive
new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first() new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first()
old_author_dir = get_valid_filename(r) old_author_dir = get_valid_filename(r, chars=96)
new_author_rename_dir = get_valid_filename(new_author.name) new_author_rename_dir = get_valid_filename(new_author.name, chars=96)
gFile = gd.getFileFromEbooksFolder(None, old_author_dir) gFile = gd.getFileFromEbooksFolder(None, old_author_dir)
if gFile: if gFile:
gd.moveGdriveFolderRemote(gFile, new_author_rename_dir) gd.moveGdriveFolderRemote(gFile, new_author_rename_dir)
else: else:
error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found
else: else:
new_authordir = get_valid_filename(book.authors[0].name) new_authordir = get_valid_filename(book.authors[0].name, chars=96)
titledir = book.path.split('/')[1] titledir = book.path.split('/')[1]
new_titledir = get_valid_filename(book.title) + u" (" + str(book_id) + u")" new_titledir = get_valid_filename(book.title, chars=96) + u" (" + str(book_id) + u")"
if titledir != new_titledir: if titledir != new_titledir:
gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir)