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 09-08-2024, 07:18 AM   #1
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
Bulk updating metadata from a plugin?

Hi - I'm looking at some (ancient) code in the Extract ISBN plugin.

The code just wants to update a batch of books to set the .isbn property of the Metadata. For reasons I have long since got no idea on (likely written 12+ years ago), the code appears to steal some behaviour from the Edit Metadata plugin back then. It currently looks like this:

Code:
        # At this point we want to re-use code in edit_metadata to go ahead and
        # apply the changes. So we will replace the Metadata objects with some
        # empty ones with only the isbn field set so only that field gets updated
        id_map = {}
        for i, title, last_modified, isbn in extracted_ids:
            mi = Metadata(_('Unknown'))
            mi.isbn = isbn
            id_map[i] = mi
        edit_metadata_action = self.gui.iactions['Edit Metadata']
        edit_metadata_action.apply_metadata_changes(id_map,
                                                    callback=self._mark_and_display_results)
As you can see fairly straightforward of only setting one property on the Metadata object and relying on magic inside apply_metadata_changes() to ensure only that property gets updated on the books.

It is entirely possibly that in "current" calibre this is a terrible approach and there is a better way of doing this. Thoughts/suggestions welcomed - it still seems to work perfectly fine for me but two users now are saying their titles are being reset to "unknown". I havent touched any of this code, the only change my latest plugin release made is to increase the likelihood of an ISBN being found (which would then invoke this code)...

Last edited by kiwidude; 09-08-2024 at 07:20 AM.
kiwidude is offline   Reply With Quote
Old 09-08-2024, 09:45 AM   #2
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: 12,038
Karma: 7257323
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I would do something like this. I didn't try to run it, but see below.
Code:
	# I assume you have an old API db reference. Get a new_api reference
	ndb = db.new_api
	book_to_id_map = {}
	for i, title, last_modified, isbn in extracted_ids:
		# Get the existing identifiers for the book
		identifiers_for_book = ndb.field_for('identifiers', i)
		# Add/replace the ISBN identifer
		identifiers_for_book['isbn'] = isbn
		# Save the (probably) updated list of identifiers
		book_to_id_map[i] = identifiers_for_book
	# Set all the books' identifier values
	ndb.set_field('identifiers', book_to_id_map)
As a test I ran this in the template tester. It worked. The first 4 books with more than one identifier ended up including "isbn:grumblebunny"
Code:
python:
def evaluate(book, context):
	db = context.db.new_api
	ids = list(db.search("identifiers:#>2"))
	book_to_id_map = {}
	for id_ in ids[0:4]:
		identifiers_for_book = db.field_for('identifiers', id_)
		identifiers_for_book['isbn'] = 'grumblebunny'
		book_to_id_map[id_] = identifiers_for_book
	db.set_field('identifiers', book_to_id_map)
	return 'foo'
chaley is offline   Reply With Quote
Advert
Old 09-08-2024, 10:04 PM   #3
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
Thanks very much chaley!
kiwidude is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Editor Plugin] Bulk Image Resizer Plugin eliteart Plugins 11 10-27-2024 04:47 PM
Updating plugin fails because plugin file is in use by Calibre? ownedbycats Calibre 1 12-03-2022 09:34 PM
Metadata Download Plugin - Bulk Downloads Bradles Development 2 03-14-2021 11:23 PM
Advice on Bulk Updating MetaData tonyd52 Library Management 2 06-06-2011 02:06 PM
Updating Metadata in Bulk Turt99 Calibre 5 06-07-2010 04:19 PM


All times are GMT -4. The time now is 04:03 PM.


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