12-09-2014, 12:14 PM | #1 |
Groupie
Posts: 161
Karma: 44048
Join Date: Jan 2012
Device: Kobo Touch
|
Custom columns not being set when adding books programmatically
I was testing an idea for programmatically adding books to a Calibre library and encountered a problem with custom columns.
I'm using mi.set('#subtitle', Subtitle) - following this with mi.get('#subtitle') shows the data is being set correctly - but this is not being 'carried over' into the library metadata. This is a 'standalone' script run using calibre-debug, not a plugin. Is this related to the metadata plugin issue I've seen mentioned elsewhere or something I'm doing wrong? |
12-09-2014, 04:43 PM | #2 | |
Grand Sorcerer
Posts: 11,773
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
mi.set changes the value in the metadata instance (in memory). It does not write that value to the database. To write to the database you should use new_api.set_field() (db.cache.py) if you are writing only a few fields or set_metadata() (db.legacy.py or cache.py) if you are changing many fields. Both assume that you have access to a database instance. The thread https://www.mobileread.com/forums/sho...d.php?t=241680 has an example of using set_field. |
|
Advert | |
|
12-10-2014, 07:06 AM | #3 |
Groupie
Posts: 161
Karma: 44048
Join Date: Jan 2012
Device: Kobo Touch
|
Umm...
Perhaps we need a new forum rule. DO NOT... Post requests for help while suffering from severe flu symptoms To clarify what I'm trying to achieve. I start with a list of filenames in the following format: <Author> - <Series_Name> <Series_Index> - <Title> (<Subtitle and/or Other Info>).<fmt> This is split and formatted into the following: Author (FN LN) Author_Sort (LN, FN) Series Series_Index Title Subtitle I then read the metadata from the source file, update it as appropriate with the above and call add_books() passing the updated metadata. This works almost exactly as intended. The book is added with the correct metadata except for the missing custom column data. |
12-10-2014, 07:27 AM | #4 | |
Grand Sorcerer
Posts: 11,773
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
I am going out on a limb here because I haven't ever done what you are trying to do. I assume you got the mi instance by something like mi = Metadata(). The problem is that this metadata instance will not contain any of the custom column metadata, which means that when you set a value for #foobar, you aren't actually setting the value for a custom column. Instead you are setting a local value in a dict that calibre will ignore. So, you need to add the custom metadata to the mi instance. What (I think) you should do is something like (fixing names and imports as needed): Code:
custom_metadata = db.field_metadata.custom_field_metadata() for each book: mi = Metadata(...) mi.set_all_user_metadata(custom_metadata) # do what you need to do to prepare to add the book # now when you do mi.set('#foobar', val) it knows that you are working with a custom column. db.add_books(....) # this should now add the custom column data |
|
12-10-2014, 07:45 AM | #5 |
Groupie
Posts: 161
Karma: 44048
Join Date: Jan 2012
Device: Kobo Touch
|
Ah... What light through yonder window breaks...
I'm reading the metadata from the source file like this: Code:
with open(src_file, 'rb') as tmp_file: src_metadata = get_metadata(tmp_file, src_type, force_read_metadata=True) OK. So if I'm interpreting this correctly. I need to add the custom field 'definition' to the source metadata object, THEN set the field data using .set() EDIT: That worked perfectly. Thanks for your help. It's much appreciated. Last edited by Howlingwolf; 12-10-2014 at 08:04 AM. Reason: Update |
Advert | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Custom columns when adding books | BookJunkieLI | Library Management | 9 | 06-10-2013 08:30 AM |
Programmatically adding/editing metadata | northernbio | Library Management | 6 | 12-29-2012 05:59 PM |
Adding books to custom column | Gazella | Calibre | 2 | 12-27-2012 11:28 AM |
Working with DateTime custom columns programmatically? | kiwidude | Development | 13 | 12-15-2011 11:09 AM |
New Metadata class (empty books) and custom columns | kiwidude | Development | 14 | 03-12-2011 06:47 AM |