diff --git a/API.md b/API.md index 7dcb3e9a..6d0b9113 100644 --- a/API.md +++ b/API.md @@ -2624,15 +2624,15 @@ Returns: ### terminate_session -Add a new notification agent. +Stop a streaming session. ``` Required parameters: - session_id (str): The id of the session to terminate - message (str): A custom message to send to the client + session_key (int): The session key of the session to terminate, OR + session_id (str): The session id of the session to terminate Optional parameters: - None + message (str): A custom message to send to the client Returns: None diff --git a/data/interfaces/default/index.html b/data/interfaces/default/index.html index 826b8748..61fb34da 100644 --- a/data/interfaces/default/index.html +++ b/data/interfaces/default/index.html @@ -675,6 +675,7 @@ $.ajax({ url: 'terminate_session', data: { + session_key: key, session_id: session_id, message: message }, diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 41a2e7e3..2ea9619d 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -518,7 +518,7 @@ def dbcheck(): # sessions table :: This is a temp table that logs currently active sessions c_db.execute( - 'CREATE TABLE IF NOT EXISTS sessions (id INTEGER PRIMARY KEY AUTOINCREMENT, session_key INTEGER, ' + 'CREATE TABLE IF NOT EXISTS sessions (id INTEGER PRIMARY KEY AUTOINCREMENT, session_key INTEGER, session_id TEXT, ' 'transcode_key TEXT, rating_key INTEGER, section_id INTEGER, media_type TEXT, started INTEGER, stopped INTEGER, ' 'paused_counter INTEGER DEFAULT 0, state TEXT, user_id INTEGER, user TEXT, friendly_name TEXT, ' 'ip_address TEXT, machine_id TEXT, player TEXT, product TEXT, platform TEXT, title TEXT, parent_title TEXT, ' @@ -1081,6 +1081,15 @@ def dbcheck(): 'ALTER TABLE sessions ADD COLUMN live_uuid TEXT' ) + # Upgrade sessions table from earlier versions + try: + c_db.execute('SELECT session_id FROM sessions') + except sqlite3.OperationalError: + logger.debug(u"Altering database. Updating database table sessions.") + c_db.execute( + 'ALTER TABLE sessions ADD COLUMN session_id TEXT' + ) + # Upgrade sessions table from earlier versions try: c_db.execute('SELECT original_title FROM sessions') diff --git a/plexpy/activity_processor.py b/plexpy/activity_processor.py index 0d94ece5..5bb0041f 100644 --- a/plexpy/activity_processor.py +++ b/plexpy/activity_processor.py @@ -34,6 +34,7 @@ class ActivityProcessor(object): def write_session(self, session=None, notify=True): if session: values = {'session_key': session.get('session_key', ''), + 'session_id': session.get('session_id', ''), 'transcode_key': session.get('transcode_key', ''), 'section_id': session.get('section_id', ''), 'rating_key': session.get('rating_key', ''), @@ -466,6 +467,16 @@ class ActivityProcessor(object): return None + def get_session_by_id(self, session_id=None): + if session_id: + session = self.db.select_single('SELECT * FROM sessions ' + 'WHERE session_id = ? ', + args=[session_id]) + if session: + return session + + return None + def set_session_state(self, session_key=None, state=None, **kwargs): if str(session_key).isdigit(): values = {} diff --git a/plexpy/pmsconnect.py b/plexpy/pmsconnect.py index edfaf585..8810bdcb 100644 --- a/plexpy/pmsconnect.py +++ b/plexpy/pmsconnect.py @@ -19,6 +19,7 @@ import time import urllib import plexpy +import activity_processor import common import helpers import http_handler @@ -1919,7 +1920,7 @@ class PmsConnect(object): return session_output - def terminate_session(self, session_id='', message=''): + def terminate_session(self, session_key='', session_id='', message=''): """ Terminates a streaming session. @@ -1927,10 +1928,22 @@ class PmsConnect(object): """ message = message or 'The server owner has ended the stream.' + if session_key and not session_id: + ap = activity_processor.ActivityProcessor() + session = ap.get_session_by_key(session_key=session_key) + session_id = session['session_id'] + + elif session_id and not session_key: + ap = activity_processor.ActivityProcessor() + session = ap.get_session_by_id(session_id=session_id) + session_key = session['session_key'] + if session_id: + logger.info(u"Tautulli Pmsconnect :: Terminating session %s (session_id %s)." % (session_key, session_id)) result = self.get_sessions_terminate(session_id=session_id, reason=urllib.quote_plus(message)) return result else: + logger.warn(u"Tautulli Pmsconnect :: Failed to terminate session %s. Missing session_id." % session_key) return False def get_item_children(self, rating_key='', get_grandchildren=False): diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 66ccc358..024f732c 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -248,23 +248,23 @@ class WebInterface(object): @cherrypy.tools.json_out() @requireAuth(member_of("admin")) @addtoapi() - def terminate_session(self, session_id=None, message=None, **kwargs): - """ Add a new notification agent. + def terminate_session(self, session_key=None, session_id=None, message=None, **kwargs): + """ Stop a streaming session. ``` Required parameters: - session_id (str): The id of the session to terminate - message (str): A custom message to send to the client + session_key (int): The session key of the session to terminate, OR + session_id (str): The session id of the session to terminate Optional parameters: - None + message (str): A custom message to send to the client Returns: None ``` """ pms_connect = pmsconnect.PmsConnect() - result = pms_connect.terminate_session(session_id=session_id, message=message) + result = pms_connect.terminate_session(session_key=session_key, session_id=session_id, message=message) if result: return {'result': 'success', 'message': 'Session terminated.'}