![]() |
#1 |
Calibre Plugins Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 4,673
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) 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; Yesterday at 06:20 AM. |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,914
Karma: 7076769
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) 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' |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Calibre Plugins Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 4,673
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
Thanks very much chaley!
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Editor Plugin] Bulk Image Resizer Plugin | eliteart | Plugins | 9 | 04-26-2024 06:58 AM |
Updating plugin fails because plugin file is in use by Calibre? | ownedbycats | Calibre | 1 | 12-03-2022 08:34 PM |
Metadata Download Plugin - Bulk Downloads | Bradles | Development | 2 | 03-14-2021 10:23 PM |
Advice on Bulk Updating MetaData | tonyd52 | Library Management | 2 | 06-06-2011 01:06 PM |
Updating Metadata in Bulk | Turt99 | Calibre | 5 | 06-07-2010 03:19 PM |