summaryrefslogtreecommitdiffstats
path: root/module/plugins
diff options
context:
space:
mode:
authorGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-10-20 20:11:02 +0200
committerGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-10-20 20:11:02 +0200
commit22cc829d68fa129f0d2bd33da2ea479c51563e7e (patch)
treefe71b3a8e039e379782d4fb5305a645be5c6285e /module/plugins
parentCode cosmetics (diff)
downloadpyload-22cc829d68fa129f0d2bd33da2ea479c51563e7e.tar.xz
Notifier framework
Diffstat (limited to 'module/plugins')
-rw-r--r--module/plugins/hooks/AndroidPhoneNotify.py99
-rw-r--r--module/plugins/hooks/PushBullet.py104
-rw-r--r--module/plugins/hooks/PushOver.py111
-rw-r--r--module/plugins/hooks/WindowsPhoneNotify.py88
-rw-r--r--module/plugins/internal/Notifier.py117
5 files changed, 161 insertions, 358 deletions
diff --git a/module/plugins/hooks/AndroidPhoneNotify.py b/module/plugins/hooks/AndroidPhoneNotify.py
index 6ac250e7b..cc7fe8d6b 100644
--- a/module/plugins/hooks/AndroidPhoneNotify.py
+++ b/module/plugins/hooks/AndroidPhoneNotify.py
@@ -1,14 +1,12 @@
# -*- coding: utf-8 -*-
-import time
+from module.plugins.internal.Notifier import Notifier
-from module.plugins.internal.Addon import Addon, Expose
-
-class AndroidPhoneNotify(Addon):
+class AndroidPhoneNotify(Notifier):
__name__ = "AndroidPhoneNotify"
__type__ = "hook"
- __version__ = "0.12"
+ __version__ = "0.13"
__status__ = "testing"
__config__ = [("activated" , "bool", "Activated" , False),
@@ -22,94 +20,19 @@ class AndroidPhoneNotify(Addon):
("sendpermin" , "int" , "Max notifications per minute" , 12 ),
("ignoreclient" , "bool", "Send notifications if client is connected", False)]
- __description__ = """Send push notifications to your Android Phone (using notifymyandroid.com)"""
+ __description__ = """Send push notifications to your Android Phone using notifymyandroid.com"""
__license__ = "GPLv3"
__authors__ = [("Steven Kosyra" , "steven.kosyra@gmail.com"),
("Walter Purcaro", "vuolter@gmail.com" )]
- def init(self):
- self.event_map = {'allDownloadsProcessed': "all_downloads_processed",
- 'plugin_updated' : "plugin_updated" }
-
- self.last_notify = 0
- self.notifications = 0
-
-
- def plugin_updated(self, type_plugins):
- if not self.get_config('notifyupdate'):
- return
-
- self.notify(_("Plugins updated"), str(type_plugins))
-
-
- def activate(self):
- self.key = self.get_config('apikey')
-
-
- def exit(self):
- if not self.get_config('notifyexit'):
- return
-
- if self.pyload.do_restart:
- self.notify(_("Restarting pyLoad"))
- else:
- self.notify(_("Exiting pyLoad"))
-
-
- def captcha_task(self, task):
- if not self.get_config('notifycaptcha'):
- return
-
- self.notify(_("Captcha"), _("New request waiting user input"))
-
+ def get_key(self):
+ return self.get_config('apikey')
- def package_finished(self, pypack):
- if self.get_config('notifypackage'):
- self.notify(_("Package finished"), pypack.name)
-
-
- def all_downloads_processed(self):
- if not self.get_config('notifyprocessed'):
- return
-
- if any(True for pdata in self.pyload.api.getQueue() if pdata.linksdone < pdata.linkstotal):
- self.notify(_("Package failed"), _("One or more packages was not completed successfully"))
- else:
- self.notify(_("All packages finished"))
-
-
- @Expose
- def notify(self,
- event,
- msg="",
- key=None):
-
- key = key or self.key
- if not key:
- return
-
- if self.pyload.isClientConnected() and not self.get_config('ignoreclient'):
- return
-
- elapsed_time = time.time() - self.last_notify
-
- if elapsed_time < self.get_config("sendtimewait"):
- return
-
- if elapsed_time > 60:
- self.notifications = 0
-
- elif self.notifications >= self.get_config("sendpermin"):
- return
+ def send(self, event, msg, key):
self.load("http://www.notifymyandroid.com/publicapi/notify",
- get={'apikey' : key,
- 'application': "pyLoad",
- 'event' : event,
- 'description': msg})
-
- self.last_notify = time.time()
- self.notifications += 1
-
- return True
+ get={'apikey' : key,
+ 'application': "pyLoad",
+ 'event' : event,
+ 'description': msg})
diff --git a/module/plugins/hooks/PushBullet.py b/module/plugins/hooks/PushBullet.py
index 8486913c6..6cf7dc173 100644
--- a/module/plugins/hooks/PushBullet.py
+++ b/module/plugins/hooks/PushBullet.py
@@ -1,21 +1,19 @@
# -*- coding: utf-8 -*-
-import time
-
import pycurl
-from module.plugins.internal.Addon import Addon, Expose
from module.network.RequestFactory import getRequest as get_request
+from module.plugins.internal.Notifier import Notifier
-class PushBullet(Addon):
+class PushBullet(Notifier):
__name__ = "PushBullet"
__type__ = "hook"
- __version__ = "0.01"
+ __version__ = "0.02"
__status__ = "testing"
__config__ = [("activated" , "bool", "Activated" , False),
- ("tokenkey" , "str" , "Access Token" , "" ),
+ ("tokenkey" , "str" , "Access Token" , "" ),
("notifycaptcha" , "bool", "Notify captcha request" , True ),
("notifypackage" , "bool", "Notify package finished" , True ),
("notifyprocessed", "bool", "Notify packages processed" , True ),
@@ -25,99 +23,21 @@ class PushBullet(Addon):
("sendpermin" , "int" , "Max notifications per minute" , 12 ),
("ignoreclient" , "bool", "Send notifications if client is connected", False)]
- __description__ = """Send push notifications to your Phone (using PushBullet / based on AndroidPhoneNotify)"""
+ __description__ = """Send push notifications to your phone using pushbullet.com"""
__license__ = "GPLv3"
__authors__ = [("Malkavi" , "")]
- def init(self):
- self.event_map = {'allDownloadsProcessed': "all_downloads_processed",
- 'plugin_updated' : "plugin_updated" }
-
- self.last_notify = 0
- self.notifications = 0
-
-
- def plugin_updated(self, type_plugins):
- if not self.get_config('notifyupdate'):
- return
-
- self.notify(_("Plugins updated"), str(type_plugins))
-
-
- def activate(self):
- self.key = self.get_config('tokenkey')
-
-
- def exit(self):
- if not self.get_config('notifyexit'):
- return
-
- if self.pyload.do_restart:
- self.notify(_("Restarting pyLoad"))
- else:
- self.notify(_("Exiting pyLoad"))
-
-
- def captcha_task(self, task):
- if not self.get_config('notifycaptcha'):
- return
-
- self.notify(_("Captcha"), _("New request waiting user input"))
+ def get_key(self):
+ return self.get_config('tokenkey')
- def package_finished(self, pypack):
- if self.get_config('notifypackage'):
- self.notify(_("Package finished"), pypack.name)
-
-
- def all_downloads_processed(self):
- if not self.get_config('notifyprocessed'):
- return
-
- if any(True for pdata in self.pyload.api.getQueue() if pdata.linksdone < pdata.linkstotal):
- self.notify(_("Package failed"), _("One or more packages was not completed successfully"))
- else:
- self.notify(_("All packages finished"))
-
-
- @Expose
- def notify(self,
- event,
- msg=None,
- key=None):
-
- key = key or self.key
- if not key:
- return
-
- if not msg:
- msg = event
-
- if self.pyload.isClientConnected() and not self.get_config('ignoreclient'):
- return
-
- elapsed_time = time.time() - self.last_notify
-
- if elapsed_time < self.get_config("sendtimewait"):
- return
-
- if elapsed_time > 60:
- self.notifications = 0
-
- elif self.notifications >= self.get_config("sendpermin"):
- return
-
+ def send(self, event, msg, key):
req = get_request()
req.c.setopt(pycurl.HTTPHEADER, ["Access-Token: %s" % str(key)])
self.load("https://api.pushbullet.com/v2/pushes",
- post={'type' : 'note',
- 'title' : event,
- 'message': msg},
- req=req)
-
- self.last_notify = time.time()
- self.notifications += 1
-
- return True
+ post={'type' : 'note',
+ 'title' : event,
+ 'message': msg},
+ req=req)
diff --git a/module/plugins/hooks/PushOver.py b/module/plugins/hooks/PushOver.py
index 66c709a3c..958af0fa0 100644
--- a/module/plugins/hooks/PushOver.py
+++ b/module/plugins/hooks/PushOver.py
@@ -2,18 +2,18 @@
import time
-from module.plugins.internal.Addon import Addon, Expose
+from module.plugins.internal.Notifier import Notifier
-class PushOver(Addon):
+class PushOver(Notifier):
__name__ = "PushOver"
__type__ = "hook"
- __version__ = "0.01"
+ __version__ = "0.02"
__status__ = "testing"
__config__ = [("activated" , "bool", "Activated" , False),
- ("tokenkey" , "str" , "API Token/Key" , "" ),
- ("userkey" , "str" , "User key" , "" ),
+ ("tokenkey" , "str" , "Token key" , "" ),
+ ("userkey" , "str" , "User key" , "" ),
("notifycaptcha" , "bool", "Notify captcha request" , True ),
("notifypackage" , "bool", "Notify package finished" , True ),
("notifyprocessed", "bool", "Notify packages processed" , True ),
@@ -23,102 +23,19 @@ class PushOver(Addon):
("sendpermin" , "int" , "Max notifications per minute" , 12 ),
("ignoreclient" , "bool", "Send notifications if client is connected", False)]
- __description__ = """Send push notifications to your Phone (using PushOver / based on AndroidPhoneNotify)"""
+ __description__ = """Send push notifications to your phone using pushover.net"""
__license__ = "GPLv3"
__authors__ = [("Malkavi" , "")]
- def init(self):
- self.event_map = {'allDownloadsProcessed': "all_downloads_processed",
- 'plugin_updated' : "plugin_updated" }
+ def get_key(self):
+ return self.get_config('tokenkey'), self.get_config('userkey')
- self.last_notify = 0
- self.notifications = 0
-
-
- def plugin_updated(self, type_plugins):
- if not self.get_config('notifyupdate'):
- return
-
- self.notify(_("Plugins updated"), str(type_plugins))
-
-
- def activate(self):
- self.keytoken = self.get_config('tokenkey')
- self.keyuser = self.get_config('userkey')
-
-
- def exit(self):
- if not self.get_config('notifyexit'):
- return
-
- if self.pyload.do_restart:
- self.notify(_("Restarting pyLoad"))
- else:
- self.notify(_("Exiting pyLoad"))
-
-
- def captcha_task(self, task):
- if not self.get_config('notifycaptcha'):
- return
-
- self.notify(_("Captcha"), _("New request waiting user input"))
-
-
- def package_finished(self, pypack):
- if self.get_config('notifypackage'):
- self.notify(_("Package finished"), pypack.name)
-
-
- def all_downloads_processed(self):
- if not self.get_config('notifyprocessed'):
- return
-
- if any(True for pdata in self.pyload.api.getQueue() if pdata.linksdone < pdata.linkstotal):
- self.notify(_("Package failed"), _("One or more packages was not completed successfully"))
- else:
- self.notify(_("All packages finished"))
-
-
- @Expose
- def notify(self,
- event,
- msg=None,
- keytoken=None,
- keyuser=None):
-
- keytoken = keytoken or self.keytoken
- if not keytoken:
- return
-
- keyuser = keyuser or self.keyuser
- if not keyuser:
- return
-
- if not msg:
- msg = event
-
- if self.pyload.isClientConnected() and not self.get_config('ignoreclient'):
- return
-
- elapsed_time = time.time() - self.last_notify
-
- if elapsed_time < self.get_config("sendtimewait"):
- return
-
- if elapsed_time > 60:
- self.notifications = 0
-
- elif self.notifications >= self.get_config("sendpermin"):
- return
+ def send(self, event, msg, key):
+ token, user = key
self.load("https://api.pushover.net/1/messages.json",
- post={'token' : keytoken,
- 'user' : keyuser,
- 'title' : event,
- 'message': msg})
-
- self.last_notify = time.time()
- self.notifications += 1
-
- return True
+ post={'token' : token,
+ 'user' : user,
+ 'title' : event,
+ 'message': msg})
diff --git a/module/plugins/hooks/WindowsPhoneNotify.py b/module/plugins/hooks/WindowsPhoneNotify.py
index e032508c0..3a72e0b39 100644
--- a/module/plugins/hooks/WindowsPhoneNotify.py
+++ b/module/plugins/hooks/WindowsPhoneNotify.py
@@ -3,13 +3,13 @@
import httplib
import time
-from module.plugins.internal.Addon import Addon, Expose
+from module.plugins.internal.Notifier import Notifier
-class WindowsPhoneNotify(Addon):
+class WindowsPhoneNotify(Notifier):
__name__ = "WindowsPhoneNotify"
__type__ = "hook"
- __version__ = "0.14"
+ __version__ = "0.15"
__status__ = "testing"
__config__ = [("activated" , "bool", "Activated" , False),
@@ -30,55 +30,8 @@ class WindowsPhoneNotify(Addon):
("Walter Purcaro", "vuolter@gmail.com" )]
- def init(self):
- self.event_map = {'allDownloadsProcessed': "all_downloads_processed",
- 'plugin_updated' : "plugin_updated" }
-
- self.last_notify = 0
- self.notifications = 0
-
-
- def plugin_updated(self, type_plugins):
- if not self.get_config('notifyupdate'):
- return
-
- self.notify(_("Plugins updated"), str(type_plugins))
-
-
- def activate(self):
- self.key = (self.get_config('push-id'), self.get_config('push-url'))
-
-
- def exit(self):
- if not self.get_config('notifyexit'):
- return
-
- if self.pyload.do_restart:
- self.notify(_("Restarting pyLoad"))
- else:
- self.notify(_("Exiting pyLoad"))
-
-
- def captcha_task(self, task):
- if not self.get_config('notifycaptcha'):
- return
-
- self.notify(_("Captcha"), _("New request waiting user input"))
-
-
- def package_finished(self, pypack):
- if self.get_config('notifypackage'):
- self.notify(_("Package finished"), pypack.name)
-
-
- def all_downloads_processed(self):
- if not self.get_config('notifyprocessed'):
- return
-
- if any(True for pdata in self.pyload.api.getQueue() if pdata.linksdone < pdata.linkstotal):
- self.notify(_("Package failed"), _("One or more packages was not completed successfully"))
- else:
- self.notify(_("All packages finished"))
+ def get_key(self):
+ return self.get_config('push-id'), self.get_config('push-url')
def get_xml_data(self, msg):
@@ -87,30 +40,8 @@ class WindowsPhoneNotify(Addon):
"</wp:Toast> </wp:Notification>" % msg)
- @Expose
- def notify(self,
- event,
- msg="",
- key=(None, None)):
-
- id, url = key or self.key
- if not id or not url:
- return
-
- if self.pyload.isClientConnected() and not self.get_config('ignoreclient'):
- return
-
- elapsed_time = time.time() - self.last_notify
-
- if elapsed_time < self.get_config("sendtimewait"):
- return
-
- if elapsed_time > 60:
- self.notifications = 0
-
- elif self.notifications >= self.get_config("sendpermin"):
- return
-
+ def send(self, event, msg, key):
+ id, url = key
request = self.get_xml_data("%s: %s" % (event, msg) if msg else event)
webservice = httplib.HTTP(url)
@@ -123,8 +54,3 @@ class WindowsPhoneNotify(Addon):
webservice.endheaders()
webservice.send(request)
webservice.close()
-
- self.last_notify = time.time()
- self.notifications += 1
-
- return True
diff --git a/module/plugins/internal/Notifier.py b/module/plugins/internal/Notifier.py
new file mode 100644
index 000000000..f562fb4bf
--- /dev/null
+++ b/module/plugins/internal/Notifier.py
@@ -0,0 +1,117 @@
+# -*- coding: utf-8 -*-
+
+import time
+
+from module.plugins.internal.Notifier import Notifier, Expose
+from module.plugins.internal.utils import isiterable
+
+
+class AndroidPhoneNotify(Notifier):
+ __name__ = "Notifier"
+ __type__ = "hook"
+ __version__ = "0.01"
+ __status__ = "testing"
+
+ __config__ = [("activated" , "bool", "Activated" , False),
+ ("notifycaptcha" , "bool", "Notify captcha request" , True ),
+ ("notifypackage" , "bool", "Notify package finished" , True ),
+ ("notifyprocessed", "bool", "Notify packages processed" , True ),
+ ("notifyupdate" , "bool", "Notify plugin updates" , True ),
+ ("notifyexit" , "bool", "Notify pyLoad shutdown" , True ),
+ ("sendtimewait" , "int" , "Timewait in seconds between notifications", 5 ),
+ ("sendpermin" , "int" , "Max notifications per minute" , 12 ),
+ ("ignoreclient" , "bool", "Send notifications if client is connected", False)]
+
+ __description__ = """Base notifier plugin"""
+ __license__ = "GPLv3"
+ __authors__ = [("Walter Purcaro", "vuolter@gmail.com" )]
+
+
+ def init(self):
+ self.event_map = {'allDownloadsProcessed': "all_downloads_processed",
+ 'plugin_updated' : "plugin_updated" }
+
+ self.last_notify = 0
+ self.notifications = 0
+
+
+ def plugin_updated(self, type_plugins):
+ if not self.get_config('notifyupdate'):
+ return
+
+ self.notify(_("Plugins updated"), str(type_plugins))
+
+
+ def exit(self):
+ if not self.get_config('notifyexit'):
+ return
+
+ if self.pyload.do_restart:
+ self.notify(_("Restarting pyLoad"))
+ else:
+ self.notify(_("Exiting pyLoad"))
+
+
+ def captcha_task(self, task):
+ if not self.get_config('notifycaptcha'):
+ return
+
+ self.notify(_("Captcha"), _("New request waiting user input"))
+
+
+ def package_finished(self, pypack):
+ if self.get_config('notifypackage'):
+ self.notify(_("Package finished"), pypack.name)
+
+
+ def all_downloads_processed(self):
+ if not self.get_config('notifyprocessed'):
+ return
+
+ if any(True for pdata in self.pyload.api.getQueue() if pdata.linksdone < pdata.linkstotal):
+ self.notify(_("Package failed"), _("One or more packages was not completed successfully"))
+ else:
+ self.notify(_("All packages finished"))
+
+
+ def get_key(self):
+ raise NotImplementedError
+
+
+ def send(self, event, msg, key):
+ raise NotImplementedError
+
+
+ @Expose
+ def notify(self, event, msg="", key=None):
+ key = key or self.get_key()
+ if isiterable(key) and all(key) or not key:
+ return
+
+ if self.pyload.isClientConnected() and not self.get_config('ignoreclient'):
+ return
+
+ elapsed_time = time.time() - self.last_notify
+
+ if elapsed_time < self.get_config("sendtimewait"):
+ return
+
+ if elapsed_time > 60:
+ self.notifications = 0
+
+ elif self.notifications >= self.get_config("sendpermin"):
+ return
+
+ try:
+ resp = self.send(event, msg, key)
+
+ except Exception, e:
+ self.log_error(_("Error sending notification"), e)
+ return False
+
+ else:
+ return True
+
+ finally:
+ self.last_notify = time.time()
+ self.notifications += 1