From 42a895b0950ca7fa9a4b811c04fa4bf9e2f29001 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Tue, 28 Mar 2017 09:38:03 -0700 Subject: [PATCH] Change Pushy to OneSignal for push notifications --- plexpy/__init__.py | 18 +++++++++++++++++- plexpy/api2.py | 8 ++++---- plexpy/notifiers.py | 41 ++++++++++++++++++----------------------- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 77efbf33..3ed46c55 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -537,7 +537,7 @@ def dbcheck(): # mobile_devices table :: This table keeps record of devices linked with the mobile app c_db.execute( 'CREATE TABLE IF NOT EXISTS mobile_devices (id INTEGER PRIMARY KEY AUTOINCREMENT, ' - 'device_token TEXT NOT NULL UNIQUE, device_name TEXT, friendly_name TEXT)' + 'device_id TEXT NOT NULL UNIQUE, device_token TEXT, device_name TEXT, friendly_name TEXT)' ) # Upgrade sessions table from earlier versions @@ -1039,6 +1039,22 @@ def dbcheck(): except: pass + # Upgrade mobile_devices table from earlier versions + try: + result = c_db.execute('SELECT SQL FROM sqlite_master WHERE type="table" AND name="mobile_devices"').fetchone() + if 'device_token TEXT NOT NULL UNIQUE' in result[0]: + logger.debug(u"Altering database. Dropping and recreating mobile_devices table.") + c_db.execute( + 'DROP TABLE mobile_devices' + ) + c_db.execute( + 'CREATE TABLE IF NOT EXISTS mobile_devices (id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 'device_id TEXT NOT NULL UNIQUE, device_token TEXT, device_name TEXT, friendly_name TEXT)' + ) + except sqlite3.OperationalError: + logger.warn(u"Failed to recreate mobile_devices table.") + pass + # Add "Local" user to database as default unauthenticated user. result = c_db.execute('SELECT id FROM users WHERE username = "Local"') if not result.fetchone(): diff --git a/plexpy/api2.py b/plexpy/api2.py index 36a796cb..b7e04a2c 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -341,13 +341,13 @@ class API2: return data - def register_device(self, device_name='', device_token='', **kwargs): + def register_device(self, device_name='', device_id='', **kwargs): """ Registers the PlexPy Android App for notifications. ``` Required parameters: - device_token (str): The device token of the PlexPy Android App device_name (str): The device name of the PlexPy Android App + device_id (str): The OneSignal device id of the PlexPy Android App Optional parameters: None @@ -361,14 +361,14 @@ class API2: self._api_result_type = 'error' return - elif not device_token: + elif not device_id: self._api_msg = 'Device registartion failed: no device token provided.' self._api_result_type = 'error' return db = database.MonitorDatabase() - keys = {'device_token': device_token} + keys = {'device_id': device_id} values = {'device_name': device_name} try: diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 8ee31771..4cbfb928 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -665,28 +665,28 @@ class ANDROIDAPP(Notifier): """ PlexPy Android app notifications """ - _DEFAULT_CONFIG = {'api_key': '', - 'device_token': '' + _DEFAULT_CONFIG = {'device_id': '' } + ONESIGNAL_APP_ID = '3b4b666a-d557-4b92-acdf-e2c8c4b95357' + def notify(self, subject='', body='', action='', **kwargs): if not subject or not body: return - data = {'to': self.config['device_token'], - 'data': {'subject': subject.encode("utf-8"), - 'body': body.encode("utf-8")} + data = {'app_id': self.ONESIGNAL_APP_ID, + 'include_player_ids': [self.config['device_id']], + 'headings': {'en': subject.encode("utf-8")}, + 'contents': {'en': body.encode("utf-8")} } - logger.debug(u"PlexPy Notifiers :: Pushy request body: %s" % json.dumps(data)) - http_handler = HTTPSConnection("api.pushy.me") + http_handler = HTTPSConnection("onesignal.com") http_handler.request("POST", - "/push?%s" % urlencode({'api_key': self.config['api_key']}), + "/api/v1/notifications", headers={'Content-type': "application/json"}, body=json.dumps(data)) response = http_handler.getresponse() request_status = response.status - logger.debug(u"PlexPy Notifiers :: Pushy response: %s" % response.read()) if request_status == 200: logger.info(u"PlexPy Notifiers :: Android app notification sent.") @@ -711,9 +711,9 @@ class ANDROIDAPP(Notifier): devices = {} for device in result: if device['friendly_name']: - devices[device['device_token']] = device['friendly_name'] + devices[device['device_id']] = device['friendly_name'] else: - devices[device['device_token']] = device['device_name'] + devices[device['device_id']] = device['device_name'] return devices @@ -724,26 +724,21 @@ class ANDROIDAPP(Notifier): devices_config = {'label': 'Device', 'description': 'No devices registered. ' \ 'Register the Android app with PlexPy.', + 'style="cursor: pointer;">Click here to get the Android App.', 'input_type': 'help' } else: devices_config = {'label': 'Device', - 'value': self.config['device_token'], - 'name': 'androidapp_device_token', - 'description': 'Set your Android app device.', + 'value': self.config['device_id'], + 'name': 'androidapp_device_id', + 'description': 'Set your Android app device or ' \ + 'register a new device with PlexPy.', 'input_type': 'select', 'select_options': devices } - config_option = [{'label': 'Pushy API Key', - 'value': self.config['api_key'], - 'name': 'androidapp_api_key', - 'description': 'Your Pushy API key.', - 'input_type': 'text' - }, - devices_config - ] + config_option = [devices_config] return config_option