Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 12-23-2020, 12:05 PM   #16
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,918
Karma: 22669818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
You read metadata from OPF files using the get_metadata() function from calibre.ebooks.metadata.opf

that will give you a Metadata object that you can apply to any book in the calibre library using the set_metadata() function of self.gui.current_db.new_api
kovidgoyal is online now   Reply With Quote
Old 12-23-2020, 12:18 PM   #17
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,757
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by chris33 View Post
It sounds good. I guess I can reuse the code found in function update_metadata
in ../calibre-src/src/calibre/ebooks/oeb/polish/main.py.
It looks like it will do the job.
If you haven't already, look at API documentation for the e-book editing tools.
Quote:
But how can I get the book/container object from the current GUI selection ?
First get the book's metadata (mi). For examples of how to do that look at almost any GUI plugin. Examples might be Embed Comic Metadata and Manage Series. Once you have the mi you can get the book path from mi.format_metadata[format_name]['path'].

You can also get the format_metadata using db.format_metadata(). You can get the paths using db.format_files().

There are many examples in calibre of using these methods.
chaley is offline   Reply With Quote
Old 12-24-2020, 08:08 AM   #18
chris33
Enthusiast
chris33 began at the beginning.
 
Posts: 34
Karma: 10
Join Date: Sep 2020
Device: ipad, kobo libra 2
Thumbs up

Thank you all for your inputs.
I made it work as needed.
It's nice to have a living community and a living software in constant development.
I will go on this dev a little later on.
Merry Xmas to all !
Long live Calibre !

chris33 is offline   Reply With Quote
Old 12-25-2020, 04:32 PM   #19
chris33
Enthusiast
chris33 began at the beginning.
 
Posts: 34
Karma: 10
Join Date: Sep 2020
Device: ipad, kobo libra 2
Question Cover regeneration

I have been able to change the book file for an exiting record in the database with :
Code:
self.gui.current_db.new_api.add_format(id, book_extension, path, True)
Unfortunately this doesn't seem to update the cover, or I missed something.
I want to regenerate the cover after uploading the new file using default page 1
Also I read "Covers are always changed if a new cover is provided, but are never deleted" in Calibre source code.
So should I remove it first, with remove_cover and then rebuild it with generate_cover ?
And use something like:
Code:
        img = generate_cover(self.mi, prefs=prefs, as_qimage=True)
        img.setDevicePixelRatio(dpr)
        self.preview_label.setPixmap(QPixmap.fromImage(img))
But in that case I don't see how to select page 1.
Is it the way to go or is there a simpler way to achieve this ?
chris33 is offline   Reply With Quote
Old 12-26-2020, 11:45 AM   #20
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
Posts: 6,358
Karma: 3966377
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
Here's the code I use to force a cover update after updating a book in FanFicFare:

First, I use db.add_format_with_hooks(book_id, options['fileform'], book['outfile'], index_is_id=True) instead of just add_format(). I believe the difference is input plugins being run.

Second, I have code that takes a cover from the ebook, using the first page if there isn't an actual image:
Code:
from calibre.ebooks.metadata.meta import get_metadata as calibre_get_metadata

            existingepub = db.format(book_id,'EPUB',index_is_id=True, as_file=True)
            epubmi = calibre_get_metadata(existingepub,'EPUB')
            if epubmi.cover_data[1] is not None:
                try:
                    db.set_cover(book_id, epubmi.cover_data[1])
                except:
                    logger.info("Failed to set_cover, skipping")
Third, there is the code to insure that Calibre is notified about all the updates:
Code:
        if len(add_list):
            self.gui.library_view.model().books_added(len(add_list))
            self.gui.library_view.model().refresh_ids(add_ids)

        if len(update_list):
            self.gui.library_view.model().refresh_ids(update_ids)

        current = self.gui.library_view.currentIndex()
        self.gui.library_view.model().current_changed(current, self.previous)
        self.gui.tags_view.recount()

        if self.gui.cover_flow:
            self.gui.cover_flow.dataChanged()
Note that a lot of this is from years ago, so it may not be the most up-to-date way.
JimmXinu is offline   Reply With Quote
Old 12-29-2020, 05:29 AM   #21
chris33
Enthusiast
chris33 began at the beginning.
 
Posts: 34
Karma: 10
Join Date: Sep 2020
Device: ipad, kobo libra 2
Thank you JimmXinu.
I'll give it a try.
I was just wondering if there was a simpler / more recent way to do this


Quote:
Originally Posted by JimmXinu View Post
Here's the code I use to force a cover update after updating a book in FanFicFare:

First, I use db.add_format_with_hooks(book_id, options['fileform'], book['outfile'], index_is_id=True) instead of just add_format(). I believe the difference is input plugins being run.

