MrCastro
MrCastro

Reputation: 485

calibredb add book - via cgi script

I wrote a python cgi script that receives the book file + some text from an html form. The scripts saves the file without a problem (in a temp dir: /opt/bibliotecha/tmp_bookcase/), however I am trying to use calibredb tool to add it to calibre library, but I am not succeeding.

I isolated the problematic part into the following script;

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cgi 
import cgitb; cgitb.enable()

print "Content-type: text/html"
print

import os, subprocess
tmp_upload_path = "/opt/bibliotecha/tmp_bookcase/"
tmp_path_book = tmp_upload_path + "Vila-Matas,Enrique-Dublinesque(NewDirections,2012).epubVila-Matas,Enrique-Dublinesque(NewDirections,2012).epub"

subprocess.call(["calibredb", "add", tmp_path_book]) 

If I call the script from the terminal, it does what is supposed to do.

If I call the script from the browser, I get the following message:

No write acces to /var/www/.config/calibre using a temporary dir instead

I believe the error might have to do with the fact that when the script is called from the browser it users a different user and group. But how could calibre don't complain about that?

I found a thread that seems to address the same problem https://bugs.launchpad.net/calibre/+bug/1299635 and they mention the environment on runs calibre in is the having to be the same in php (in their case) and the console.

but I don't really get the calibre environment, never heard about it.

Any hints?? :D

a

Upvotes: 1

Views: 942

Answers (2)

Anthon
Anthon

Reputation: 76568

I used this method, successfully for some time but at some point it stopped working altogether, as Calibre had been changed to no longer allow multiple programs to write to its database (probably to prevent corruption).

What you now (2021) need to do:

Configure and start the server. Go to 'Preferences -> Sharing -> Sharing over the net'. You need to [Start] the server, and probably select "run server automatically".

You need to take note of the port number, or alternatively you can dynamically scan the file ~/.config/calibre/server-config.txt. Look for the line # The port on which to listen for connections then the next line should be port NNNN with NNNN the port number. If you cannot find those lines, the default port (NNNN=8080) was selected in the GUI.

To add a book you need the name of the library you want to put it into. To get a list of libraries available run:

calibredb add --with-library=http://localhost:NNNN/#-

Either hard code the resulting library name (LLL), or if you have only one library, you can again dynamically extract the name from the output of the command above. If you have multiple libraries, you'll need to decide on where new additions should go.

Now you have the port number (NNNN) and the library name (LLL), you can do:

calibredb add --with-library=http://localhost:NNNN/#LLL

You can additionally use the option --automerge=ignore to prevent books already in the database to be added again. Replace ignore with overwrite to replace existing files, and with new_record to create extra entries.

The above assumes calibre is running on the same machine as the cgi script. If not, you need to configure authentication

Upvotes: 0

cchristelis
cchristelis

Reputation: 2015

Firstly have a look at who owns the directory and which permissions your current user will have on the directory.

This can be done by using ls -l. Once you have determined this you can use the chmod to allow your user to write to this directory.

If for some reason you really need to use a specific user when running the actual write command, then have a look at this post's answer:

Run child processes as different user from a long running process

Upvotes: 0

Related Questions