View Single Post
Old 11-05-2020, 07:54 AM   #3
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 sigizmund View Post
Hey folks,

I am trying to understand if I can write a Python code running not as a plugin which will be manipulating Calibre without UI. Example: add book to the specified Calibre library. Obviously starting calibredb command is one option, but I'm interested if I can just `import` some libraries and manipulate database in-process. In pseudocode:

Code:
import calibre.database as db

database = db.OpenDb('/path/to/library')
database.AddBook('/path/to/book.epub')
database.Close()
Thanks!
Yes, you can do what you want. Use the calibre-debug command, and in particular calibre-debug -e. The entire calibre api is available.

This example prints all the tags in a library, sorted.
Code:
from calibre.utils.icu import sort_key
def init_cache(library_path):
	from calibre.db.backend import DB
	from calibre.db.cache import Cache
	backend = DB(library_path)
	cache = Cache(backend)
	cache.init()
	return cache

cache = init_cache(library_path = sys.argv[1])
all_tags = sorted(list(cache.all_field_names('tags')), key=sort_key)
for t in all_tags:
	print(t)
You execute it with
Code:
calibre-debug -e all_tags.py PATH-TO-LIBRARY
chaley is offline   Reply With Quote