Allow custom notifier to return stdout/stderr messages to lazylibrarian log

This commit is contained in:
Phil Borman 2018-02-22 16:20:10 +01:00
parent 836551b583
commit c2cc081f2f
5 changed files with 59 additions and 52 deletions

View File

@ -1,26 +1,23 @@
#!/usr/bin/python
# The parameter list passed is a list of database columns (name and data)
# for a book or magazine. Read them back into a dictionary.
# For this example, just write the dictionary to a file
# For this example, just return the formatted dictionary
import sys
err = ''
mydict = {}
try:
args = sys.argv[1:]
except Exception as e:
err = str(e)
with open('notification.out', 'w') as f:
if err:
f.write(err)
else:
mydict = {}
n = len(args)
while n:
try:
mydict[args[n-2]] = args[n-1]
n -= 2
except IndexError:
break
while len(args) > 1:
mydict[args[0]] = args[1]
args = args[2:]
except Exception as err:
sys.stderr.write("%s\n" % err)
exit(1)
for item in mydict:
# column name: value
f.write("%s: %s\n" % (item, mydict[item]))
res = ''
for item in mydict:
# column name: value
res = res + "%s: %s\n" % (item, mydict[item])
print(res)
exit(0)

View File

@ -5,10 +5,10 @@
# program from calibre to make sure we have both epub and mobi of the new book.
# Note it is not fully error trapped, just a basic working example.
# Would be better to check "res" return value from the converter too
# The result returned (in this case "msg") is passed back to the "test" button
# and gets displayed as an error message (telling you why the test failed).
# Returning "msg" at this point is useful for testing the notifier script
# Always exit zero.
# Error messages appear as errors in the lazylibrarian log
# Anything you print to stdout appears as debug messages in the log
# The exit code is passed back to the "test" button
# Always exit zero on success, non-zero on fail
import sys
import os
@ -16,16 +16,15 @@ import subprocess
converter = "ebook-convert" # if not in your "path", put the full pathname here
args = sys.argv[1:]
n = len(args)
mydict = {}
while n:
try:
mydict[args[n-2]] = args[n-1]
n -= 2
except IndexError:
break
try:
args = sys.argv[1:]
while len(args) > 1:
mydict[args[0]] = args[1]
args = args[2:]
except Exception as err:
sys.stderr.write("%s\n" % err)
exit(1)
# mydict is now a dictionary of the book/magazine table entry
# and the wanted table entry for the relevant book/magazine
@ -41,24 +40,29 @@ if 'Event' in mydict and mydict['Event'] == 'Added to Library':
basename, extn = os.path.splitext(mydict['BookFile'])
have_epub = os.path.exists(basename + '.epub')
have_mobi = os.path.exists(basename + '.mobi')
if have_epub and not have_mobi:
params = [converter, basename + '.epub', basename + '.mobi']
try:
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
msg = "Created mobi for %s" % mydict['BookName']
print("Created mobi for %s" % mydict['BookName'])
exit(0)
except Exception as e:
msg = str(e)
sys.stderr.write("%s\n" % e)
exit(1)
if have_mobi and not have_epub:
params = [converter, basename + '.mobi', basename + '.epub']
try:
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
msg = "Created epub for %s" % mydict['BookName']
print("Created epub for %s" % mydict['BookName'])
exit(0)
except Exception as e:
msg = str(e)
sys.stderr.write("%s\n" % e)
exit(1)
else:
msg = "No bookfile found"
sys.stderr.write("%s\n" % "No bookfile found")
exit(1)
else:
msg = "Not a download event"
if len(msg):
sys.stderr.write("%s\n" % msg)
exit(0)
sys.stderr.write("%s\n" % "Not a download event")
exit(1)

View File

@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with LazyLibrarian. If not, see <http://www.gnu.org/licenses/>.
import subprocess
from subprocess import Popen, PIPE
import lazylibrarian
from lazylibrarian import logger, database
@ -47,7 +47,7 @@ class CustomNotifier:
bookid = " ".join(words[:-1])
book = myDB.match('SELECT * from books where BookID=?', (bookid,))
if not book:
book = myDB.match('SELECT * from magazines where BookID=?', (bookid,))
book = myDB.match('SELECT * from magazines where Title=?', (bookid,))
if event == 'Added to Library':
wanted_status = 'Processed'
@ -87,8 +87,16 @@ class CustomNotifier:
params.append(str(dictionary[item]))
try:
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
return makeUnicode(res).strip()
p = Popen(params, stdout=PIPE, stderr=PIPE)
res, err = p.communicate()
rc = p.returncode
res = makeUnicode(res)
err = makeUnicode(err)
if rc:
logger.error("Custom notifier returned %s: res[%s] err[%s]" % (rc, res, err))
return False
logger.debug(res)
return True
except Exception as e:
logger.warn('Error sending command: %s' % e)
return False
@ -113,9 +121,6 @@ class CustomNotifier:
self._notify(message=title, event=notifyStrings[NOTIFY_DOWNLOAD])
def test_notify(self, title="Test"):
res = self._notify(message=title, event="Test", force=True)
logger.debug(res)
return res
return self._notify(message=title, event="Test", force=True)
notifier = CustomNotifier

View File

@ -354,9 +354,10 @@ def search_magazines(mags=None, reset=False):
# Issue/No/Nr/Vol nn, YYYY or Issue/No/Nr/Vol nn
if not regex_pass:
nouns = ["issue", "no", "nr", "vol", "volume"]
pos = 0
while pos < len(nzbtitle_exploded):
if nzbtitle_exploded[pos].lower() in ["issue", "no", "nr", "vol"]:
if nzbtitle_exploded[pos].lower().strip('.') in nouns:
if pos + 1 < len(nzbtitle_exploded):
issue = check_int(nzbtitle_exploded[pos + 1], 0)
if issue:

View File

@ -3323,8 +3323,8 @@ class WebInterface(object):
if 'script' in kwargs:
lazylibrarian.CONFIG['CUSTOM_SCRIPT'] = kwargs['script']
result = notifiers.custom_notifier.test_notify()
if result:
return "Custom notification failed,\n%s" % result
if result is False:
return "Custom notification failed"
else:
lazylibrarian.config_write('Custom')
return "Custom notification successful"