diff --git a/couchpotato/core/notifications/base.py b/couchpotato/core/notifications/base.py index ea577ab3..85c762e4 100644 --- a/couchpotato/core/notifications/base.py +++ b/couchpotato/core/notifications/base.py @@ -1,20 +1,47 @@ +from couchpotato.api import addApiView from couchpotato.core.event import addEvent from couchpotato.core.helpers.request import jsonified +from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin +log = CPLog(__name__) + class Notification(Plugin): default_title = 'CouchPotato' test_message = 'ZOMG Lazors Pewpewpew!' + listen_to = [] + dont_listen_to = [] + def __init__(self): addEvent('notify', self.notify) + addEvent('notify.%s' % self.getName().lower(), self.notify) - def notify(self, message = '', data = {}): + addApiView(self.testNotifyName(), self.test) + + def notify(self, message = '', data = {}, type = ''): pass def test(self): - success = self.notify(message = self.test_message) - return jsonified({'success': success}) + test_type = self.testNotifyName() + + log.info('Sending test to %s' % test_type) + + success = self.notify( + message = self.test_message, + data = {}, + type = test_type + ) + + #return jsonified({'success': success}) + + def dontNotify(self, type = ''): + return (not type in self.listen_to and len(self.listen_to) == 0 and type != self.testNotifyName()) \ + or type in self.dont_listen_to \ + or self.isDisabled() + + def testNotifyName(self): + return 'notify.%s.test' % self.getName().lower() diff --git a/couchpotato/core/notifications/core/main.py b/couchpotato/core/notifications/core/main.py index 43bfce29..dd4b96ec 100644 --- a/couchpotato/core/notifications/core/main.py +++ b/couchpotato/core/notifications/core/main.py @@ -2,13 +2,13 @@ from couchpotato.api import addApiView from couchpotato.core.event import addEvent, fireEvent from couchpotato.core.helpers.request import jsonified from couchpotato.core.logger import CPLog -from couchpotato.core.plugins.base import Plugin +from couchpotato.core.notifications.base import Notification import time log = CPLog(__name__) -class CoreNotifier(Plugin): +class CoreNotifier(Notification): messages = [] @@ -22,7 +22,7 @@ class CoreNotifier(Plugin): static = self.registerStatic(__file__) fireEvent('register_script', static + 'notification.js') - def notify(self, message = '', data = {}): + def notify(self, message = '', data = {}, type = None): self.add(data = { 'message': message, 'raw': data, diff --git a/couchpotato/core/notifications/growl/main.py b/couchpotato/core/notifications/growl/main.py index c17f270a..76d44936 100644 --- a/couchpotato/core/notifications/growl/main.py +++ b/couchpotato/core/notifications/growl/main.py @@ -12,19 +12,13 @@ log = CPLog(__name__) class Growl(Notification): - def __init__(self): - addEvent('notify', self.notify) - addEvent('notify.growl', self.notify) - - addApiView('notify.growl.test', self.test) + listen_to = ['movie.downloaded', 'movie.snatched'] def conf(self, attr): return Env.setting(attr, 'growl') - def notify(self, message = '', data = {}): - - if self.isDisabled(): - return + def notify(self, message = '', data = {}, type = None): + if self.dontNotify(type): return hosts = [x.strip() for x in self.conf('host').split(",")] password = self.conf('password') diff --git a/couchpotato/core/notifications/history/__init__.py b/couchpotato/core/notifications/history/__init__.py new file mode 100644 index 00000000..f4c53efa --- /dev/null +++ b/couchpotato/core/notifications/history/__init__.py @@ -0,0 +1,6 @@ +from .main import History + +def start(): + return History() + +config = [] diff --git a/couchpotato/core/notifications/history/main.py b/couchpotato/core/notifications/history/main.py new file mode 100644 index 00000000..05d38b8d --- /dev/null +++ b/couchpotato/core/notifications/history/main.py @@ -0,0 +1,33 @@ +from couchpotato import get_session +from couchpotato.core.event import addEvent +from couchpotato.core.logger import CPLog +from couchpotato.core.notifications.base import Notification +from couchpotato.core.settings.model import History as Hist +import time + +log = CPLog(__name__) + + +class History(Notification): + + listen_to = ['movie.downloaded', 'movie.snatched', 'movie.renaming.'] + + def __init__(self): + + addEvent('notify', self.notify) + + addEvent('app.load', self.test) + + + def notify(self, message = '', data = {}, type = None): + if self.dontNotify(type): return + + db = get_session() + history = Hist( + added = int(time.time()), + message = message, + type = type, + release_id = data.get('id', 0) + ) + db.add(history) + db.commit() diff --git a/couchpotato/core/notifications/history/static/history.js b/couchpotato/core/notifications/history/static/history.js new file mode 100644 index 00000000..e69de29b diff --git a/couchpotato/core/notifications/nmj/main.py b/couchpotato/core/notifications/nmj/main.py index 1098d010..32c143e2 100644 --- a/couchpotato/core/notifications/nmj/main.py +++ b/couchpotato/core/notifications/nmj/main.py @@ -1,5 +1,4 @@ from couchpotato.api import addApiView -from couchpotato.core.event import addEvent from couchpotato.core.helpers.request import getParams, jsonified from couchpotato.core.logger import CPLog from couchpotato.core.notifications.base import Notification @@ -19,11 +18,11 @@ log = CPLog(__name__) class NMJ(Notification): - def __init__(self): - addEvent('notify', self.notify) - addEvent('notify.nmj', self.notify) + listen_to = ['movie.downloaded', 'movie.snatched'] + + def __init__(self): + super(NMJ, self).__init__() - addApiView('notify.nmj.test', self.test) addApiView('notify.nmj.auto_config', self.autoConfig) def conf(self, attr): @@ -76,10 +75,8 @@ class NMJ(Notification): 'mount': mount, }) - def notify(self, message = '', data = {}): - - if self.isDisabled(): - return False + def notify(self, message = '', data = {}, type = None): + if self.dontNotify(type): return host = self.conf('host') mount = self.conf('mount') diff --git a/couchpotato/core/notifications/notifo/main.py b/couchpotato/core/notifications/notifo/main.py index 0bea3673..feafc3b4 100644 --- a/couchpotato/core/notifications/notifo/main.py +++ b/couchpotato/core/notifications/notifo/main.py @@ -16,19 +16,13 @@ class Notifo(Notification): url = 'https://api.notifo.com/v1/send_notification' - def __init__(self): - addEvent('notify', self.notify) - addEvent('notify.notifo', self.notify) - - addApiView('notify.notifo.test', self.test) + listen_to = ['movie.downloaded', 'movie.snatched'] def conf(self, attr): return Env.setting(attr, 'notifo') - def notify(self, message = '', data = {}): - - if self.isDisabled(): - return False + def notify(self, message = '', data = {}, type = None): + if self.dontNotify(type): return try: data = urllib.urlencode({ diff --git a/couchpotato/core/notifications/plex/main.py b/couchpotato/core/notifications/plex/main.py index 6756ef01..b93e114b 100644 --- a/couchpotato/core/notifications/plex/main.py +++ b/couchpotato/core/notifications/plex/main.py @@ -10,16 +10,10 @@ log = CPLog(__name__) class Plex(Notification): - def __init__(self): - addEvent('notify', self.notify) - addEvent('notify.plex', self.notify) + listen_to = ['movie.downloaded', 'movie.snatched'] - addApiView('notify.plex.test', self.test) - - def notify(self, message = '', data = {}): - - if self.isDisabled(): - return + def notify(self, message = '', data = {}, type = None): + if self.dontNotify(type): return log.info('Sending notification to Plex') hosts = [x.strip() for x in self.conf('host').split(",")] diff --git a/couchpotato/core/notifications/prowl/main.py b/couchpotato/core/notifications/prowl/main.py index 5886323b..a43ab5e9 100644 --- a/couchpotato/core/notifications/prowl/main.py +++ b/couchpotato/core/notifications/prowl/main.py @@ -1,5 +1,3 @@ -from couchpotato.api import addApiView -from couchpotato.core.event import addEvent from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.logger import CPLog from couchpotato.core.notifications.base import Notification @@ -11,16 +9,10 @@ log = CPLog(__name__) class Prowl(Notification): - def __init__(self): - addEvent('notify', self.notify) - addEvent('notify.prowl', self.notify) + listen_to = ['movie.downloaded', 'movie.snatched'] - addApiView('notify.prowl.test', self.test) - - def notify(self, message = '', data = {}): - - if self.isDisabled(): - return + def notify(self, message = '', data = {}, type = None): + if self.dontNotify(type): return http_handler = HTTPSConnection('api.prowlapp.com') diff --git a/couchpotato/core/notifications/xbmc/main.py b/couchpotato/core/notifications/xbmc/main.py index 6b86e6e7..ead59b38 100644 --- a/couchpotato/core/notifications/xbmc/main.py +++ b/couchpotato/core/notifications/xbmc/main.py @@ -1,5 +1,3 @@ -from couchpotato.api import addApiView -from couchpotato.core.event import addEvent from couchpotato.core.logger import CPLog from couchpotato.core.notifications.base import Notification import base64 @@ -11,16 +9,10 @@ log = CPLog(__name__) class XBMC(Notification): - def __init__(self): - addEvent('notify', self.notify) - addEvent('notify.xbmc', self.notify) + listen_to = ['movie.downloaded', 'movie.snatched'] - addApiView('notify.xbmc.test', self.test) - - def notify(self, message = '', data = {}): - - if self.isDisabled(): - return + def notify(self, message = '', data = {}, type = None): + if self.dontNotify(type): return for host in [x.strip() for x in self.conf('host').split(",")]: self.send({'command': 'ExecBuiltIn', 'parameter': 'Notification(CouchPotato, %s)' % message}, host) diff --git a/couchpotato/core/plugins/base.py b/couchpotato/core/plugins/base.py index e617fb1f..936d55c1 100644 --- a/couchpotato/core/plugins/base.py +++ b/couchpotato/core/plugins/base.py @@ -5,7 +5,7 @@ import os.path import re -class Plugin(): +class Plugin(object): def conf(self, attr, default = None): return Env.setting(attr, self.getName().lower(), default = default) @@ -35,4 +35,4 @@ class Plugin(): return not self.isEnabled() def isEnabled(self): - return self.conf('enabled') + return self.conf('enabled') or self.conf('enabled') == None diff --git a/couchpotato/core/plugins/renamer/main.py b/couchpotato/core/plugins/renamer/main.py index 83b04930..80fa800c 100644 --- a/couchpotato/core/plugins/renamer/main.py +++ b/couchpotato/core/plugins/renamer/main.py @@ -149,6 +149,10 @@ class Renamer(Plugin): if multiple: cd += 1 + # Notify on download + download_message = 'Download of %s (%s) successful.' % (group['library']['titles'][0]['title'], replacements['quality']) + fireEvent('notify', type = 'movie.downloaded', message = download_message, data = replacements) + # Before renaming, remove the lower quality files db = get_session() library = db.query(Library).filter_by(identifier = group['library']['identifier']).first() @@ -172,6 +176,10 @@ class Renamer(Plugin): for rename_me in rename_files: filename = os.path.basename(rename_me) rename_files[rename_me] = rename_me.replace(filename, '_EXISTS_%s' % filename) + + # Notify on rename fail + download_message = 'Renaming of %s (%s) canceled, exists in %s already.' % (movie.library.titles[0].title, group['meta_data']['quality']['label'], release.quality.label) + fireEvent('notify', type = 'movie.renaming.canceled', message = download_message, data = group) break @@ -200,7 +208,7 @@ class Renamer(Plugin): #print rename_me, rename_files[rename_me] - # Search for trailers + # Search for trailers etc fireEvent('renamer.after', group) def moveFile(self, old, dest, suppress = True): diff --git a/couchpotato/core/plugins/searcher/main.py b/couchpotato/core/plugins/searcher/main.py index 64e6daf0..dff307ea 100644 --- a/couchpotato/core/plugins/searcher/main.py +++ b/couchpotato/core/plugins/searcher/main.py @@ -91,7 +91,6 @@ class Searcher(Plugin): successful = fireEvent('download', data = nzb, movie = movie, single = True) if successful: - log.info('Downloading of %s successful.' % nzb.get('name')) # Mark release as snatched db = get_session() @@ -105,6 +104,9 @@ class Searcher(Plugin): mvie.status_id = snatched_status.get('id') db.commit() + log.info('Downloading of %s successful.' % nzb.get('name')) + fireEvent('notify', type = 'movie.snatched', message = 'Downloading of %s successful.' % nzb.get('name'), data = rls.to_dict()) + return True return False diff --git a/couchpotato/core/settings/model.py b/couchpotato/core/settings/model.py index 2d10b0f0..d9e11a1e 100644 --- a/couchpotato/core/settings/model.py +++ b/couchpotato/core/settings/model.py @@ -2,8 +2,8 @@ from elixir.entity import Entity from elixir.fields import Field from elixir.options import options_defaults from elixir.relationships import OneToMany, ManyToOne -from libs.elixir.options import using_options -from libs.elixir.relationships import ManyToMany +from elixir.options import using_options +from elixir.relationships import ManyToMany from sqlalchemy.types import Integer, Unicode, UnicodeText, Boolean, Float, \ String @@ -179,7 +179,10 @@ class History(Entity): """History of actions that are connected to a certain release, such as, renamed to, downloaded, deleted, download subtitles etc""" + added = Field(Integer) message = Field(UnicodeText()) + type = Field(Unicode(50)) + release = ManyToOne('Release')