Quote:
Originally Posted by Terisa de morgan
Thank you, as I'm going to use it as the base for duplicating empty books (I use it from time to time).
|
I actually have a different module that does exactly that. It suffered from the same problem as the other one, which is now fixed.
This one creates the book with the metadata already copied from the book that was selected when you activate the plugin, so it's probably more efficient than creating a new book and copying over metadata fields afterward.
A couple of notes:
1. It only allows you to duplicate one book at a time, because I didn't need anything more robust when I wrote it. The code is simple enough though that if you're at all familiar with Python it shouldn't be too hard to modify it to handle multiple books, if that's what you need.
2. It only duplicates the metadata; it doesn't copy the book formats. Since you mentioned duplicating
empty books, I figure that shouldn't be a problem for you.
Here is the source code:
Code:
from calibre_plugins.action_chains.actions.base import ChainAction
class DuplicateBook(ChainAction):
name = 'Duplicate Book'
def run(self, gui, settings, chain):
db = gui.current_db
rows = gui.current_view().selectionModel().selectedRows()
book_ids = [ gui.library_view.model().db.id(row.row()) for row in rows ]
#Only applies if exactly one book is selected.
if len(book_ids) == 1:
book_id = book_ids[0]
#get the metadata from the existing book.
meta = db.new_api.get_metadata(book_id)
ids, duplicates = db.new_api.add_books([(meta, {})])
newid = ids[0]
db.data.refresh()
gui.library_view.model().resort()
gui.tags_view.recount()
gui.library_view.select_rows([newid])
else:
raise Exception("Please select only one book first.")