From 36324d10dc2fff4bb2ded897fd1567d0b2b2886b Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sun, 12 Jul 2020 14:44:31 -0700 Subject: [PATCH] Add onesignal_id to register device API --- API.md | 5 +++-- .../default/mobile_device_config.html | 2 +- plexpy/__init__.py | 11 ++++++++++- plexpy/api2.py | 10 ++++++---- plexpy/mobile_app.py | 12 ++++++++---- plexpy/notifiers.py | 19 ++++++++++--------- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/API.md b/API.md index 352ba45e..c9aea64c 100644 --- a/API.md +++ b/API.md @@ -2674,11 +2674,12 @@ Registers the Tautulli Android App for notifications. ``` Required parameters: - device_name (str): The device name of the Tautulli Android App - device_id (str): The OneSignal device id of the Tautulli Android App + device_id (str): The unique device identifier for the mobile device + device_name (str): The device name of the mobile device Optional parameters: friendly_name (str): A friendly name to identify the mobile device + onesignal_id (str): The OneSignal id for the mobile device Returns: None diff --git a/data/interfaces/default/mobile_device_config.html b/data/interfaces/default/mobile_device_config.html index f97f6a67..5cbe78f5 100644 --- a/data/interfaces/default/mobile_device_config.html +++ b/data/interfaces/default/mobile_device_config.html @@ -33,7 +33,7 @@
Your OneSignal device ID for notifications.
diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 0db15980..b96b3c11 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -743,7 +743,7 @@ def dbcheck(): 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, ' - 'last_seen INTEGER, official INTEGER DEFAULT 0)' + 'onesignal_id TEXT, last_seen INTEGER, official INTEGER DEFAULT 0)' ) # tvmaze_lookup table :: This table keeps record of the TVmaze lookups @@ -2012,6 +2012,15 @@ def dbcheck(): c_db.execute('UPDATE mobile_devices SET official = ? WHERE device_id = ?', [mobile_app.validate_device_id(device_id), device_id]) + # Upgrade mobile_devices table from earlier versions + try: + c_db.execute('SELECT onesignal_id FROM mobile_devices') + except sqlite3.OperationalError: + logger.debug("Altering database. Updating database table mobile_devices.") + c_db.execute( + 'ALTER TABLE mobile_devices ADD COLUMN onesignal_id TEXT' + ) + # Upgrade notifiers table from earlier versions try: c_db.execute('SELECT custom_conditions FROM notifiers') diff --git a/plexpy/api2.py b/plexpy/api2.py index ce8363d7..82204223 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -391,16 +391,17 @@ class API2(object): return data - def register_device(self, device_id='', device_name='', friendly_name='', **kwargs): + def register_device(self, device_id='', device_name='', friendly_name='', onesignal_id='', **kwargs): """ Registers the Tautulli Android App for notifications. ``` Required parameters: - device_name (str): The device name of the Tautulli Android App - device_id (str): The OneSignal device id of the Tautulli Android App + device_id (str): The unique device identifier for the mobile device + device_name (str): The device name of the mobile device Optional parameters: friendly_name (str): A friendly name to identify the mobile device + onesignal_id (str): The OneSignal id for the mobile device Returns: None @@ -419,7 +420,8 @@ class API2(object): result = mobile_app.add_mobile_device(device_id=device_id, device_name=device_name, device_token=self._api_apikey, - friendly_name=friendly_name) + friendly_name=friendly_name, + onesignal_id=onesignal_id) if result: self._api_msg = 'Device registration successful.' diff --git a/plexpy/mobile_app.py b/plexpy/mobile_app.py index 3c29e123..a4653368 100644 --- a/plexpy/mobile_app.py +++ b/plexpy/mobile_app.py @@ -87,13 +87,14 @@ def get_mobile_device_by_token(device_token=None): return get_mobile_devices(device_token=device_token) -def add_mobile_device(device_id=None, device_name=None, device_token=None, friendly_name=None): +def add_mobile_device(device_id=None, device_name=None, device_token=None, friendly_name=None, onesignal_id=None): db = database.MonitorDatabase() keys = {'device_id': device_id} values = {'device_name': device_name, 'device_token': device_token, - 'official': validate_device_id(device_id=device_id)} + 'onesignal_id': onesignal_id, + 'official': validate_onesignal_id(onesignal_id=onesignal_id)} if friendly_name: values['friendly_name'] = friendly_name @@ -172,11 +173,14 @@ def set_last_seen(device_token=None): return -def validate_device_id(device_id): +def validate_onesignal_id(onesignal_id): + if onesignal_id is None: + return False + headers = {'Content-Type': 'application/json'} payload = {'app_id': _ONESIGNAL_APP_ID} - r = requests.get('https://onesignal.com/api/v1/players/{}'.format(device_id), headers=headers, json=payload) + r = requests.get('https://onesignal.com/api/v1/players/{}'.format(onesignal_id), headers=headers, json=payload) return r.status_code == 200 diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 933b1b8b..1a7801d8 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -929,7 +929,7 @@ class ANDROIDAPP(Notifier): #logger.debug("Salt (base64): {}".format(base64.b64encode(salt))) payload = {'app_id': mobile_app._ONESIGNAL_APP_ID, - 'include_player_ids': [self.config['device_id']], + 'include_player_ids': [device['onesignal_id']], 'contents': {'en': 'Tautulli Notification'}, 'data': {'encrypted': True, 'cipher_text': base64.b64encode(encrypted_data), @@ -942,7 +942,7 @@ class ANDROIDAPP(Notifier): "Install the library to encrypt the notifications.") payload = {'app_id': mobile_app._ONESIGNAL_APP_ID, - 'include_player_ids': [self.config['device_id']], + 'include_player_ids': [device['onesignal_id']], 'contents': {'en': 'Tautulli Notification'}, 'data': {'encrypted': False, 'plain_text': plaintext_data} @@ -958,7 +958,8 @@ class ANDROIDAPP(Notifier): db = database.MonitorDatabase() try: - query = 'SELECT * FROM mobile_devices WHERE official = 1' + query = 'SELECT * FROM mobile_devices WHERE official = 1 ' \ + 'AND onesignal_id IS NOT NULL AND onesignal_id != ""' result = db.select(query=query) except Exception as e: logger.warn("Tautulli Notifiers :: Unable to retrieve Android app devices list: %s." % e) @@ -983,9 +984,9 @@ class ANDROIDAPP(Notifier): 'The content of your notifications will be sent unencrypted!