View Single Post
Old 07-04-2023, 10:25 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,471
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I'm not sure what you are trying to do. I assume you want to read and write author links in a script using the calibre API.

The correct API to use is in db.cache:
Code:
def set_link_map(self, field, value_to_link_map, only_set_if_no_existing_link=False):
        '''
        Sets links for item values in field.
        Note: this method doesn't change values not in the value_to_link_map

        :param field: the lookup name
        :param value_to_link_map: dict(field_value:link, ...). Note that these are values, not field ids.

        :return: books changed by setting the link

        '''
In your case the map would be
Code:
{author_name: link_value, ... }
If you are trying to use/export existing links then use
Code:
    def get_link_map(self, for_field):
        '''
        Return a dictionary of links for the supplied field.

        :param for_field: the lookup name of the field for which the link map is desired

        :return: {field_value:link_value, ...} for non-empty links
        '''
Here is a code sample that manipulates the author links.
Code:
from calibre.library import db as DB

lib_path = sys.argv[1]
db = DB(path = lib_path)
cache = db.new_api

# print all the links in the map
author_link_map = cache.get_link_map('authors')
for aut,link in author_link_map.items():
    print(aut, link)
    
print('*******************')
# Save the existing link for author 'A B'
existing_link = author_link_map.get('A B')
print('existing link', existing_link)
print('*******************')

# modify a link
new_links = dict()
new_links['A B'] = 'This is a new link' # Author named "A B" must exist
cache.set_link_map('authors', new_links)

# Test that it was changed
author_link_map = cache.get_link_map('authors')
print('New link:', author_link_map.get('A B'))
print('*******************')

# Now put the old link back
new_links['A B'] = existing_link
cache.set_link_map('authors', new_links)
author_link_map = cache.get_link_map('authors')
print('Restored link:', author_link_map.get('A B'))
If the above is in the file foo.py you would call it with
Code:
calibre-debug -e foo.py 'path_to_library'
chaley is offline   Reply With Quote