diff --git a/plexpy/api2.py b/plexpy/api2.py index f25487b6..c2a7f5e9 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -341,6 +341,52 @@ class API2: return data + def register_android_app(self, notifier_id=None, device_id='', **kwargs): + """ Registers the PlexPy Android App for notifications. + + ``` + Required parameters: + notifier_id (int): The notifier id of the PlexPy Android App notifier + device_id (str): The device id of the PlexPy Android App + + Optional parameters: + None + + Returns: + None + ``` + """ + import notifiers + + if not notifier_id: + self._api_msg = 'Device registartion failed: no notifier id provided.' + self._api_result_type = 'error' + return + + elif not device_id: + self._api_msg = 'Device registartion failed: no device id provided.' + self._api_result_type = 'error' + return + + if notifier_id: + notifier = notifiers.get_notifier_config(notifier_id=notifier_id) + if not notifier or notifier['agent_id'] != 21: + self._api_msg = 'Device registartion failed: Incorrect notifier id provided' + self._api_result_type = 'error' + return + + config = {'androidapp_apikey': notifier['config']['apikey'], + 'androidapp_deviceid': device_id} + + if notifiers.set_notifier_config(notifier_id=notifier_id, agent_id=notifier['agent_id'], **config): + self._api_msg = 'Device registration successful.' + self._api_result_type = 'success' + return + + else: + self._api_msg = 'Device registration failed.' + self._api_result_type = 'error' + def _api_make_md(self): """ Tries to make a API.md to simplify the api docs. """ diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index b546e904..2e9a911a 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -68,12 +68,17 @@ AGENT_IDS = {'growl': 0, 'browser': 17, 'join': 18, 'hipchat': 19, - 'discord': 20 + 'discord': 20, + 'androidapp': 21 } def available_notification_agents(): - agents = [{'label': 'Boxcar', + agents = [{'label': 'PlexPy Android App', + 'name': 'androidapp', + 'id': AGENT_IDS['androidapp'] + }, + {'label': 'Boxcar', 'name': 'boxcar', 'id': AGENT_IDS['boxcar'] }, @@ -337,6 +342,8 @@ def get_agent_class(agent_id=None, config=None): return HIPCHAT(config=config) elif agent_id == 20: return DISCORD(config=config) + elif agent_id == 21: + return ANDROIDAPP(config=config) else: return Notifier(config=config) else: @@ -654,6 +661,62 @@ class Notifier(object): return config_options +class ANDROIDAPP(Notifier): + """ + PlexPy Android App notifications + """ + _DEFAULT_CONFIG = {'apikey': '', + 'deviceid': '' + } + + def _register_device(self, apikey='', device_id=''): + config = {'apikey': apikey, + 'deviceid': device_id} + + if self.set_config(config): + return True + + def notify(self, subject='', body='', action='', **kwargs): + if not subject or not body: + return + + data = {'to': self.config['deviceid'], + 'data': {'subject': subject.encode("utf-8"), + 'body': body.encode("utf-8")} + } + + http_handler = HTTPSConnection("api.pushy.me") + http_handler.request("POST", + "/push", + headers={ + 'Content-type': "application/json" + }, + body=json.dumps(data)) + response = http_handler.getresponse() + request_status = response.status + + if request_status == 200: + logger.info(u"PlexPy Notifiers :: Android App notification sent.") + return True + elif request_status >= 400 and request_status < 500: + logger.warn(u"PlexPy Notifiers :: Android App notification failed: [%s] %s" % (request_status, response.reason)) + return False + else: + logger.warn(u"PlexPy Notifiers :: Android App notification failed.") + return False + + def return_config_options(self): + config_option = [{'label': 'Pushy API Key', + 'value': self.config['apikey'], + 'name': 'androidapp_apikey', + 'description': 'Your Pushy API key.', + 'input_type': 'text' + } + ] + + return config_option + + class BOXCAR(Notifier): """ Boxcar notifications