From 7c801c2f5e238df1ee6f6cd6f726164c4830ee38 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sat, 20 Jun 2020 16:16:35 -0700 Subject: [PATCH] Add flag for offical mobile app --- .../interfaces/default/mobile_devices_table.html | 8 +++++++- data/interfaces/default/settings.html | 1 + plexpy/__init__.py | 15 ++++++++++++++- plexpy/api2.py | 5 ----- plexpy/mobile_app.py | 14 +++++++++++++- plexpy/notifiers.py | 16 +++------------- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/data/interfaces/default/mobile_devices_table.html b/data/interfaces/default/mobile_devices_table.html index b6b9ebe8..0f6770a9 100644 --- a/data/interfaces/default/mobile_devices_table.html +++ b/data/interfaces/default/mobile_devices_table.html @@ -13,7 +13,11 @@ DOCUMENTATION :: END % for device in sorted(devices_list, key=lambda k: k['device_name']):
  • - + % if device['official']: + + % else: + + % endif ${device['friendly_name'] or device['device_name']}  (${device['id']}) @@ -138,4 +142,6 @@ DOCUMENTATION :: END } verifiedDevice = true; }) + + $('.officail-tooltip').tooltip(); \ No newline at end of file diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index bb7b0d90..3bc355d5 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -1435,6 +1435,7 @@

    Register a new device using a QR code, or configure an existing device by clicking the settings icon on the right.

    Warning: The API must be enabled under Web Interface to use the app.

    +
    Loading registered devices...
    diff --git a/plexpy/__init__.py b/plexpy/__init__.py index a9844c65..f5f3fa46 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -745,7 +745,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)' + 'last_seen INTEGER, official INTEGER DEFAULT 0)' ) # tvmaze_lookup table :: This table keeps record of the TVmaze lookups @@ -2001,6 +2001,19 @@ def dbcheck(): 'ALTER TABLE mobile_devices ADD COLUMN last_seen INTEGER' ) + # Upgrade mobile_devices table from earlier versions + try: + c_db.execute('SELECT official FROM mobile_devices') + except sqlite3.OperationalError: + logger.debug("Altering database. Updating database table mobile_devices.") + c_db.execute( + 'ALTER TABLE mobile_devices ADD COLUMN official INTEGER DEFAULT 0' + ) + # Update official mobile device flag + for device_id, in c_db.execute('SELECT device_id FROM mobile_devices').fetchall(): + c_db.execute('UPDATE mobile_devices SET official = ? WHERE device_id = ?', + [mobile_app.validate_device_id(device_id), device_id]) + # 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 37f31a8c..31c26960 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -416,11 +416,6 @@ class API2(object): self._api_result_type = 'error' return - elif not notifiers.ANDROIDAPP().validate_device_id(device_id=device_id): - self._api_msg = 'Device registration failed: invalid OneSignal Player ID.' - self._api_result_type = 'error' - return - result = mobile_app.add_mobile_device(device_id=device_id, device_name=device_name, device_token=self._api_apikey, diff --git a/plexpy/mobile_app.py b/plexpy/mobile_app.py index 3de44044..96494fff 100644 --- a/plexpy/mobile_app.py +++ b/plexpy/mobile_app.py @@ -18,6 +18,7 @@ from __future__ import unicode_literals from future.builtins import str +import requests import threading import plexpy @@ -34,6 +35,8 @@ else: TEMP_DEVICE_TOKEN = None INVALIDATE_TIMER = None +_ONESIGNAL_APP_ID = '3b4b666a-d557-4b92-acdf-e2c8c4b95357' + def set_temp_device_token(token=None): global TEMP_DEVICE_TOKEN @@ -84,7 +87,8 @@ def add_mobile_device(device_id=None, device_name=None, device_token=None, frien keys = {'device_id': device_id} values = {'device_name': device_name, - 'device_token': device_token} + 'device_token': device_token, + 'official': validate_device_id(device_id=device_id)} if friendly_name: values['friendly_name'] = friendly_name @@ -161,6 +165,14 @@ def set_last_seen(device_token=None): return +def validate_device_id(device_id): + 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) + return r.status_code == 200 + + def blacklist_logger(): devices = get_mobile_devices() for d in devices: diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index aa46a08e..dff085a6 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -882,16 +882,6 @@ class ANDROIDAPP(Notifier): 'priority': 3 } - _ONESIGNAL_APP_ID = '3b4b666a-d557-4b92-acdf-e2c8c4b95357' - - def validate_device_id(self, device_id): - headers = {'Content-Type': 'application/json'} - payload = {'app_id': self._ONESIGNAL_APP_ID} - - r = requests.get('https://onesignal.com/api/v1/players/{}'.format(device_id), headers=headers, json=payload) - if r.status_code == 200: - return r.json() - def agent_notify(self, subject='', body='', action='', notification_id=None, **kwargs): # Check mobile device is still registered device = mobile_app.get_mobile_devices(device_id=self.config['device_id']) @@ -938,7 +928,7 @@ class ANDROIDAPP(Notifier): #logger.debug("Nonce (base64): {}".format(base64.b64encode(nonce))) #logger.debug("Salt (base64): {}".format(base64.b64encode(salt))) - payload = {'app_id': self._ONESIGNAL_APP_ID, + payload = {'app_id': mobile_app._ONESIGNAL_APP_ID, 'include_player_ids': [self.config['device_id']], 'contents': {'en': 'Tautulli Notification'}, 'data': {'encrypted': True, @@ -951,7 +941,7 @@ class ANDROIDAPP(Notifier): "Android app notifications will be sent unecrypted. " "Install the library to encrypt the notifications.") - payload = {'app_id': self._ONESIGNAL_APP_ID, + payload = {'app_id': mobile_app._ONESIGNAL_APP_ID, 'include_player_ids': [self.config['device_id']], 'contents': {'en': 'Tautulli Notification'}, 'data': {'encrypted': False, @@ -968,7 +958,7 @@ class ANDROIDAPP(Notifier): db = database.MonitorDatabase() try: - query = 'SELECT * FROM mobile_devices' + query = 'SELECT * FROM mobile_devices WHERE official = 1' result = db.select(query=query) except Exception as e: logger.warn("Tautulli Notifiers :: Unable to retrieve Android app devices list: %s." % e)