Add register android app for push notifications

This commit is contained in:
JonnyWong16 2017-03-25 23:13:35 -07:00
parent 50969c24cc
commit d18c2ffddb
2 changed files with 111 additions and 2 deletions

View file

@ -341,6 +341,52 @@ class API2:
return data 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): def _api_make_md(self):
""" Tries to make a API.md to simplify the api docs. """ """ Tries to make a API.md to simplify the api docs. """

View file

@ -68,12 +68,17 @@ AGENT_IDS = {'growl': 0,
'browser': 17, 'browser': 17,
'join': 18, 'join': 18,
'hipchat': 19, 'hipchat': 19,
'discord': 20 'discord': 20,
'androidapp': 21
} }
def available_notification_agents(): def available_notification_agents():
agents = [{'label': 'Boxcar', agents = [{'label': 'PlexPy Android App',
'name': 'androidapp',
'id': AGENT_IDS['androidapp']
},
{'label': 'Boxcar',
'name': 'boxcar', 'name': 'boxcar',
'id': AGENT_IDS['boxcar'] 'id': AGENT_IDS['boxcar']
}, },
@ -337,6 +342,8 @@ def get_agent_class(agent_id=None, config=None):
return HIPCHAT(config=config) return HIPCHAT(config=config)
elif agent_id == 20: elif agent_id == 20:
return DISCORD(config=config) return DISCORD(config=config)
elif agent_id == 21:
return ANDROIDAPP(config=config)
else: else:
return Notifier(config=config) return Notifier(config=config)
else: else:
@ -654,6 +661,62 @@ class Notifier(object):
return config_options 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): class BOXCAR(Notifier):
""" """
Boxcar notifications Boxcar notifications