View Single Post
Old 01-16-2021, 05:17 AM   #1489
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,506
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Terisa de morgan View Post
Thank you, I thought that was the way but I was not sure if there was an automated way. As I have a whole library with authors, that is not polished, I think I'll keep it for a future development... or I'll think for the future authors and I'll explore from there (I already copy through a plugin new authors to the authors library).
Generating the author links is straightforward using the calibre API. For each author in the library, find the book containing that author then write the link to standard output.

The code below generates links to a book for every author in the database. The output is 2-column CSV where column 1 is the author name and col 2 is the link.
Code:
from calibre.library import db as DB
import posixpath

lib_path = sys.argv[1]
lib_name = '_hex_-' + posixpath.basename(lib_path).encode('utf-8').hex()
db = DB(path = lib_path, read_only=True)
cache = db.new_api

# Loop through all the books in the library
author_info = cache.author_data()
for auth_id in author_info:
    auth_books = cache.books_for_field('authors', auth_id)
    print('"{}",calibre://show-book/{}/{}'.format(
            author_info[auth_id]['name'], lib_name, str(next(iter(auth_books)))))
You run this code using
Code:
calibre-debug -e script.py path-to-library
The other side to put the author links into the author table would be similar.
  1. Read the data from above into a link_dict author_name:link
  2. Get the author data
  3. Make a list author_dict of author-name: author_id
  4. Make an empty dict new_link_dict
  5. for each name in link_dict check if the name is in author_dict. If so, add author_id:link to new_link_dict
  6. call cache.set_link_for_authors(new_link_dict)

Last edited by chaley; 01-16-2021 at 05:06 PM. Reason: Forgot script argument to calibre-debug
chaley is offline   Reply With Quote