View Single Post
Old 08-30-2021, 04:58 AM   #9
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,118
Karma: 1954138
Join Date: Aug 2015
Device: Kindle
Set covers from book(s) action

Note: This action is now available as part of the builtin Single Field Edit Action.

Code:
from calibre.ebooks.metadata.meta import metadata_from_formats
from calibre_plugins.action_chains.actions.base import ChainAction

class CoverFromBook(ChainAction):

    name = 'Cover from book'
    support_scopes = True

    def run(self, gui, settings, chain):
        db = gui.current_db.new_api
        book_ids = chain.scope().get_book_ids()
        for book_id in book_ids:
            fmts = db.formats(book_id, verify_formats=False)
            paths = list(filter(None, [db.format_abspath(book_id, fmt) for fmt in fmts]))
            mi = metadata_from_formats(paths)
            if mi.cover_data:
                cdata = mi.cover_data[-1]
            if cdata is not None:
                db.set_cover({book_id: cdata})

Last edited by capink; 09-24-2021 at 04:17 AM.
capink is offline   Reply With Quote