Refactor rename author/title on gdrive
This commit is contained in:
parent
127bf98aac
commit
d8f5bdea6d
@ -355,23 +355,51 @@ def downloadFile(path, filename, output):
|
||||
f = getFileFromEbooksFolder(path, filename)
|
||||
f.GetContentFile(output)
|
||||
|
||||
'''def renameGdriveFolderRemote(origin_file, target_folder):
|
||||
drive = getDrive(Gdrive.Instance().drive)
|
||||
previous_parents = ",".join([parent["id"] for parent in origin_file.get('parents')])
|
||||
children = drive.auth.service.children().list(folderId=previous_parents).execute()
|
||||
gFileTargetDir = getFileFromEbooksFolder(None, target_folder)
|
||||
if not gFileTargetDir or gFileTargetDir['title'] != target_folder:
|
||||
# Folder is not existing, rename folder
|
||||
drive.auth.service.files().patch(fileId=origin_file['id'],
|
||||
body={'title': target_folder},
|
||||
fields='title').execute()
|
||||
# gFileTargetDir = drive.CreateFile(
|
||||
# {'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}],
|
||||
# "mimeType": "application/vnd.google-apps.folder"})
|
||||
# gFileTargetDir.Upload()
|
||||
else:
|
||||
# Move the file to the new folder
|
||||
drive.auth.service.files().update(fileId=origin_file['id'],
|
||||
addParents=gFileTargetDir['id'],
|
||||
removeParents=previous_parents,
|
||||
fields='id, parents').execute()
|
||||
# if previous_parents has no children anymore, delete original fileparent
|
||||
if len(children['items']) == 1:
|
||||
deleteDatabaseEntry(previous_parents)
|
||||
drive.auth.service.files().delete(fileId=previous_parents).execute()'''
|
||||
|
||||
def moveGdriveFolderRemote(origin_file, target_folder):
|
||||
drive = getDrive(Gdrive.Instance().drive)
|
||||
previous_parents = ",".join([parent["id"] for parent in origin_file.get('parents')])
|
||||
children = drive.auth.service.children().list(folderId=previous_parents).execute()
|
||||
gFileTargetDir = getFileFromEbooksFolder(None, target_folder)
|
||||
if not gFileTargetDir:
|
||||
if not gFileTargetDir or gFileTargetDir['title'] != target_folder:
|
||||
# Folder is not existing, create, and move folder
|
||||
gFileTargetDir = drive.CreateFile(
|
||||
{'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}],
|
||||
"mimeType": "application/vnd.google-apps.folder"})
|
||||
gFileTargetDir.Upload()
|
||||
# Move the file to the new folder
|
||||
drive.auth.service.files().update(fileId=origin_file['id'],
|
||||
addParents=gFileTargetDir['id'],
|
||||
removeParents=previous_parents,
|
||||
fields='id, parents').execute()
|
||||
drive.auth.service.files().patch(fileId=origin_file['id'],
|
||||
body={'title': target_folder},
|
||||
fields='title').execute()
|
||||
#gFileTargetDir = drive.CreateFile(
|
||||
# {'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}],
|
||||
# "mimeType": "application/vnd.google-apps.folder"})
|
||||
#gFileTargetDir.Upload()
|
||||
else:
|
||||
# Move the file to the new folder
|
||||
drive.auth.service.files().update(fileId=origin_file['id'],
|
||||
addParents=gFileTargetDir['id'],
|
||||
removeParents=previous_parents,
|
||||
fields='id, parents').execute()
|
||||
# if previous_parents has no children anymore, delete original fileparent
|
||||
if len(children['items']) == 1:
|
||||
deleteDatabaseEntry(previous_parents)
|
||||
|
@ -331,7 +331,7 @@ def delete_book_file(book, calibrepath, book_format=None):
|
||||
path=book.path)
|
||||
|
||||
|
||||
def clean_author_database(renamed_author, calibrepath, local_book=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]
|
||||
for r in renamed_author:
|
||||
if local_book:
|
||||
@ -350,9 +350,19 @@ def clean_author_database(renamed_author, calibrepath, local_book=None):
|
||||
# change location in database to new author/title path
|
||||
book.path = os.path.join(all_new_authordir, all_titledir).replace('\\', '/')
|
||||
for file_format in book.data:
|
||||
shutil.move(os.path.normcase(
|
||||
os.path.join(all_new_path, file_format.name + '.' + file_format.format.lower())),
|
||||
os.path.normcase(os.path.join(all_new_path, all_new_name + '.' + file_format.format.lower())))
|
||||
if not gdrive:
|
||||
shutil.move(os.path.normcase(os.path.join(all_new_path,
|
||||
file_format.name + '.' + file_format.format.lower())),
|
||||
os.path.normcase(os.path.join(all_new_path,
|
||||
all_new_name + '.' + file_format.format.lower())))
|
||||
else:
|
||||
gFile = gd.getFileFromEbooksFolder(all_new_path,
|
||||
file_format.name + '.' + file_format.format.lower())
|
||||
if gFile:
|
||||
gd.moveGdriveFileRemote(gFile, all_new_name + u'.' + file_format.format.lower())
|
||||
else:
|
||||
log.error("File {} not found on gdrive"
|
||||
.format(all_new_path, file_format.name + '.' + file_format.format.lower()))
|
||||
file_format.name = all_new_name
|
||||
|
||||
|
||||
@ -461,23 +471,18 @@ def update_dir_structure_gdrive(book_id, first_author, renamed_author):
|
||||
new_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == r).first()
|
||||
old_author_dir = get_valid_filename(r)
|
||||
new_author_rename_dir = get_valid_filename(new_author.name)
|
||||
'''if os.path.isdir(os.path.join(calibrepath, old_author_dir)):
|
||||
try:
|
||||
old_author_path = os.path.join(calibrepath, old_author_dir)
|
||||
new_author_path = os.path.join(calibrepath, new_author_rename_dir)
|
||||
shutil.move(os.path.normcase(old_author_path), os.path.normcase(new_author_path))
|
||||
except (OSError) as ex:
|
||||
log.error("Rename author from: %s to %s: %s", old_author_path, new_author_path, ex)
|
||||
log.debug(ex, exc_info=True)
|
||||
return _("Rename author from: '%(src)s' to '%(dest)s' failed with error: %(error)s",
|
||||
src=old_author_path, dest=new_author_path, error=str(ex))'''
|
||||
gFile = gd.getFileFromEbooksFolder(None, old_author_dir)
|
||||
if gFile:
|
||||
gd.moveGdriveFolderRemote(gFile, new_author_rename_dir)
|
||||
else:
|
||||
error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found
|
||||
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:
|
||||
'''if titledir != new_titledir:
|
||||
gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir)
|
||||
if gFile:
|
||||
gFile['title'] = new_titledir
|
||||
@ -496,10 +501,47 @@ def update_dir_structure_gdrive(book_id, first_author, renamed_author):
|
||||
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
|
||||
error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found'''
|
||||
|
||||
if authordir != new_authordir or titledir != new_titledir:
|
||||
new_path = os.path.join(new_authordir, new_titledir)
|
||||
new_name = get_valid_filename(book.title) + ' - ' + get_valid_filename(new_authordir)
|
||||
gFile = gd.getFileFromEbooksFolder(new_authordir, new_titledir)
|
||||
if not gFile:
|
||||
# move original path to new path
|
||||
log.debug("Moving title: %s to %s", path, new_path)
|
||||
gFile = gd.getFileFromEbooksFolder(authordir, titledir)
|
||||
# move author and title -> currently only title??
|
||||
gd.moveGdriveFileRemote(gFile, new_titledir)
|
||||
# shutil.move(os.path.normcase(path), os.path.normcase(new_path))
|
||||
else: # path is valid copy only files to new location (merge)
|
||||
log.info("Moving title: %s into existing: %s", path, new_path)
|
||||
# Take all files and subfolder from old path (strange command)
|
||||
# gd.moveGdriveFileRemote(gFile, new_name + u'.' + file_format.format.lower())
|
||||
for dir_name, __, file_list in os.walk(path):
|
||||
for file in file_list:
|
||||
shutil.move(os.path.normcase(os.path.join(dir_name, file)),
|
||||
os.path.normcase(os.path.join(new_path + dir_name[len(path):], file)))
|
||||
# change location in database to new author/title path
|
||||
book.path = os.path.join(new_authordir, new_titledir).replace('\\', '/')
|
||||
|
||||
#for file_format in book.data:
|
||||
# gFile = gd.getFileFromEbooksFolder(path, file_format.name + '.' + 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
|
||||
|
||||
# Todo: Rename all authors on gdrive
|
||||
# Rename all files from old names to new names
|
||||
'''
|
||||
clean_author_database(renamed_author, "")
|
||||
if first_author not in renamed_author:
|
||||
clean_author_database([first_author], "", book)
|
||||
#if not renamed_author and not orignal_filepath and len(os.listdir(os.path.dirname(path))) == 0:
|
||||
# shutil.rmtree(os.path.dirname(path))
|
||||
|
||||
'''# Rename all files from old names to new names
|
||||
try:
|
||||
clean_author_database(renamed_author, calibrepath)
|
||||
if first_author not in renamed_author:
|
||||
@ -510,15 +552,7 @@ def update_dir_structure_gdrive(book_id, first_author, renamed_author):
|
||||
log.error("Error in rename file in path %s", ex)
|
||||
log.debug(ex, exc_info=True)
|
||||
return _("Error in rename file in path: %(error)s", error=str(ex))'''
|
||||
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
|
||||
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
# GDrive Integration
|
||||
google-api-python-client>=1.7.11,<2.36.0
|
||||
google-api-python-client>=1.7.11,<2.37.0
|
||||
gevent>20.6.0,<22.0.0
|
||||
greenlet>=0.4.17,<1.2.0
|
||||
httplib2>=0.9.2,<0.21.0
|
||||
@ -14,7 +14,7 @@ six>=1.10.0,<1.17.0
|
||||
|
||||
# Gmail
|
||||
google-auth-oauthlib>=0.4.3,<0.5.0
|
||||
google-api-python-client>=1.7.11,<2.36.0
|
||||
google-api-python-client>=1.7.11,<2.37.0
|
||||
|
||||
# goodreads
|
||||
goodreads>=0.3.2,<0.4.0
|
||||
|
@ -59,7 +59,7 @@ install_requires =
|
||||
|
||||
[options.extras_require]
|
||||
gdrive =
|
||||
google-api-python-client>=1.7.11,<2.36.0
|
||||
google-api-python-client>=1.7.11,<2.37.0
|
||||
gevent>20.6.0,<22.0.0
|
||||
greenlet>=0.4.17,<1.2.0
|
||||
httplib2>=0.9.2,<0.21.0
|
||||
@ -73,7 +73,7 @@ gdrive =
|
||||
six>=1.10.0,<1.17.0
|
||||
gmail =
|
||||
google-auth-oauthlib>=0.4.3,<0.5.0
|
||||
google-api-python-client>=1.7.11,<2.36.0
|
||||
google-api-python-client>=1.7.11,<2.37.0
|
||||
goodreads =
|
||||
goodreads>=0.3.2,<0.4.0
|
||||
python-Levenshtein>=0.12.0,<0.13.0
|
||||
|
Loading…
Reference in New Issue
Block a user