Hmm... is there any way to do that programmatically?
There's the ebook-meta tool, but it doesn't seem to be able to do what you just described. It can set the cover from a file but not from the book itself according to the description:
https://manual.calibre-ebook.com/gen...book-meta.html