View Single Post
Old 07-08-2023, 01:43 PM   #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,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by geekraver View Post
Sounds like I would need to make a PR against Calibre.
If you know how to code well enough to make a PR (github Pull Request) against calibre then you should be able to write a script that does the work. The script would walk the library looking for an a.txt file (or whatever it is named) in the book folder. When it finds one then it uses the calibre API method cache.add_format() method to add it as a book.

After verifying that the script worked, delete all the a.txt files.

This script might be a place to start.
Code:
import os
from calibre.library import db as DB

lib_path = sys.argv[1]
db = DB(path = lib_path)
cache = db.new_api
# Get the normalized library path
library_path = cache.backend.library_path

for book_id in cache.all_book_ids():
    # Construct the full path to the book folder
    path = os.path.join(library_path, cache.field_for('path', book_id).replace('/', os.sep))
    
    # Check if the file a.txt is in that folder
    txt_file = os.path.join(path, 'a.txt')
    if os.path.exists(txt_file):
        # It is. Add it as a format
        cache.add_format(book_id, 'txt', txt_file, run_hooks=False)
        print('added', txt_file)
You run it with calibre-debug -e script_file path_to_library

Of course, back up your library before running it!

Last edited by chaley; 07-08-2023 at 01:45 PM.
chaley is offline   Reply With Quote