Add mobile device last seen

This commit is contained in:
JonnyWong16 2017-12-08 22:05:44 -08:00
parent 019787b32d
commit ecd0a199f1
5 changed files with 61 additions and 22 deletions

View file

@ -540,7 +540,8 @@ 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_id TEXT NOT NULL UNIQUE, device_token TEXT, device_name TEXT, friendly_name TEXT)'
'device_id TEXT NOT NULL UNIQUE, device_token TEXT, device_name TEXT, friendly_name TEXT, '
'last_seen INTEGER)'
)
# tvmaze_lookup table :: This table keeps record of the TVmaze lookups
@ -1084,6 +1085,15 @@ def dbcheck():
logger.warn(u"Failed to recreate mobile_devices table.")
pass
# Upgrade mobile_devices table from earlier versions
try:
c_db.execute('SELECT last_seen FROM mobile_devices')
except sqlite3.OperationalError:
logger.debug(u"Altering database. Updating database table mobile_devices.")
c_db.execute(
'ALTER TABLE mobile_devices ADD COLUMN last_seen INTEGER'
)
# Upgrade notifiers table from earlier versions
try:
c_db.execute('SELECT custom_conditions FROM notifiers')

View file

@ -89,11 +89,6 @@ class API2:
elif 'apikey' not in kwargs:
self._api_msg = 'Parameter apikey is required'
elif (kwargs.get('apikey', '') != plexpy.CONFIG.API_KEY and
kwargs.get('apikey', '') != mobile_app.TEMP_DEVICE_TOKEN and
not mobile_app.get_mobile_device_by_token(kwargs.get('apikey', ''))):
self._api_msg = 'Invalid apikey'
elif 'cmd' not in kwargs:
self._api_msg = 'Parameter cmd is required. Possible commands are: %s' % ', '.join(self._api_valid_methods)
@ -108,18 +103,26 @@ class API2:
# Allow override for the api.
self._api_out_type = kwargs.pop('out_type', 'json')
if ((self._api_apikey == plexpy.CONFIG.API_KEY or
self._api_apikey == mobile_app.TEMP_DEVICE_TOKEN or
mobile_app.get_mobile_device_by_token(self._api_apikey)) and
plexpy.CONFIG.API_ENABLED and self._api_cmd in self._api_valid_methods):
self._api_authenticated = True
self._api_msg = None
self._api_kwargs = kwargs
elif self._api_cmd in ('get_apikey', 'docs', 'docs_md') and plexpy.CONFIG.API_ENABLED:
self._api_authenticated = True
# Remove the old error msg
self._api_msg = None
self._api_kwargs = kwargs
if plexpy.CONFIG.API_ENABLED and not self._api_msg:
if self._api_apikey in (plexpy.CONFIG.API_KEY, mobile_app.TEMP_DEVICE_TOKEN):
self._api_authenticated = True
elif mobile_app.get_mobile_device_by_token(self._api_apikey):
mobile_app.set_last_seen(self._api_apikey)
self._api_authenticated = True
else:
self._api_msg = 'Invalid apikey'
if self._api_authenticated and self._api_cmd in self._api_valid_methods:
self._api_msg = None
self._api_kwargs = kwargs
elif not self._api_authenticated and self._api_cmd in ('get_apikey', 'docs', 'docs_md'):
self._api_authenticated = True
# Remove the old error msg
self._api_msg = None
self._api_kwargs = kwargs
if self._api_msg:
logger.api_debug(u'PlexPy APIv2 :: %s.' % self._api_msg)

View file

@ -13,6 +13,8 @@
# You should have received a copy of the GNU General Public License
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
import time
import plexpy
import database
import helpers
@ -121,6 +123,19 @@ def delete_mobile_device(mobile_device_id=None):
return False
def set_last_seen(device_token=None):
db = database.MonitorDatabase()
last_seen = int(time.time())
try:
result = db.action('UPDATE mobile_devices SET last_seen = ? WHERE device_token = ?',
args=[last_seen, device_token])
except Exception as e:
logger.warn(u"PlexPy MobileApp :: Failed to set last_seen time for device: %s." % e)
return
def blacklist_logger():
devices = get_mobile_devices()