|
|
#1 |
|
Member
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 24
Karma: 1088
Join Date: Dec 2009
Device: Pocketbook && Android
|
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()
|
|
|
|
|
|
#2 |
|
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,609
Karma: 28549044
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
|
|
|
|
| Advert | |
|
|
|
|
#3 | |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,525
Karma: 8065948
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
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)
Code:
calibre-debug -e all_tags.py PATH-TO-LIBRARY |
|
|
|
|
|
|
#4 |
|
Member
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 24
Karma: 1088
Join Date: Dec 2009
Device: Pocketbook && Android
|
Thanks folks! Are there any examples of set up of how I can set up development environement for this (ideally using something like pyvenv so it depends less on external configuration)?
|
|
|
|
|
|
#5 |
|
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,609
Karma: 28549044
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
There's no external configuration and no need for pyenv. Write your script, run it with calibre-debug -e. If your script requires python packages that are not bundles with calibre, simply add the path to them at the top of the script with
sys.path.append('/path/to/wherever your python package is') |
|
|
|
| Advert | |
|
|
|
|
#6 |
|
Member
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 24
Karma: 1088
Join Date: Dec 2009
Device: Pocketbook && Android
|
Got it - thanks for help!
|
|
|
|
|
|
#7 |
|
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 77
Karma: 2136220
Join Date: Sep 2012
Device: none
|
Is it possible to use this method even if an instance of calibre is already running or is it necessary (or better) to close calibre before running calibre-debug?
I'm going to use it in a shell script and I'm sure that I will not be doing any modification in calibre while the shell script will be running. |
|
|
|
|
|
#8 |
|
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,609
Karma: 28549044
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
depends on what you plan to do with calibre-debug. Any kind of database/library access should not be done while calibre is running. Anything else should mostly be fine
|
|
|
|
|
|
#9 |
|
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 77
Karma: 2136220
Join Date: Sep 2012
Device: none
|
Since I would like to modify a metadata, I suppose it is better to do that when calibre is not running, then. Is it correct?
|
|
|
|
|
|
#10 |
|
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,609
Karma: 28549044
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
if youa re modifying metadata in a file use ebook-meta.exe if you are modifying it in the calibre library use calibredb.exe
|
|
|
|
|
|
#11 |
|
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 77
Karma: 2136220
Join Date: Sep 2012
Device: none
|
OK, thanks again
|
|
|
|
![]() |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problems using Calibre 3.15.0 Database API | einnordlicht | Development | 2 | 01-06-2018 10:28 PM |
| Is there any Python API for file conversion? | avin | Development | 0 | 12-27-2016 09:11 AM |
| Calibre API for Mobi conversion | Sebadub | Development | 4 | 07-03-2013 12:59 PM |
| Calibre update breaks Calibre API | TechnoCat | Recipes | 5 | 01-29-2013 12:25 AM |
| calibre's new plugin API | kovidgoyal | Plugins | 26 | 05-07-2011 03:43 PM |