View Single Post
Old 04-02-2024, 10:56 AM   #83
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,108
Karma: 1954138
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by nqk View Post
I would like to request for "Download external resources" action.

Thank you for the plugin.

👍👍👍👍

Sent from my Pixel 7 Pro using Tapatalk
Here is a custom action to do what you want. You have to copy/paste the code below into a new module (Editor Chains > Module manager > Add):

Code:
import traceback

from calibre.utils.localization import ngettext
from polyglot.builtins import iteritems
from calibre.ebooks.oeb.polish.download import (
    download_external_resources, get_external_resources, replace_resources,
)

from calibre_plugins.editor_chains.actions.base import EditorAction

class ExternalResources(EditorAction):

    name = 'Download External Resources'
    headless = True

    def run(self, chain, settings, *args, **kwargs):
        container = chain.current_container
        try:
            resources = get_external_resources(container)
        except Exception as err:
            print(err, traceback.format_exc())
            return
        if not resources:
            print(_('No external resources were found in this book.'))
            return
        try:
            downloaded = download_external_resources(container, resources)
        except Exception as err:
            print(_('Failed to download external resources.'))
            print(err, traceback.format_exc())
            return

        replacements, failures = downloaded
        if failures:
            tb = [f'{url}\n\t{err}\n' for url, err in iteritems(failures)]
            det_msg='\n'.join(tb)
            if not replacements:
                print(_(
                    'Failed to download external resources.'), det_msg)
                return
            else:
                print(_(
                    'Warning: Failed to download some external resources.'), det_msg)

        try:
            ret = replace_resources(container, resources, replacements)
        except Exception as err:
            print(_('Failed to replace external resources'))
            print(err, traceback.format_exc())
            return

    def validate(self, settings):
        return True
This is not something I am welling to work on debugging, so that is why I am not including it as part of the plugin. If you test it for some time and it is working without problems, maybe it can be added in the future.
capink is offline   Reply With Quote