Second, I have code that takes a cover from the ebook, using the first page if there isn't an actual image:
Code:
from calibre.ebooks.metadata.meta import get_metadata as calibre_get_metadata

            existingepub = db.format(book_id,'EPUB',index_is_id=True, as_file=True)
            epubmi = calibre_get_metadata(existingepub,'EPUB')
            if epubmi.cover_data[1] is not None:
                try:
                    db.set_cover(book_id, epubmi.cover_data[1])
                except:
                    logger.info("Failed to set_cover, skipping")
Third, there is the code to insure that Calibre is notified about all the updates:
Code:
        if len(add_list):
            self.gui.library_view.model().books_added(len(add_list))
            self.gui.library_view.model().refresh_ids(add_ids)

        if len(update_list):
            self.gui.library_view.model().refresh_ids(update_ids)

        current = self.gui.library_view.currentIndex()
        self.gui.library_view.model().current_changed(current, self.previous)
        self.gui.tags_view.recount()

        if self.gui.cover_flow:
            self.gui.cover_flow.dataChanged()
Note that a lot of this is from years ago, so it may not be the most up-to-date way.
chris33 is offline   Reply With Quote
Old 12-30-2020, 11:30 AM   #22
chris33
Enthusiast
chris33 began at the beginning.
 
Posts: 34
Karma: 10
Join Date: Sep 2020
Device: ipad, kobo libra 2
Question

Hi,
I tried to adapt but unfortunately get_metadata only returns the following fields correctly:
Title, Title sort, Author(s), Publisher, Tags , Series, Languages, Timestamp, Published
but no cover_data

Here is my code:
This overwrites the ebook file and works fine
Code:
self.gui.current_db.new_api.add_format(searchId, fmt, path, replace=True, run_hooks=True)
Then if I go to the ebook metadata GUI and press button "Set the cover for the book from the selected format" I can see the first pages of the new imported book.
But the cover is not updated from this list (default first page)

Code:
mi = self.gui.current_db.new_api.format_metadata(searchId, fmt, allow_cache=True, update_db=True)
OR
mi = self.gui.current_db.new_api.get_metadata(searchId, get_cover=True, get_user_categories=False, cover_as_data=True)
Printing the mi contents doesn't show any cover_data: only Title, Title sort, Author(s), Publisher, Tags , Series, Languages, Timestamp, Published

Code:
self.gui.current_db.new_api.set_cover({searchId, mi.cover_data[1]})
Fails with:

Quote:
File "calibre_plugins.reimport.main", line 159, in process
self.gui.current_db.new_api.set_cover({searchId, mi.cover_data[1]})
File "calibre/db/cache.py", line 72, in call_func_with_lock
File "calibre/db/cache.py", line 1301, in set_cover
File "polyglot/builtins.py", line 92, in iteritems
AttributeError: 'set' object has no attribute 'items'

I think that the cover needs to be generated before being saved in a temporary file so I can send the buffer in the metadata cover field.
Unfortunately I've found no way to generate the cover at least page 1.

How can I force cover generation from a given book id and format ?
Thank you

chris33 is offline   Reply With Quote
Old 12-30-2020, 11:40 AM   #23
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,757
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by chris33 View Post
Code:
self.gui.current_db.new_api.set_cover({searchId, mi.cover_data[1]})
I can't speak to most of your problems, but you are passing the wrong parameter to set_cover. It should be a dict, not a set. Something like
Code:
{ book_id: cover_data }
where cover_data is one of a QImage, QPixmap, file object or bytestring.
chaley is offline   Reply With Quote
Old 12-30-2020, 12:38 PM   #24
chris33
Enthusiast
chris33 began at the beginning.
 
Posts: 34
Karma: 10
Join Date: Sep 2020
Device: ipad, kobo libra 2
yes you are right. Thank you.
there is no error anymore now.
Code:
self.gui.current_db.new_api.set_cover({searchId: mi.cover_data[1]})
But still cover_data is empty ...
chris33 is offline   Reply With Quote
Reply

Tags
plugin development


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
TOC reference in Guide section of OPF fails Jeff L Calibre 0 10-20-2011 04:04 AM
Multiple reference elements with the same type in the <guide> Valloric ePub 8 04-25-2010 11:18 AM
Adding a <reference> item to <guide> in OPF file GRiker Sigil 2 04-12-2010 01:06 PM
Reference Guide: How to Prepare Images for EPUB (and other) Formats Zorba ePub 13 11-22-2009 08:28 AM
Free PDF: Ubuntu Pocket Guide and Reference TadW Deals and Resources (No Self-Promotion or Affiliate Links) 4 01-30-2009 11:22 PM


All times are GMT -4. The time now is 12:25 AM.


MobileRead.com is a privately owned, operated and funded community.