Added custom notifier

This commit is contained in:
Phil Borman 2017-03-16 17:27:34 +01:00
parent 377b283341
commit a764703e17
6 changed files with 194 additions and 2 deletions

View File

@ -1748,6 +1748,52 @@
</div>
</fieldset>
</div>
<div class="checkbox">
<%
if lazylibrarian.CONFIG['USE_CUSTOM'] == True:
checked = 'checked="checked"'
else:
checked = ''
%>
<label for="use_custom" class="control-label">
<input type="checkbox" id="use_custom" name="use_custom" value="1" ${checked} />
Enable Custom Notifications</label>
</div>
<div id="customoptions">
<fieldset>
<legend>Custom</legend>
<div class="checkbox">
<%
if lazylibrarian.CONFIG['CUSTOM_NOTIFY_ONSNATCH'] == True:
checked = 'checked="checked"'
else:
checked = ''
%>
<label for="custom_notify_onsnatch">
<input type="checkbox" id="custom_notify_onsnatch" name="custom_notify_onsnatch" value="1" ${checked} />
Notify on snatch</label>
</div>
<div class="checkbox">
<%
if lazylibrarian.CONFIG['CUSTOM_NOTIFY_ONDOWNLOAD'] == True:
checked = 'checked="checked"'
else:
checked = ''
%>
<label for="custom_notify_ondownload">
<input type="checkbox" id="custom_notify_ondownload" name="custom_notify_ondownload" value="1" ${checked} />
Notify on download</label>
</div>
<div class="form-group">
<label for="custom_script">Custom script to run</label>
<input type="text" id="custom_script" name="custom_script" value="${lazylibrarian.CONFIG['CUSTOM_SCRIPT']}" class="form-control" placeholder="Custom Script">
</div>
<div class="form-group">
<input type="button" value="Test Custom" id="testCustom" class="btn btn-default">
<span class="help-block">Save settings before testing</span>
</div>
</fieldset>
</div>
</div>
</div>
@ -2499,6 +2545,26 @@
}
});
if ($("#use_custom").is(":checked"))
{
$("#customoptions").show();
}
else
{
$("#customoptions").hide();
}
$("#use_custom").click(function(){
if ($("#use_custom").is(":checked"))
{
$("#customoptions").slideDown();
}
else
{
$("#customoptions").slideUp();
}
});
if ($("#use_email").is(":checked"))
{
$("#emailoptions").show();
@ -2567,6 +2633,12 @@
});
$('#testCustom').click(function () {
$.get("testCustom",
function (data) { window.alert(data) });
});
$('#testEmail').click(function () {
$.get("testEmail",
function (data) { window.alert(data) });

25
example_custom_notification.py Executable file
View File

@ -0,0 +1,25 @@
#!/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
import sys
err = ''
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:
try:
n = len(args)
mydict = {}
while n:
mydict[args[n-2]] = args[n-1]
n -= 2
except Exception as e:
f.write(str(e))
for item in mydict:
f.write("%s: %s\n" % (item, mydict[item]))

View File

@ -296,6 +296,10 @@ CONFIG_DEFINITIONS = {
'SLACK_NOTIFY_ONSNATCH': ('bool', 'Slack', 0),
'SLACK_NOTIFY_ONDOWNLOAD': ('bool', 'Slack', 0),
'SLACK_TOKEN': ('str', 'Slack', ''),
'USE_CUSTOM': ('bool', 'Custom', 0),
'CUSTOM_NOTIFY_ONSNATCH': ('bool', 'Custom', 0),
'CUSTOM_NOTIFY_ONDOWNLOAD': ('bool', 'Custom', 0),
'CUSTOM_SCRIPT': ('str', 'Custom', ''),
'USE_EMAIL': ('bool', 'Email', 0),
'EMAIL_NOTIFY_ONSNATCH': ('bool', 'Email', 0),
'EMAIL_NOTIFY_ONDOWNLOAD': ('bool', 'Email', 0),

View File

@ -24,6 +24,7 @@ import pushbullet
import pushover
import slack
import tweet
import custom_notify
# online
twitter_notifier = tweet.TwitterNotifier()
@ -34,6 +35,8 @@ androidpn_notifier = androidpn.AndroidPNNotifier()
nma_notifier = nma.NMA_Notifier()
slack_notifier = slack.SlackNotifier()
email_notifier = email_notify.EmailNotifier()
#
custom_notifier = custom_notify.CustomNotifier()
notifiers = [
twitter_notifier,
@ -46,12 +49,13 @@ notifiers = [
email_notifier
]
def custom_notify(bookid):
custom_notifier.notify_download(bookid)
def notify_download(title):
for n in notifiers:
n.notify_download(title)
def notify_snatch(title):
for n in notifiers:
n.notify_snatch(title)

View File

@ -0,0 +1,85 @@
# This file is part of LazyLibrarian.
#
# LazyLibrarian is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LazyLibrarian is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LazyLibrarian. If not, see <http://www.gnu.org/licenses/>.
import lazylibrarian
import subprocess
from lazylibrarian import logger, database
from lazylibrarian.common import notifyStrings, NOTIFY_SNATCH, NOTIFY_DOWNLOAD
class CustomNotifier:
@staticmethod
def _notify(message, event, force=False):
# suppress notifications if the notifier is disabled but the notify options are checked
if not lazylibrarian.CONFIG['USE_CUSTOM'] and not force:
return False
subject = event
text = message
logger.debug('Custom Event: %s' % event)
logger.debug('Custom Message: %s' % message)
myDB = database.DBConnection()
if subject == "Test":
# grab the first entry in the book table
data = myDB.match('SELECT * from books')
else:
# message is a bookid or a magazineid
data = myDB.match('SELECT * from books where BookID="%s"' % message)
if not data:
data = myDB.match('SELECT * from magazines where BookID="%s"' % message)
dictionary = dict(zip(data.keys(), data))
try:
# call the custom notifier script here, passing dictionary deconstructed as strings
if lazylibrarian.CONFIG['CUSTOM_SCRIPT']:
params = [lazylibrarian.CONFIG['CUSTOM_SCRIPT']]
for item in dictionary:
params.append(item)
if hasattr(dictionary[item], 'encode'):
params.append(dictionary[item].encode('utf-8'))
else:
params.append(str(dictionary[item]))
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
if len(res):
return res
return True
else:
logger.warn('Error sending custom notification: Check config')
return False
except Exception as e:
logger.warn('Error sending custom notification: %s' % e)
return False
#
# Public functions
#
def notify_snatch(self, title):
if lazylibrarian.CONFIG['EMAIL_NOTIFY_ONSNATCH']:
self._notify(message=title, event=notifyStrings[NOTIFY_SNATCH])
def notify_download(self, title):
if lazylibrarian.CONFIG['EMAIL_NOTIFY_ONDOWNLOAD']:
self._notify(message=title, event=notifyStrings[NOTIFY_DOWNLOAD])
def test_notify(self, title="Test"):
return self._notify(message=title, event="Test", force=True)
notifier = CustomNotifier

View File

@ -32,7 +32,7 @@ from lazylibrarian.gr import GoodReads
from lazylibrarian.importer import addAuthorToDB
from lazylibrarian.librarysync import get_book_info, find_book_in_db, LibraryScan
from lazylibrarian.magazinescan import create_id, create_cover
from lazylibrarian.notifiers import notify_download
from lazylibrarian.notifiers import notify_download, custom_notify
from lib.deluge_client import DelugeRPCClient
from lib.fuzzywuzzy import fuzz
@ -496,6 +496,7 @@ def processDir(reset=False):
logger.info('Successfully processed: %s' % global_name)
ppcount += 1
custom_notify(book['BookID'])
if internet():
notify_download("%s from %s at %s" % (global_name, book['NZBprov'], now()))
else:
@ -707,6 +708,7 @@ def import_book(pp_path=None, bookID=None):
logger.debug("Not removing original files as in download root")
logger.info('Successfully processed: %s' % global_name)
custom_notify(bookID)
if internet():
notify_download("%s %s at %s" % (global_name, snatched_from, now()))
return True