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 01-06-2025, 05:50 PM   #1
mactastik
Junior Member
mactastik began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Jan 2025
Device: none
calibre python script to update custom fields (help)

I'm working on converting over from goodreads to calibre. So far, so good. One of the things I'd like is two custom fields: Read and Stars to match whether I've read the book or not and the stars I give the book.

I was able to make the fields and can set them manually in Calibre no problem. I can read in and match books pretty well, but I can't seem to store the custom fields via python. Here's what I have (in brief):

Code:
metadata = calibre_db.get_metadata(id)
stars = metadata.get_user_metadata("#stars", True)
stars["#value#"] = 4
metadata.set_user_metadata("#stars", stars)
read = metadata.get_user_metadata("#read", True)
read["#value#"] = True
metadata.set_user_metadata("#read", read)
I can print values that I've manually entered, so I know I'm reading the right things, but setting doesn't seem to work. What am I missing?
mactastik is offline   Reply With Quote
Old 01-06-2025, 08:47 PM   #2
mactastik
Junior Member
mactastik began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Jan 2025
Device: none
Since it's not much longer, though this would benefit from a more complete test script:

Code:
# Run with calibre-debug -e calibre_filler.py
import calibre.library

LIBRARY = "calibre/andy"

calibre_db = calibre.library.db(LIBRARY).new_api

# calibre_books = get_calibre_books()
metadata = calibre_db.get_metadata(291)
stars = metadata.get_user_metadata("#stars", True)
stars["#value#"] = 7  # values are 0 to 10
read = metadata.get_user_metadata("#read", True)
read["#value#"] = True
print(f"   Update metadata from [{stars['#value#']}] [{read['#value#']}]")
metadata.set_user_metadata("#stars", stars)
metadata.set_user_metadata("#read", read)
It reads the values fine but doesn't store them in the database. Is there a "write" command of some kind I'm not aware of?
mactastik is offline   Reply With Quote
Advert
Old 01-06-2025, 09:34 PM   #3
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: 45,364
Karma: 27230406
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
calibre_db.set_metadata or set_field
kovidgoyal is offline   Reply With Quote
Old 01-06-2025, 09:44 PM   #4
mactastik
Junior Member
mactastik began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Jan 2025
Device: none
Thanks. I'm still unclear on how this works.

https://manual.calibre-ebook.com/db_api.html
Is this the only documentation?

set_metadata requires an "mi" (Metadata object), which I assume is the *full* metadata for the book (because it says that you need to specify None if you don't want a given field replaced). But I don't see where the Metadata object itself is documented, so I'm not sure how to index into it to edit just the user-defined fields.

Similarly, set_field asks for a "mapping of book id's to values". What is that? And what is the "name" of the field if it's a user-defined field?
mactastik is offline   Reply With Quote
Old 01-07-2025, 05:57 AM   #5
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by mactastik View Post
Similarly, set_field asks for a "mapping of book id's to values". What is that? And what is the "name" of the field if it's a user-defined field?
The "mapping" is documented in the set_field comments.
Code:
:param book_id_to_val_map: Mapping of book_ids to values that should be applied.
It is a python dict (map) from book id to value. Books with an id in the dict will have the field value changed to that id's value in the dict.

The name of the field is the lookup name, #something for custom columns.
chaley is offline   Reply With Quote
Advert
Old 01-07-2025, 06:01 AM   #6
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
BTW:
  • Using get_proxy_metadata() is much more efficient in this case than get_metadata.
  • In your case, using set_field() is much easier than set_metadata. You deal with books and values, not Metadata instances.
chaley is offline   Reply With Quote
Old 01-07-2025, 09:10 AM   #7
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Moderator Notice
Moved to the development subforum
chaley is offline   Reply With Quote
Old 01-07-2025, 11:10 AM   #8
mactastik
Junior Member
mactastik began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Jan 2025
Device: none
I see. Looks like a quick test of set_field does work well. Thanks!
mactastik is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calibre Access in Python Script KissMeKiwi Development 5 04-14-2024 05:42 AM
mass update/mass-editing as a list custom fields alkmaar Library Management 7 08-30-2019 01:24 PM
3.34 calibre-debug runs the script code under Python 3 EbokJunkie Conversion 2 11-20-2018 07:43 PM
Calibre beta available that fully integrates custom fields chaley Calibre 206 10-03-2010 02:28 AM
Calibre custom news feed and python help. harrynewman Calibre 4 10-08-2009 09:26 AM


All times are GMT -4. The time now is 09:36 PM.


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